Setting # of items per row from database...

I am trying to do something like this

http://i26.photobucket.com/albums/c111/jch02140/Screenshots/img.gif

but the code I have doesn’t seem to do what I want…

[php] <?php
while($item_rows = mysql_fetch_array($item_result))
{
$output = "
<tr bgcolor="#000000">
<td align=“center”>

<img src="…/images/".$item_rows[‘item_id’].".gif" width=“45” height=“45” />


“.$item_rows[‘name’].”

Attack: +".$item_rows[‘attack’]."

<img src="…/images/credit.gif" width=“17” height=“17” />$".$item_rows[‘sell_price’]."


[Equipt|Destory]


";
$remainder = $i % 5;
	  echo ($remainder == 0 ? ($i == 0 ? "" : "\n</tr>")."\n$output " : $output);
   }
   $colspan = (4 - $remainder);
   $last_row = $colspan ? "  <td ".($colspan? "colspan=$colspan" : "")."></td>" : "";
   echo $last_row. "
    </tr>
  </table>";

?>[/php]

I am not a good programmer at all… but i have something similar on my site and if you are better than me, this COULD help you…

It grabs the rows in my data base and display it in 7 columns and creates as many rows as users i have… so idk if this can help you but here it goes

[php]

<?php mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error()); mysql_select_db(DB_NAME) or die(mysql_error()); $getitem = mysql_query("SELECT * FROM users ORDER BY timestamp DESC") or die(mysql_error()); $i =0; while ($rows = mysql_fetch_array($getitem)) { extract($rows); if ($i != 0 && $i % 7 == 0) { echo "\n \n"; echo "\n \n"; } echo "".$rows['username'] ."
Age: ".$rows['userage'] ."

Donation: $".$rows['user_status']."/hr"; $i++; } ?>

[/php]

The result is:
User Name - Age
PICTURE HERE
USER STATUS HERE
al wrapped inside a table row/column like your picture

I tried your code and replace the last echo with my content, but I got a parse error when I try to view the page

something about an unexpected $end after the last echo…

put at the very end of your code this

[php] <?php
}
?>[/php]

That might get rid of the unexpected $end

I actually just noticed a few errores in my previous code and fixed them. It now seems to work correctly. However, every data displayed in the table is the last record in the database… I am not sure how to fix it…

[php]<?php
$item_query = “SELECT * FROM game_item WHERE facebook_id = ‘$user’”;
$item_result = mysql_query($item_query) or die(mysql_error());
$item_rows = mysql_fetch_array($item_result);
$count_result = mysql_num_rows($item_result);

  	  ?>          
      <table width="500" border="0">          
          <tr>
            <td colspan="5">Bag Pack (<?php echo $count_result; ?>/15)</td>
            </tr>
            
            <tr bgcolor="#000000">
          	<?php				
		  	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$output " : $output);
			  
		  	}
   		  	$colspan = (4 - $remainder);
 		  	$last_row = $colspan ? "  <td ".($colspan? "colspan=$colspan" : "")."></td>" : "";
  		  	echo $last_row. "
		    </tr>
		  </table>";
		  ?>[/php]

do your user/items have a time stamp or registration? if so try adding ORDER BY timestamp DESC, or something like that… if that doesnt work use ORDER BY timestamp ASC

Not really. I don’t really have a timestamp field since it seems to be useless in my case, as I don’t need to reorder the item based on time and date…

how about id? id auto increment… ? almost every table has one of those as primary key

I have an item_id but since I am using the code for player’s inventory in a game… There is a possiblility of the player have multiple of the same item… As primary key must be unique… I guess it will not work in this case?..

Sponsor our Newsletter | Privacy Policy | Terms of Service