<UL> basesd on common id fetch from databse - php

I'm trying to iterate through my array and group li's with their relative ul, based on a common id.
expected result is as follows
<ul>
<li>b</li>
<li>c</li>
<li>a</li>
<li>g</li>
<li>e</li>
</ul>
<ul>
<li>d</li>
<li>f</li>
<li>i</li>
</ul>
<ul>
<li>d</li>
<li>f</li>
<li>i</li>
</ul>
I have tried following code
<?php
$IMPLODED_trid = 1,2,3,4,5,6 ;
$result=mysqli_query('SELECT * FROM tablegroup where id IN ($IMPLODED_trid)');
while($row=mysqli_fetch_array($result))
{
$name=$row['name'];
?>
<ul>
<li><?php $name ;?></li>
</ul>
<?php
}
?>
but above code gives following result
<ul>
<li>c</li>
</ul>
<ul>
<li>a</li>
</ul>
<ul>
<li>d</li>
</ul>
<ul>
<li>i</li>
</ul>

You'll want to use two loops.
One outer for generating the <ul> elements, another for generating the <li> elements within the <ul>
Such code might look like this
<?php
$result = []; //grouped by ul ID
for ($i = 1; $i < count($ids); $i++) {
echo '<ul>';
while ($row = $result[$i])
echo '<li>' . $result[$i]['name'] . '</li>';
echo '</ul>';
}
?>
It's valid PHP but it doesn't work ofcourse.
This is the structure you're looking for tho since you'll want an outer loop to go through the amount of <ul> elements you need and within that loop you'll want to loop through the list-items themselves.
The outer loop wouldn't be needed if you only had one <ul> ofcourse.

Your code is wrong. Put ul tag out of the body of while loop. Try this:
<ul>
<?php
$IMPLODED_trid = 1,2,3,4,5,6 ;
$result=mysqli_query('SELECT * FROM tablegroup where id IN ($IMPLODED_trid)');
while($row=mysqli_fetch_array($result))
{
$name=$row['name'];
?>
<li><?php $name ;?></li>
<?php
}
?>
</ul>

Related

foreach every 3 put inside a new ul

Actually I have this foreach loop:
<ul>
<?php foreach ( $tasks as $task ) { ?>
<li>
...
</li>
<?php } ?>
</ul>
It returns:
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
...
</ul>
Now I need to change the loop to put every 3 <li> into a new <ul>, so I can have:
<div>
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
...
</div>
How I have to modify the foreach loop to achieve that result?
This should work for you:
Just array_chunk() your array into chunks of 3 and then implode() each subArray, e.g.
<div>
<?php
foreach (array_chunk($tasks, 3) as $task) {
echo "<ul><li>" . implode("</li><li>", $task) . "</li></ul>";
}
?>
</div>
try this
foreach (array_chunk($arrays, 3, true) as $array)
{
echo '<ul>';
foreach($array as $value)
{
echo "<li>".$value."</li>";
}
echo '</ul>';
}
Please try this..
<ul>
<?php foreach ( $tasks as $k=>$task ) { ?>
<?php if($k % 3 == 1) { ?>
<ul>
<?php } ?>
<li></li>
<?php if($k % 3 == 0) { ?>
</ul>
<?php } ?>
<?php } ?>
</ul>
I like #Rizer123's answer for conciceness, but if you wish to keep iterating over the individual items, rather than chunking (for example; if you need to perform some manipulation on the individual items) I'd suggest something like the following:
<ul>
<?php foreach ( array_values($tasks) as $k => $task ) {
if($k % 3 == 0) { ?>
<ul>
<?php } ?>
<li>
...
</li>
if($k % 3 == 2) { ?>
</ul>
<?php } ?>
<?php } ?>
</ul>

Show a hierarchy in PHP

