Ryukotsusei Posted June 23, 2010 Share Posted June 23, 2010 I'm having a problem using the in_array() function. First I'll post my code and then I'll explain. for($i=0;$i<count($dragon_array);$i++) { if(in_array($dragon_array[$i],$codes_array)) { $status="$code was selected."; } else { $status="$code was not selected."; } } $dragon_array is my master list of values. $codes_array is an array containing only checked values from a form. I'm trying to display one message for checked items and another message for unchecked items, and I was told using a method like this should work. However, all of the items are showing the "not selected" status. I've also tried using a foreach loop and a while loop, but neither made any difference. I feel like there's some very obvious thing I'm doing wrong that just isn't jumping out at me. Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/205692-using-in_array-with-variables/ Share on other sites More sharing options...
brianlange Posted June 24, 2010 Share Posted June 24, 2010 Use array_diff to get all the values that were not selected http://us2.php.net/manual/en/function.array-diff.php array_diff($dragon_array,$codes_array) You don't need to write code for the selected items since you have that array already. Quote Link to comment https://forums.phpfreaks.com/topic/205692-using-in_array-with-variables/#findComment-1076321 Share on other sites More sharing options...
Ryukotsusei Posted June 24, 2010 Author Share Posted June 24, 2010 I had thought about using the array_diff, but then all I'm left with is the two arrays of selected and unselected items. How would I then assign the different status message to each value within the arrays? Currently they all still say "not selected" even with the changes. Eventually I want the items to be added and removed from a database depending on whether they were checked or not, but I was starting out with the simple status messages for testing purposes. Quote Link to comment https://forums.phpfreaks.com/topic/205692-using-in_array-with-variables/#findComment-1076332 Share on other sites More sharing options...
Ryukotsusei Posted June 24, 2010 Author Share Posted June 24, 2010 I went back to my original code without using the array_diff, and it now does appear to work. I think it had something to do with the placement of this code within the loops of the page. However, now I have a new problem. When using this code, it echoes the correct status message as I wanted. for($i=0;$i<$max;$i++) { if(in_array($dragon_array[$i],$codes_array)) { echo "$code was selected."; } else { echo "$code was not selected."; } } But when I try to change it to this, $status displays nothing at all. for($i=0;$i<$max;$i++) { if(in_array($dragon_array[$i],$codes_array)) { $status="$code was selected."; } else { $status="$code was not selected."; } } Why might that be? (Also, I know double-posting is generally frowned on in most forums, but I did not see an edit button. I apologize.) Quote Link to comment https://forums.phpfreaks.com/topic/205692-using-in_array-with-variables/#findComment-1076343 Share on other sites More sharing options...
Andy-H Posted June 24, 2010 Share Posted June 24, 2010 I had thought about using the array_diff, but then all I'm left with is the two arrays of selected and unselected items. How would I then assign the different status message to each value within the arrays? Currently they all still say "not selected" even with the changes. Eventually I want the items to be added and removed from a database depending on whether they were checked or not, but I was starting out with the simple status messages for testing purposes. $not_selected = array_diff($codes_array, $dragon_array); $selected = array_diff($codes_array, $not_selected); echo 'Codes selected: ' . implode(', ', $selected) . '.<br ><br >'; echo 'Codes not selected: ' . implode(', ', $not_selected) . '.<br >'; Would that work? Quote Link to comment https://forums.phpfreaks.com/topic/205692-using-in_array-with-variables/#findComment-1076349 Share on other sites More sharing options...
Ryukotsusei Posted June 24, 2010 Author Share Posted June 24, 2010 I have each item set up in a table, and I would like the status message to be displayed next to it, like this: (Not my site) I do now have it correctly identifying which ones are checked and which ones aren't, but I can't figure out how to get the status message next to each one. I think it's going through the entire loop and then only saving the final message to status and giving that message to all of the items. I'm just not quite sure how to fix that without completely rethinking everything... Quote Link to comment https://forums.phpfreaks.com/topic/205692-using-in_array-with-variables/#findComment-1076354 Share on other sites More sharing options...
Andy-H Posted June 24, 2010 Share Posted June 24, 2010 Is your table displayed dynamically by PHP, in that case it will be easy, please show the code that displays your version of that table, or full code if it isn't too long? Quote Link to comment https://forums.phpfreaks.com/topic/205692-using-in_array-with-variables/#findComment-1076359 Share on other sites More sharing options...
Ryukotsusei Posted June 24, 2010 Author Share Posted June 24, 2010 This page uses a 3-step form, so displaying the full code would be quite long. I'll just place the relevant part in since everything else works correctly: elseif (isset($submitdragons)) { $data = unserialize(file_get_contents("http://dragcave.net/api/KEY/serialize/user_young/$scrollname")); foreach($data['errors'] as $error) { if($error[0] == 3) {echo "Sorry, we were unable to find a user with that code."; return;} else { $codes_array=$_POST["codes"]; $dragon_array=$_POST['dragonid']; $max=sizeof($dragon_array); echo "<b>User:</b> $scrollname <br /> <br /> <form name='finalform' action='$PHP_SELF' method='post'> <input type='hidden' name='scroll' value='{$_POST['scroll']}' /> <table border='0'> <tr> <td> <b>Dragon</b> </td> <td> <b>Gender</b> </td> <td> <b>Views</b> </td> <td> <b>Unique Views</b> </td> <td> <b>Clicks</b> </td> <td> <b>Time Left</b> </td> <td> <b>Status</b> </td> </tr>"; foreach($data['dragons'] as $dragon) { $code=$dragon['id']; $gender=$dragon['gender']; $views=$dragon['views']; $unique=$dragon['unique']; $clicks=$dragon['clicks']; $hours=$dragon['hoursleft']; $d=floor($hours / 24); $dmult=$d * 24; $h=ceil($hours - $dmult); for($i=0;$i<$max;$i++) { if(in_array($dragon_array[$i],$codes_array)) { $status="$dragon_array[$i] was selected."; } else { $status="$dragon_array[$i] was not selected."; } } if($hours != '-2') { echo "<tr> <td> <a href='http://www.dragcave.net/view/$code' target='_blank'><img src='http://www.dragcave.net/image/$code' border='0'></a> </td> <td> $gender </td> <td> $views </td> <td> $unique </td> <td> $clicks </td> <td> $d days $h hours </td> <td> $status </td>"; } } echo "</tr> </table> </form>"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/205692-using-in_array-with-variables/#findComment-1076365 Share on other sites More sharing options...
Andy-H Posted June 24, 2010 Share Posted June 24, 2010 for($i=0;$i<$max;$i++) { if(in_array($dragon_array[$i],$codes_array)) { $status="$dragon_array[$i] was selected."; } else { $status="$dragon_array[$i] was not selected."; } } It will just run to the last one like you said and display that for each of them, IF the codes array has the posted dragon ID's you can do this. foreach($data['dragons'] as $dragon) { $code=$dragon['id']; $gender=$dragon['gender']; $views=$dragon['views']; $unique=$dragon['unique']; $clicks=$dragon['clicks']; $hours=$dragon['hoursleft']; $d=floor($hours / 24); $dmult=$d * 24; $h=ceil($hours - $dmult); if(in_array($code,$codes_array)) { $status="$code was selected."; } else { $status="$code was not selected."; } if($hours != '-2') { echo "<tr> <td> <a href='http://www.dragcave.net/view/$code' target='_blank'><img src='http://www.dragcave.net/image/$code' border='0'></a> </td> <td> $gender </td> <td> $views </td> <td> $unique </td> <td> $clicks </td> <td> $d days $h hours </td> <td> $status </td>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/205692-using-in_array-with-variables/#findComment-1076367 Share on other sites More sharing options...
Ryukotsusei Posted June 24, 2010 Author Share Posted June 24, 2010 Removing the for loop like you showed caused them all to display with the "not selected" message, which is the problem I originally had. EDIT: Success! I didn't notice a few of the changes you made, so when I added them back in it works. Thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/205692-using-in_array-with-variables/#findComment-1076370 Share on other sites More sharing options...
Andy-H Posted June 24, 2010 Share Posted June 24, 2010 Gdgd, no problem Quote Link to comment https://forums.phpfreaks.com/topic/205692-using-in_array-with-variables/#findComment-1076371 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.