I get my dynamic navigation menu from the database because I have a CMS, so here's my code:
<ul>
<?php
$result = mysql_query("SELECT id, name, DESCRIPTION FROM menu where VISIBLE='1' ORDER BY `order` ASC") or die(mysql_error());
while($row = mysql_fetch_array($result)){
printf('<li>%s %s </a></li> ', $row['name'],$row['DESCRIPTION']);
}
?>
to highlight the current page, i have to add this inside the li element
how should i do this? Thanks in advance.
u can try the following code
<?php
$currentpage = $_SERVER['REQUEST_URI'];?>
<ul>
<?php
$result = mysql_query("SELECT id, name, DESCRIPTION FROM menu where VISIBLE='1' ORDER BY
`order` ASC") or die(mysql_error());
while($row = mysql_fetch_array($result))
{
?>
<li<?php if(preg_match("/index/i", $currentpage)||($currentpage=="/")) { echo "
class='active'"; } ?>>Home</li>
<?
}
?>
instead of index you can also write $row[name] in a variable and replace /index/i with it
set a variable on the page like
$navlink = '<somevalue>'
and check the the value in li
<li <?php if($navlink == '<somevalue>') {echo "class='active'"}?>>
i think it will work.
Related
This question already has an answer here:
Struggling to output PHP array as unordered HTML list
(1 answer)
Closed 2 years ago.
I want to return some data from my table as navbar links, my table holds this information:
Then I coded this:
<?php
$result = $db->query("SELECT * FROM topnav ORDER BY 'table_id' ASC");
while($row = $result->fetch_object()){
echo "
<li><a href='contact.html'>".$row->name_link."</a></li>
<li><a href='about.html'>".$row->name_link."</a></li>
<li><a href='news.html'>".$row->name_link."</a></li>
<li><a href='news.html'>".$row->name_link."</a></li>
<li><a href='news.html'>".$row->name_link."</a></li>
<li><a href='blog.html'>".$row->name_link."</a></li>
<li class='active'><a href='index.html'>".$row->name_link."</a></li>
";
}
?>
But this is wrong because it prints this as result:
And I want each item to be printed once and then another name_link appears.
So how can I do that?
Assuming the value from name_link column is the same of html file, then:
<?php
$result = $db->query("SELECT * FROM topnav ORDER BY 'table_id' ASC");
while($row = $result->fetch_object()){
echo "<li><a href='".$row->name_link.".html'>".$row->name_link."</a></li>";
}
?>
This would seem to be more like what you need - create one <li> per row returned from your table. (I've assumed href-link is the field in your table which should be used as the URL.)
<?php
$result = $db->query("SELECT * FROM topnav ORDER BY 'table_id' ASC");
while($row = $result->fetch_object()){
echo "<li><a href='".$row->href_link."'>".$row->name_link."</a></li>";
}
?>
What you were doing before is repeating your entire menu for every row in your table, which doesn't make any sense.
I want to print sql result in list using php.
<?php
$query="select name from plant ";
$q=mysqli_query($con,$query)or die("Could Not Perform the Query");
while ($row = $q->fetch_assoc()) {
$hh=$row['name'];
}
?>
<ul id="myUL">
<li><?php echo $hh ?></li>
</ul>
like:-
.Mango
.Apple
.Banana
<?php
$query="select name from plant ";
$q=mysqli_query($con,$query)or die("Could Not Perform the Query");
echo "<ul id="myUL">";
while ($row = $q->fetch_assoc()) {
echo "<li> <a href='#'>".$row['name']."</a></li>";
}
echo "</ul>";
?>
Generate the HTML in the for loop
<?php
$query="select name from plant ";
$q=mysqli_query($con,$query)or die("Could Not Perform the Query");
$hh = ''; //empty string first
while ($row = $q->fetch_assoc())
{
$hh .= '<li>' . $row['name'] . '</li>';
// ^--------------------- concat with the previous result
}
?>
<ul id="myUL">
<?php echo $hh; /* display */ ?>
</ul>
$hh will be the last fetched value because it's being rewritten on every loop cycle. You need to append to that instead. Take a look at this:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$query = "SELECT name FROM tablename";
$query = mysqli_query($con, $query)or die("Failed to fetch data");
$lis = "";
// as long as row is not empty
while ($row = $q->fetch_assoc()) {
$lis .= "<li> <a href='#'>".$row['name']."</a></li>";
}
echo "<ul>$lis</ul>";
?>
The issue is that $hh is always the last row since while($row = $q->fetch_assoc()) over writes the value of the variable with each row.
In order to output a column value in each row, you need to place the output inside of the while loop: it will do it once for each record.
You can use conditional statements for readability:
<!-- Start of List -->
<ul id="myUL">
<?php
$query = "select name from plant ";
$q = mysqli_query($con,$query) or die("Could Not Perform the Query");
while ($row = $q->fetch_assoc()):
?>
<!-- Option -->
<li><?= $row['name']; ?></li>
<?php endwhile; ?>
</ul>
<!-- End of List -->
Like referenced by #Cid in the comments, each element needs to be added inside of the list. Keep your <ul> outside of the loop.
In pseudo code, so you can understand better, it would look like this:
list
foreach row do:
output option
end foreach
end list
I have two tables in a database, sight_country and sightseeing. I am inserting the ID of the country field from the sight_country table to s_country field of the table sightseeing. In php I am showing country field values from sight_country in a CSS drop-down menubar.
the code is
<li class="menu-item-has-children">Sightseeing
<ul class="sub-menu">
<?php
$qry_st = "select * from `sight_country` limit 5";
$rec_st = mysql_query($qry_st );
if( mysql_num_rows($rec_st) > 0)
{
while($res_st = mysql_fetch_array($rec_st))
{
echo "<li><a href='sightseeing.php?id=$res_st[id]'>".$res_st['country']."</a></li>";
}
}
?>
</ul>
</li>
When click on link of county value then I am showing all sightseeing data from the table sightseeing in php page.
the code is
$sql = "select * from `sightseeing` where `s_country` ='$id'";
$res = mysql_query($sql);
$rec = mysql_fetch_array($res);
the country may have two or more related sightseeing data, so I am displaying sightseeing titles from the sightseeing table in a sidebar menu in my PHP page.
the code is
<ul class="st_lnks">
<?php
$qry_st = "select * from `sightseeing` where s_country = '$id'";
$rec_st = mysql_query($qry_st );
if( mysql_num_rows($rec_st) > 0)
{
while($res_st = mysql_fetch_array($rec_st))
{
echo "<li><a href='sightseeing.php?id=$res_st[s_country]'>".$res_st['stitle']."</a></li>";
}
}
?>
</ul>
when I click link of stitle I want to show it's related sightseeing data in same page. How it can be done?
I am assuming that;
The whole script is on one page (sightseeing.php), which varies depending on any GET variables (variables in the URL).
Originally the page just displays the first menu. Then when u click a country, you are sent again to sightseeing.php. Now also with ?id=* which shows also a second list, containing the list of sightseeing relevant to the country selected.
You have a field called 'id' in your sightseeing table that has the unique sightseeing id.
To now additionally show details of the sightseeing selected (clicked by user);
Modify the links in the second list. rather than:
echo "<li><a href='sightseeing.php?id=$res_st[s_country]'>".$res_st['stitle']."</a></li>";
Write:
echo "<li><a href='sightseeing.php?id=$res_st[s_country]&ss_id=$res_st[id]'>".$res_st['stitle']."</a></li>";
Now when u click one of the links and are sent back to to sightseeing.php you will also have another get variable GET['ss_id'] (which has the id of the sightseeing that you want to view).
You can use this variable to pull the relevant details of the sightseeing.
$sightSeeingId = $_GET['ss_id'];
$sql3 = "select * from `sightseeing` where `id` ='$sightSeeingId' LIMIT 1";
$res3 = mysql_query($sql3);
$sightSeeingData = mysql_fetch_array($res3);
check that it has data and print it out
if(!$res3) die(mysql_error());
if(mysql_num_rows($res3) > 0){
echo "Sight Seeing id:" . $sightSeeingData['id'];
}
As a side note you should be aware that mysql_* functions are outdated and your code is vunerable to sql injection, see here;
GET parameters vulnerable to SQL Injection - PHP
You could give your second link a second parameter. Give the first parameter a unique name
<ul class="sub-menu">
<?php
$qry_st = "select * from `sight_country` limit 5";
$rec_st = mysql_query($qry_st );
if ( mysql_num_rows($rec_st) > 0) {
while ($res_st = mysql_fetch_array($rec_st)) {
echo "<li><a href='sightseeing.php?idCountry=$res_st[id]'>".$res_st['country']."</a></li>";
}
}
?>
</ul>
Then add a second parameter when generating the second link:
<ul class="st_lnks">
<?php
$qry_st = "select * from `sightseeing` where s_country = '$id'";
$rec_st = mysql_query($qry_st );
if ( mysql_num_rows($rec_st) > 0) {
while($res_st = mysql_fetch_array($rec_st)) {
echo "<li><a href='sightseeing.php?idCountry=$idCountry&idSightseeing=res_st['id']'>".$res_st['stitle']."</a></li>";
}
}
?>
</ul>
I have the below code :
<?php
//Fetching the Category Name
$sql="SELECT * FROM subcategory WHERE CategoryID='$catid'";
$rs = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($rs)){
?>
<?php echo $row['SubCategoryName']; ?>
<?php } ?>
I have done a .htaccess file but still the rewrite is not working.
mysite/article.php?scid=9&page=1
I want them as mysite.com/categoryname_description_subcategoryname-1
You should alter the link names you've used in your design/html layout to the example I've posted below
<a href="question/categoryname/categoryid.html"</a>
I am having some problem in retrieving sub categories from mysql database.I want to display the sub-categeories for the parent categories.I am able to get only the last sub category of a main category. The first sub-categories are not displaying **. In my table **i have category_id and category_parent_id.where category_parent_id will be '0' for parent category. .Thanks in advance
<ul class="betterList">
<?php
$con = mysql_connect("localhost","root","pwd") or die('couldnot connect to database'.mysql_error());
mysql_select_db("DB",$con);
$result=mysql_query("select * from table ")or die("No table available with this name"."<br/><br/>".mysql_error());
while($row=mysql_fetch_array($result))
{
$parent_id=$row['category_parent_id'];
$category_id=$row['category_id'];
if($parent_id==0)
{
?>
<li>
<?php echo $row['category_id'].$row['name_en-GB'];
$result1=mysql_query("select * from table where category_parent_id=".$category_id)or die("No data available with this name"."<br/><br/>".mysql_error());
echo $num_row = mysql_num_rows($result1);
if($num_row>0) {
for($i=0;$i<$num_row;$i++)
{
while($row1=mysql_fetch_array($result1))
{
?>
<ul style="margin:0px;padding:0;">
<li><?php echo $row1['name_en-GB']?></li>
</ul>
<?php
}
}
}
?>
</li>
<?php } ?>
<?php }?>
</ul>
when i remove <li> tag which is at the end and keep it after at the end of in while i could display all the sub-catgeories but the css is not applying for that. Some thing is going wrong there but i couldn't figuer it out
Remove below and try again:
for($i=0;$i<$num_row;$i++)
{
Wow ! o_O
You're using old mysql_* functions ...
You wrote :
for($i=0;$i<$num_row;$i++)
And After :
while($row1=mysql_fetch_array($result1))
Both of these instructions are looping on each rows you got with this query.
Remove all of that:
echo $num_row = mysql_num_rows($result1);
if($num_row>0) {
for($i=0;$i<$num_row;$i++) {
Cause this is useless.
The only important thing to loop on your results is
while($row1=mysql_fetch_array($result1))
You can also replace mysql_fetch_array() by mysql_fetch_assoc() that is lighter.
Your code will be optimizable but this should solve your problem.
Instead of doing nested loops, get everything with a join:
SELECT t1.category_id parent, t1.`name_en-GB` parent_name,
t2.category_id child, t2.`name_en-GB` child_name
FROM table t1
JOIN table t2 ON t2.parent_category_id = t1.category_id
WHERE t1.parent_category_id = 0
Then your loop would be:
$last_parent = null;
$first_cat = true;
while ($row = mysql_fetch_assoc($result)) {
if ($row['parent'] != $last_parent) {
$last_parent = $row['parent'];
if (!$first_cat) { // Close out the last UL and LI
echo '</ul></li>';
} else {
$first_cat = false;
}
echo '<li>' . $row['parent'] . $row['parent_name'];
echo '<ul style="margin:0px;padding:0;">';
}
echo '<li>' . $row['child_name'] . </li>
}
if (!$first_cat) {
echo '</ul></li>';
}
You had too many nested loops in your code: you had both a for and while loop that were both trying to loop over the rows of the inner query. Also, you were putting each child into its own <ul>, which is probably not what you wanted.
Just try whether this solutions work for u, if it works adjust your code accordingly
$result = mysql_query("select * from table WHERE category_parent_id = 0 ");
while($row=mysql_fetch_array($result)) {
$parent_id = $row['category_parent_id'];
$query = mysql_query("select * from table where category_parent_id = {$parent_id}");
while($sub_cats=mysql_fetch_array($query)) {
echo '<pre>'.print_r($sub_cats).'</pre>';
}
}
just by adding internal <ul> before while loop i could get subcategories.
<?php
echo "<ul>";
while($row1=mysql_fetch_array($result1))
{
?>
<li><?php echo $row1['name_en-GB']?></li>
<?php
}
echo " </ul>";