I have a database which stores a hierarchy of foods.
Category(id_cat,name_cat);
his_low_cat(id_cat,id_low_cat);
A category can have 0..n low category. If it had no lower category I do a id_cat,-1 field in his_low_cat.
I do not know if it's possible but I would like to show it in a kind of "pulldown menu"
(if you have any other idea on how to show a full hierarchy please suggest it)
Like this :
echo " <div id=\"menu\">
<ul class=\"niveau1\">
<li class=\"sousmenu\">Food
<ul class=\"niveau2\">
<li class=\"sousmenu\">Sous menu 1.1
<ul class=\"niveau3\">
<li>Sous sous menu 1.1.1</li>
</ul>
</li>
<li>Sous menu 1.2</li>
</ul>
</li>
</ul>
</div>";
My first cat is "food" and then it derives into 4 lowers categories, which derive themselves in more.
The problem is that it must be dynamic and load field from my database.
The goal would be to be able to catch the clicked value and use it in another .php
How would I do this?
Recursion is definitely the way to go with this problem, I've coded up this solution:
<?php
function nestElements($elements, $depth=0)
{
foreach($elements as $elementName=>$element)
{
echo str_repeat("\t", $depth).'<ul class="niveau'.($depth+1).'">'."\n";
if(is_array($element))
{
echo str_repeat("\t", $depth+1)."<li class=\"sousmenu\">${elementName}\n";
nestElements($element, $depth+2);
echo str_repeat("\t", $depth+1)."</li>\n";
}
else
{
echo str_repeat("\t", $depth+1)."<li class=\"sousmenu\">${elementName}</li>\n";
}
echo str_repeat("\t", $depth)."</ul>\n";
}
}
nestElements(array("Food"=>array("Meat"=>array("Poultry"=>array("Chicken"=>"Meat/Poultry/Chicken"), "Beef"=>array("Hamburgers"=>"Meat/Beef/Hamburgers", "Steak"=>"Meat/Beef/Steak")), "Dairy"=>array("Cow"=>"Dairy/Cow", "Sheep"=>"Dairy/Sheep")), "name"=>"url"));
?>
Testing with this:
<?php
nestElements(array("Food"=>array("Meat"=>array("Poultry"=>array("Chicken"=>"Meat/Poultry/Chicken"), "Beef"=>array("Hamburgers"=>"Meat/Beef/Hamburgers", "Steak"=>"Meat/Beef/Steak")), "Dairy"=>array("Cow"=>"Dairy/Cow", "Sheep"=>"Dairy/Sheep")), "name"=>"url"));
?>
Results in:
<ul class="niveau1">
<li class="sousmenu">Food</li>
<ul class="niveau2">
<li class="sousmenu">Meat</li>
<ul class="niveau3">
<li class="sousmenu">Poultry</li>
<ul class="niveau4">
<li class="sousmenu">Chicken</li>
</ul>
</ul>
<ul class="niveau3">
<li class="sousmenu">Beef</li>
<ul class="niveau4">
<li class="sousmenu">Hamburgers</li>
</ul>
<ul class="niveau4">
<li class="sousmenu">Steak</li>
</ul>
</ul>
</ul>
<ul class="niveau2">
<li class="sousmenu">Dairy</li>
<ul class="niveau3">
<li class="sousmenu">Cow</li>
</ul>
<ul class="niveau3">
<li class="sousmenu">Sheep</li>
</ul>
</ul>
</ul>
<ul class="niveau1">
<li class="sousmenu">name</li>
</ul>
To parse it you'd have to make a mod_rewrite which redirects to index.php?r=TheURL and from their, explode the r parameter using "/" as the delimeter, then you have a list of menus and submenus that the clicked link was from. By adding another parameter the url coul be automatically generated.
Edit: Fixed problem with original code output seen below
<li class="sousmenu">Sheep</li>
<li class="sousmenu">Sheep</li>
To generate the array:
<?php
function genArray(&$targetArray, $parentID=null){
$res=(is_null($parentID))?mysql_query("SELECT * FROM categorie WHERE id_cat NOT IN (SELECT id_low_cat FROM hislowcat) ORDER BY id_cat DESC;"):mysql_query("SELECT *, (SELECT name_cat FROM categorie WHERE id_cat= '".$parentID ."') AS name_cat FROM hislowcat WHERE id_cat= '" .$parentID ."'");
if(!is_null($parentID) && !mysql_num_rows($res))
{
$res3=mysql_query("SELECT name_cat FROM categorie WHERE id_cat='${parentID}';");
$row3=mysql_fetch_array($res3);
$targetArray[$row3['name_cat']]=$row3['name_cat'];
return;
}
while(($row=mysql_fetch_array($res)))
{
//echo $row->name_cat;
if(is_null($parentID))
{
if(!isset($targetArray[$row['name_cat']]))
{
$targetArray[$row['name_cat']]=array();
}
genArray($targetArray[$row['name_cat']], $row['id_cat']);
}
else
{
genArray($targetArray[$row['name_cat']], $row['id_low_cat']);
}
}
}
$array=array();
genArray($array);
print_r($array);
?>
Notice how $targetArray is set up as a reference, this way we can treat it one-dimensionally.

margin list after 5 li items with php

I hope topic title will be okey, doesnt really find good idea for naming topic. What i want is to produce that my UL list will get some class or margin after a few LI in list.
Something like that
<ul>
<li></li>
<li></li>
<li></li>
</ul>
<ul class="1">
<li></li>
<li></li>
<li></li>
</ul>
<ul class="2">
<li></li>
<li></li>
<li></li>
</ul>
Now what i have in code is something like that, but offcourse doesnt work...:
$datas = array('a','b','c','d','e','f','g','h');
$countData = count($datas);
for($i = 0; $i < $countData; $i++){
echo '<ul>';
$j = 0;
foreach($datas as $data){
$j++;
if($j < 3){
echo '<li>'.$data.'</li>';
}
}
echo '</ul>';
}
Guess you can try this:
$datas = array('a','b','c','d','e','f','g','h');
echo "<ul>\n";
$class = 0;
foreach ($datas as $i => $data) {
echo "<li>$data</li>\n";
if (($i+1) % 3 == 0)
echo "</ul>\n<ul class=".++$class.">\n";
}
echo "</ul>";
It produces:
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
<ul class=1>
<li>d</li>
<li>e</li>
<li>f</li>
</ul>
<ul class=2>
<li>g</li>
<li>h</li>
</ul>

