Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tables - controlling rows and columns with php

Status
Not open for further replies.

trantron

Programmer
Joined
Feb 12, 2004
Messages
15
Location
US
I am pulling variables out of mySQL to complete the path to display pictures.

My problem is that I can not get rows and columns; I can only get my pictures to display in a single column that runs either top to bottom or side to side. I want to display 4 across and then I want a </tr> to start a new row.

If you can help me or direct me to a web source I would be truly appreciative! Thank you for your interest.

Here is my code:

echo "<table border='1' width='200'>";
if ($row = mysql_fetch_array($result))//{
do{
$photo = $row["photo"];
$descc = $row["descc"];
echo "<td align='center'>";
if ($photo !=null){
echo "<img src='./elemental_jpg/$photo' >";
if ($descc !=null){
echo "<br>$descc";

}//end if inner
}// end if outer
echo "</td></tr>";

} //end do
while ($row = mysql_fetch_array($result));

echo "</table>";


 
What I do is use a counter.

Outside of your loop, initialize the counter variable to 0. Every time you write a <td>...</td> element, increment that counter.

When the counter hits the number of columns you want to have, output "</tr><td>" and set the column counter back to zero.

My description is inexact -- you'll have to tweak it.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
This will do the trick! tested!
Code:
$count = 0;
echo "<table border='1' width='200'>";
if ($row = mysql_fetch_array($result))
   do {
     $photo = $row["photo"];
     $descc = $row["descc"];
     echo "<td align='center'>";
     if ($photo !=null)
        {
        echo "<img src='./elemental_jpg/$photo'>";
        if ($descc !=null)
          {
          echo "<br>$descc</td>";
          }
        $count++;
        if ($count == 4)
          {
          echo "</tr><tr>";
          $count = 0;
          }
       }
   }
while ($row = mysql_fetch_array($result));
echo "</table>";
 
This is great! It works perfect.

Thanks for your help on this, I really appreciate it!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top