SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Wizard GoldFire's Avatar
    Join Date
    Oct 2002
    Location
    Oklahoma City, OK
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Problem With Tables And While Loops....

    okay..i am making virtual shops for my virtual pet site, and i have gotten it all to work...except i don't know how to make it where the items go next to eachother in the same row for like 5 or 6 rows and then go to the next line. go to www.schooglepets.net/testshop.php to see what it is doing for me.

    My code is:

    PHP Code:
    <?
    include ("preferences.inc.php");
    $name $HTTP_COOKIE_VARS["usernamecookie"];
    $get_the_user mysql_fetch_array(mysql_query("SELECT * FROM tblUsers WHERE username = '$name'"));
    $get_the_items mysql_query("SELECT * FROM tblItems WHERE shop = 'Food Shop' ORDER BY itemtypeid");
    $time date("YmdHis");
        if (
    $action == "")
        {
            echo 
    "$openHTML<p align=center><b>Food Shop</b><br>In this shop you can buy most any Androdian food you can think of.  From sheriages to apples...we have it all!</p>";
            while (
    $result mysql_fetch_array($get_the_items))
            {
                    if (
    $result[instock] != 0)
    {

                print 
    "$openHTML";
                
    ?>
                    <table border=0 width=100>
                        <tr>
                            <td width=100><a href="testshop.php?action=get&item=<?=$result[name]?>"><img src="images/items/<?=$result[itemimg]?>" height="80" width="80" alt="<?=$result[descr]?>" border="0"></a><p>
                            <center><font size=2><b><?=$result[name]?></b><br><b><?=$result[instock]?> in stock</b><br><b>Price : <?=$result[price]?> SL</b>
                            </font></center></td>
                        </tr>
                    </table>
                <?
                
    print "$closeHTML";
                }
                }
                }
                
    $get_the_items2 mysql_query("SELECT * FROM tblItems WHERE shop = 'Food Shop' && name = '$item'");
                
    $get_the_lastbuy mysql_query("SELECT lastbuy FROM tblItems WHERE username = '$name'");
    if (
    $action == "get")    
    {
        if (
    mysql_num_rows($get_the_items2) == "0")
        {
        echo 
    "You have tried to purchase a fake item!";
        }
            if (
    $get_the_lastbuy[lastbuy] >= $time 5)
            {
            echo 
    "You may only buy an item every 5 seconds!";
            }
        
    $price mysql_fetch_array(mysql_query("SELECT price FROM tblItems WHERE name = '$item'"));
        
    mysql_query("INSERT INTO tblUserItems SET username = '$name', itemtype = '$item', dob = '$time'");
        
    mysql_query("UPDATE tblUsers SET points = points - '$price[price]' WHERE username = '$name'");
        
    mysql_query("UPDATE tblUserItems SET lastbuy = $time WHERE username = '$name'");
        print 
    "<meta http-equiv=refresh content=1;url=testshop.php>";
        
        echo 
    "$openHTML<p align=center><b>Food Shop</b><br>In this shop you can buy most any Androdian food you can think of.  From sheriages to apples...we have it all!</p>";
            while (
    $result mysql_fetch_array($get_the_items))
            {
                            if (
    $result[instock] != 0)
                            {
                print 
    "$openHTML";
                
    ?>
                    <table border=0 width=100>
                        <tr>
                            <td><a href="testshop.php?action=get&item=<?=$result[name]?>"><img src="images/items/<?=$result[itemimg]?>" height="80" width="80" alt="<?=$result[descr]?>" border="0"></a><p>
                            <center><font size=2><b><?=$result[name]?></b><br><b><?=$result[instock]?> in stock</b><br><b>Price : <?=$result[price]?> SL</b>
                            </font></center></td>
                        </tr>
                    </table>
                
                <?
                
    print "$closeHTML";
                }
                }
                }

        
    ?>

  2. #2
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use this code for that. Maybe you can make it work for you.

    PHP Code:
        $columns 4;
        
    //we add this line because we need to know the number of rows
        
    $num_rows mysql_num_rows($result);
        for(
    $i 0$i $num_rows$i++) {

        if(
    $i $columns == 0) {
            
    //if there is no remainder, we want to start a new row
            
    echo "<TR>\n";
        }

        if((
    $i $columns) == ($columns 1) || ($i 1) == $num_rows) {
            
    //if there is a remainder of 1, end the row
            //or if there is nothing left in our result set, end the row
        
    echo "</TR>\n";
        }


  3. #3
    SitePoint Wizard GoldFire's Avatar
    Join Date
    Oct 2002
    Location
    Oklahoma City, OK
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how would i put that into my code? I messed around with it for a while and couldn't figure out how to get it in...

  4. #4
    SitePoint Wizard xyuri's Avatar
    Join Date
    Jul 2002
    Location
    Brisbane
    Posts
    1,487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Schooglepets, try the following code, it will most likely work, but might not ( as usual )

    PHP Code:
    <?
    include ("preferences.inc.php");
    $name $HTTP_COOKIE_VARS["usernamecookie"];
    $get_the_user mysql_fetch_array(mysql_query("SELECT * FROM tblUsers WHERE username = '$name'"));
    $get_the_items mysql_query("SELECT * FROM tblItems WHERE shop = 'Food Shop' ORDER BY itemtypeid");
    $time date("YmdHis");
    ?>

    <Table cellpdding="6px" cellspacing="0px" border="1px">

    <?PHP

    $cols 
    = (int)4;
    $rows = (int)5;

    $x = (int)0;
    $i = (int)0;

    IF (
    $action == "")
        {
        ECHO 
    "<center><u><b>Food Shop</b></u> :)</center>";

        While (
    $i $rows)
            {
            ECHO 
    '<tr>&nbsp;';
            While (
    $result mysql_fetch_array($get_the_items) AND $x $cols)
                {
                IF (
    $result[instock] != (int)0)
                    {
                    
    ?>

                        <td width=100><a href="testshop.php?action=get&item=<?=$result[name]?>"><img src="images/items/<?=$result[itemimg]?>" height="80" width="80" alt="<?=$result[descr]?>" border="0"></a><p>
                        <center><font size=2><b><?=$result[name]?></b><br><b><?=$result[instock]?> in stock</b><br><b>Price : <?=$result[price]?> SL</b>
                        </font></center></td>

                    <?PHP
                    
    }
                ELSE
                    {
                        ECHO    
    $result[name] . ' - <b>Out Of Stock</b>';
                    }
                    
    $x++;
                } Unset(
    $x);
            ECHO 
    '</tr>';
            
    $i++;
            } Unset(
    $i);
    ?>

    </table>

    <?PHP
                $get_the_items2 
    mysql_query("SELECT * FROM tblItems WHERE shop = 'Food Shop' && name = '$item'");
                
    $get_the_lastbuy mysql_query("SELECT lastbuy FROM tblItems WHERE username = '$name'");

    if (
    $action == "get")    
    {
        if (
    mysql_num_rows($get_the_items2) == "0")
        {
        echo 
    "You have tried to purchase a fake item!";
        }
            if (
    $get_the_lastbuy[lastbuy] >= $time 5)
            {
            echo 
    "You may only buy an item every 5 seconds!";
            }
        
    $price mysql_fetch_array(mysql_query("SELECT price FROM tblItems WHERE name = '$item'"));
        
    mysql_query("INSERT INTO tblUserItems SET username = '$name', itemtype = '$item', dob = '$time'");
        
    mysql_query("UPDATE tblUsers SET points = points - '$price[price]' WHERE username = '$name'");
        
    mysql_query("UPDATE tblUserItems SET lastbuy = $time WHERE username = '$name'");
        print 
    "<meta http-equiv=refresh content=1;url=testshop.php>";
        
        echo 
    "$openHTML<p align=center><b>Food Shop</b><br>In this shop you can buy most any Androdian food you can think of.  From sheriages to apples...we have it all!</p>";
            while (
    $result mysql_fetch_array($get_the_items))
            {
                            if (
    $result[instock] != 0)
                            {
                print 
    "$openHTML";
                
    ?>
                    <table border=0 width=100>
                        <tr>
                            <td><a href="testshop.php?action=get&item=<?=$result[name]?>"><img src="images/items/<?=$result[itemimg]?>" height="80" width="80" alt="<?=$result[descr]?>" border="0"></a><p>
                            <center><font size=2><b><?=$result[name]?></b><br><b><?=$result[instock]?> in stock</b><br><b>Price : <?=$result[price]?> SL</b>
                            </font></center></td>
                        </tr>
                    </table>
                
                <?
                
    print "$closeHTML";
                }
                }
                }

        
    ?>

  5. #5
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I left out the display part. This prints out $sector['sector'] in four columns and then starts another row.


    PHP Code:
         //set the number of columns
        
    $columns 4;
            echo 
    "<tr>";
            echo 
    "<td colspan=\"4\" align=\"center\"><br /><br /><font color=\"#00ffff\"><h3>Sectors</h3></font></td>";
            echo 
    "</tr>";
        
    //we add this line because we need to know the number of rows
        
    $num_rows mysql_num_rows($result);
        for(
    $i 0$i $num_rows$i++) {
    $sector mysql_fetch_array($result);
        if(
    $i $columns == 0) {
            
    //if there is no remainder, we want to start a new row
            
    echo "<tr>\n";
        }
       echo 
    "<td><b><font color=\"#00ffff\">" $sector['sector'] . "</font></b></font></td>\n";
        if((
    $i $columns) == ($columns 1) || ($i 1) == $num_rows) {
            
    //if there is a remainder of 1, end the row
            //or if there is nothing left in our result set, end the row
        
    echo "</tr>\n";
        }

    Last edited by Jump; Jan 28, 2003 at 23:41.

  6. #6
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a good tutorial on multi row output. http://codewalkers.com/tutorials/15/1.html

  7. #7
    SitePoint Wizard xyuri's Avatar
    Join Date
    Jul 2002
    Location
    Brisbane
    Posts
    1,487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jump, you very srongly doubt that my code will work

    *sniff, sniff*

  8. #8
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, not at all. Why would I doubt that it would work?

  9. #9
    SitePoint Wizard GoldFire's Avatar
    Join Date
    Oct 2002
    Location
    Oklahoma City, OK
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank both of you very much It now works!!

  10. #10
    SitePoint Wizard xyuri's Avatar
    Join Date
    Jul 2002
    Location
    Brisbane
    Posts
    1,487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jump, I was just pulling your leg

    well, schooglepets, well done

  11. #11
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So I guess we'll never know which way schoogle made it work.

  12. #12
    SitePoint Wizard GoldFire's Avatar
    Join Date
    Oct 2002
    Location
    Oklahoma City, OK
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have a problem with the script. the ones on the second row (7 items total) when you click them it takes away the points and takes away 1 from the stock....but it doesn't put the item into the db. All the other items work fine. Also the fifth item doesn't show.

    The altered script :

    PHP Code:
    <?
    include ("preferences.inc.php");
    $name $HTTP_COOKIE_VARS["usernamecookie"];
    $get_the_user mysql_fetch_array(mysql_query("SELECT * FROM tblUsers WHERE username = '$name'"));
    $get_the_items mysql_query("SELECT * FROM tblItems WHERE shop = 'Food Shop' ORDER BY itemtypeid");
    $time date("YmdHis");

    $cols = (int)4;
    $rows = (int)5;

    $x = (int)0;
    $i = (int)0;
    if (
    $_GET[$action] == "")
        {
        echo 
    "$openHTML<p align=center><b>Food Shop</b><br>In this shop you can buy most any Androdian food you can think of.  From sheriages to apples...we have it all!</p><div align=center><img src=/images/food_shop_shopkeeper.gif></div><table cellpdding=6px cellspacing=0px border=0 align=center>";

        while (
    $i $rows)
            {
            echo 
    "<tr>&nbsp;";
            while (
    $result mysql_fetch_array($get_the_items) AND $x $cols)
                {
                if (
    $result[instock] > (int)0)
                    {
                    print 
    "$openHTML";
                    
    ?>

                        <td width=150 align=center><a href="foodshop.php?action=get&item=<?=$result[name]?>"><img src="images/items/<?=$result[itemimg]?>" height="80" width="80" alt="<?=$result[descr]?>" border="0"></a><p>
                        <font size=2><b><?=$result[name]?></b><br><b><?=$result[instock]?> in stock</b><br><b>Price : <?=$result[price]?> SL</b>
                        </font></td>

                    <?PHP
                    
    print "$closeHTML";
                    }
                    
    $x++;
                } unset(
    $x);
            echo 
    "</tr>";
            
    $i++;
            } unset(
    $i);
    ?>

    </table>

    <?PHP
                $get_the_items2 
    mysql_query("SELECT * FROM tblItems WHERE shop = 'Food Shop' && name = '$item'");
                
    $get_the_items3 mysql_fetch_array(mysql_query("SELECT * FROM tblItems WHERE shop = 'Food Shop' && name = '$item'"));
                
    $get_the_lastbuy mysql_fetch_array(mysql_query("SELECT lastbuy FROM tblUserItems WHERE username = '$name'"));
    if (
    $action == "get")    
    {
        if (
    mysql_num_rows($get_the_items2) == "0")
        {
        echo 
    "You have tried to purchase a fake item!";
        }
        if (
    $get_the_items3[instock] == "0")
        {
        echo 
    "That item is out of stock!";
        }
            if (
    $get_the_lastbuy[lastbuy] >= $time 5)
            {
            echo 
    "You may only buy an item every 5 seconds!";
            }
            
    $result3 mysql_fetch_array(mysql_query("SELECT * FROM tblItems WHERE name = '$item'"));
        
    $stuff mysql_fetch_array(mysql_query("SELECT price, itemimg, weight, est_price, descr, rarity FROM tblItems WHERE name = '$item'"));
        
    mysql_query("INSERT INTO tblUserItems SET username = '$name', itemtype = '$item', dob = '$time', itemimg = '$stuff[itemimg]', weight = '$stuff[weight]', est_price = '$stuff[est_price]', descr = '$stuff[descr]', rarity = '$stuff[rarity]'");
        
    mysql_query("UPDATE tblUsers SET points = points - '$stuff[price]' WHERE username = '$name'");
        
    mysql_query("UPDATE tblUserItems SET lastbuy = '$time' WHERE username = '$name'");
        if (
    $result3[instock] > 0)
        {
        
    mysql_query("UPDATE tblItems SET instock = instock - 1 WHERE name = '$item'");
        }
        print 
    "$openHTML<meta http-equiv=refresh content=1;url=foodshop.php>$closeHTML";
                }
                }
        
    ?>


Bookmarks

Posting Permissions

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