Hi,

I have a While Loop to display my MySQL info.

This makes a list of all my data, in one row.
But what I want is it to display it in a table with 5 columns so that the data is more readable.

this is my code:

while ($row = mysql_fetch_array($retd)) 
{ 
$code = $row["code"];
$name = $row["name"];
echo("<td width=150 align=center>");
echo ("<a href=../item?scode=$code><img src=pictures/$code.gif border=0 alt=Item $name</a>");
echo ("<br><a href=../item?scode=$code><span class=fs13>$name</span></a>");
echo("</td>");
}

Now I have tried to make it like this:

<tr>
     <td></td>
     <td></td>
     <td></td>
     <td></td>
     <td></td>
</tr>

But it didn't work, since it repeated the same item for 5 times.

Does anyone know what can I change to make it work?

If you need more explaination, ask.
THANKS FOR THE HELP!!

First, you are missing double quotes for html attributes. I put them in the code below and they are escaped when also using double quotes as a string delimiter. Since you provided only two fields as a result from your query I prepared a code for a table with two columns. If you want a table with five columns the logic is the same.

<?php

// ...

// beginning of the table
echo '<table>';

while ($row = mysql_fetch_array($retd)) 
{ 
    $code = $row["code"];
    $name = $row["name"];
    
    // beginning of one row
    echo '<tr>';
    
    // first cell showing a field from the $row
    echo '<td width="150" align="center">';
    echo "<a href=\"../item?scode=$code\"><img src=\"pictures/$code.gif\" border=\"0\" alt=\"Item $name\"></a>";
    echo "</td>";
    
    // second cell showing a field from the $row
    echo "<td width=\"150\" align=\"center\">";
    echo "<a href=\"../item?scode=$code\"><span class=\"fs13\">$name</span></a>";
    echo "</td>";
    
    // end of one row
    echo '</tr>';
}

// end of the table
echo '</table>';
?>

As I saw the pritaeas's reply I realized what the question really was so ignore my reply.

Thank you for your reply.

I have tried the method of pritaeas and while I have managed to make 2 coloums (even if I wanted to make 5) but all the data was put in the 1st coloum while the 2nd column was empty.

I have also tried the method of broj1 and it put the picture in 1st coloum and the text in 2nd column.

Now, in one cell i'd like to make the picture and the text. and it is placed in a table which will have 5 columns ( in each column there will be pic & text) then after 5 columns it will begin another row.

if you can help me solve the problem i will really appreciated, since I have been trying to solve this for a long time.

give me the database structure and list the columns that are to be displayed.

I have prepared a modified code based on the link sent by pritaeas. Note I used a test array since I did not bother to set up a database.

<?php

    // test array
    $test_array = array();
    for($j = 1; $j <= 103; $j++) {
    
        $test_array[$j]["code"] = "CODE-$j";
        $test_array[$j]["name"] = "NAME-$j";
    }

    // counter for cells
    $i = 1;

    // begin a table
    echo '<table border="1">' . "\n";

    foreach($test_array as $row)
    // while($row = mysql_fetch_array($retd)) 
    { 
        $code = $row["code"];
        $name = $row["name"];

        // for 1st, 6th, 11th etc record insert a tag for a new row
        if (($i == 1) or (($i - 1) % 5) == 0) {
        
            echo '<tr>' . "\n";
        }

        // insert a cell
        echo("<td width=150 align=center>");
        echo ("<a href=../item?scode=$code><img src=pictures/$code.gif border=0 alt=Item $name</a>");
        echo ("<br><a href=../item?scode=$code><span class=fs13>$name</span></a>");
        echo("</td>");

        // for 5th, 10th, 15th etc record insert a tag for end of the row
        if ((($i) % 5) == 0) {
        
            echo '</tr>';
        }
        
        // increment $i
        $i++;
    }

    // if there are no records left and the row has not yet been filled, fill it
    // with empty cells
    while(($i - 1) % 5 != 0) {
    
        // insert a blank cell
        echo '<td>&nbsp;</td>';
        
        // increment $i
        $i++;
        
        // for 5th, 10th, 15th etc record insert a tag for end of the row
        if (($i - 1) % 5 == 0) {
        
            echo '</tr>';
        }
    }

    // end the table
    echo '</table>';
?>

I have tried the code that broj1 wrote and it worked as I wanted it to be.

but when i tried to modify it to work with my database it didn't work?

is there a way to put the data from the database into the array, or there is a better way to do it?

Your while loop from the first post should do if it is constructed OK while ($row = mysql_fetch_array($retd)) . Post the table structure and the query you use to get the data.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.