I have two lists (1 and 2) and I want to update them when an item is moved from one to another.
HTML:
<ul class="sortable" id="task-list">
<li id="listItem_1">Value 1 </li>
<li id="listItem_2">Value 2 </li>
<li id="listItem_3">Value 3 </li>
</ul>
<ul class="sortable" id="task-list-two">
<li id="listItem_4">Value 1 </li>
<li id="listItem_5">Value 2 </li>
<li id="listItem_6">Value 3 </li>
</ul>
JS:
$("#task-list, #task-list-two").sortable({
handle : '.handle',
connectWith: ".sortable"
update : function () {
var order = $('#task-list').sortable('serialize');
$("#info").load("process-sortable.php?"+order);
}
});
But I want to update the list (1 or 2) as well. Do I need to post an additional variable to process.sortable.php ?
The foreach I currently have is only for the first list (simplified):
$task = nl2br($_POST['task']);
$userid = $_SESSION['userid'];
$position = $_POST['p'];
$date = $_POST['date'];
$i = "INSERT INTO tasks VALUES('','$task','$userid','0','$position','$date')";
$doi = mysql_query($i) or die(mysql_error());
Thanks in advance!
UPDATED FOREACH PHP:
foreach ($_GET['listItem'] as $position => $item)
{
$list = $_GET['list'];
if($list == "task-list")
{
$list = 1;
}
if($list == "task-list-two")
{
$list = 2;
}
$sql = "UPDATE `tasks` SET `position` = $position AND date = '$list' WHERE `id` = $item";
print($sql);
$dosql = mysql_query($sql) or die(mysql_error());
}
You could just pass the lists id to the PHP page like:
$("#info").load("process-sortable.php?"+order+"&list="+$(this).attr('id'));
Once on the PHP page, you can make a condition for the list type.
You will also need to change the order variable to account for the different lists:
var order = $(this).sortable( "serialize" );
Related
I have category table which include 35 category inside. I want them aline into four div with one while loop.
<?php
$count = 0;
$cat = pullcategories();
$catcount = mysqli_num_rows($cat);
$percat = ceil($catcount / 4);
$topcats = pulltopcategories($count, $percat);
//same with pullcategories, just LIMIT $count,$per //
while ($topcats = mysqli_fetch_object($topcats)) {
?>
<div class="col3">
<ul class="list-unstyled list-dashed">
<li>
$topcats->title
</li>
</ul>
</div>
<?php
$count = $count + $percat;
$percat = $percat * 2;
}
?>
$count = 0;
$cat = pullcategories();
$catcount = mysqli_num_rows($cat);
$percat = ceil($catcount / 4);
$topcats = pulltopcategories($count, $percat);
This breaks your lists down in to 4 parts, and you would need to call "$topcats = pulltopcategories($count, $percat);" 4 times to be able to get through all the data.
I would change it to be something like this.
Assuming that "pulltopcategories($count, $percat)" uses count as the limit, and percat as the offset in the SQL this should work.
Please try and let me know - I can fix any mistakes if there are any (my PHP is a little rusty and i dont have data to try this with)
<?php
$count = 0;
$cat = pullcategories();
$catcount = mysqli_num_rows($cat);
$percat = ceil($catcount / 4);
while ($count < catcount){ /* Loop until you have finished all cats */
$topcats = pulltopcategories($count, $percat);
echo "<div class="col3">
<ul class='list-unstyled list-dashed'>";
while ($topcats = mysqli_fetch_object($topcats)) {
echo "<li>
<a href=''>$topcats->title</a>
</li>"
}
echo "</ul>
</div>";
$count = $count + $percat;
}
?>
I'm beginner and I'm trying to make a simple navigation menu with a dropdown but the first loop stops when the nested loop is completed. Is there any suggestion? Please be analytic because of my low programming skills.
<?php
function nav_main($dbc, $path) {
$q = "SELECT * FROM navigation ORDER BY position ASC";
$r = mysqli_query($dbc, $q);
while($nav = mysqli_fetch_assoc($r)) {
$nav['slug'] = get_slug($dbc, $nav['url']);
if($nav['parent_id'] == 0) {
?>
<li<?php selected($path['call_parts'][0], $nav['slug'], ' class="active"') ?>><?php echo $nav['label']; ?></li>
<?php
}
elseif ($nav['parent_id'] == 1) {
?>
<li class="dropdown<?php selected($path['call_parts'][0], $nav['slug'], ' active"') ?>"><?php echo $nav['label']; ?><span class="caret"></span>
<ul class="dropdown-menu">
<?php
while($subnav = mysqli_fetch_assoc($r)) {
$subnav['slug'] = get_slug($dbc, $subnav['url']);
if($subnav['parent_id'] == 3) {
?>
<li><?php echo $subnav['label']; ?></li>
<li role="separator" class="divider"></li>
<?php
}
}
?>
</ul>
</li>
<?php
}
}
}
?>
Both your while loops are processing the same result set!
So obviously when the second while loop finishes the resultset is completely consumed and there is nothing left for the outer while look to do but terminate.
This is a precise of what you are doing :-
$q = "SELECT * FROM navigation ORDER BY position ASC";
$r = mysqli_query($dbc, $q);
// first while loop
while($nav = mysqli_fetch_assoc($r)) {
// inner while loop
while($subnav = mysqli_fetch_assoc($r)) {
Both are using $r and therefore regardless of you using a different variable to hold the returned row data the inner loop will consume row 2->end of the same result set as is being processed in the outer while loop
I want to make menu's last element is javascript:void(0); last <li> of a tag is void(0);
I want exactly like this
<ul>
<li>Home</li>
<li>About</li>
<li>Service</li>
<li id="top2"><a href="javascript:void(0);" >UP</a></li>
</ul>
How it posssible using typoscript ? i added my typoscript here
lib.Footer_menu = COA
lib.Footer_menu{
10 = HMENU
10.special = directory
10.special.value = 12
10
{
wrap = <ul class="col-left-menu">|</ul>
1 = TMENU
1{
expAll = 1
noBlur = 1
NO{
doNotLinkIt = 1
allWrap = |
stdWrap.htmlSpecialChars = 1
wrapItemAndSub = <li>|</li>||<li>|</li>||<li>|</li>||<li>|</li>
}
ACT < .NO
ACT = 1
ACT {
wrapItemAndSub = <li>|</li>||<li>|</li>||<li>|</li>||<li id="top">|</li>
}
IFSUB < .NO
IFSUB = 1
IFSUB {
stdWrap.htmlSpecialChars = 1
wrapItemAndSub = <li>|</li>||<li>|</li>||<li>|</li>||<li id="top">|</li>
}
ACTIFSUB < .IFSUB
ACTIFSUB = 1
ACTIFSUB {
stdWrap.htmlSpecialChars = 1
wrapItemAndSub = <li class="active dropdown">|</li>
stdWrap.wrap = <b class="caret"></b>
}
}
}
}
You can use TypoScript for rendering this li
include this in TMENU section
stdWrap.append = TEXT
stdWrap.append.value = <li id="top2"><a href="javascript:void(0);" >UP</a></li>
In working on a dynamic menu w/ CRUD I have been having some trouble with the finishing touches. It works, but there are some extra tags getting inserted where there shouldn't be and can't figure out how to clean it up before I share it with the world. I used this as a starting point, then changed it to work with an accordion menu (http://www.phpro.org/tutorials/Simple-Mysql-PHP-Menu.html).
Below is the data from the table (I changed the names on the first 2 fields to get it to fit in the SO format from menu_item_id to id, and from menu_parent_id to pid).
id pid menu_item_name menu_url sortorder status
1 0 Settings 0 ACTIVE
2 5 Grid Demo grid.php ACTIVE
3 5 setGridOptions gridoptions.php ACTIVE
4 1 Menu Items adminmenu.php 1 ACTIVE
5 0 Grid Settings 100 ACTIVE
6 1 General Settings settings.php 100 ACTIVE
Here is the PHP that connects to the mysql database and creates the hierarchical tree array to make it work:
include 'db.php';
$sql = "SELECT * FROM menu_items WHERE status = \"ACTIVE\" ORDER BY sortorder, menu_item_name";
$query = $db->query($sql);
while($data = $query->fetch(PDO::FETCH_ASSOC))
// loop over the results
{
// Assign by reference
$thisref = &$refs[ $data['menu_item_id'] ];
// add the the menu parent
$thisref['menu_item_id'] = $data['menu_item_id'];
$thisref['menu_parent_id'] = $data['menu_parent_id'];
$thisref['menu_item_name'] = $data['menu_item_name'];
$thisref['menu_url'] = $data['menu_url'];
// if there is no parent id
if ($data['menu_parent_id'] == 0)
{
$list[ $data['menu_item_id'] ] = &$thisref;
}
else
{
$refs[ $data['menu_parent_id'] ]['children'][ $data['menu_item_id'] ] = &$thisref;
}
}
function create_list( $arr )
{
$html = "";
foreach ($arr as $key=>$v)
{
if ($v['menu_parent_id'] == '0')
{
$html .= '<a class="menuitem submenuheader" href="'. $v['menu_url'] .'">'.$v['menu_item_name']."</a>\n";
$html .= "<div class=\"submenu\">\n<ul>\n";
$html .= "<li>" . create_list($v['children']) . "</li>";
$html .= "</ul>\n";
}
else{
$html .= '<li><a id="' . $v['menu_item_id'] . '">'.$v['menu_item_name']."</a></li>\n";
}
}
$html .= "</div>\n";
return $html;
}
echo "<div class=\"glossymenu\">";
echo create_list( $list );
echo "</div>";
When I run it, it outputs the following:
<div class="glossymenu"><a class="menuitem submenuheader">Settings</a>
<div class="submenu">
<ul>
<li><li><a id="4">Menu Items</a></li>
<li><a id="6">General Settings</a></li>
</div>
</li></ul>
<a class="menuitem submenuheader" href="">Grid Settings</a>
<div class="submenu">
<ul>
<li><li><a id="2">Grid Demo</a></li>
<li><a id="3">setGridOptions</a></li>
</div>
</li></ul>
</div>
</div>
As you can see there are extra <li> tags, the </ul> is in the wrong
spot (should be after the </div>) Other than that, it is working great.
The other thing I can't figure out is if I have a root menu item with no children, I would love it to have a different output like
<a id="8">No Children Menu Item</a>
Instead of:
<a class="menuitem submenuheader">No Children Menu Item</a>
The second example would create the make it show up the little (+/-) for expanding and contracting and wouldn't allow me to click on it. For clarification on the <a> tags, I am using javascript to do a .get() based off the id which is why there is no href or url shown.
UPDATE
It is working correctly and I posted it on Github for anyone that wants it.
https://github.com/ajhalls/php-accordian-menu
Try this :
<?php
include 'db.php';
$sql = "SELECT * FROM menu_items WHERE status = 'ACTIVE' ORDER BY pid ASC, sortorder ASC, menu_item_name ASC";
$query = $db->query($sql);
$menu_items = array();
while($data = $query->fetch(PDO::FETCH_ASSOC)) {
if($data['pid'] == 0) {
$menu_items[$data['id']] = array();
$menu_items[$data['id']]['id'] = $data['id'];
$menu_items[$data['id']]['name'] = $data['menu_item_name'];
$menu_items[$data['id']]['url'] = $data['menu_url'];
$menu_items[$data['id']]['children'] = array();
} else if($data['pid'] != 0) {
$tmp = array();
$tmp['id'] = $data['id'];
$tmp['name'] = $data['menu_item_name'];
$tmp['url'] = $data['menu_url'];
array_push($menu_items[$data['pid']]['children'],$tmp);
unset($tmp);
}
}
function create_list($arr)
{
$html = "";
foreach($arr as $key => $value) {
if(count($value['children']) > 0) {
$html .= ' <a class="menuitem submenuheader" href="'. $value['url'] .'">'.$value['name'].'</a>
<div class="submenu">
<ul>';
foreach($value['children'] AS $child) {
$html .= ' <li>
<a id="'.$child['id'].'">'.$child['name'].'</a>
</li>';
}
$html .= ' </ul>
</div>';
} else{
$html .= ' <a id="'.$value['id'].'">'.$value['name'].'</a>';
}
}
return $html;
}
echo "<div class=\"glossymenu\">";
echo create_list($menu_items);
echo "</div>";
?>
I have a nested foreach loop running through categories in a database. When the user is viewing a category the category ID is set and that menu item gets it's class set as "active".
The issue I'm having is when a user is viewing a subcategory, I still need it's PARENT to be set to active.
// Run through category ID's and output info
foreach ($GLOBALS['AKB_CLASS_HELPER']->tree->nodesByPid[$catid] as $rowid) {
$row = $GLOBALS['AKB_CLASS_HELPER']->catsById[$rowid];
//Check to see if user has access
if (($accessible_cats !== false) && (!in_array($row['categoryid'],$accessible_cats)) && ($row['security'] == "private")) {
continue;
}
// If we're viewing a category, output "active" as a class in the menu
if (isset($GLOBALS['CategoryId'])
&& ($GLOBALS['CategoryId']) == $row['categoryid']) {
$activeCat = "active";
} else {
$activeCat = "";
}
//Pass through global variables
$GLOBALS['Link'] = GetUrl('category', $row['categoryid']);
$GLOBALS['Text'] = $GLOBALS['AKB_CLASS_TEMPLATE']->DisablePlaceholders(strip_tags($row['name']));
$GLOBALS['Active'] = $activeCat;
$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('CategoryRowCell');
//If subcategories exist
if (isset($GLOBALS['AKB_CLASS_HELPER']->tree->nodesByPid[$rowid])
&& is_array($GLOBALS['AKB_CLASS_HELPER']->tree->nodesByPid[$rowid])
&& !empty($GLOBALS['AKB_CLASS_HELPER']->tree->nodesByPid[$rowid])) {
$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('CategoryGridHeaderSub');
//For every subcategory, output data
foreach ($GLOBALS['AKB_CLASS_HELPER']->tree->nodesByPid[$rowid] as $subrowid) {
$subrow = $GLOBALS['AKB_CLASS_HELPER']->catsById[$subrowid];
//Here's the part I can't get working. Need to output "active" to the PARENT id($row I believe)
if (isset($GLOBALS['CategoryId'])
&& ($GLOBALS['CategoryId']) == $subrow['categoryid']) {
$activeCat = "active";
} else {
$activeCat = "";
}
//Set Global Variables
$GLOBALS['Link'] = GetUrl('category', $subrow['categoryid']);
$GLOBALS['Text'] = $GLOBALS['AKB_CLASS_TEMPLATE']->DisablePlaceholders(strip_tags($subrow['name']));
$GLOBALS['Active'] = $activeCat;
$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('CategoryRowCellSub');
}
}
$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('CategoryGridFooterSub');
}
So the part I need to figure out is in the nested foreach loop, how can I affect the parent ID or something of the like.
Current Output when viewing parents(Working perfectly)
<li class="categoryItem active">Parent 1
<ul class="categorySubList">
<li class="categorySubItem"><a class="categorylink" href="/categories/Parent+1/Subcategory+1/">Subcategory 1</a></li>
<li class="categorySubItem "><a class="categorylink" href="/categories/Parent+1/Subcategory+2/">Subcategory 2</a></li>
</ul>
Current Output when viewing children(not working)
<li class="categoryItem">Parent 1<!--this list item needs to be active-->
<ul class="categorySubList">
<li class="categorySubItem active"><a class="categorylink" href="/categories/Parent+1/Subcategory+1/">Subcategory 1</a></li>
<li class="categorySubItem "><a class="categorylink" href="/categories/Parent+1/Subcategory+2/">Subcategory 2</a></li>
</ul>