I want make menu with 2 levels sub menu. Eventually, the HTML should like the code below:
<ul>
<li>elektronikos prekes
<ul>
<li>dar kaskas
<ul>
<li>ka ka 2</li>
</ul>
</li>
<li>tranzistoriai
<ul>
<li>sub 1</li>
<li>tikri 1</li>
</ul>
</li>
</ul>
</li>
<li>kompiuteriai
<ul>
<li>nesiojami 8</li>
</ul>
</li>
</ul>
Here is my code:
$queryreg=mysql_query("select distinct a.meniu as meniu , b.sub as sub, c.sub2 as sub2,a.meniu_id as m_id, b.sub_id as s_id,
c.sub2_id as s_id2, count(distinct p.id) as pre
from meniu a
LEFT OUTER JOIN sub AS b ON ( a.meniu_id = b.meniu_id and b.rodyti='1' )
LEFT OUTER JOIN preke AS p ON ( b.sub = p.sub and a.meniu=p.meniu)
LEFT OUTER JOIN sub2 AS c ON ( b.sub_id = c.sub_id AND c.rodyti='1')
LEFT OUTER JOIN preke AS pr ON ( pr.sub2 = c.sub2 and pr.sub=b.sub and pr.meniu=a.meniu)
where a.rodyti='1' and a.meniu='elektronikos prekes' or a.meniu='kompiuteriai'
group by a.meniu, a.meniu_id, b.sub, b.sub_id, c.sub2, c.sub_id");
$AnkstesnisSub='';
$ankstesnisMeniu='';
$output = $ankstesnisMeniu.$AnkstesnisSub = '';
echo'<ul>';
while ($row = mysql_fetch_assoc ($queryreg))
{
if ($row['meniu'] != $ankstesnisMeniu )
{
if (!empty ($ankstesnisMeniu) )
{
$output .= " </ul></li></ul></li>";
}
$output .= '<li>' . $row['meniu'] . "<ul>";
$ankstesnisMeniu = $row['meniu'];
}
if ( $row['sub'] != $AnkstesnisSub)
{
if (!empty ($AnkstesnisSub))
{
$output .= "</ul></li>";
}
if($row['sub2']==NULL){$prekiu_kiekis = $row['pre'];}
elseif($row['sub2']!=NULL){$prekiu_kiekis ='';}
$output .= '<li>' . $row['sub'] . " $prekiu_kiekis<ul>";
$AnkstesnisSub = $row['sub'];
$prevID2 = $row['s_id'];
}
if($row['sub2']!=NULL){$prekiu_kiekis = $row['pre'];}
elseif($row['sub2']==NULL){$prekiu_kiekis ='';}
$output .= '<li>' . $row['sub2'] ." $prekiu_kiekis</li>";
$prevID3=$row['sub2'];
}
$output = '' . $output . "</ul></li></ul></li>";
echo"$output";
echo'</ul>';
But I get:
<ul>
<li>elektronikos prekes
<ul>
<li>dar kaskas
<ul>
<li>ka ka 2</li>
</ul>
</li>
<li>tranzistoriai
<ul>
<li>sub 1</li>
<li>tikri 1</li>
</ul>
</li>
</ul>
</li>
<li>kompiuteriai
<ul></ul>
</li>
<li>nesiojami 8
<ul>
<li></li>
</ul>
</li>
</ul>
</li></ul>
To the first menu item everything is fine, but the second wrong. maybe somebody can help me?
i think it's very complicated code, why not to separate this for 2 table?
top_menu => link_id, href, link_text, count_sub
sub_menu => link_id, parent_id, href, link_text, count_sub
the code for getting the menu:
$query = mysql_query("SELECT * FROM top_menu");
while ( $row = mysql_fetch_assoc($queryreg) )
{
echo '<li>' . $row['href'] . '</li>';
if( $row['subcount'] != 0 ) {
{ make query for the sub_menu and do more while... and in the while you can add more }
}
}
and so on.. it's more clean for my opinion.
Related
I have a result like this:
Using foreach loop i can get the output like this:
<ul>
<li>1 group1 group1_title group1_link 2</li>
<li>2 group1 group1_title group1_link 1</li>
<li>3 group2 group2_title group2_link 2</li>
<li>4 group2 group2_title group2_link 1</li>
<li>5 group3 group3_title group3_link 2</li>
<li>6 group3 group3_title group3_link 1</li>
<li>7 group3 group3_title group3_link 2</li>
</ul>
But I need to make the result like this:
<ul data-group="1">
<li>1 group1 group1_title group1_link 2</li>
<li>2 group1 group1_title group1_link 1</li>
</ul>
<ul data-group="2">
<li>1 group2 group2_title group2_link 2</li>
<li>2 group2 group2_title group2_link 1</li>
</ul>
<ul data-group="3">
<li>1 group3 group3_title group3_link 2</li>
<li>2 group3 group3_title group3_link 1</li>
<li>3 group3 group3_title group3_link 2</li>
</ul>
Here is my simple php code
if ($getlinks->num_rows() > 0)
{
$x = 0;
foreach ($getlinks->result() as $group)
{
$x++;
echo '<ul group="'.$x.'">';
echo '<li>'.$group->id.' - '.$group->group.' - '.$group->title.' - '.$group->link.' - '.$group->value.'</li>';
echo '</ul>';
}
}
Update:
using bellow code i can group by value but i need make something like this by key not value
$people = array("Peter", "Peter", "Joe", "Glenn", "Cleveland");
//echo current($people) . "<br>";
//echo end($people);
foreach ($people as $p){
echo ($p);
if (current($people) !== next($people)){
echo " bah<br>";
}
}```
You need to track when the group changes:
if ($getlinks->num_rows() > 0)
{
$x = 0;
$prev_group = null;
foreach ($getlinks->result() as $group)
{
// I am assuming $group->id can't be null for an actual group
if ( $group->id !== $prev_group ) {
if ( $x > 0 ) {
// Close previous group
echo '</ul>';
}
echo '<ul group="'.$group->id.'">';
$x = 0;
$prev_group = $group->id;
}
$x++;
echo '<li>'$x.' - '.$group->id.' - '.$group->group.' - '.$group->title.' - '.$group->link.' - '.$group->value.'</li>';
}
if ( $x > 0 ) {
// Close last group
echo '</ul>';
}
}
I'm not clear what your data structures are and if it's $group->id or $group->group that should be compared, but you see the idea I hope.
I done a code for displaying unordered list like this. But it's not working correctly how do i make correctly.i am new to php please help me thanks in advance.
<ul>
<li class='mainnode'>A</li>
<ul>
<li class='chlnode'>A1</li>
<ul>
<li class='chlnode'>A3</li>
</ul>
<ul></ul>
<li class='chlnode'>A2</li>
<ul>
<li class='chlnode'>A4</li>
</ul>
<ul>
<ul>
<li class='chlnode'>A5</li>
</ul>
<ul></ul>
</ul>
</ul>
<li class='mainnode'>B</li>
<ul>
<li class='chlnode'>B1</li>
<li class='chlnode'>B2</li>
<ul>
<li class='chlnode'>B3</li>
</ul>
<ul>
<ul>
<li class='chlnode'>B4</li>
</ul>
<ul></ul>
</ul>
</ul>
<li class='mainnode'>C</li>
<ul>
<li class='chlnode'>C1</li>
<ul>
<li class='chlnode'>C2</li>
</ul>
<ul>
<ul>
<li class='chlnode'>C3</li>
</ul>
<ul>
<ul>
<li class='chlnode'>C4</li>
</ul>
<ul>
<ul>
<li class='chlnode'>C5</li>
</ul>
</ul>
</ul>
</ul>
</ul>
<li class='mainnode'>D</li>
<ul>
<li class='chlnode'>D1</li>
<ul>
<li class='chlnode'>D4</li>
</ul>
<ul></ul>
<li class='chlnode'>D3</li>
<ul>
<li class='chlnode'>D5</li>
</ul>
<ul>
<ul>
<li class='chlnode'>D6</li>
</ul>
<ul>
<ul>
<li class='chlnode'>D7</li>
</ul>
<ul></ul>
</ul>
</ul>
</ul>
<li class='mainnode'>E</li>
<ul>
<li class='chlnode'>E1</li>
</ul>
<li class='mainnode'>F</li>
<ul>
<li class='chlnode'>F1</li>
</ul>
<li class='mainnode'>G</li>
<ul>
<li class='chlnode'>J</li>
<ul>
<li class='chlnode'>J1</li>
</ul>
<ul></ul>
</ul>
</ul>
this is my php function for creating html code.
<?php
function menu()
{
echo "<ul>";
//this is sql query for parent element
$list = mysql_query("SELECT * FROM `abc` WHERE col2='0' ");
while ($row = mysql_fetch_array($list)) {
echo "<li class='mainnode'>" . $row['col3'] . "</li>";
echo "<ul>";
//this is sql query for child element
$list1 = mysql_query("SELECT * FROM `abc` WHERE `col2`= " . $row['col1']);
while ($row = mysql_fetch_array($list1)) {
echo "<li class='chlnode'>" . $row['col3'] . "</li>";
$list2 = mysql_query("SELECT * FROM `abc` WHERE `col2`= " . $row['col1']);
while ($row = mysql_fetch_array($list2)) {
echo "<ul>" . "<li class='chlnode'>" . $row['col3'] . "</li>" . "</ul>";
echo "<ul>";
$list3 = mysql_query("SELECT * FROM `abc` WHERE `col2`=" . $row['col1']);
while ($row = mysql_fetch_array($list3)) {
echo "<ul>" . "<li class='chlnode'>" . $row['col3'] . "</li>" . "</ul>";
echo "<ul>";
$list4 = mysql_query("SELECT * FROM `abc` WHERE `col2`= " . $row['col1']);
while ($row = mysql_fetch_array($list4)) {
echo "<ul>" . "<li class='chlnode'>" . $row['col3'] . "</li>" . "</ul>";
echo "<ul>";
$list5 = mysql_query("SELECT * FROM `abc` WHERE `col2`= " . $row['col1']);
while ($row = mysql_fetch_array($list5)) {
echo "<ul>" . "<li class='chlnode'>" . $row['col3'] . "</li>" . "</ul>";
}
echo "</ul>";
}
echo "</ul>";
}
echo "</ul>";
}
}
echo "</ul>";
}
echo "</ul>";
}
?>
Do not use mysql_ Deprecated use mysqli_ . Try to make Unordered list <ul> before while loop and try to loop every row value with <li>.
Here the Glimpse on it.
<?php $list1 = mysqli_query($con,"SELECT * FROM abc WHERE col2= ".$row['col1'] ); ?>
<ul>
<?php while($row = mysqli_fetch_array($list1)) { ?>
<li><?php echo $row['col3']; ?>"</li>
<?php $list2 = mysqli_query($con,"SELECT * FROM `abc` WHERE `col2`= ".$row['col1'] ); ?>
<ul>
<?php while($row = mysqli_fetch_array($list2))
{ ?>
<li><?php echo $row['col3']; ?>"</li>
<?php } ?>
</ul>
<?php } ?>
</ul>
first you need to take the count of the returning rows from mysql.
then put a for loop to iterate and print the list.inside the for loop you can put your while loop and print the list tags as you wish.
All the nested ul should start within Li as follows.
<ul>
<Li> c
<ul>
<Li> c1
<ul>
<Li> c2 </Li>
</ul>
</Li>
</ul>
</Li>
</ul>
Hope this will solve the display problem
I have a problem with closing my li's and ul's at the correct moment.
With the code we have we retrieve all childeren of a specific categorie in the magento shop.
Now the problem is that i want to divide all children in lists. So we can have them sorted by category -> sub-category -> sub-sub category. I want my structure to be;
<ul>
<li>
<a>Head Child</a>
<ul>
<li>
<a>Sub child</a>
<ul>
<li><a>Sub sub child</a></li>
<li><a>Sub sub child</a></li>
</ul>
</li>
</ul>
</li>
<li>
<a>Head Child</a>
<ul>
<li>
<a>Sub child</a>
<ul>
<li><a>Sub sub child</a></li>
<li><a>Sub sub child</a></li>
</ul>
</li>
</ul>
</li>
</ul>
The output im getting now is
<ul>
<a title="View the products for the " href="#">Head child</a>
<li class="sub_cat">
<a title="View the products for the " href="#">Sub child</a>
</li>
<li class="sub_cat">
<a title="View the products for the " href="#">Sub sub child</a>
</li>
</ul>
This is our php code;
<?php
$cat = Mage::getModel('catalog/category')->load(9);
$subcats = $cat->getChildren();
foreach(explode(',',$subcats) as $subCatid)
{
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()) {
echo '<ul>'.$_category->getName().'';
$sub_cat = Mage::getModel('catalog/category')->load($_category->getId());
$sub_subcats = $sub_cat->getChildren();
foreach(explode(',',$sub_subcats) as $sub_subCatid)
{
$_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
if($_sub_category->getIsActive()) {
echo '<li class="sub_cat">'.$_sub_category->getName().'';
$sub_sub_cat = Mage::getModel('catalog/category')->load($sub_subCatid);
$sub_sub_subcats = $sub_sub_cat->getChildren();
foreach(explode(',',$sub_sub_subcats) as $sub_sub_subCatid)
{
$_sub_sub_category = Mage::getModel('catalog/category')->load($sub_sub_subCatid);
if($_sub_sub_category->getIsActive()) {
echo '<li class="sub_cat">'.$_sub_sub_category->getName().'';
}
}
}
}
echo '</ul>';
}
}
?>
You should consider using recursive function here
$cat = Mage::getModel('catalog/category')->load(9);
$html = '';
$html = getSubCategoriesHTML($cat, $html);
function getSubCategoriesHTML($cat, $html) {
$html .= ''.$cat->getName().'';
$html .= '<ul>';
$subcats = $cat->getChildren();
foreach(explode(',',$subcats) as $subCatid) {
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()) {
$html .= '<li>';
$html .= getSubCategoriesHTML($_category, $html);
$html .= '</li>;
}
}
$html .= '</ul>';
return $html;
}
You can add this function in a helper or in your category model.
I haven't tested it at all so it may not work correctly. But I hope it can help you.
Problem:
I am trying to group related posts from SQL with each other using the ul and li-elements. However, I am stuck as I can't get the output correct.
PHP code (1st level):
$query = "SELECT CID, Item AS Name, Parent, Weight FROM betyg_category";
$result = mysql_query($query) or die ('Error (' . mysql_errno() . ') ' . mysql_error());
$ctree .= '<ul>';
while ($row = mysql_fetch_assoc($result))
{
$ctree .= '<li class="category">'.$row['Name'] . '';
$ctree .= getLowerRanks($row['CID'], 1, true);
}
$ctree .= '</li></ul>';
echo $ctree;
Recursive function in PHP:
function getLowerRanks( $id, $level, $option = false )
{
if ( $option )
{
$response = '';
$query = "SELECT CID, Item AS Name, Parent, Weight FROM betyg_category WHERE Status = 1 AND Parent= " . $id . " ORDER BY CID ASC";
$result = mysql_query($query) or die ('Error (' . mysql_errno() . ') ' . mysql_error());
$response .= '<ul>';
while ($row = mysql_fetch_assoc($result))
{
for ($i = 0; $i < ($level-1); $i++) $response .= ' ';
$response .= '<li class="category"> '.$row['Name'] . '</li>';
$response .= getLowerRanks($row['CID'],$level+1, true);
}
$response .= '</ul>';
}
return $response;
}
Desired output:
<ul>
<li class="category">Litteratur
<ul>
<li class="category"> Integration av källorna</li>
<li class="category"> Belysning av egna resultat</li>
<li class="category"> Referenser</li>
</ul>
</li>
<li class="category">Validitet
<ul>
<li class="category"> Huvudsyfte</li>
<li class="category"> Oberoende och beroende variabler</li>
<li class="category"> Analysmetoderna</li>
</ul>
</li>
<li class="category">Reliabilitet
<ul>
<li class="category"> Metodval</li>
<li class="category"> Metodbeskrivning</li>
<li class="category"> Databearbetning</li>
</ul>
</li>
<li class="category">Språk, stil och struktur
<ul>
<li class="category"> Språk och stil</li>
<li class="category"> Struktur</li>
</ul>
</li>
<li class="category">Arbetssätt
<ul>
<li class="category"> Försvar och opposition</li>
<li class="category"> Etiska och samhälleliga aspekter</li>
</ul>
</li>
</ul>
In your PHP code (1st level): you need to close your <li> in the while loop.
$ctree .= '<ul>';
while ($row = mysql_fetch_assoc($result))
{
$ctree .= '<li class="category">'.$row['Name'] . '';
$ctree .= getLowerRanks($row['CID'], 1, true);
$ctree .= '</li>';
}
$ctree .= '</ul>';
I have this MySQL statement
Select type.type, color.color, ShotName, Item.name, Item.Item_id
From type
Inner Join Item
On type.type_id =Item.type_id
Inner Join color
On color.color_id =Item.color_id
Where Item.state=0 And Item.offline = 0
Group By color.color, Item.name, type.order_nr
Order By type_D.order_nr, color.color, Item.name_d
How can do this with php to get a result formatted as the following HTML? I have managed to list the items but I am not able to put <ul> properly
<body>
<h2> Type AA </h2>
<h3> color black </h3>
<ul>
<li> ShortName 1 name 1</li>
<li> ShortName 2 name 2</li>
<li> ShortName 3 name 3</li>
<li> ShortName 4 name 4</li>
<li> ShortName 5 name 5</li>
</ul>
<h3> color green </h3>
<ul>
<li> ShortName 7 name 7</li>
<li> ShortName 8 name 8</li>
<li> ShortName 9 name 9</li>
<li> ShortName 10 name 10</li>
<li> ShortName 11 name 11</li>
</ul>
<h2> Type AB </h2>
<h3> color black </h3>
<ul>
<li> ShortName 12 name 12</li>
<li> ShortName 13 name 13</li>
<li> ShortName 14 name 14</li>
<li> ShortName 15name 15</li>
<li> ShortName 16name 16</li>
</ul>
<h3> color green </h3>
<ul>
<li> ShortName 17 name 17</li>
<li> ShortName 18 name 18</li>
<li> ShortName 19 name 19</li>
<li> ShortName 20 name 20</li>
<li> ShortName 22 name 22</li>
</ul>
</body>
Here is my PHP code
$myfile = mysql_query($query_myfile, $db) or die(mysql_error());
$totalRows_myfile = mysql_num_rows($myfile);
while ( $row_myfile = mysql_fetch_assoc($myfile) )
{
if ( $type != $row_myfile[ 'type' ] )
{
$type = $row_myfile[ 'type' ];
echo "<h2>$type</h2>";
}
if ( $color != $row_myfile[ 'color' ] )
{
$color = $row_myfile[ 'color' ];
echo "<h3>$color</h3>";
}
echo "<li><a href=\"itemDetail.php?item_id=".$row_myfile['myfile_id']. "\">";
echo $row_myfile['ShortName'].' ';
echo $row_myfile['name']; ?></a></li>
Add more things you want to display in the li-tag
$sql = "copy your sql";
$result_set = $result = mysql_query($sql);
echo "<body>";
$type = false;
$color = false;
$close_ul = false;
while ($row = mysql_fetch_assoc($result)) {
if ($type != $row['type']) {
if ($close_ul) {
echo "</ul>";
$close_ul = false;
}
$type = $row['type'];
echo "<h2>$type</h2>";
$color = false;
}
if ($color != $row['color']) {
if ($close_ul) {
echo "</ul>";
$close_ul = false;
}
$color = $row['color'];
echo "<h3>$color</h3>";
echo "<ul>";
$close_ul = true;
}
echo "<li>",$row['shortname'],"</li>";
}
if ($close_ul) {
echo "</ul>";
$close_ul = false;
}
echo "</body>";
contact your html properly. Here is the solution.
while ( $row_myfile = mysql_fetch_assoc($myfile) ){
if ( $type != $row_myfile[ 'type' ] )
{
$type = $row_myfile[ 'type' ];
echo "<h2>$type</h2>";
}
if ( $color != $row_myfile[ 'color' ] )
{
$color = $row_myfile[ 'color' ];
echo "<h3>$color</h3>";
}
echo "<ul>";
echo "<li>";
echo "<a href='itemDetail.php?item_id='$row_myfile['myfile_id']'>ShortName ".$row_myfile['ShortName']." Name " .$row_myfile['name']." </a>";
echo "</li>";
echo "</ul>";
}