How would I create a simple menu from a database?

Codes here !

Moderators: egami, macek, gesf

Post Reply
IChaps
New php-forum User
New php-forum User
Posts: 123
Joined: Tue Mar 01, 2016 3:07 pm

Tue Jul 04, 2017 2:58 pm

Hello.

Just a brief query.

I'm looking into creating a simple menu, however I'm considering using a database to store the data, ie menu text (Home) and hyperlink (index.php) as an example.

I'm just asking what would be the best way to create the hyperlink in php as I'm getting a blank page?

I've written so far:-

Code: Select all

<?
                        try {
                                $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
                                // set the PDO error mode to exception
                                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                                $results = $conn->query('SELECT * FROM menu');
                                while ($row = $results->fetch()) {
                                        <a href=$row["menu_path"]>echo $row["menu_display"]</a>;
                                /*        echo "<br><hr width='50%'";

                                    // <a href="index.php">Index Page</a>
                                }
                                $conn = null; // Close Connection
                            }catch(PDOException  $e ){
                            echo "Error: ".$e;
                            }

                        ?> 
I'm unsure if the line <a href=$row["menu_path"]>echo $row["menu_display"]</a>;
is ok or not. I can access the database table fine, but can't create the hyperlink.

I'd be grateful for any assistance or how would I go about it.

Thank You.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 755
Joined: Mon Feb 22, 2016 5:52 pm

Tue Jul 04, 2017 3:35 pm

You should be getting errors (php errors).

Code: Select all

<a href=$row["menu_path"]>echo $row["menu_display"]</a>;
is not valid php or html, so no it is not ok.

A couple of questions:

1) How does php send information to the browser?

2) How does php decide if it is to parse php as php?

The above questions will answer why your above code does not work, feel free to discuss...

IChaps
New php-forum User
New php-forum User
Posts: 123
Joined: Tue Mar 01, 2016 3:07 pm

Fri Jul 07, 2017 2:47 pm

Hello Hyper,

Thank you for your reply, and soory for for not getting back to you sooner.
I've had a quick browse on the web, and I think I've achieved what I'm trying to do (some-what).

To add to items to the menu I've created a form, and once submitted I've written:-

Code: Select all

session_start();
$menu_header = $_POST["txtmenu_display"];
$menu_locate = ""; //$_POST["X"];
$menu_pathway = $_POST["txtapp_pathway"];
$menu_enabled = $_POST["comenable_app"];
$control_menu_header = $_POST["txtadmin_menu_display"];
$control_pathway = $_POST["txtcontrol_pathway"];

/*  All fields are complete, but states there's a blank field.
if ($menu_headeer == "" || $menu_pathway  == "" || $menu_enabled == "" || $control_menu_header == "" || $control_pathway == "" ) {
    echo "All fields are required.  There is one or more blank fields.  Please go back and try again.";
    exit;
} */



$servername = $_SESSION['dbf_host'];
$username = $_SESSION['dbf_user'];
$password = $_SESSION['dbf_pass'];
$dbname = $_SESSION['dbf_databasename'];

    // Check for existing enties
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $results = $conn->query('SELECT * FROM menu');
    while ($row = $results->fetch()) {
        if ($row['menu_path'] == $menu_pathway) {
            echo "An entry with matching pathway, may already exist.  Please re-check and try again.  Thank You.";
            $conn = null;
            exit;
        }
    }
    $conn = null; // Close Connection
}catch(PDOException  $e ){
echo "";
}
echo "<br>[*]<br>";

    // Add New Menu Item
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        // Insert
    $sql = "INSERT INTO menu (
            menu_display,
            menu_locate,
            menu_path,
            menu_enabled,
            ad_menu_display,
            ad_menu_path)

        VALUES (
            '$menu_header',
            '0',
            '$menu_pathway',
            '$menu_enabled',
            '$control_menu_header',
            '$control_pathway' )";
     // use exec() because no results are returned
    $conn->exec($sql);
    $conn = null; // Close Connection
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }
Then to read back and create the menu, I found on the web, and modified for my use:-

Code: Select all

<body>
<?
$servername = $_SESSION['dbf_host'];
$username = $_SESSION['dbf_user'];
$password = $_SESSION['dbf_pass'];
$dbname = $_SESSION['dbf_databasename'];
?>
<div id="wrapper">
        <!-- Navigation -->

            <div class="navbar-default sidebar" role="navigation">
                <div class="sidebar-nav navbar-collapse">
                    <ul class="nav" id="side-menu">
                        <li class="sidebar-search">
                            <div class="input-group custom-search-form">
                                <input type="text" class="form-control" placeholder="Search...">
                                <span class="input-group-btn">
                                <button class="btn btn-default" type="button">
                                    <i class="fa fa-search"></i>
                                </button>
                            </span>
                            </div>
                            <!-- /input-group -->
                        </li>
                        <li>
                            <a href="/Core/admincp/aindex.php"><i class="fa fa-dashboard fa-fw"></i> Admin Area</a>
                        </li>
                        <li>
                            <a href="/Core/user/user.php"><i class="fa fa-dashboard fa-fw"></i> Dashboard</a>
                        </li>

                        <?
                        try {
                                $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
                                // set the PDO error mode to exception
                                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                                $results = $conn->query('SELECT * FROM menu');
                                while ($row = $results->fetch()) {
                                    if ($row['menu_enabled'] = 1) {
                                        Print $row['menu_display'] . "<br> ";
                                        Print "<a href=".$row['menu_path'] . ">" .$row['menu_path'] . "</a><br>";
                                        Print "<hr><br>";
                                    }
                                }
                                $conn = null; // Close Connection
                            }catch(PDOException  $e ){
                            echo "Error: ".$e;
                            }

                        ?>
                    </ul>
                </div>
                <!-- /.sidebar-collapse -->
            </div>
            <!-- /.navbar-static-side -->

    </div>
    <!-- /#wrapper -->
</body>
In both cases my code looks a bit of a mess, as I'm currently working on it, and in the process of tidying it up.

Thank You once again for your reply.
Kind Regards.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 755
Joined: Mon Feb 22, 2016 5:52 pm

Sat Jul 08, 2017 9:40 am

Code: Select all

/*  All fields are complete, but states there's a blank field.
if ($menu_headeer == "" || $menu_pathway  == "" || $menu_enabled == "" || $control_menu_header == "" || $control_pathway == "" ) {
    echo "All fields are required.  There is one or more blank fields.  Please go back and try again.";
    exit;
} */

Code: Select all

$menu_header = $_POST["txtmenu_display"];

You should not be doing this:

Code: Select all

$servername = $_SESSION['dbf_host'];
$username = $_SESSION['dbf_user'];
$password = $_SESSION['dbf_pass'];
$dbname = $_SESSION['dbf_databasename'];
Storing any of the above information in sessions is a bad idea.

IChaps
New php-forum User
New php-forum User
Posts: 123
Joined: Tue Mar 01, 2016 3:07 pm

Mon Jul 10, 2017 2:21 pm

Hello Hyper,

Thank you for your reply.

Post Reply