Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,

I'm trying to create my own PHP forum.

I have managed to retrieve and display 3 of 3 rows from my database but only the first record is being styled appropriately. Below is the PHP code I have so far:

PHP
<?php
$sql = "SELECT cat_id, cat_name, cat_description FROM categories";
 
$result = mysql_query($sql);
 
if(!$result)
{
    echo 'The categories could not be displayed, please try again later.';
}
else
{
    if(mysql_num_rows($result) == 0)
    {
        echo 'No categories defined yet.';
    }
    else
    {
		?>
        <table>
        	<tr>
            	<th>Category</th>
                <th>Last topic</th>
         	</tr>
        <?php     
        while($row = mysql_fetch_assoc($result))
        {               
			echo '<tr>';
                echo '<td class="leftpart">';
                    echo '<h3><a href="category.php?cat_id">' . $row['cat_name'] . '</a></h3>' . $row['cat_description'];
                echo '</td>';
                echo '<td class="rightpart">';
                            echo '<a href="topic.php?topic_id">Topic subject</a> at 10-10';
                echo '</td>';
            echo '</tr>';
		   echo '</table>';
        }
    }
}
?>
<?php 


Is there something i'm missing?
Posted

1 solution

set your 'echo ''; outside the while!
 
Share this answer
 
Comments
Herman<T>.Instance 29-Apr-14 14:46pm    
set echo '</table>' outside the while
jba1991 29-Apr-14 14:49pm    
Thanks you!

Why doesn't that work in the while?
Herman<T>.Instance 29-Apr-14 14:51pm    
it is the html tables end tag. After each row in the recordset you do not want to end the html table. After the last row of the recordset is in the table, then you want to end the html table. That's why!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900