how to get the HREF value using POST - php

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']

Related

How I can make drop down menu from my categories mysql table only show if parent_id is not 0

So hey there as the title said I am looking for away to make my categories with subcategories. I been looking in stackoverflow for what I need but none has help me of the examples..
Here is how my table look like
So I know what I want and what I need but I have no idea how I can do that possible
I have to SELECT * FROM categories ORDER by position ASC
I have to check if parent_id is bigger then 0.
I have to remove the parent_id from my navbar and show them only under the category name where it should be by dropdown menu .
But I have no idea how I could do all of that ..
Here is how I am selecting only my categories and display them
$catsq = mysqli_query($con,"SELECT * FROM categories ORDER by position ASC");
while($catinfo=mysqli_fetch_assoc($catsq)) {
echo '
<li class="nav-item'.(isset($_GET["cat"]) && $_GET["cat"]==$catinfo["id"] ? " active" : "").'">
<a class="nav-link" href="./index.php?cat='.$catinfo["id"].'">'.$catinfo["name"].'</a>
</li>
';
}
and it's look like this
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="cat=1">TestCat</a>
</li>
<li class="nav-item">
<a class="nav-link" href="cat=2">TestCat2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="cat=3">TestSub</a>
</li>
</ul>
but I want It to look like this
<ul class="nav navbar-nav">
<li class="">TestCat</li>
<li class="dropdown ">
//TestCat2 have to doing nothing always.
TestCat2</i>
<ul class="dropdown-menu">
<li><a class="nav-link" href="cat=3">TestSub</a></li>
</ul>
</li>
</ul>
when the parent_id is more then 0..
If anyone can help me with this would be great..
Thanks to everybody.
There are several approaches you can take:
Build an array
Nested queries
Recursion
Array
This approach builds a data structure that you can iterate through in your view. Working example
<?php
// get db connection...
// create categories array
$stmt = mysqli_query($con, "SELECT * FROM categories ORDER BY position ASC");
while( $row = mysqli_fetch_assoc($stmt)) {
// $category[ $row['parent_id] ][ $row['id'] ] = $row; // use if you need to access other fields in addition to name
$category[ $row['parent_id] ][ $row['id'] ] = $row['name'];
}
// other php stuff...
?>
<html>
... snip ...
<ul class="nav navbar-nav">
<?php foreach($category[0] as $id => $name): ?>
<?php if( isset( $category[$id]) ): ?>
<li class="dropdown ">
<?= $name ?>
<ul class="dropdown-menu">
<?php foreach($category[$id] as $sub_id => $sub_name): ?>
<li><a class="nav-link" href="?cat=<?= $sub_id ?>" ><?= $sub_name ?></a></li>
<?php endforeach; ?>
</ul>
</li>
<?php else: ?>
<li class="">
<?= $name ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
Nested Queries
This method is easiest to display using an imaginary class that does all the sql stuff behind the scenes. For the sake of argument, we will assume a class Category that has a method named listByParent($parent_id) which returns a list of rows having the designated parent_id.
<?php
$cat = new Category();
$topLevel = $cat->listByParent(0);
?>
<html>
... snip ...
<ul class="nav navbar-nav">
<?php foreach( $topLevel as $topRow ): ?>
<!-- note, this method is run on every iteration of top level categories -->
<?php $subRows = $cat->listByParent($topRow['id']) ?>
<?php if( count($subRows)): ?>
<li class="dropdown ">
<?= $topRow['name'] ?>
<ul class="dropdown-menu">
<?php foreach($subRows as $row): ?>
<li><a class="nav-link" href="?cat=<?= $row['id'] ?>" ><?= $row['name'] ?></a></li>
<?php endforeach; ?>
</ul>
</li>
<?php else: ?>
<li class="">
<?= $topRow['name'] ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
Recursion
Using recursion would allow you to have “unlimited” levels of subcategories. However, it’s a level of complexity that does not seem warranted in this case. But should you want to pursue it, note that the best way to approach it would be to make a template for the html that could be accessed programatically, with $cat->findByParent() being a key player...

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>';
}

Can't Link <a href="#"> W/ PHP code

I tried to link $child['id'] but can't do it. Link is removed on line 13. Can anyone tell me correct way to generate category link on click?
<!-- fetch parent categories -->
<?php
while ($parent = mysqli_fetch_assoc($parentquery)) : ?>
<?php
$parent_id=$parent['cat_id'];?>
<!--fetch sub-categories-->
<?php
$sql2 = "SELECT * FROM categories WHERE cat_parent = '$parent_id'";
$child_query = $db->query($sql2); // database object
?>
<div class="col-menu col-md-3">
<h6 class="title"><?php echo $parent['cat_name'] ?></h6>
<div class="content">
<ul class="menu-col">
<?php while($child = mysqli_fetch_assoc($child_query)) : ?>
Now I want to link each category on the below. Loops works fine. I am seeing category names but no link. Please help
<li> <a href='#'>
<?php echo $child['cat_name']; ?></a></li>
<?php endwhile; ?>
</ul>
</div>
</div>
<?php endwhile; ?>
You need to pass it in href
<li>
<a href='<?php echo $child['link'];?'>
<?php echo $child['cat_name']; ?>
</a>
</li>
Try this if ur href is empty then you should add
<a href='"<?php echo $child['link'];?>"'> // double quotes
you should pass the id to the href,
<a href='<?=$child['id'];?>'>

making class="active" for nav menu in php

I'm creating a website..When it comes to the navigation menu, I listed it and need the nav bar class to be active when that menu is clicked. It's fine to give class="active" for the list tag. But I need it to be active only when clicked. How can I achieve this .?
My list is :
<div class="menu">
<ul class="nav" id="nav">
<li class="active">
<?php echo anchor("cntrl/index","Home"); ?>
</li>
<li><?php echo $this->session->userdata('user_name'); ?></li>
<li>About</li>
<li>
<?php echo anchor("cntrl/jobs","Jobs"); ?>
</li>
<li>
<?php echo anchor('cntrl/logout','Logout'); ?>
</li>
<div class="clearfix"></div>
</ul>
<script type="text/javascript" src="<?php echo base_url(); ?>/js/responsive-nav.js"></script>
</div>
Well, I handle this sort of issue as follows:
set the active link (which is resided in the url when the link is clicked) into an variable in the controller function which is called by the link
$data['active_link'] = 'home'; # for example
check it in the view file (JS):
$(document).ready(function(){
var active_link = "<?php echo $active_link;?>";
$("li").removeClass('active');
$("#"+active_link).addClass('active');
});
Use the following code.
<li class="<?php echo (endsWith($_SERVER['REQUEST_URI'], 'cntrl/jobs')? 'active':''); ?>">
<?php echo anchor("cntrl/jobs","Jobs"); ?>
</li>
Main part of this code is
endsWith($_SERVER['REQUEST_URI'], 'cntrl/jobs')
Where $_SERVER['REQUEST_URI'] is your current URL. and 'cntrl/jobs' is your menu which is being checked with current URL.

drop-down navigation menu with HTML PHP MySQL

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>

Categories