Ruddy Posted February 29, 2012 Share Posted February 29, 2012 // Get the users armour names $user_armour = array($user_head, $user_upper, $user_hands, $user_lower, $user_feet); $i=0; while($i < count($user_armour)) { $query1="SELECT * FROM armour WHERE id='$user_armour[$i]'"; $result1=mysql_query($query1); $array1=mysql_fetch_array($result1); $armour_name[$i]=$array1["name"]; $i++; } // set the users armours name $user_prot = array($user_head, $user_upper, $user_hands, $user_lower, $user_feet); // Get the users overall protection $i=0; while($i < count($user_prot)) { $query1="SELECT * FROM armour WHERE id='$user_prot[$i]'"; $result1=mysql_query($query1); $array1=mysql_fetch_array($result1); $armour_prot[$i]=$array1["protection"]; $i++; } // set the users protection $user_protection=$armour_prot[0] + $armour_prot[1] + $armour_prot[2] + $armour_prot[3] + $armour_prot[4]; Is there a way to get them as one so: $user_armour = array($user_head, $user_upper, $user_hands, $user_lower, $user_feet); $i=0; while($i < count($user_armour)) { $query1="SELECT * FROM armour WHERE id='$user_armour[$i]'"; $result1=mysql_query($query1); $array1=mysql_fetch_array($result1); $armour_name[$i]=$array1["name"]; $armour_prot[$i]=$array1["protection"]; $i++; } // set the users armours name $user_prot = array($user_head, $user_upper, $user_hands, $user_lower, $user_feet); Will that work and if so how would you then get the 2 different values out of say "$user_prot[0]". Cheers guys, Ruddy Quote Link to comment https://forums.phpfreaks.com/topic/257992-array-loop-with-more-than-1-value/ Share on other sites More sharing options...
creata.physics Posted February 29, 2012 Share Posted February 29, 2012 In your code $user_prot and $user_armour are the exact same arrays, you can test them out by using print_r on the variables that contain those arrays. That immediately makes your code redundant and is where your problems start. Two get multiple values out of $user_prot like you are asking, you will need to assign another array to the key you want to have multiple values. So $user_prot[0] will need to be an array, it'll have two extra keys 0 and 1 which can contain the two values that you need. Also look into using foreach instead of while, i believe you'll find it easier to work with and recreate arrays that way. Quote Link to comment https://forums.phpfreaks.com/topic/257992-array-loop-with-more-than-1-value/#findComment-1322454 Share on other sites More sharing options...
batwimp Posted February 29, 2012 Share Posted February 29, 2012 Could you post your database table schema? Quote Link to comment https://forums.phpfreaks.com/topic/257992-array-loop-with-more-than-1-value/#findComment-1322461 Share on other sites More sharing options...
Ruddy Posted February 29, 2012 Author Share Posted February 29, 2012 Thank you creata.physics, I get what I was doing wrong now. fixed and the code is a lot less. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/257992-array-loop-with-more-than-1-value/#findComment-1322470 Share on other sites More sharing options...
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.