Jump to content

Recommended Posts

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. :D

Link to comment
https://forums.phpfreaks.com/topic/205692-using-in_array-with-variables/
Share on other sites

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.

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.)

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?

I have each item set up in a table, and I would like the status message to be displayed next to it, like this:

 

2rzw6du.jpg

(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...

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>";
       }
     }
     }

           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>";
             }
         }

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. :D

 

Thank you very much.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.