need help looping through and updating multiple records

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

need help looping through and updating multiple records

Post by cdoyle »

Hi,

I got my market place all built for my game, and now finishing up the very last part which I thought would be easy.
I need to run a nightly cron that updates how many days the item is left in the market, and then when it reaches 0. It puts the item back into the players inventory.

I've been able to come up with the code below in a test page. It almost works, but it only updates the first row in the table each time it runs. So I think I need a while loop?

I tried changing
$marketview1 = $getmarket->fetchrow();
to

while ($marketview1 = $getmarket->fetchrow())

but that didn't work, the test page wouldn't do anything when I made that change.
What do I need to do to make it so it updates all records when the days_left reach 0?

Code: Select all

 
  $getmarket = $db->execute("Select `market_ID`, `Seller_ID`, `Item_Sold_ID`, `days_left` FROM market_new");
    $marketview1 = $getmarket->fetchrow();
    $sellerid=$marketview1['Seller_ID'];
    $sellitem=$marketview1['Item_Sold_ID'];
    $marketid=$marketview1['market_ID'];
    if ($getmarket->recordcount() >= 1)
    {
      
        if ($marketview1['days_left'] <= 1) 
        {
            $inventorycheck = $db->execute("Select `id`, `player_id`, `item_id` FROM `items` Where `player_id`=? and `item_id`=?", array($sellerid, $sellitem));
     
            if ($inventorycheck->recordcount() == 0)
            {
                $insert = $db->execute("INSERT INTO items (player_id,item_id,status,quantity) VALUES ('$sellerid;','$sellitem', 'unequipped','1')");    
            }
            else
            {
                $updatequantity = $db->execute("UPDATE `items` SET `quantity` = `quantity` + ? WHERE `player_id`=? and `item_id`=?", array(1, $sellerid, $sellitem )); 
        
            }
            $deleteitem = $db->execute("delete from `market_new` where `market_ID`=?", array($marketid));                     
        }
        else
        {
            $updatedays = $db->execute("UPDATE `market_new` SET `days_left` = `days_left` - ?", array(1));         
  
        }
    }
 
?> 
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: need help looping through and updating multiple records

Post by requinix »

How do you expect it to do more than one if you don't have any kind of loop?

Use that while loop substitution you tried, but remember to put {}s in the right place. Or simply remember to use them in the first place.

Code: Select all

while ($marketview1 = $getmarket->fetchrow()) {
    stuff to do for each thing
}
Post Reply