Jump to content

help please


djrichwz

Recommended Posts

Hi I am trying to display items from a mysql database in a table but I need it to start a new table row each time multiples of 3 rows of the database table have been displayed like the following example:

 

item 1  item 2  item3

 

item 4  item 5  item 6

 

Does anybody know how I can make this work using php.

Link to comment
https://forums.phpfreaks.com/topic/59401-help-please/
Share on other sites

<?php
  print '<table cellspacing="0" cellpadding="2" border="1">';
  $query=mysql_query("SELECT items FROM table");
  while ($row=mysql_fetch_assoc($query)) {
    print '<tr><td>'.$row[0].'</td><td>'.$row[1].'</td><td>'.$row[2].'</td></tr>';
  }
  print '</table>';
?>

Link to comment
https://forums.phpfreaks.com/topic/59401-help-please/#findComment-295100
Share on other sites

Hi I tried the code Below with my database table inserted instead and it did not show any of my results have you any idea why this wouldnt work.

 

I want to display each row of my table until it gets to three rows then start a new table row.

 

Hope somebody can help?

 

<?php

  print '<table cellspacing="0" cellpadding="2" border="1">';

  $query=mysql_query("SELECT msg FROM news");

  while ($row=mysql_fetch_assoc($query)) {

    print '<tr><td>'.$row[0].'</td><td>'.$row[1].'</td><td>'.$row[2].'</td></tr>';

  }

  print '</table>';

?>

Link to comment
https://forums.phpfreaks.com/topic/59401-help-please/#findComment-296223
Share on other sites

I tried to figure out your problem using a nested for loop. I was not able to get it to work since my inside loop only works once. However this is in the direction of what you need. Perhaps you can work from this. If someone else can figure out why the inner loop only works once, then this will work. I would definately like to know.

 

To test this just change:

 

$total_table_rows = ceil($total_results / $max_rows);

to

$total_table_rows = 15; (or any number you like)

 

<?php
$max_colums = 3;  //Set the number of columns you want in your table
$total_results = mysql_result($result,0);     //Get the number of results from your query
$total_table_rows = ceil($total_results / $max_rows);  //Figure out how many rows needed in table
?>
<table>
<?php
for($i = 1; $i <= $total_table_rows; $i++)
{

$from = (($i * $max_rows) - $max_rows);     
echo "<tr><td>$i</td>"; //Show which table row it is

for ( $j = $from; $j < $max_rows ; $j++) 
{
echo "<td>result: $j</td>";
}

echo "</tr>\n";
}

?>
</table>

Link to comment
https://forums.phpfreaks.com/topic/59401-help-please/#findComment-296267
Share on other sites

I don't know where djrichwz is at, but im still curious about this.

 

Heres what the output of my "solution" looks like:

<table border="1">
<tr><td>1</td><td>result: 0</td><td>result: 1</td><td>result: 2</td></tr>
<tr><td>2</td></tr>
<tr><td>3</td></tr>
<tr><td>4</td></tr>
<tr><td>5</td></tr>
</table>

 

It is still stopping after only working once.

Link to comment
https://forums.phpfreaks.com/topic/59401-help-please/#findComment-298021
Share on other sites

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.