Hello Guys.... Im a first year student in web coding.
Im making my exam project right now and i really need you guys help for this simple thing :D.
In my school we are using PHP, and ofcause im making my menu with a database and php..
Well my problem is that i really want the (selected) feature in my menu... But since im pulling all of my menu point out in one, i dont know how seperate the selected option.
As you can see im not the best at english, so im just gonna show you guys my code and really hope u can help me, if u got any questions or there is something u dont understand, please ask me.

Here is where im pulling the menu points out of my database

the problem as u can see is i made a ID on my <li> named selected, but since on doing it this way it just counts for every single menu point

 <?php
function menu($db){
    echo '<ul>';
    $sql_menu="SELECT * FROM menu";
    $result_menu=mysqli_query($db,$sql_menu);
        while($row_menu = mysqli_fetch_array($result_menu)){
            echo '<b>';
            echo '<li id="selected"><a href="index.php?side='.$row_menu['menu_id'].'">'.$row_menu['menu_text'].'</a></li>';
            echo '</b>';
    }
        echo '</ul>';
}
?>
And here is my CCS
#menu1 ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    margin:0px 0px 0px 0px;
    z-index:2;
}

#menu1 li {
    float: left;
    z-index:2;
}

#menu1 a:link, a:visited {
    display: block;
    width:136px;
    height:18px;
    color:black;
    z-index:2;
    text-align: center;
    padding: 12px;
    text-decoration:none;
    text-transform: uppercase;
    font-size:15px;
    margin-top:10px;

}

#menu1 a:hover, a:active {
    color:white;
    background:#41A9DF;
    border-radius:30px;
    font-family: 'Dancing Script', cursive;
}

#menu1 li#selected a{
    color:#F00;

}

1) <li> inside <b> isn't ok - replace to <b> inside <li> instead
2) id need unique but in your example any <li> has same id
if you want multiple selected <li> use class instead of id and replace CSS #menu1 li#selected to #menu1 li.selected

Member Avatar for diafol

Perhaps a get_menu function like this with AndrisP suggestion:

function get_menu($dbo, $menu_id)
{
    $eOpen = '<li>';
    $eSel = '<li class="selected">';
    $eClose = '</li>';
    $output = [];

    $query = 'SELECT * FROM menu'; //horrible need to specify fields and place ORDER BY clause 
    if ($result = $mysqli->query($query)) {
        while ($row = $result->fetch_assoc()) {
            $start = ($menu_id == $row['menu_id']) ? $eSel : $eOpen;
            $output[] = "$start<a href='index.php?side={$row['menu_id']}'>{$row['menu_text']}</a>$eClose";
        }
        $result->free();
    }
    return implode("\n\t", $output);
}

Then wherever you need it in your markup:

<ul>
    <?=get_menu($dbo, $menu_id)?>
</ul>

Provided your menus open new php files or pages. You can use conditional statements and a class in css to produce te editing.

So if for example
Home opens index.php
Contact opens contact.php
About Us opens about.php

You first create a class let me say select in css

.selected {
    //and your styles go here
}

Then on the <li> tags you use the conditional statement and the $_SERVER['PHP_SELF'] superGlobal.

so on the li

<li <?php if ($_SERVER['PHP_SELF'] == '/index.php') { echo 'class="selected"'; } ?>>Home<a href="index.php"></li>
<li <?php if ($_SERVER['PHP_SELF'] == '/contact.php') { echo 'class="selected"'; } ?>>Contact<a href="contact.php"></li>
<li <?php if ($_SERVER['PHP_SELF'] == '/about.php') { echo 'class="selected"'; } ?>>About Us<a href="about.php"></li>
<style>.selected {background: yellow}</style>
<?php 

$menu[] = array('menu_id' => 1, 'menu_text'  => 'Home');
$menu[] = array('menu_id' => 2, 'menu_text'  => 'Service');
$menu[] = array('menu_id' => 3, 'menu_text'  => 'About Us');
$menu[] = array('menu_id' => 4, 'menu_text'  => 'Contact Us');

$selected = (isset($_GET['side']) and !preg_match('/\D/',$_GET['side'])) ? $_GET['side'] : 0;

$i = 0;
echo '<ul>';
while ($i < count($menu)) {
  $row_menu = $menu[$i];
  echo $selected == $row_menu['menu_id'] ? '<li class="selected">' : '<li>';
  echo '<a href="index.php?side='.$row_menu['menu_id'].'">'.$row_menu['menu_text'].'</a>';
  echo '</li>';
  $i++;
}
echo '</ul>';

I've emulated your db with an array.
As above comments, use CSS for styling.
As I'm a fan of minimalist php code, if your project allows Javascript, I would probably look at using Javscript to apply the style.
I spend far too much time thinking what to call my variables, but I think you should use "active" rather than "selected" as "selected" is almost a reserved word in HTML.

Member Avatar for diafol

Just a note. Building up a string (concatenation) and echoing just once is better than multiple echoes all over the place.

Hi diafol,

I think that is a personal preference. I myself prefer to use the alternative PHP syntax and simply output PHP variables inline.

Member Avatar for diafol

That's all well and good. Performance-wise, I don't think there's much in it, but with in-line echoing, you lose flexibility. Instead of neatly placing your code in a function and storing it somewhere, like a functions.php file, you have to place it right in the middle of your markup, which is not ideal, as we usually try to separate markup and code as much as possible. Just an observation, each to his own as you say.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.