Jump to content

Dynamic table from Sql statement


Recommended Posts

Hi there all, first post so please be gentle!

 

I'm trying to pull four stories out of the database and display them in a table fomat.  I know doing it by CSS is preferred, but time is of the essence and this is a quick and dirty fix to my problem.  For the life of me, I can't seem to figure out how to arrange the results in the format I desire.  Below is how I want it to appear, was wondering if someone could crank out the PHP code to make this a reality?

 

Many thanks in advance...!

 

<table width="620" border="0">

  <tr>

    <td>photo1</td>

    <td>description1</td>

    <td>photo3</td>

    <td>description3</td>

  </tr>

  <tr>

    <td>photo2</td>

    <td>description2</td>

    <td>photo4</td>

    <td>desciprtion 4 </td>

  </tr>

</table>

 

This essentially produces a 4 x 4 table, though each story will have a picture associated with it.

Link to comment
https://forums.phpfreaks.com/topic/206407-dynamic-table-from-sql-statement/
Share on other sites

Without knowing your database structure, and other criteria, this is about the best I can do for you.

 

Pseudo-code:


$query = " SELECT `photo`, `text1`, `text2`, `text3 FROM `table` LIMIT 4";
$result = mysql_query($query);
echo "<table>";
while( $array = mysql_fetch_assoc($result) {
     echo "<tr><td>{$array['photo']}</td><td>{$array['text1']}</td><td>{$array['text2']}</td><td>{$array['text3']}</td></tr>";
}
echo "</table>";

So you want to place them in a grid like..?

 

1  3

2  4

 

That's a little more difficult to do as you're not counting through them in a logical way.

 

1  2

3  4

 

... would be easier as on the second 'story', you could just start a new table row within your loop.

 

I've got to shoot for a couple of hours but if you don't have a solution to achieve that later I'll come back.

Disregard my first post; it looked as though you were pulling 4 fields for each of 4 records, but I now see that isn't the case. You want 2 fields for each of 4 records.

 

I agree with MrAdam on the way you've described the output as 1.3.2.4 being the more difficult way to do this. If the order can be simply 1,2,3,4, then this will work regardless of whether the number of results returned is odd or even, so you can eliminate or change the LIMIT and add a WHERE clause if needed.

 


$query = "SELECT `photo`, `text` FROM `test` LIMIT 4";
$result = mysql_query($query) or die(mysql_error());

echo "<table>\n";
$i = 1;

while( $array = mysql_fetch_assoc($result) ) {
          if( $i % 2 != 0 ) {
     echo "<tr>\n";
     }
     echo "<td>{$array['photo']}</td>\n<td>{$array['text']}</td>\n";
     if ( $i % 2 == 0 ) {
          echo "</tr>\n";
     }
     $i ++;
}

if( $i % 2 == 0 ) {
     echo "<td></td>\n</tr>\n";
}
echo "</table>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.