Jump to content

Specifying Number Of Records Per Row?...


jch02140

Recommended Posts

You have a counter of which you get the modulo of. When the modulo is 0 then you start a new row: Here's a plain text example:

echo "Begin list\n"; $amount_per_row = 5;$i = 1;// Supposing this is a querywhile($row = mysql_fetch_array($query)) {  // If this is the first record of a row, print a row beginner  if($i-1 % $amount_per_row == 0) {	echo "  Beginrow\n";  }  // Print the record  echo "	$row['data']\n";   // If this is the last record of a row, print a row end  if($i % $amount_per_row == 0) {	echo "  End row\n";  }  $i++}// Make sure a row end is printed if there are less records than the amount per row.if($i % $amount_per_row != 1) {  echo "  End row\n"}echo 'End list';

You can do this with HTML tables, HTML lists, or using one <div> per row. Here's an example of a possible output of the code I gave:

Begin list  Begin row    Record 1    Record 2    Record 3    Record 4    Record 5  End row  Begin row    Record 6    Record 7  End rowEnd list

Link to comment
Share on other sites

Thanks for the reply. I was thinking it might be better to use for loop... but I seem to get some error in the code which I can't figure out...

			  <?php	 while($item_rows = mysql_fetch_array($item_result))	 {	  for($i = 0; $i < $count_result; $i++)	  {					  $output = "<td align=\"center\">		   <p><img src=\"../images/".$item_rows['item_id']. ".gif\" width=\"45\" height=\"45\" /></p>					   <p> ".$item_rows['name']. "		 <br />					    Attack: +" .$item_rows['attack']. "(" . $item_rows['damage']. ")		 <br />					    <img src=\"../images/credit.gif\" width=\"17\" height=\"17\" />$" .$item_rows['sell_price']. "</p>					   <p>[Equipt|Destory]<br />					   </p>	    </td>";		 $remainder = $i % 5;	   echo ($remainder == 0 ? ($i == 0 ? "" : "\n</tr>") . "\n<tr bgcolor=\"#000000\"> $output " : $output);  	    }	  $next_row = (4 - $remainder);	  $last_row = $next_row ? " <td ".($next_row ? "colspan = $next_row" : "")."></td>" : "";	  $echo "\n $last_row \n </tr></table></td>";	 }	 ?>

Link to comment
Share on other sites

Why are you using a for loop inside the while loop? Look back at the example code I gave earlier. It's a simple structure. The only things you really need to change are the strings and the $amount_per_row variable.

Link to comment
Share on other sites

I still can't seem to get it display right...

	   <?php    $i=1;    $amount_per_row=5;    while($item_rows = mysql_fetch_array($item_result))    {	    if($i-1 % $amount_per_row == 0)	 {    ?>	  			  <tr bgcolor="#000000">	   <?php	 }    ?>			    <td align="center"><p><img src="../images/<?php $item_rows['item_id'] ?>.gif" width="45" height="45" /></p>				  <p><?php $item_rows['name'] ?><br />				    Attack: +<?php $item_rows['attack'] ?><br />				    <img src="../images/credit.gif" width="17" height="17" />$<?php $item_rows['sell_price'] ?></p>				  <p>[Equipt|Destory]<br />				  </p>			    </td>	   <?php	  if($i % $amount_per_row == 0)   {    echo "</tr>";   }  $i++;    }    if($i % $amount_per_row != 1)    {	 echo "</tr>   </table>   </td>";    }    echo "</tr>	  </table>";    ?>

Link to comment
Share on other sites

This should work:

<?phpecho '<table id="weapons">';$amount_per_row = 5;$i = 1;// Supposing this is a querywhile($row = mysql_fetch_array($query)) {  // If this is the first record of a row, print a row beginner  if($i-1 % $amount_per_row == 0) {	    echo "<tr>";  }  // Print the record?><td>  <!-- If you want the background color to be black, use CSS to style it -->  <img src="../images/<?php $item_rows['item_id'] ?>.gif" width="45" height="45" /><!-- No need for br elements, blocks already do that for you --><!-- "p" tags are not a good choice here because there are no paragraphsm use "div" instead -->  <div><?php $item_rows['name'] ?></div>  <div>Attack: +<?php $item_rows['attack'] ?></div>  <div><img src="../images/credit.gif" width="17" height="17" />$<?php $item_rows['sell_price'] ?></div>  <div>[Equipt|Destory]</div></td><?php  // If this is the last record of a row, print a row end  if($i % $amount_per_row == 0) {	    echo "</tr>";  }  $i++}// Make sure a row end is printed if there are less records than the amount per row.if($i % $amount_per_row != 1) {  echo "</tr>"}echo '</table>';?>

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...