Jump to content
MakeWebGames

while loop


mikemastah

Recommended Posts

Hi I have a table in my MySQL DB 'shopitems' with the columns

id / name / attack / defence / costs / shop

which contains something like

grd / grenades / 800 / 0 / 950 / attack

snp / sniper / 1150 / 0 / 2000 / attack

etc. etc.

now in my shop i want to run the following code:

if ($_POST['grd']) {
   if (($_POST['grd'])*950 > $cash) {
       die (header("Location: /shop/money"));
   }
   else {
       $newgrenades = ($grenades + $_POST['grd']);
       mysql_query("UPDATE items SET grenades = '$newgrenades' WHERE username = '$nick'");
       $newatt = $attack + ($_POST['grd']*800);
       $newdef = $defence + ($_POST['grd']*0);
       $newcash = $cash - ($_POST['grd']*950);
       mysql_query("UPDATE users SET cashmoney = '$newcash', attack = '$newatt', defence = '$newdef' WHERE username = '$nick'");
   }
}

 

but then I would have to make that for every single item.

so I thought why not use a while loop. so here is my question.

Is it possible to use something like this?

(haven't tested it yet because my PHP installation is currently screwed up)

$q=mysql_query("SELECT * FROM shopitems");
while($shopitems=mysql_fetch_array($q)) {

if ($_POST[$shopitems['id']]) {
   if (($_POST[$shopitems['id']]*$shopitems['costs']) > $cash) {
       die (header("Location: /shop/money"));
   }
   else {
       $new = (${$shopitems['name']} + $_POST[$shopitems['id']]);
       mysql_query("UPDATE items SET $shopitems['name'] = '$new' WHERE username = '$nick'");
       $newatt = $attack + ($_POST[$shopitems['id']]*$shopitems['attack']);
       $newdef = $defence + ($_POST[$shopitems['id']]*$shopitems['defence']);
       $newcash = $cash - ($_POST[$shopitems['id']]*$shopitems['costs']);
       mysql_query("UPDATE users SET cashmoney = '$newcash', attack = '$newatt', defence = '$newdef' WHERE username = '$nick'");
   }
}

}
Link to comment
Share on other sites

Re: while loop

It does appear that you would be able to loop through all the rows in the table that way.

I'm not sure why you take an 'id' and multiply by 'costs'. I could see a 'qty' * 'costs'. But perhaps your naming conventions are just diff than mine.

So, I dun really get what exactly this code accomplishes, but it will iterate over all rows in that table. ;)

Link to comment
Share on other sites

Re: while loop

 

I'm not sure why you take an 'id' and multiply by 'costs'. I could see a 'qty' * 'costs'. But perhaps your naming conventions are just diff than mine.

It actually is a quantity, I have an input field with id $shopitems['id'] which says how many you want.

${$shopitems['name']} and $_POST[$shopitems['id']] will work then? as I'm not that certain about the first one.

Link to comment
Share on other sites

Re: while loop

You've got the right idea, but you also have syntax errors:

$new = (${$shopitems['name']} + $_POST[$shopitems['id']]);

You've got a $ and a { and } just sitting in there, as if you are in a string context and you are not in a string context there.

Link to comment
Share on other sites

Re: while loop

What I want to do is call an variable which is 'created' before this piece of code and the variable has the same name as $shopitems['name']

So when "$shopitems['name'] === grenades" I need to call $grenades..

can this be done easily or do I need a switch?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...