drop-down navigation menu with HTML PHP MySQL - php

EDIT FIXED by user #jeroen
You have to move the li and a tags inside the while loop.
Thank you for this, F4LLCON
So the final code (not so clean, but works now):
<?php
$query = mysql_query("SELECT * FROM `apps` ");
while ($query_row = mysql_fetch_assoc($query))
{
?>
<li>
<?php
$meer = $query_row['TITLE'];
$desc_inject = '';
$sub_string = substr($desc_inject, 0, 200);
echo $sub_string." " . '' . '' . $meer . '';
?>
</li>
<br />
<?php
}
?>
I want a drop-down menu that will show the TITLES out of my MySQL database.
I know how to do everything concerning the ID etc.
The only problem is that the PHP version of the drop-down menu will SELECT all the TITLES in my database and parse it as ONE link.
So instead of
Home
>About
>Contact
>Another
it will look like
Home
>About
Contact
Another
With other words
Home
>About Contact Another
It will not make multiple drop-down links but only one drop-down link
If you for example do:
<div>
<?php
echo $query_row['TITLE'];
?>
</div>
It will make a individual <div></div> for every TITLE in my database, so I thought this method would also work for the drop-down links..
Does anybody know how to fix it so it will make individual <li></li> for every TITLE?
Here is a normal drop-down menu:
<li id="media">
<ul>
<li id="1a">About</li>
<li id="1b">Contact</li>
<li id="1b">Another</li>
</ul>
</li>
And here is the PHP drop-down menu:
<li id="media">
<ul>
<li>
<a href="">
<?php
$query = mysql_query("SELECT * FROM `apps` ");
while ($query_row = mysql_fetch_assoc($query))
{
echo $query_row['TITLE'];
?>
<br />
<?php
}
?>
</a>
</li>
</ul>
</li>

You have to move the li and a tags inside the while loop.
echo '<li>' . $query_row['TITLE'] . '</li>';

<li id="media">
<ul>
<?php
$query = mysql_query("SELECT * FROM `apps` ");
while ($query_row = mysql_fetch_assoc($query))
{
echo "<li><a href=''>" . $query_row['TITLE'] ."</a></li>";
}
</ul>
</li>

Related

how to get the HREF value using POST

I'm making a dynamic menu where each link in my dropdown menu will be directed to a page base on the ID in the HREF link. How can I POST the ID from my link?
Here's my code:
<?php
$lesson_sql = "SELECT * FROM lesson WHERE lessonID = 1";
$lesson_query = mysqli_query($db, $lesson_sql);
$lesson = mysqli_fetch_assoc($lesson_query);
?>
<nav id="navbar">
<ul id="navmenu">
<div class="navmenu">
<li><strong>Catalog</strong><span class="darrow"></span>
<ul class="sub1">
<li>Topic 1
<ul class="sub1_1">
<?php
do {
?>
<li>
<a href="lesson1.1.php?lessonID=<?php echo $lesson['lessonID'];?>">
<?php echo $lesson['lessonName']; ?>
</a>
</li>
<?php
}while ($lesson = mysqli_fetch_assoc($lesson_query));
?>
</ul>
</li>
</ul>
</li>
</div>
</ul>
</nav>
And here is the code from the code above I wanted to get the ID, its beside the href value.
<li><?php echo $lesson['lessonName']; ?></li>
Sorry for my english, I hope you understand me!
in your lesson1.1.php file you can use $_GET to access the id as below.
$id = $_GET["lessonID"];
If you have query string then you use
$_GET['name of control']

how to pass value from while loop to next page after clicking on menu item php

I have a a navigation bar and one of the nav items is a dropdown with sub categories. Only the subcategories are being pulled from the database by using a while loop.
When clicking on one of the dropdown items they will be redirected to dancerProfile.php.
I want dancerProfile.php to pull the menu item name from the other pages.
html
<li class="li-spacing">Home</li>
<li class="dropdown li-spacing">
<a class="dropdown-toggle" data-toggle="dropdown">Dancers<b class="caret"></b></a>
<ul class="dropdown-menu">
<?php
$dancers = "SELECT `dancer_name` FROM `dancers` WHERE name = '$name'";
$dres = mysqli_query($con,$dancers);
//if($res==FALSE){
//die('there was an error running query [' . $con->error . ']');
// }
while($data=mysqli_fetch_array($dres)){
$dancerName = $data['dancer_name'];
foreach ($dancerName as $DANCER){
echo '
<li>'.$data["dancer_name"].'</li>
<li class="divider"><hr></li>
';
}
}
?>
<li>Add New</li>
</ul>
</li>
This works well as all dancers appear in the dropdown.
What I want is that when I click on dancerA the dancerProfile.php will show dancerA information. And when I click on dancerB it will show dancerB info, etc.
But it will only show the last dancer's information no matter which name I click on.
dancerProfile.php
<div class="col-sm-6 dancerInfo">
<div class="row">
<div class="col-sm-6">
<div class="dancerName"><?php echo $dancerName;?></div>
</div>
So When I click on dancerA on the nav bar in any other page, in dancerProfile.php $dancerName should print dancerA. And it should print dancerB if I clicked on dancerB from the nav bar.
but it is only printing dancerB no matter which link I click.
I am using bootstrap. Can anyone help me?
EDIT
Here's an update of what my code looks like now.
<li class="li-spacing">Home</li>
<li class="dropdown li-spacing">
<a class="dropdown-toggle" data-toggle="dropdown">Dancers<b class="caret"></b></a>
<ul class="dropdown-menu">
<?php
$dancers = "SELECT `dancer_name`, `id` FROM `dancers` WHERE name = '$name'";
$dres = mysqli_query($con,$dancers);
$dancerId= 'id';
}
while($data=mysqli_fetch_array($dres)){
$dancerName = $data['dancer_name'];
echo '
<li>'.$data["dancer_name"].'</li>
<li class="divider"><hr></li>
';
}
?>
<li>Add New</li>
</ul>
</li>
And dancerProfile.php:
<?PHP
if(isset($_GET['id'])) {
$dancerId=$_GET['id'];
$dancerquery = "SELECT `dancer_name` FROM `dancers` WHERE id = " . $_GET['id'] . ";";
$dancer_res = mysqli_query($con,$dancers);
if($dancer_res){
$DANCER='dancer_name';
}
}
?>
<div class="col-sm-6 dancerInfo">
<div class="row">
<div class="col-sm-6">
<div class="dancerName"><?php echo " $DANCER ";?></div>
</div>
I also forgot to mention that this navigation is also on dancerProfile page. So all of the code I am providing is on dancerProfile page. I don't know if that matters
My database table
You need pass dancer id or which is unique in your dancer table. something like given below.
<li>'.$data["dancer_name"].'</li>
And now go to dancerProfile.php and try something like this.
if (isset($_GET['dancer_id'])) {
$dancer_id=$_GET['dancer_id'];
//your query
}
Your full code:
<li class="li-spacing">Home</li>
<li class="dropdown li-spacing">
<a class="dropdown-toggle" data-toggle="dropdown">Dancers<b class="caret"></b></a>
<ul class="dropdown-menu">
<?php
$dancers = "SELECT `dancer_name`, `id` FROM `dancers` WHERE name = '$name'";
$dres = mysqli_query($con,$dancers);
$dancerId= 'id';
}
while($data=mysqli_fetch_array($dres)){ ?>
$dancerName = $data['dancer_name'];
<li><?php echo $data['dancer_name']; ?>'</li>
<li class="divider"><hr></li>
<?php } ?>
<li>Add New</li>
</ul>
</li>
Your dancerProfile.php should be
<?PHP
if(isset($_GET['id'])) {
$dancerId=$_GET['id'];
$dancerquery = "SELECT `dancer_name` FROM `dancers` WHERE id = '$dancerId'";
$dancer_res = mysqli_query($con,$dancerquery);
$data=mysqli_fetch_array($dancer_res);
}
?>
<div class="col-sm-6 dancerInfo">
<div class="row">
<div class="col-sm-6">
<div class="dancerName"><?php echo $data['dancer_name'];?></div>
</div>
You are currently overriding the value of $DANCER with each iteration, so the last will always be the used value.
Just change your loop a bit:
while($data=mysqli_fetch_array($dres)){
echo '<li>'.$data['dancer_name'].'</li>';
}

creating class active dynamically in php

