0

Im trying to convert data from one table to another, from the ITEMS_VEHICLES table to the VEHICLES table. My code is this:

$sql_result = mysql_query("SELECT * FROM items_vehicles", $db); 
while ($rs = mysql_fetch_array($sql_result)) {
    if ($rs[motorbike] > 0) { mysql_query("INSERT INTO vehicles (type, player, hp) VALUES('1', '$rs[player]', '50')"); }
    if ($rs[banger] > 0) { mysql_query("INSERT INTO vehicles (type, player, hp) VALUES('2', '$rs[player]', '50')"); 
}

etc

But the $rs[motorbike] and other fields i want them to insert that code the amount of times in that column. Example, if $rs[motorbike] was '5', I want that insert query ran 5 times.

I'm in a bind, not sure how to do it.

3
  • This: '$rs[player]' should be '{$rs[player]}' or '".$rs[player]."'. You should also avoid using inherent strings (such as $rs[player], see player) and instead use quoted strings like '{$rs["player"]}'. Commented Nov 25, 2011 at 21:49
  • Please put quotes around your array keys, and turn on display_errors, as your code produces all sorts of notices you must not be seeing. $rs['motorbike'] Commented Nov 25, 2011 at 21:49
  • Your code as is will produce lots of notices like Notice: Use of undefined constant motorbike Commented Nov 25, 2011 at 21:51

2 Answers 2

2

Judging by your explanation, something like this:

$sql_result = mysql_query("SELECT * FROM items_vehicles", $db); 
while ($rs = mysql_fetch_array($sql_result)) {
    for ($i = 1; $i <= $rs['motorbike']; $i++) { 
        mysql_query("INSERT INTO vehicles (type, player, hp) VALUES('1', '$rs[player]', '50')"); 
    }
    for ($i = 1; $i <= $rs['banger']; $i++) { 
        mysql_query("INSERT INTO vehicles (type, player, hp) VALUES('2', '$rs[player]', '50')");    
    }
    etc
}
Sign up to request clarification or add additional context in comments.

5 Comments

thanks, what if i wanted a restriction of 5 max on each one, is that hard to edit?
for ($i = 1; $i <= min(5, $rs['motorbike']); $i++)
Is it running more than once for the same user? e.g. once with $rs['motorbike'] set to 4 and another with it set to 2, for example? That would make 6 in total, because each time it runs the number is less than 5. If not that, I don't know why it's not working. min(5, $rs['motorbike']) should return the minimum value of either 5 or $rs['motorbike'].
oh, i want it to either be $rs[motorbike] or 5 max. So if rs[motorbike] is over 5, then it just stops, 5 being max.
Yes, so you want the minimum of 5 and $rs['motorbike']; but if there is more than one record for a given user, he might still end up with more than 5 doing it that way.
0

Replace your if statements with a for loop that runs x times, where x is the size of $rs['motorbike'].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.