2

I'm trying to loop a table by comparing the results that I get from another table but it seems to be skipping the null index value.

What I want to get from the result is if one of the value of $a can be found on $b, it should echo the loop that it is associated or rather the index of that array.

       <ol>
            <?php
            $questions = db_query("SELECT * FROM tbl_questions");
            while ($quests = $questions->fetch_assoc()) {
                $b[] = $quests['question_id'];
                $comp = db_select("SELECT * FROM tbl_votes WHERE `user_id` = $userids");
                foreach($comp as $compare){
                    $check[] = $compare['question_id'];
                }
                $a = array_intersect($b, $check);
                if($a){
                    echo "<li><a class='qstyle' href='questions/".$quests['question_id'].".php'>".$quests['question_title']."</a></li>";
                }
                elseif($quests['question_live'] == 1){
                        echo "<li><strong><a class='qstyle' href='vote_page.php?var=".$quests['question_id']."'>".$quests['question_title']."</a></strong></li>";
                }
                elseif($quests['question_live'] == 0){
                    echo "<li style='color:#968c8d'>".$quests['question_title']."</li>";
                }                       
            }
            ?>
        </ol>
0

1 Answer 1

1

you questions array loop should be closed before you intersect with your $check, or you cant get the total questions.

Also, you need to alter your logic to show the difference. Based on your requirement, I dont think you should use array_intersect.

Actually if you use print_r($b); after $b[] = $quests['question_id']; you will find your $b is not what you expect.

Try this:

<ol>
            <?php
            $questions = db_query("SELECT * FROM tbl_questions");
            while ($quests = $questions->fetch_assoc()) {
                $b[$quests['question_id']] = $quests;
            }
            $comp = db_select("SELECT * FROM tbl_votes WHERE `user_id` = $userids");
            foreach($comp as $compare){
                $check[] = $compare['question_id'];
            }
            foreach($b as $qid=>$quest){
                if(in_array($qid, $check)) echo "<li><a class='qstyle' href='questions/".$qid.".php'>".$quest['question_title']."</a></li>";
                elseif($quest['question_live'] == 1) echo ...;
                elseif($quest['question_live'] == 0) echo ...;
            }
            ?>
        </ol>
Sign up to request clarification or add additional context in comments.

Comments

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.