dynamic navigation using mysql php and ajax - php

i have created navigation using mysql php. now i am trying to pass value to another page using ajax.please help me
<ul id="all_navs">
<?php $sql = mysql_query("select * from sub_category_master where main_category =1");
while($row = mysql_fetch_array($sql))
{?>
<li id="s1">
<a href="#tab_deals" onclick="showUsers('<?php echo $row['sub_category_id']; ?>')" id="insert">
<?php echo $row['sub_category_name']; ?></a>
</li>
<?php }?>
</ul>

if i understood your problem why not try something like this:
<ul id="all_navs">
<?php $sql = mysql_query("select * from sub_category_master where main_category =1");
while($row = mysql_fetch_array($sql)){
echo '<li id="s1">';
echo '<a href="#tab_deals" onclick="showUsers('.$row['sub_category_id'].')" id="insert">';
echo $row['sub_category_name'].'</a>';
echo '</li>';
}
?>
</ul>
it would help more if you told us what the problem was.

Related

Dynamic Dropdown menu using PHP & mysql

I've been trying to build a dropdown menu but I'm not getting my desired results. Here's my code:
<?php require_once 'core/init.php'?>
<?php
$sql = 'SELECT * FROM categories WHERE parent = 0';
$pquery = mysqli_query($db,$sql);
?>
<?php while($parent = mysqli_fetch_assoc($pquery)):?>
<?php
$parent_id = $parent['id'];
$sql2 = 'SELECT * FROM categories WHERE parent = "parent_id"';
$cquery = mysqli_query($db,$sql2);
?>
<li class='dropdown'>
<a href='#' class='dropdown-toggle' data-toggle='dropdown'>
<?php echo $parent['id'];?><span class='caret'</span</a>
<ul class='dropdown-menu' role='menu'>
<?php while($child = mysqli_fetch_assoc($cquery)):>
<li><a href='#'><?php echo $child['parent'];?></a>
</li>
<?php endwhile; ?>
</ul>
</li>
<?php endwhile;?>
My DB is like this and the result is this.
Try with console phpmyadmin before write codes in page.
I think your codes is wrong.
Your code must like this
$parent_id = $parent['id'];
$sql2 = 'SELECT * FROM categories WHERE parent = '.$parent_id;
I suggest to you for use ajax to be beautiful again.

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

Query for display catergories from database php mysql

I'm new to coding with php and using MySQL. I am having trouble to display a list of categories by their ID so that each category is displayed individually as a heading. Instead I got it to display a category name but its only echoing out a category name twice that's the same. Here is my code...
$sql= "SELECT * FROM categories ";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query))
{
$id=$row['id'];
$cat_name=$row['cat_name'];
}
?>
<ul class="nav nav-list">
<li class="nav-header"><?php echo $cat_name;?></li>
<li class="nav-header"><?php echo $cat_name;?></li>
</ul>
Your li tag is outside the while loop. So the $id and $cat_name is only the last record in the DB, then you echo them twice. That's way you got the same name twice.
Try echo the li tag in the loop (but not the ul):
<ul class="nav nav-list">
<?php
while($row = mysql_fetch_array($query))
{
$id=$row['id'];
$cat_name=$row['cat_name'];
echo '<li class="nav-header">' .$cat_name. '</li>';
}
?>
</ul>
You can use this code as-is :
$sql= "SELECT * FROM categories ";
$query = mysql_query($sql); ?>
<ul class="nav nav-list">
<?php while($row = mysql_fetch_array($query)) : ?>
<li class="nav-header"><?php echo $row['cat_name'];?></li>
<?php endwhile; ?>
</ul>
This will loop through your records and for each record, it will print an entire li with the required data.
Note that separating your PHP code from your HTML code like this has several benefits. It will be better colored in your editor and it is also easier to integrate.
The reason you are printing the same value out twice is because $cat_name is a string variable and will only hold one value at a time you may want to save the items an array and loop at a seperate time, like such
<?php
$sql= "SELECT * FROM categories ";
$query = mysql_query($sql);
$category = array();
while($row = mysql_fetch_array($query))
{
$array = array(
id => $row['id'],
name => $row['cat_name']
);
array_push($category,$array);
}
?>
<ul class="nav nav-list">
<?php
foreach($category as $c)
{
echo '<li class="nav-header">'.$c['name'].'</li>';
};
?>
</ul>
$sql= "SELECT * FROM categories ";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query))
{
?>
<ul class="nav nav-list">
<li class="nav-header"><?php echo $row['id']; ?></li>
<li class="nav-header"><?php echo $row['cat_name']; ?></li>
</ul>
<?php } ?>