How to put dynamically-generated n-amount of <div>s in <li>

Hello :) I realy need your help here. I dynamically generate list of items but instead of putting each item in separate <li> I want to get something like this:
<ul>
<li>
<div>$pt</div>
<div>$pt</div>
<div>$pt</div>
</li>
<li>
<div>$pt</div>
<div>$pt</div>
<div>$pt</div>
</li>
</ul>
Here is code I have:
<ul class="some-ul-class">
<?php $itemCount = 3; ?>
<?php $i=0; foreach ($p->getItems() as $pt): ?>
<?php if ($i++%$itemCount==0): ?>
<li class="item">
<?php endif; ?>
<div>$pt</div>
</li>
<?php endforeach; ?>
</ul>
But as the result I get structure like this:
<ul>
<li>
<div>$pt</div>
</li>
<div>$pt</div>
<div>$pt</div>
<li>
<div>$pt</div>
</li>
<div>$pt</div>
<div>$pt</div>
</ul>
Thank you for help
<ul class="some-ul-class">
<?php $itemCount = 3; ?>
<?php $i=0; foreach ($p->getItems() as $pt): ?>
<?php if ($i%$itemCount==0): ?>
<li class="item">
<?php endif; ?>
<div>$pt</div>
<?php if ($i%$itemCount==2): ?>
</li>
<?php endif; $i++; ?>
<?php endforeach; ?>
</ul>
You can try this.
<ul class="some-ul-class">
<?php $itemCount = 3;
$i=0;
foreach ($p->getItems() as $pt):
if ($i%$itemCount==0):
echo '<li class="item">';
endif;
echo "<div>$pt</div>";
if ($i%$itemCount==2):
echo '</li>';
endif; $i++;
endforeach; ?>
</ul>
Try something like this:
<ul class="some-ul-class">
<?php $itemCount = 4; ?>
<li>
<?php $i = 1; foreach ($p->getItems() as $pt): ?>
<?php if ( $i % $itemCount == 0): ?>
</li><li>
<?php endif; ?>
<?php $i++; ?>
<div><?php echo $pt; ?></div>
<?php endforeach; ?>
</li>
</ul>
This generates:
<ul class="some-ul-class">
<li>
<div>1</div>
<div>2</div>
<div>3</div>
</li><li>
<div>4</div>
<div>5</div>
<div>6</div>
</li>
</ul>
Demo
Your code will not achieve nested DIVS in LI because you need a multidimensional array to nest the items within the container. The solution is to break the initial DB result set in to chunks with array chunk.
This just splits your array (1,2,3,4,5,6) to ([0] => array(1,2,3), [2] => array(4,5,6)
Run through the loop below you will get two LI with 3 nested DIV. The code is not tested but should be something like operational.
<?php
$items = array(1,2,3,4,5,6,8,9,10,11,12,13,14,15);
// Your initial item array
$rows = 3;
// Number of rows in each li
$items = array_chunk($items, $rows);
// Final nested array in blocks of 3
if ($items) {
echo "<ul class='some-ul-class'>\n";
foreach ( $items as $item ) {
echo "<li class='items'>\n";
foreach ($item as $divs) {
echo "<div>{$divs}</div>\n";
}
echo "</li>\n";
}
echo "</ul>\n";
}
?>

How do I add UL LI every two loops on while

I want to add my ul & li every two loop.. Example
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<ul>
<li> <?php the_title() ?> - <?php the_content() ?></li>
</ul>
<?php endwhile; ?>
Let's say I have 4 posts and I want the result should be like this
<ul>
<li>Title 1 - content 1</li>
<li>Title 2 - content 2</li>
</ul>
<ul>
<li>Title 3 - content 3</li>
<li>Title 4 - content 4</li>
</ul>
add a counter variable (start =0) that increments at the end of each pass through the loop. Then at the beginning of each pass, test if($counter%2==0){ echo "</ul><ul>";}and put the first <ul> and last </ul> outside of the loop
I would do something like this:
for($i = 0; $i < $numberOfUls; $i++)
{
$result = '<ul>';
for($j = 0; $j < $numberOfLis; $j++)
{
$result .= '<li>Title content</li>'; // Perhaps an array with the whole list $listContent[$i][$j];
}
$result .= '</ul>';
}
echo $result;

Categories