How to check if element of array exists in Mysql?

I have a static menu with a list of courses, everything is static.

I want the menu to display only active courses, I want to adopt the same order.

I have all the contents of the submenu in a variable, if active in the DB, and if not active, then it is not displayed.

I am learning and I want to do it right.

But this code doesn't work yet. Can anyone help me?

<?php
//conexion DB
$dbLink = new PDO('mysql:host=localhost;dbname=dbname;charset=utf8', 'username', 'password', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

//array for order
$coursesarr = array('spanish', 'english', 'latin');


//query mysql
$qry_courses = "SELECT `shortname`, `visible` FROM `courses` WHERE visible = 1";


//define static varibles
$spanish = '<li><a href="/spanish.html">spanish</a>
<ul>
    <li>task 1 select course</li>
    <li>task 2 select course</li>
    <li>task 3 select course</li>
    ...
</ul></li>';
$english = '<li><a href="/english.html">English</a>
<ul>
    <li>task 1 select course</li>
    <li>task 2 select course</li>
    <li>task 3 select course</li>
    ...
</ul></li>';
$latin = '<li><a href="/latin.html">Latin</a>
<ul>
    <li>task 1 select course</li>
    <li>task 2 select course</li>
    <li>task 3 select course</li>
    ...
</ul></li>';

//PDO execute
$stmt = $dbLink->prepare($qry_courses);
$stmt->execute();

//Generate query DB and create menu
echo '<nav><ul>';
    while($row_courses = $stmt->fetchAll(PDO::FETCH_ASSOC))  {
        if(in_array("$row_courses['shortname']", $coursesarr){
            echo $.'$row_courses['shortname']'.;
        }
    }
echo '<ul><nav>';
?>

      

thank

+3


source to share


1 answer


Change as follows

    if(in_array("$row_courses['shortname']", $coursesarr){
        echo $.'$row_courses['shortname']'.;

      



To

 if($row_courses['shortname'] == $coursesarr){
        echo $row_courses['shortname'];

      

0


source







All Articles