2

I've updated my PHP version to 7.4 and this error started showing afterwards. The error shows in the options, in <selected tag, when I try to maintain data even after the page has been refreshed.

<?PHP
     $posts = filter_input_array(INPUT_POST, FILTER_DEFAULT);
?>
<form method="post" name="create_posts">
<label>Categories available</label>
<select name="category_id" class="select_list">
  <option value="null">Pick a category: </option>

<?PHP
     $readCategs= new Read();
     $readCategs->query("SELECT DISTINCT ca.id AS cat_id,
                                      ca.title AS title, ca.content AS content
                                      FROM categories ca LEFT JOIN posts p ON p.cat_id = ca.id
                                        ORDER BY title ASC");
                            //var_dump($readCategs);
     if(!$readCategs->getResult()):
       echo "<a href='create_category'><option disabled='disabled'>
         There are no categories</option></a>";
     else:
       foreach($readCategs->getResult() AS $categs):
         echo "<option value=\"{$categs['cat_id']}\" class='options' ";
           if($posts['category_id'] == $categs['cat_id']):
             echo 'selected="selected" ';
           endif;
         echo ">{$categs['title']}</option>";
       endforeach;
     endif;
?>
</select>
</form>````        

The error shows on the "if($posts['category_id'] == $categs['cat_id']):" line

1 Answer 1

1

Tends to suggest either $posts or $categs is null or not an array. Try the follow to show which is not an array var_dump($posts); var_dump($categs);

You can test for an array with is_array

Sign up to request clarification or add additional context in comments.

1 Comment

Well, your suggestion didn't work, but I tried really hard to make it work, and it did!! Here's what I added: in ````if($posts['category_id'] == $categs['cat_id']):````` , I just put that condition inside of another one, if(isset($posts['$category_id)): if($posts['category_id'] == $categs['cat_id']): endif; It turns out that it'll only compare $posts['category_id] to $categs['cat_id'] when the array $posts has values in it. The issue was that this array didn't have value in it WHEN the comparison happened. Anyway, I appreciate your support, partner. Thanks

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.