Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Simple loop thing

    Hey,

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

    PHP Code:
     $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.

  • #2
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    yes you can divide by 5 then truncate
    You can not say you know how to do something, until you can teach it to someone else.

  • #3
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    PHP Code:
    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
    Last edited by timgolding; 08-01-2007 at 12:07 AM.
    You can not say you know how to do something, until you can teach it to someone else.

  • #4
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Post an example =X? I don't quite understand what you mean.

    edit: thanks!
    Last edited by ole90; 07-31-2007 at 01:41 PM.

  • #5
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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.
    Last edited by ole90; 07-31-2007 at 01:52 PM.

  • #6
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    ignore that i didnt mean abs i meant floor
    You can not say you know how to do something, until you can teach it to someone else.

  • #7
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    PHP Code:
    if (($count/5)==floor($count/5))
    {
    // was a mutliple of 5 so break



    Last edited by timgolding; 08-01-2007 at 12:07 AM.
    You can not say you know how to do something, until you can teach it to someone else.

  • #8
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Gimme 20 minutes ill test it for you
    You can not say you know how to do something, until you can teach it to someone else.

  • #9
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    try this

    PHP Code:
    <?php
    if ($counter == 0) echo "multiple of 5";
    ?>
    You can not say you know how to do something, until you can teach it to someone else.

  • #10
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    both these methods work i tested them i prefer the second one. Here is the whole thing:

    PHP Code:
    <?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 == 0)  {
     echo
    "</tr><tr bgcolor='116FE4'>";
     } 
    }
    ?>
    You can not say you know how to do something, until you can teach it to someone else.

  • #11
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks alot I got it working now. Cheers!

  • #12
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    cool
    You can not say you know how to do something, until you can teach it to someone else.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •