...

View Full Version : Simple loop thing



ole90
07-31-2007, 01:32 PM
Hey,

i can't seem to figure out this simple solution- i don't think my brain is working today.


$count = 0;
while($plz=mysql_fetch_array($resolt)) {
$count++;
$itemid=$plz['itemid'];
$theitemid=$plz['ID'];
//Now create another while loop and grab the information from item table.
$dot="SELECT * FROM `items` WHERE `ID`='$itemid' LIMIT 0,5";
$geez=mysql_query($dot) or die(mysql_error());
$item=mysql_fetch_array($geez);
$itemname=$item['itemname'];
$itemurl=$item['itemimageurl'];
echo"<td width='200'><a href='items.php?id={$theitemid}'><center><img src='$itemurl'></center><br><center>$itemname</center></a> </td>";
//Break line for inventory
if($count == 5) {
echo"</tr><tr bgcolor='116FE4'>";
}

The problem here is that i keep having to type in if($count == 10) {
etc. I was wondering if there was a simple solution so that it would break the line every multiple of 5 without having to have different if statements.

timgolding
07-31-2007, 01:36 PM
yes you can divide by 5 then truncate

timgolding
07-31-2007, 01:38 PM
if (($count/5)==abs($count/5)) //<----My mistake
{// was a mutliple of 5 so break


}


It depends more on what these statements do after the condition

ole90
07-31-2007, 01:39 PM
Post an example =X? I don't quite understand what you mean.

edit: thanks!

ole90
07-31-2007, 01:48 PM
Hmm, that doesn't seem to be working. It just displays all items in the inventory in one big vertical line. One item per line. =X

EDIT: nevermind, i used ceil function instead of abs.

timgolding
07-31-2007, 01:53 PM
ignore that i didnt mean abs i meant floor

timgolding
07-31-2007, 01:53 PM
if (($count/5)==floor($count/5))
{// was a mutliple of 5 so break


}

timgolding
07-31-2007, 01:54 PM
Gimme 20 minutes ill test it for you

timgolding
07-31-2007, 01:56 PM
try this



<?php
if ($counter % 5 == 0) echo "multiple of 5";
?>

timgolding
07-31-2007, 02:09 PM
both these methods work i tested them i prefer the second one. Here is the whole thing:



<?PHP
$count = 0;
while($plz=mysql_fetch_array($resolt)) {
$count++;
$itemid=$plz['itemid'];
$theitemid=$plz['ID'];
//Now create another while loop and grab the information from item table.
$dot="SELECT * FROM `items` WHERE `ID`='$itemid' LIMIT 0,5";
$geez=mysql_query($dot) or die(mysql_error());
$item=mysql_fetch_array($geez);
$itemname=$item['itemname'];
$itemurl=$item['itemimageurl'];
echo"<td width='200'><a href='items.php?id={$theitemid}'><center><img src='$itemurl'></center><br><center>$itemname</center></a> </td>";
//Break line for inventory
if($count % 5 == 0) {
echo"</tr><tr bgcolor='116FE4'>";
}
}
?>

ole90
07-31-2007, 02:43 PM
Thanks alot :D I got it working now. Cheers!

timgolding
07-31-2007, 03:03 PM
cool :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum