mikemastah Posted April 18, 2009 Posted April 18, 2009 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'"); } } } Quote
Floydian Posted April 18, 2009 Posted April 18, 2009 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. ;) Quote
mikemastah Posted April 18, 2009 Author Posted April 18, 2009 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. Quote
Floydian Posted April 18, 2009 Posted April 18, 2009 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. Quote
mikemastah Posted April 18, 2009 Author Posted April 18, 2009 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? Quote
Floydian Posted April 18, 2009 Posted April 18, 2009 Re: while loop Variable variables.... $$shopitems['name'] // assuming $shopitems['name'] holds the value of grenades is equivalent to: $grenades http://us2.php.net/manual/en/language.v ... riable.php Quote
mikemastah Posted April 18, 2009 Author Posted April 18, 2009 Re: while loop wow ok cool thanks Quote
DELETE ME NOW! Posted April 18, 2009 Posted April 18, 2009 Re: while loop Floydian thanks for this link (http://us2.php.net/manual/en/language.v ... riable.php) ty Quote
Floydian Posted April 19, 2009 Posted April 19, 2009 Re: while loop You're welcome Crazy-T and mikemastah Do keep in mind that if you ever do something like $blah = $_GET['id']; $foo = $_GET['amount']; $$blah = $foo; That you have a SERIOUS SECURITY risk. Use variable variables with caution :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.