I am creating a menu dynamically based on the categories registered on the dataBase, What I need is:
When someone click in the link, this option should have the css class 'active', showing in with page the user is and even the pages are created dynamically.
I have no idea how to do this because I am php student and I couldn't find this information in google for "Dynamically menus" Thank you very much.
<nav class="menu">
<ul class="list">
<li class="line"><a id="item" href="index.php">Home</a></li>
<?php
$categories = $db->select("select * from tbl_category");
if($categories){
while ($result = $categories->fetch_assoc()){
echo <<<HTML
<li id="line" ><a class="item" href="categories.php?category=$result[id]">$result[name]</a></li>
HTML;
}
}
?>
</ul>
<nav>
First of all, you're looping through that while loop and adding the id of "line" to each and every <li> element. I suggest you create unique id's for each list item. I don't necessary advocate using the code the way you have it, just as an example, here's your code edited to do just that:
if($categories){
$i = 1;
while ($result = $categories->fetch_assoc()){
$i++;
echo <<<HTML
<li id="line$i" ><a class="item" href="categories.php?category=$result[id]">$result[name]</a></li>
HTML;
}
}
Then when someone clicks on your link, just use jQuery with something like this:
$(".item").click(function() {
$(this).parent('li').addClass('active');
});
Try something like that:
<nav class="menu">
<ul class="list">
<li class="line"><a id="item" href="index.php">Home</a></li>
<?php
$current_page = isset($_GET['category']) ? $_GET['category']: -1;
$categories = $db->select("select * from tbl_category");
if($categories){
while ($result = $categories->fetch_assoc()){
echo '<li id="line">';
if($result['category'] === $current_page) {
// If we're currently in the active page then add the active class to the <a>
echo '<a class="item active" href="categories.php?category='. $result[id] .'">';
} else {
echo '<a class="item" href="categories.php?category='. $result[id] .'">';
}
echo $result['name'];
echo '</a>';
echo '</li>';
}
}
?>
</ul>
<nav>
Thank you all for help, based on your code, I solved doing this:
menu.php:
<nav class="menu" id="menu">
<ul id="list" class="list">
<li id="line" class="<?php if($presentPage == '0')echo 'active';?>"><a id="item" class="item" href="index.php">Home</a></li>
<?php
function checked($pp, $id){
if($pp == $id){
$status = 'active';
return $status;
}
}
$query = "select * from tbl_category";
$category = $db->select($query);
if($category){
while ($result = $category->fetch_assoc()){
echo "
<li id='line' class='".checked($presentPage, $result['id'])."'><a id='item' class='item' href='categories.php?category=".$result['id']."'>".$result['name']."</a></li>
";//end echo
}//end while
}//end if
?>
</ul>
</nav>
In my index.php:
<?php $presentPage = 0;?>
<?php require_once 'header.php';?>
<?php require_once 'menu.php';?>
and in my categories.php:
<?php
if (!isset($_GET['category']) || $_GET['category'] == NULL) {
$presentPage = 0;
}else{
$presentPage = intval($_GET['category']);//just to make sure that the variable is a number
}
?>
<?php require_once 'header.php';?>
<?php require_once 'menu.php';?>
///...my code...
and in my CSS of course:
.active{
background: linear-gradient(#821e82, #be5abe);
}
Thats is working perfectly for me, Thank for all help.

What am I doing wrong here with echo list and bootstrap?

I'm echoing this list from my mysql database, but I don't want bullets so I'm using bootstrap list-group-item. I feel like I'm making a really dumb mistake somewhere with my list tags but I'm not sure. I'm still getting the bullets next to my list. I'm not including all of my connecting to my database php because that's not the problem.
Here is what I have,
<div class="panel panel-info">
<div class="panel-heading">Contents</div>
<ul class="list-group">
<li class="list-group-item">
<?php
basic connect to mysql database stuff here
}
$query = mysqli_query($dat, "SELECT * FROM Content ORDER BY ContentName") or die(mysqli_error($dat));
while($list = mysqli_fetch_array($query)){
echo"<li>";
echo"<a href = >";
echo $list['ContentName'];
echo"</a>";
echo "</li>";
}
mysqli_close($dat);
?>
</li>
</ul>
</div>
you are adding li inside another li. it should be -
<ul class="list-group">
<?php
}
$query = mysqli_query($dat, "SELECT * FROM Content ORDER BY ContentName") or die(mysqli_error($dat));
while($list = mysqli_fetch_array($query)){
echo"<li class='list-group-item'>";
echo"<a href = >";
echo $list['ContentName'];
echo"</a>";
echo "</li>";
}
mysqli_close($dat);
?>
</ul>
Use echo "<li class='list-group-item'>"; instead of <li> and remove the <li> before php code.
<div class="panel panel-info">
<div class="panel-heading">Contents</div>
<ul class="list-group">
<?php
basic connect to mysql database stuff here
}
$query = mysqli_query($dat, "SELECT * FROM Content ORDER BY ContentName") or die(mysqli_error($dat));
while($list = mysqli_fetch_array($query)){
echo "<li class='list-group-item'>";
echo "<a href ='www.example.com' >";
echo $list['ContentName'];
echo "</a>";
echo "</li>";
}
mysqli_close($dat);
?>
</ul>
</div>

a sortable list that comes from database

I have a simple list in html:
<ul id="sortable">
<li id="1">item1</li>
<li id="2">item2</li>
<li id="3">item3</li>
</ul>
I make it sortable with jQuery UI. This lets the user move the order of each item:
$('#sortable').sortable();
But when I get the same items from a MySql database the "sortable" doesn't work any more I cannot change the order of the items. So my question is: how to make a list sortable when the list comes from a database?
<?php
$result = mysqli_query($con, 'SELECT * FROM principal ORDER BY ordre');
while ($row = mysqli_fetch_array($result)) {
?>
<ul id="sortable">
<li id="<?php echo $row["id"] ?>"><?php echo $row["concepte"]?>;</li>
</ul>
<?php
You are generating ul elements for each record row, where as it should be one single UL having multiple LI elements, also fix the li HTML code
<?php
$result = mysqli_query($con, 'SELECT * FROM principal ORDER BY ordre');
echo '<ul id="sortable">';
while ($row = mysqli_fetch_array($result)) {
?>
<li id="<?php echo $row["id"]; ?>"><?php echo $row["concepte"]; ?></li>
<?php
}
echo '</ul>';
?>
I'm not a PHP guy but this looks like it's going to generate a ul with each iteration ? So your markup would be all screwed up

Categories