954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

While loop in Table

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 ("<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!!

microbert
Newbie Poster
7 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 
pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

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>';
?>
broj1
Posting Whiz
363 posts since Jan 2011
Reputation Points: 29
Solved Threads: 43
 

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

broj1
Posting Whiz
363 posts since Jan 2011
Reputation Points: 29
Solved Threads: 43
 

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.

microbert
Newbie Poster
7 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

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

dinhunzvi
Newbie Poster
21 posts since Jul 2010
Reputation Points: 10
Solved Threads: 2
 

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 ("<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>';
?>
broj1
Posting Whiz
363 posts since Jan 2011
Reputation Points: 29
Solved Threads: 43
 

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?

microbert
Newbie Poster
7 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

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.

broj1
Posting Whiz
363 posts since Jan 2011
Reputation Points: 29
Solved Threads: 43
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: