DeanWhitehouse Posted April 2, 2009 Share Posted April 2, 2009 Hiya, Long time since i have posted a coding problem, but here it goes. I have created this code to work out what a user can steal from another user, but it seems to allow the user to try and steal anything even if it is on the list of unstealable items, any ideas as to why? Does in_array return a different value than i thought? Code Snippet function StealType($extort) { $car = mysql_query("SELECT id FROM user_cars WHERE user_id = '".$extort['id']."' AND up_for_sale = '0' ORDER BY RAND() LIMIT 1"); $cash = mysql_query("SELECT money FROM user_stats WHERE user_id = '".$extort['id']."' LIMIT 1"); $c_check = mysql_fetch_assoc($cash); $bullets = mysql_query("SELECT bullets FROM user_stats WHERE user_id = '".$extort['id']."' LIMIT 1"); $b_check = mysql_fetch_assoc($bullets); $Not = array(); if(mysql_num_rows($car) == 0) { $Not[] = 1; } if($c_check['money'] < 100) { $Not[] = 2; } if($b_check['bullets'] == 0) { $Not[] = 3; } print_r($Not); if(in_array(array(1,2,3),$Not)) { echo "S1"; return false; } elseif(!in_array(array(1,2,3),$Not)) { echo "S2"; return rand(1,3); } elseif(!in_array(array(1),$Not) && in_array(array(2,3),$Not)) { echo "S3"; return 1; } elseif(!in_array(array(2),$Not) && in_array(array(1,3),$Not)) { echo "S4"; return 2; } elseif(!in_array(array(3),$Not) && in_array(array(1,2),$Not)) { echo "S5"; return 3; } elseif(!in_array(array(1,2),$Not) && in_array(array(3),$Not)) { echo "S6"; return rand(1,2); } elseif(!in_array(array(1,3),$Not) && in_array(array(2),$Not)) { echo "S7"; return 1; } elseif(!in_array(array(2,3),$Not) && in_array(array(1),$Not)) { echo "S8"; return rand(2,3); } } $steal = StealType($extort); if($steal == false) { echo "Failed! This user has nothing!"; } elseif($steal == 1) { echo "To come car"; } elseif($steal == 2) { echo "To come cash"; } elseif($steal == 3) { echo "To come bullets"; } else echo "Error! What the fuck?"; Quote Link to comment https://forums.phpfreaks.com/topic/152300-solved-in_array-return-values-or-coding-logic-wrong/ Share on other sites More sharing options...
Mark Baker Posted April 2, 2009 Share Posted April 2, 2009 bool in_array ( mixed $needle , array $haystack [, bool $strict ] ) You're reversing needle and haystack Quote Link to comment https://forums.phpfreaks.com/topic/152300-solved-in_array-return-values-or-coding-logic-wrong/#findComment-799800 Share on other sites More sharing options...
jonsjava Posted April 2, 2009 Share Posted April 2, 2009 also, I was looking at your SQL: wouldn't this be cleaner: $car = mysql_query("SELECT id FROM user_cars WHERE user_id = '".$extort['id']."' AND up_for_sale = '0' ORDER BY RAND() LIMIT 1"); $cash = mysql_query("SELECT money FROM user_stats WHERE user_id = '".$extort['id']."' AND `money` > 100 LIMIT 1"); $c_check = mysql_fetch_assoc($cash); $bullets = mysql_query("SELECT bullets FROM user_stats WHERE user_id = '".$extort['id']."' AND `bullets` > 1 LIMIT 1"); $b_check = mysql_fetch_assoc($bullets); ? Quote Link to comment https://forums.phpfreaks.com/topic/152300-solved-in_array-return-values-or-coding-logic-wrong/#findComment-799802 Share on other sites More sharing options...
DeanWhitehouse Posted April 2, 2009 Author Share Posted April 2, 2009 The sql would be better, i will change that and Mark, no i am not, please show me an example in my code where i am Quote Link to comment https://forums.phpfreaks.com/topic/152300-solved-in_array-return-values-or-coding-logic-wrong/#findComment-799808 Share on other sites More sharing options...
Mark Baker Posted April 2, 2009 Share Posted April 2, 2009 For in_array() to work in this case, you need to redefine $Not if(mysql_num_rows($car) == 0) { $Not[0][] = 1; } if($c_check['money'] < 100) { $Not[0][] = 2; } if($b_check['bullets'] == 0) { $Not[0][] = 3; } However, it's an incredibly complicated mechanism you're trying to use. Straight variable comparison would be easier; or if you have to use arrays you could use array_intersect() Quote Link to comment https://forums.phpfreaks.com/topic/152300-solved-in_array-return-values-or-coding-logic-wrong/#findComment-799815 Share on other sites More sharing options...
jonsjava Posted April 2, 2009 Share Posted April 2, 2009 cleaner version: <?php function StealType($extort) { $car = mysql_query("SELECT id FROM user_cars WHERE user_id = '".$extort['id']."' AND up_for_sale = '0' ORDER BY RAND() LIMIT 1"); $cash = mysql_query("SELECT money FROM user_stats WHERE user_id = '".$extort['id']."' AND `money` > 100 LIMIT 1"); $c_check = mysql_fetch_assoc($cash); $bullets = mysql_query("SELECT bullets FROM user_stats WHERE user_id = '".$extort['id']."' AND `bullets` > 1 LIMIT 1"); $b_check = mysql_fetch_assoc($bullets); $total = 0; if(mysql_num_rows($car) == 0) { $total = $total + 1; } if($c_check['money'] < 100) { $total = $total + 2; } if($b_check['bullets'] == 0) { $total = $total + 4; } switch ($total){ case 0: echo "S2"; return rand(1,3); break; case 1: echo "S8"; return rand(2,3); break; case 2: echo "S7"; return 1; break; case 3: echo "S5"; return 3; break; case 4: echo "S6"; return rand(1,2); break; case 5: echo "S4"; return 2; break; case 6: echo "S3"; return 1; break; case 7: echo "S1"; return false; break; } } $steal = StealType($extort); if($steal == false) { echo "Failed! This user has nothing!"; } elseif($steal == 1) { echo "To come car"; } elseif($steal == 2) { echo "To come cash"; } elseif($steal == 3) { echo "To come bullets"; } else echo "Error! What the fuck?"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/152300-solved-in_array-return-values-or-coding-logic-wrong/#findComment-799820 Share on other sites More sharing options...
DeanWhitehouse Posted April 2, 2009 Author Share Posted April 2, 2009 Still not working, and "Straight variable comparison would be easier; " not possible as i need a list of unaval steals, which a var can't have (not simply) Jons thanks, clever way to do it wouldn't of ever thought of that, topic solved (as far as i can tell) Quote Link to comment https://forums.phpfreaks.com/topic/152300-solved-in_array-return-values-or-coding-logic-wrong/#findComment-799822 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.