Jump to content

[SOLVED] Selecting php into tables with multiple columns?


mkoenig

Recommended Posts

Selecting php into tables with multiple columns?

 

I know how to select the data into tables with one column, but how do you select into 4 columns and 4 rows?

 

Ex. If i had 16 images i wanted displayed

 

1234

5678

9101112

13141516

 

I've seen it done before?

 

Thanks PHP Freaks. :)

You dont state how you are getting the data: array, query, etc? Here is an example using an array

 

<?php

$columns = 4;
$currentRow = array();

echo "<table>";

foreach ($imageArray as $image) {
  $currentRow[] = $image;
  if (count($currentRow)==$columns) {
    echo "<tr><td>".implode('</td><td>', $currentRow)."</td></tr>";
    $currentRow = array();
  }
}

if (count($currentRow)>0) {
  while (count($currentRow)<$columns) {
    $currentRow[] = " "
  }
  echo "<tr><td>".implode('</td><td>', $currentRow)."</td></tr>";
}

echo "</table>";

?>

Not working for me? Thanks

 

What I want is to select from a normal

 

  $query = "Select * from tablename order by entryid desc limit 16";

 

  $result = mysql_query($query);

 

and take that output and put it into a 4x4 table with 16 total blocks.

 

Does anyone have the code for this?

 

Thanks

 

 

You would just need to modify the code I gave for iterating through a result set instead of an array.

 

Here is the same basic functionality utilizing a db query and also with some other enhancements

 

- Uses a function to create the table row instead of hard coding it in two places

- Uses array_pad() instead of a while loop

 

I have tested this code, so i know it works. However, this will display as many records as you give it in your result set into the number of columns specified in $columns. If you want to limit it to 16 records only then you should do that in your query. But, it will handle record sets that are not divisible by the number of columns by adding more cells to complete the last row.

 

<?php

function showRow($rowData) {

  echo "<tr>\n<td>".implode("</td>\n<td>", $rowData)."</td>\n</tr>\n";

}

$columns = 4;

$query = "SELECT * FROM genres";
$result = mysql_query($query);

if (!mysql_num_rows($result)) {

  echo "NO results returned";

} else {

  echo "<table border=\"1\">\n";

  while ($record = mysql_fetch_assoc($result)) {
    $image = $record['name'];
    $currentRow[] = $image;
    if (count($currentRow)==$columns) {
      showRow($currentRow);
      $currentRow = array();
    }
  }

  if (count($currentRow)>0) {
    array_pad($currentRow, 5, ' ');
    showRow($currentRow);
  }

  echo "</table>\n";

}


?>

try

echo '<table>';
while ($record = mysql_fetch_assoc($result)) {
   echo  '<tr>;
   // echo data
   echo '<td><img src="', $record['field name'],'"></img></td>';
   for( $i = 1; $i < 4; $i++) {
      if ($record = mysql_fetch_assoc($result)) 
         echo '<td><img src="', $record['field name'],'"></img></td>';
      else echo '<td> </td>';
   }
   echo '</tr>';
}
echo '</table>';

Thanks so much  ;D

 

Although im sure they both worked i tried...

 

 

echo '<table>';
while ($record = mysql_fetch_assoc($result)) {
   echo  '<tr>';
   // echo data
   echo '<td><img src="', $record['field name'],'"></img></td>';
   for( $i = 1; $i < 4; $i++) {
      if ($record = mysql_fetch_assoc($result)) 
         echo '<td><img src="', $record['field name'],'"></img></td>';
      else echo '<td> </td>';
   }
   echo '</tr>';
}
echo '</table>';

first. :)

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.