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.
Related
just wondering if anyone can help. I am trying to load data into tabs from my SQL database and was wondering if anyone knows how to load four rows of data into four different arrays. I think I am nearly there with my code but am a bit stuck.
Alternatively if anyone knows of any tutorials for loading data from a database into tabs that also loads data into each [li] it would be much appreciated.
Thanks for the help.
<?php
$query = mysql_query("SELECT title, author, date_added FROM table ORDER BY date_added DESC LIMIT 4");
$array = array();
while($row = mysql_fetch_assoc($query)){
$array[] = $row1;
?>
<div class="tabs-c">
<ul class="tab-c-nav">
<li class="tab-c-active"><?php echo $row1['title'];?></li>
<li><?php echo $row2['title'];?></li>
<li><?php echo $row3['title'];?></li>
<li><a href="#tab-c-4"><?php echo $row4['title'];?>/a></li>
</ul>
<div class="tab-c-content">
<div class="text-details-content-half-row">
<div class="text-details-content-row-text-left">Written by</div>
<div class="text-details-content-row-text-right"><?php echo $row1['author']?></div>
</div>
<div class="text-details-content-row-space"></div>
<div class="text-details-content-half-row">
<div class="text-details-content-row-text-left">Date Added</div>
<div class="text-details-content-row-text-right"><?php echo $row1['date_added']?></div>
</div>
</div>
Replace
$array[] = $row1;
With:
$array[] = $row;
And then:
<ul class="tab-c-nav">
<li class="tab-c-active"><?php echo $array[0]['title'];?></li>
<li><?php echo $array[1]['title'];?></li>
<li><?php echo $array[2]['title'];?></li>
<li><?php echo $array[3]['title'];?></li>
</ul>
I think you mess it up a bit. The variables $row1, $row2, $row3, $row4 you use are not declared anywhere. What you you need is a while loop to populate your $array as follows:
$array[] = $row;
Then you can have access in the different rows like this:
echo $array[0]['title'];
Replace your
$array[] = $row1;
With this
$array[] = $row;
It's simple way to achieve that create reach row of table as array.
Then we can traverse into each array using their index like $array[0]['title']
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
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 } ?>
I have successfully created a custom drop-down-menu for each specific category I needed but one of them needs to load the subcategory within a subcategory and I can't get it to work.
The working code without the "subcategory within a subcategory" is the following but I need to find out how to add the 3rd level on this code.
<!-- Vending -->
<?php $main = Mage::getModel('catalog/category')->load(355) ?>
<li class="eight"><?php echo $main->getName(); ?>
<?php $children = Mage::getModel('catalog/category')->getCategories(355); ?>
<ul class="nav_static">
<?php foreach ($children as $category): ?>
<li>
<a href="<?php echo $category->getRequestPath(); ?>">
<?php echo $category->getName(); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</li>
<!-- END - Vending -->
Well, as I have suggested in https://stackoverflow.com/a/14586422/653867 you have to load a category object for your second level categories:
$cat = Mage::getModel('catalog/category')->load($category->getEntityId());
then you can access its children by executing
$children = $cat->getChildrenCategories();
The $children variable is a collection of type Mage_Catalog_Model_Resource_Category_Collection, and you can iterate through it to output the next level categories
I think, your code can be improved a bit if you called getChildrenCategories() on your $main in the first place. You wouldn't have to load every child in a loop, which can be performance punishing. Instead use this (and it can actually be improved even further with recursive calls, but such setup would include creating extra blocks, which might be too much hassle for this particular case):
<?php $main = Mage::getModel('catalog/category')->load(355) ?>
<li class="eight"><?php echo $main->getName(); ?>
<?php $children = $main->getChildrenCategories(); ?>
<ul class="nav_static">
<?php foreach ($children as $category): ?>
<li>
<a href="<?php echo $category->getUrl(); ?>">
<?php echo $category->getName();
$subCategories = $category->getChildrenCategories();
foreach ($subCategories as $subCat) {
/**
*your code to output the next level categories
*/
}
?>
</a>
</li>
<?php endforeach; ?>
</ul>
</li>
The drop down menu only show the first loop with its first item from database and then every items of the first item shown in the first loop. There is no second item listed of the first loop. Any idea to help me with this loop???
<ul class="pureCSSMenum">
<li class="pureCssMenui">E-Policy
<ul class="pureCssMenum">
<?
$sql = "SELECT * FROM e_category";
$result = mysql_query($sql);
while ($epolicy = mysql_fetch_array($result)) {
?>
<li class="pureCssMenui"><a class="pureCssMenui" href="#"><?=$epolicy['e_cat_name']?></a>
<ul class="pureCssMenum">
<?php
$sql = "SELECT * FROM e_subcategory";
$result = mysql_query($sql);
while ($epolicysub = mysql_fetch_array($result)) {
?>
<li class="pureCssMenui"><a class="pureCssMenui" href="#"><?=$epolicysub['e_subcat_name']?></a></li>
<? } ?>
</ul>
<? } ?>
</li>
</ul>
</li>
</ul>
You must use two result variables $result1, $result2 for outer and inner variable, rest all is fine i think.
Hope this will help