3

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

I wish only the courses that are active are displayed on the menu, I want to take the same order.

I have all the content of submenus in a variable, if active in the DB is displayed and if not active is not displayed.

I'm learning and I want to do it the right way possible.

But this code does not work yet. Could someone 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 you

laur
  • 500
  • 1
  • 9
  • 23
  • 5
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 19 '15 at 16:55
  • 3
    For this line: `if($row_courses['shortname'] == $coursesarr){`, have a look at [in_array()](http://php.net/manual/ru/function.in-array.php) function. –  May 19 '15 at 16:56
  • @JayBlanchard Thanks friend I changed to PDO, be so kind to check if I did correctly. caCtus i changed to in_aaray() – laur May 19 '15 at 17:45
  • 1
    `fetchAll` returns all of the rows so you will need to loop through the resulting array to check each row. – Jay Blanchard May 19 '15 at 17:47

1 Answers1

0

Change as follows

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

To

 if($row_courses['shortname'] == $coursesarr){
        echo $row_courses['shortname'];
Arshid KV
  • 9,631
  • 3
  • 35
  • 36