create new ul after every 13 li elements - php

First of all, I don't know how to count in php, maybe someone could recommend me a good source to read;
Second, I'm not asking to solve this for me, but I just want a hint or simpler explanation that would make sense;
Here is my function:
<ul>
<?php foreach ($categories as $category) { ?>
<li>
<p><?php echo $category['name']; ?></p>
<?php if ($category['children']) { ?>
<div>
<?php for ($i = 0; $i < count($category['children']);) { ?>
<?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['children'][$i])) { ?>
<ul>
<li class="none"><h1><?php echo $category['children'][$i]['name']; ?></h1></li>
<?php if ($category['children'][$i]['children_level2']) { ?>
<?php for ($wi = 0; $wi < count($category['children'][$i]['children_level2']); $wi++) { ?>
<li>
<?php echo $category['children'][$i]['children_level2'][$wi]['name']; ?>
</li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
</li>
<?php } ?>
</ul>
I want this part, every 13 li elements to create a new .ul. ./ul. tags
<?php if (isset($category['children'][$i])) { ?>
<ul>
<li class="none"><h1><?php echo $category['children'][$i]['name']; ?></h1></li>
<?php if ($category['children'][$i]['children_level2']) { ?>
<?php for ($wi = 0; $wi < count($category['children'][$i]['children_level2']); $wi++) { ?>
<li>
<?php echo $category['children'][$i]['children_level2'][$wi]['name']; ?>
</li>
<?php } ?>
<?php } ?>
</ul>
<ul><li>maximum of 13 elements</li></ul>
after 13 <li></li> elements create new <ul></ul> tags and put the 14 <li></li> element into the new <ul></ul> tag
I hope I explained what I want to do, for now I'll be waiting for your answers,
p.s. this is more for my learning skills then actual work, so thanks in advice

It's simple. Use condition
if ($wi && $wi % 13 == 0) {
print '</ul><ul>';
}

In order to count 13 elements, you can use the modulo operator %. The idea is that ($iw % 13) is equal to 0 if the content of $iw is a multiple of 13. (see more about the modulo operation)
With an if statement you can insert </ul><ul> when you need in your for loop.

There's two possibilities I can think of:
you could have a separate counter wich resets to 1 and outputs a <ul> every time it reaches 13
or you could check each time if $wi id divisible by 13:
<?php
for ($wi = 0; $wi < count($category['children'][$i]['children_level2']); $wi++) {
if ($wi && $wi % 13 == 0) {
// output <ul>
}
}
?>
If you're unsure what % means, here's the appropriate PHP manual page

Related

How to make 2 columns in loop data

I do foreach loop to output my data so my data will be like this
Dog
Cat
Mouse
Bird
Egg
Eagle
Fish
using this code
<div class="col-md-6">
<?php foreach ($treefields as $key=>$item): ?>
<?php if($key==0);?>
<?php if($item['title'] == $estate_data_option_1057): ?>
<div class="additional-amenities">
<span class="available"><i class="fa fa-check-square"></i></span><strong> <?php echo $options_name_1057; ?>:</strong><?php echo $estate_data_option_1057;?></span>
</div>
<?php if (count($item['childs_4']) > 0) foreach ($item['childs_4'] as $child): ?>
<div class="additional-amenities">
<span class="available"><i class="fa fa-check-square"></i></span><strong><?php _che($child['description']); ?></strong><span><?php echo $child['title']; ?></span>
</div>
<?php endforeach; ?>
<?php else: ?>
<?php ?>
<?php endif;?>
<?php endforeach; ?> </div></div>
How can i make it 2 columns like this?
- Dog - Egg
- Cat - Eagle
- Mouse - Fish
- Bird
First count the number of items in your array, then figure out what half of that would be and ceil it, so it's not a float. Then use that value in a conditional to echo your markup to break it into multiple columns.
For example, here is the technique boiled down to put the array into two different ul elements.
<ul>
<?php
$animals = array('Dog','Cat','Mouse','Bird','Egg','Eagle','Fish');
$numAnimals = count($animals);
$maxAnimalsPerColumn = ceil($numAnimals/2);
for($i=0; $i < $numAnimals; $i++) {
echo "<li>".$animals[$i]."</li>";
if ($i+1 == $maxAnimalsPerColumn ) {
echo "</ul><ul>";
}
}
?>
</ul>
$numAnimals would be 7
$maxAnimalsPerColumn would be 4 ($numAnimals divided by 2, ceil'd)
When the loop value (plus 1 since it starts at zero) matches $maxAnimalsPerColumn, it will echo a closing ul tag and a new one to start the second.
The resulting markup would be roughly:
<ul>
<li>Dog</li>
<li>Cat</li>
<li>Mouse</li>
<li>Bird</li>
</ul>
<ul>
<li>Egg</li>
<li>Eagle</li>
<li>Fish</li>
</ul>
Use a counter. Example:
$nbrOfColumns = 2;
$rowsPerColumn = ceil(count($treefields)/$nbrOfColumns);
$counter = 1;
<div class="col-md-6">
foreach ($treefields as $key=>$item){
if($counter === $rowsPerColumn){
echo "</div><div class="col-md-6">";
$counter = 0;
}
(generate your HTML here)
$counter++;
}
</div>

For loop, runs once?

I'm using OpenCart and I'm trying to achieve rendering out the meta_description (used to identify the comic publisher) and use it to make a drop-down list with sub-categories, or to give the illusion of it. Here is my code now, it's an adopted version of the current OpenCart code. ['class'] is how I grab the child categories meta_description.
Basically, the second for statement doesn't work - it only does the first one. I would appreciated any kind of support on this.
<div class="menu">
<div id="top"></div>
<span>
<ul id="nav">
<?php foreach ($categories as $category) { ?>
<li><?php echo $category['name']; ?>
<?php if ($category['children']) { ?>
<div class="subs">
<div>
<?php for ($i = 0; $i < count($category['children']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
<h3>DC Comics</h3>
<?php for (; $i < $j; $i++) { ?>
<?php if($category['children'][$i]['class'] == "DC Comics"){ ?>
<li>
<ul>
<?php if (isset($category['children'][$i])) { ?>
<li><?php echo $category['children'][$i]['name']; ?></li>
<?php } ?>
</ul>
</li>
<?php } ?>
<?php } ?>
<h3>Marvel</h3>
<?php for (; $i < $j; $i++) { ?>
<?php if($category['children'][$i]['class'] == "Marvel"){ ?>
<li>
<ul>
<?php if (isset($category['children'][$i])) { ?>
<li><?php echo $category['children'][$i]['name']; ?></li>
<?php } ?>
</ul>
</li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
</div>
</div>
<?php } ?>
</li>
<?php } ?>
</ul>
</span>
</div>
Use different variables in loops, in you code $i is used in main loop and incremented in inner loops you can use foreach loop like ,
<?php foreach ($categories as $category) { ?>
<li><?php echo $category['name']; ?>
<?php if (is_array($category['children']) and isset($category['children'])) { ?>
<div class="subs">
<div>
<ul>
<?php
$li1='<li><h3>DC Comics</h3><ul>';
$li2='<li><h3>Marvel</h3><ul>';
foreach($category['children'] as $child)
{
if($child['class'] == "DC Comics")
{
$li1.='<li>'.$child['name'].'</li>';
}
if($child['class'] == "Marvel")
{
$li2.='<li>'.$child['name'].'</li>';
}
}
$li1.='</ul></li>';
$li2.='</ul></li>';
echo $li1;
echo $li2;
?>
</ul>
</div>
</div>
<?php } ?>
</li>
<?php } ?>

How to pass a variable (a database row) into a while loop function

I am using a simple PHP script to display links in my footer. These links and anchor text are in my database. I have no problem displaying the links and anchor text with the code I am using. Here is an example of how my code looks when it's functioning.
Using PHP/MySQL how can I pull a value from my database down into my website and inside the while loop comparison?
Small excerpt of my code:
<?php
// Change the limit in db_connect.
$link_counter = 0;
while ($link_counter < $counter_query_result[1]['counter_value']; ){
echo $links_array[$link_counter];
$link_counter++;
}
?>
</ul>
</div>
<div id="footer_links_box">
<ul>
<?php
while ($link_counter < $counter_query_result[2]['counter_value']; ){
echo $links_array[$link_counter];
$link_counter++;
}
?>
</ul>
</div>
<div id="footer_links_box">
<ul>
<?php
while ($link_counter < $counter_query_result[3]['counter_value']; ){
echo $links_array[$link_counter];
$link_counter++;
}
?>
</ul>
</div>
<div id="footer_links_box">
<ul>
<?php
while ($link_counter < $counter_query_result[4]['counter_value']; ){
echo $links_array[$link_counter];
$link_counter++;
}
?>
</ul>
</div>
<div id="footer_links_box">
<ul>
<?php
while ($link_counter < $counter_query_result[5]['counter_value']; ){
echo $links_array[$link_counter];
$link_counter++;
}
?>
echo $links_array[$link_counter];
$link_counter++;
}
?>
</ul>
</div>
<div id="footer_links_box">
<ul>
<?php
while ($link_counter < echo $counter_query_result[1]['counter_value_2']; ){
echo $links_array[$link_counter];
$link_counter++;
}
?>
You don't need to echo it. It is already a variable to use for comparison:
while ($link_counter < $counter_query_result[1]['counter_value_2'] ){
echo $links_array[$link_counter];
$link_counter++;
}
This assumes that the value of $counter_query_result[1]['counter_value_2'] is in fact an int
Let's assume we have 20 links and we want 5 blocks consisting of 4 links in each.
$links_array = array_chunk($links_array, 5);
foreach ( $links_array as $block_num => $block_links ) { ?>
<div id="footer_links_box">
<ul>
<?
foreach ( $block_links as $link ) {
echo $link;
}
?>
</ul>
</div>
<? } ?>
Answering my own question. Here is the solution. You can not put the database query/result in the while loop: (The first 6 lines of code right below here is the old code that does not work)
<?php
while ($link_counter < $counter_query_result[3]['counter_value']; ){
echo $links_array[$link_counter];
$link_counter++;
}
?>
Instead you will need to assign the statement to a variable and call that variable from within the loop. The following is a completed bit of code that did in fact work 110%. I am now using this in my completed website.
this code below is the finished code and works.
<?php
// Start Database Variable For Footer Link Counter //
$var1 = $counter_result[1]['counter_row_value'];
$var2 = $counter_result[2]['counter_row_value'];
$var3 = $counter_result[3]['counter_row_value'];
$var4 = $counter_result[4]['counter_row_value'];
$var5 = $counter_result[5]['counter_row_value'];
// End Database Varibale For Footer Link Counter //
$link_counter = 0;
while ($link_counter < $var1 ){
echo $links_array[$link_counter];
$link_counter++;
}
?>
</ul>
</div>
<div id="footer_links_box">
<ul>
<?php
while ($link_counter < $var2 ){
echo $links_array[$link_counter];
$link_counter++;
}
?>
</ul>
</div>
<div id="footer_links_box">
<ul>
<?php
while ($link_counter < $var3 ){
echo $links_array[$link_counter];
$link_counter++;
}
?>
</ul>
</div>
<div id="footer_links_box">
<ul>
<?php
while ($link_counter < $var4 ){
echo $links_array[$link_counter];
$link_counter++;
}
?>
</ul>
</div>
<div id="footer_links_box">
<ul>
<?php
while ($link_counter < $var5 ){
echo $links_array[$link_counter];
$link_counter++;
}
?>

OpenCart: Add categories to main menu?

I've just installed OpenCart for the first time, deleted all the dummy products and categories it comes with and added my own. However, now nothing appears in my menu - no product categories. I can't seem to fine where to add items to menus.
Can anyone point me in the right direction? Thanks
UPDATE:
THis is the php in my header tpl file:
?php if ($categories) { ?>
<div id="menu">
<ul>
<?php foreach ($categories as $category) { ?>
<li><?php echo $category['name']; ?>
<?php if ($category['children']) { ?>
<div>
<?php for ($i = 0; $i < count($category['children']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['children'][$i])) { ?>
<li><?php echo $category['children'][$i]['name']; ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
</div>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>
I have not acutally changed any of it from the original
Create a new parent category and sub categories in Catalog->Category->insert then add the new product Catalog->product->insert. Product should be appear in frond end. for more details see this video tutorials : http://www.opencart.com/index.php?route=documentation/screencast

Add automatic classes to each <li>

I have a list of < li >'s but without classes, I want to make for EACH < li > a different class.. something like this
< li class="1" >
< li class="2" >
< li class="3" >
Here is the code that I have:
<ul id="tralila">
<?php
foreach($lists as $key=>$region)
{
?>
<li>
<?php $regionLink = "index.php?option=$option&Itemid=$listitemid&task=regions.region&rid=$region->id";
echo ''.$region->title.'';?>
</li>
<?php
$location = $key+1;
} ?>
</ul>
You can try:
<?php
$i = 1;
foreach($lists as $key=>$region)
{
?>
<li class="<?php echo "className$i"; ?>">
<?php $regionLink = "index.php?option=$option&Itemid=$listitemid&task=regions.region&rid=$region->id";
echo ''.$region->title.'';?>
</li>
<?php
$location = $key+1;
$i++;
} ?>
Or, instead, you could use the $key from your foreach instead of creating, and incrementing, a counter variable.
So this is how you would do it using the key from $lists. Note I have added a prefix of li- as classes cannot start with a digit. See: Which characters are valid in CSS class names/selectors?
<ul id="tralila">
<?php
foreach($lists as $key=>$region)
{
?>
<li class="<?php echo 'li-' . htmlentities($key); ?>">
<?php $regionLink = "index.php?option=$option&Itemid=$listitemid&task=regions.region&rid=$region->id";
echo ''.$region->title.'';?>
</li>
<?php
$location = $key+1;
} ?>
</ul>
As all of the classes are unique I would use IDs instead of a class though.
Something like this?:
<ul id="tralila">
<?php
$class_name = 0;
foreach($lists as $key=>$region)
{
?>
<li class="<?=++$class_name;?>">
<?php $regionLink = "index.php?option=$option&Itemid=$listitemid&task=regions.region&rid=$region->id";
echo ''.$region->title.'';?>
</li>
<?php
$location = $key+1;
} ?>
</ul>

Categories