Creating Dynamic Links with PHP/MySQL

i'm creating my first PHP/MySQL site and i'm having difficulty figuring out how to generate dynamic links and creating a new page for those links.
My index page is pulling in certain details from my database as a preview, and when the visitor clicks on that item, i want them to be taken to a page which shows the full information from the database for that row.
The code on my index page for displaying the previews is below, any help on amending it to generate the link and page would be greatly appreciated.
<?php
$query="SELECT * FROM $tbl_name ORDER BY job_id DESC";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"company_name");
$f2=mysql_result($result,$i,"job_title");
$f3=mysql_result($result,$i,"city");
$f4=mysql_result($result,$i,"country");
$job_id=mysql_result($result,$i,"job_id");
?>
<div class = "hjl">
<ul>
<li id = "jobtitle"><?php echo $f2; ?></li><br />
<li id = "compname"><?php echo $f1; ?></li>
</ul>
<ul>
<li id = "city"><?php echo $f3; ?>, <?php echo $f4; ?></li><br />
</ul>
</div>
<?php
$i++;
}
?>
I'm pretty sure what i'm asking is really simple, i just can't get my head around acheieving it.
Thanks to you both for your answers, but i have managed to fix it (or work-around it) with this on my index page:
<?php
$query="SELECT * FROM $tbl_name ORDER BY job_id DESC";
$result=mysql_query($query) or die(mysql_error());
$rsjobinfo=mysql_fetch_assoc($result);
mysql_close();
do {?>
<div class = "hjl"><a href="paging.php?job_id=<?php echo $rsjobinfo['job_id'];?>">
<ul>
<li id = "jobtitle"><?php echo $rsjobinfo['job_title'];?></li><br />
<li id = "compname"><?php echo $rsjobinfo['company_name'];?></li>
</ul>
<ul>
<li id = "city"><?php echo $rsjobinfo['city'];?>,
<?php echo $rsjobinfo['country'];?></li>
</ul>
</a>
</div>
<?php } while ($rsjobinfo=mysql_fetch_assoc($result))?>
</div>
Followed by this on my content page:
<?php
$job_id = $_GET['job_id'];
$query="SELECT * FROM $tbl_name WHERE job_id = $job_id";
$result=mysql_query($query) or die(mysql_error());
$rsjobinfo=mysql_fetch_assoc($result);
mysql_close();
?>
Thanks for your help everyone.
Dan
put mysql_close after you use mysql_result, but once you get it working you might look into a more modern approach like PDO.
to your code add link (which I think you already have somewhere):
//...................
<li id = "jobtitle">
<a href="<?php echo '?id='.$job_id; ?>">
<?php echo $f2; ?>
</a>
</li>
//...................
Read more...
//...................
then your code must check for variable $_GET['id'], so put IF in the beginning of your code:
$where = '';
if( isset($_GET['id']) && strlen($_GET['id']) > 0 ) {
$where = ' job_id = "'. mysql_real_escape_string( $_GET['id'] ) .'"' ;
}
<?php
$query="SELECT * FROM $tbl_name $where ORDER BY job_id DESC";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"company_name");
$f2=mysql_result($result,$i,"job_title");
$f3=mysql_result($result,$i,"city");
$f4=mysql_result($result,$i,"country");
$job_id=mysql_result($result,$i,"job_id");
?>
<div class = "hjl">
<ul>
<li id = "jobtitle">
<a href="<?php echo '?id='.$job_id; ?>">
<?php echo $f2; ?>
</a>
</li><br />
<li id = "compname"><?php echo $f1; ?></li>
</ul>
<ul>
<li id = "city"><?php echo $f3; ?>, <?php echo $f4; ?></li><br />
</ul>
Read more...
</div>
<?php
$i++;
}
?>
edit: Try changing the following line:
$where = " job_id = '". mysql_real_escape_string( $_GET['id'] ) ."'" ;

Categories