a sortable list that comes from database - php

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

Related

menu with while and foreach loop

I am stuck with my current knowledge of PHP. I want to make a menu where the info is stored into a database. What I want to achieve is the following:
<?php
$names= array();
$aliases= array();
$items = array();
?>
<?php while($row = mysqli_fetch_array($menu, MYSQLI_ASSOC)){
$names[] = $row['page'];
$aliases[] = $row['alias'];
$items[] = $row['item'];
}?>
<div id="menu">
<ul>
<?php foreach($names as $page_name){
<?php echo $page_name;?>
?>
*** IF THERE ARE SUB MENU ITEMS, SHOW THEM ***
<ul>
<li>
*** NAME OF SUB ITEM***
</li>
</ul>
</ul>
</div>
I have absolutely no idea if i am on the right pad. The foreach works fine for the names or aliasses only, but i want to sort of combine them. Then i want to check if there is a submenu for the hover, but also set the parent item on class "active".
Since it is hard for me to explain it in English, please ask me any question needed so i can provide more info.
Thanks in advance!
First of all, you don't need three different arrays, only $names array would be enough. Second, change your while() loop in the following way,
while($row = mysqli_fetch_array($menu, MYSQLI_ASSOC)){
$names[] = array('page_name' => $row['page'], 'alias' => $row['alias'], 'item' => $row['item']);
}
This will create a multidimensional array comprising of name, alias and sub-items.
And finally, display the entire menu in the following way,
<div id="menu">
<ul>
<?php foreach($names as $page_array){ ?>
<?php echo $page_array['page_name']; ?>
<?php
if(!empty($page_array['item'])){
?>
<ul>
<li>
<?php echo $page_array['item']; ?>
</li>
</ul>
<?php
}
} ?>
</ul>
</div>
You are overcomplicating things:
<?php
echo '<div id="menu">
<ul>';
while($row = mysqli_fetch_array($menu, MYSQLI_ASSOC)){
$names[] = $row['page'];
$aliases[] = $row['alias'];
$items[] = $row['item'];
echo '<li class="pagename">'.$row['page'].'';
if ('checkforsubitem') {
echo '<ul>
<li>
'.$row['name_of_subitem'].'
</li>
</ul>';
}
}
echo '</li>
</ul>
</div>';
?>
However, this assumes some things about the submenu-bits. Normally, you would either fetch those items within the same query, and have them available to you via $row, but you can also do separate queries for those items based on a connection from the $row['id'] or something, and pull those subitems that corresponds to that id, and run another while-loop around the secondary <li>-items.

dynamic navigation using mysql php and ajax

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.

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>

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 } ?>

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