I would like to display all existing categories and all posts.
I wrote a foreach loop, but I don't know where is the place of the closing ul tag.
My code:
<?php
$current_cat = 0;
$curr_nb_item = 0;
foreach($blog_widget as $row)
{
if($current_cat != $row->category_id)
{
$current_cat = $row->category_id;
echo "<ul> " . $row->category_id . " ";
echo "<li>";
echo $row->title;
echo "</li>";
++$curr_nb_item;
}
else {
echo "<li>";
echo $row->title;
echo "</li>";
++$curr_nb_item;
}
}
?>
Now I got this result in html:
<ul>1
<li>
cat 1 post n
</li>
<li>
cat 1 post h
</li>
<ul>2
<li>
cat 2 post x
</li>
<li>
cat 2 post y
</li>
I tried a lot of variation without result. I hope somebody could help for me. Many thanks.
This may not be the cleanest, but it should work. Modify the top of your if statement to look like this:
if($current_cat != $row->category_id)
{
if ($row != $blog_widget[0])
{
echo "</ul>";
}
$current_cat = $row->category_id;
echo "<ul> " . $row->category_id . " ";
// etc...
And then after the closing brace for your foreach loop:
echo "</ul>";
Related
I am display data from the database. Currently, I have 6 records in my database and I am getting my output like
<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
<li>Records5</li>
<li>Records6</li>
</ul>
Now what I am doing is, I have to close the </ul> tag after 4th li tag and then start new ul after 4th li.
My expected output is
<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
</ul>
<ul>
<li>Records5</li>
<li>Records6</li>
</ul>
is it possible?
I am using below code
<?php
if ($tyler_query->have_posts()) {
$index = 0;
$check=0;
$first4=0;
while ( $tyler_query->have_posts() ) {
$tyler_query->the_post();
if ($index < 4) {
if ($first4==0){?>
<ul>
<?php $first4=1;}?>
<li>
<!--output here-->
</li>
<?php if ($first4==4){?>
</ul>
<?php }?>
<?php }
else {
if ($check==0){?>
<ul>
<?php $check=1;}?>
<li>
<!--output here-->
</li>
<?php } $index++;}?>
</ul>
<?php }?>
You can just insert a closing tag followed by an opening tag, whenever it meets your condition. In the following after every third item:
<?php
$items = [
'Syd',
'Roger',
'Nick',
'David',
'Richard'
];
$i = 0;
echo '<ul>';
foreach($items as $item) {
if($i++%3 == 0)
echo '</ul><ul>';
echo '<li>' . $item . '</li>';
}
echo '</ul>';
Output:
<ul><li>Syd</li><li>Roger</li><li>Nick</li></ul><ul><li>David</li><li>Richard</li></ul>
It's quick example. Hope help you.
if ($tyler_query->have_posts()) {
$index = 0;
$check = 6;
?>
<ul>
<?php while ($tyler_query->have_posts()) {
$tyler_query->the_post(); ?>
<li><?php echo 'some_value' ?></li>
<?php if ($index % $check === 0 ) { ?>
</ul><ul>
<?php }
$index++;
} ?>
</ul>
<?php } ?>
your code would work if you echo the HTML tag/output you want to see in the browser.
<?php
if ($tyler_query->have_posts()) {
$index = 0;
$check = 0;
$first4 = 0;
while ($tyler_query->have_posts()) {
$tyler_query->the_post();
$output = "whatever the output object is";
if ($index < 4) {
if ($first4 == 0) {
echo '<ul>';
$first4 = 1; // increment so that the this if block wont trigger again
}
echo '<li>' . $output . '</li>';
// increment so that the next if block trigger once
if ($first4 == 4) {
echo '</ul>';
}
$first4++;
}
if ($index >= 4){
if ($check == 0) {
echo '<ul>';
$check = 1;
}
// assuming you want to have the rest of the data in this block.
// data 5 and above
else {
echo '<li>' . $output . '</li>';
}
}
$index++;
}
echo '</ul>';
}
?>
I use osclass and I have this code for display regions and their cities. All working good but I need display regions and cities only with items, how can i do it? Please help me i'm beginner.
<?php
$aRegions = Region::newInstance()->getByCountry('AT');
if(count($aRegions) > 0 ) {
?>
<ul>
<?php
foreach($aRegions as $region) {
//print_r ($region);
echo "<li>";
//$region['pk_i_id'].
?>
<div class="accordionButton">
<a href="javascript:void()">
<?php echo $region['s_name']."\n"; ?>
</a>
<?php // echo "</em>(". $region['items'].")</em>";?>
</div>
<?php
$aCities = City::newInstance()-> getByRegion($region['pk_i_id']);
if(count($aCities) > 0 ) {
echo "<div class=\"accordionContent\">";
echo "<ul>";
foreach($aCities as $city) {
// print_r ($city);
//$city["pk_i_id"].'
echo "<li>";
echo "<a href='". osc_search_url( array( 'sRegion'=>$region["s_name"], 'sCity' => $city['s_name'] ) ) ."'> ";
echo $city["s_name"]."\n";
echo "</em>(". $city['items'].")</em>";
echo "</a>";
echo "</li>";
}
}
echo "</ul>";
echo "</li>";
}
?>
I am trying to build a php loop which generates a html navigation with years like this:
<ul>
<li>
1900
<ul>
<li>1901</li>
<li>1902</li>
</ul>
</li>
<li>
2000
<ul>
<li>2001</li>
<li>2002</li>
</ul>
</li>
</ul>
Php I am trying:
foreach (range(0, 1000, 100) as $number) { ?>
<ul>
// Here I should be checking
// if it is year 1942 or 2000 or 1080
// and place ti correctly
<?php echo $number; ?>
</ul>
<?php }
The above works, gives me a new ul every 100.
But I don't understand how I should set my php to open a new <ul> and <li> if we have "sub years" inside the century itself, also I should be checking what year is it that I am outputting in order to have it in its right <ul> or <li>
echo "<ul>";
for ($century=1900; $century <= 2000 ; $century =$century + 100) {
echo "<li>". $century;
echo "<ul>";
for ($year = $century; $year < $century + 100; $year++) {
echo "<li>". $year . "</li>";
}
echo "</ul>";
echo "</li>";
}
echo "</ul>";
I hope this is what you need:
<?php
$start_year=1900;
$end_year=2100;
for($year=$start_year;$year<=$end_year;$year++){
if($year%10==0) //Replace 10 (for decades) with 100 (for centuries)
{
if($year!=$start_year){
echo '</ul></li></ul>';
}
echo '<ul><li>'.$year.'<ul>';
}
else{
echo '<li>'.$year.'</li>';
}
}
?>
I have an array
$foo = array(1,2,3,4,5,6,7,8,9);
when I use foreach loop for this
foreach($foo as $val):
print '<li>'.$val.'</li>';
endforeach
Out put is ,
<li> 1 </li>
<li> 2 </li>
<li> 3 </li>
<li> 4 </li>
<li> 5 </li>
But I want out put something like that
<li> 1, 2 </li>
<li> 3, 4 </li>
<li> 5, 6 </li>
<li> 7, 8 </li>
Is it possible?
$foo = array(1,2,3,4,5,6,7,8,9);
foreach (array_chunk($foo, 2) as $chunk) {
echo "<li>" . implode(', ', $chunk) . "</li>\n";
}
For php you can do:
for($i=0;$i<count($foo);$i+=2) {
echo "<li>{$foo[$i]}, {$foo[$i+1]}</li>";
}
This might be what you require:
foreach($foo as $key=>$val) {
if ($val&1) {
echo '<li>' . $val;
if($key == (count($foo)-1)){
echo '</li>';
}
} else {
echo ',' . $val . '</li>';
}
}
Please try this:
$foo = array(1,2,3,4,5,6,7,8,9);
$i=1;
$firstElement = "";
foreach($foo as $val):
if($i%2==0)
{
print '<li>'.$firstElement.','.$val.'</li>';
}
else
{
$firstElement = $val;
}
$i++;
endforeach
I have the array array ( [0] => array(1,2,3,4,5) [1] => array(6,7,8,9,10)) and I would like to display it like this:
<ul>
<li>
<a href=""/>FIRST ELEMENT OF THE array ==> 1</a>
<a href=""/>2ND ELEMENT OF THE TAB ==> 2</a>
<a href=""/>3THIRD ELEMENT==> 3</a>
<a href=""/>FORTH ELEMENT OF THE TAB ==> 4</a>
<a href=""/>FIFTH ELEMENT==> 5</a>
</li>
<li>
<a href=""/>6th ELEMENT==> 6</a>
<a href=""/>7th ELEMENT OF THE TAB ==> 7</a>
<a href=""/>8th ELEMENT==> 8</a>
<a href=""/>9th ELEMENT OF THE TAB ==> 9</a>
<a href=""/>10th ELEMENT OF THE TAB ==> 9</a>
</li>
</ul>
How can I achieve this in PHP? I am thinking of creating a sub array with array_slice.
Updated to take into account your actual array structure
Your solution is a simple nested foreach.
$tab = array(array(1,2,3,4,5), array(6,7,8,9,10));
echo '<ul>';
foreach ($tab as $chunks) {
echo '<li>';
foreach($chunks as $chunk) {
echo '' . $chunk . '';
}
echo '</li>';
}
echo '</ul>';
try
echo "<ul>";
$i=0;
$theCount = count($tab);
while($i<$theCount){
echo "<li>";
echo " <a href=""/>FIRST ELEMENT OF THE TAB ==> {$tab[$i]}</a>";
$i++;
echo " <a href=""/>FIRST ELEMENT OF THE TAB ==> {$tab[$i]}</a>";
echo "</li>";
$i++;
}
echo "</ul>";
Here is another way to do this (demo here):
<?php
$tab = array(1,2,3,4,5,6,7,8,9,10);
//how many <a> elements per <li>
$aElements = 2;
$totalElems = count($tab);
//open the list
echo "<ul><li>";
for($i=0;$i<$totalElems;$i++){
if($i != 0 && ($i%$aElements) == 0){ //check if I'm in the nTh element.
echo "</li><li>"; //if so, close curr <li> and open another <li> element
}
//print <a> elem inside the <li>
echo "<a href =''>".$tab[$i]."</a>";
}
//close the list
echo "</li></ul>";
?>
Tip explanation: $i%n (mod) equals 0 when $i is the nTh element (remainder of division is 0)
EDITED: made a general solution
<?php
for($i = 0 ; $i < count($tab) ; $i += 2) {
echo "<a href>" . $tab[$i] . "</a>";
echo "<a href>" . $tab[$i+1] . "</a>";
}
?>
Like that.
try this:
$sections = array_chunk(array(1,2,3,4,5,6,7,8,9,10), 2);
echo '<ul>';
foreach($sections as $value)
{
echo '<li>';
echo '<a href=""/>'.$value[0].' ELEMENT OF THE TAB ==> '.$value[0].'</a>';
echo '<a href=""/>'.$value[1].' ELEMENT OF THE TAB ==> '.$value[1].'</a>';
echo '</li>';
}
echo '</ul>';