Do something Inside a PHP foreach Loop Only Once - php

I am using a foreach loop to create a brands page for our Magento store. In the loop every 5 items are put into an un-ordered list. They are also split up by letters of the alphabet (A - D, E - H, I - L, M - P, Q - U, V - Z). I need to insert a header before each set of lists. For example:
<h2>A - D</h2>
<ul>
<li>Brand 1</li>
<li>Brand 2</li>
<li>Brand 3</li>
<li>Brand 4</li>
<li>Brand 5</li>
</ul>
This is how I am getting the brands and splitting them up:
<?php foreach($brands as $brand) : ?>
<?php $name = $brand->getName(); ?>
<?php if((substr($name, 0, 1) == 'A') || (substr($name, 0, 1) == 'B') ||
(substr($name, 0, 1) == 'C') || (substr($name, 0, 1) == 'D')) : ?>
<?php if($count % 5 == 0) : ?>
</ul><ul>
<?php endif; ?>
<li><a href="<?php echo $brand->getUrl(); ?>">
<?php
$id = $brand->getId();
$imageUrl = Mage::getModel('catalog/category')->load($id)->getThumbnailUrl();
?>
<img src="<?php echo $imageUrl; ?>" alt="<?php echo $name; ?>">
</a></li>
<?php $count++; ?>
<?php endif; ?>
<?php endforeach; ?>
I thought I could insert the header into the right place by counting how many brands there are for each set of 4 or 5 letters then doing the same as what I have done to put in the 's. But it doesn't work.
Is there any way I can maybe tell the header to insert only on the first run of the loop?

Try this
$ascci_val = ord( strtolower(sub_str($name, 0, 1)) );
if( ($ascci_val >= 97) && ($ascci_val <= 100) ){
// brand name stats with a letter between A-D / a-d
}

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>

Pagination if statement

I'm sitting at record id 1 and hit the Previous link, it goes to record 0, then record -1, then -2, and so on. I'm trying to show just the 'Next' link if I'm on record id 1, else show both links.
<ul class="pager">
<?php if (href="?read=<?=htmlspecialchars($_GET['read'] = 1)) { ?>
<li class="previous">Next</li>
<?php } else { ?>
<li class="previous">Previous</li>
<li class="previous">Next</li>
</ul>
<?php
//get page number or set to 1 if not page is set
$read = isset($_GET['read']) ? (int)$_GET['read'] : 1;
$limit = 5;
?>
<ul class="pager">
<?php if ($read > 1): ?>
<li class="previous">Previous</li>
<?php endif ?>
<?php if ($read < $limit): ?>
<li class="previous">Next</li>
<?php endif ?>
</ul>
ps: for NEXT link there should probably class next not previous but you have previous in your code so I left it at it is
Change this line
<?php if (href="?read=<?=htmlspecialchars($_GET['read'] = 1)) { ?>
To this
<?php if (href="?read=<?=htmlspecialchars($_GET['read'] == 1)) { ?>
$_GET['read'] = 1 statement always sets $_GET['read'] variable to 1.

How to use PHP to display active class on sub nav links?

I have PHP code to display the active class on links in the nav, but I'm having trouble finding a solution to applying the active class properly for the sub nav - that being the pages that are in folders outside of the root directory. Here is the code that I have!
Here is the example code: (it works on the real code)
<?php
$current_page = basename($_SERVER['PHP_SELF']);
?>
This is an example for a stand alone nav link:
<li class="<?php if ($current_page == "index.php"){ echo "active "; }?> item">Home</li>
This is an example for the drop down menu:
<li class="<?php if ($current_page == "index.php"){ echo "active "; }?> item has-dropdown">About Us...
Now the code for the sub nav's so far isn't working for me, but here it is with the real code!
<?php
$cp = basename($_SERVER['PHP_SELF']);
$cf = dirname($_SERVER['PHP_SELF']);
?>
Here is an example for a stand alone nav link:
<li class="<?php if ($cp == "index.php"){ echo "active "; }?> item">Home</li>
Here is an actual drop down menu from my nav.
<li class="<?php if ($cp == "courses.php" || $cp == "oshawa.php" && $cf == "courses" || $cp == "bowmanville.php" && $cf == "courses"){ echo "active "; }?> item has-dropdown">
Courses <!-- COURSES -->
<ul class="dropdown">
<li>Courses</li>
<li><label>Locations</label></li>
<li>Oshawa</li>
<li>Bowmanville</li>
</ul>
</li>
This specifically is where the code breaks, the first part works fine, but the second we add the && $cf == "courses" it breaks.
$cp == "oshawa.php" && $cf == "courses"
I don't think it's a formatting issue, I think it's just the code itself, not sure where to go from here. I'm not very good with PHP, all help is appreciated, thanks =)
Try this, (Assuming your all courses pages exist in courses directory itself.)
<?php
$cp = basename($_SERVER['PHP_SELF']);
$cf = dirname($_SERVER['PHP_SELF']);
?>
<li class="<?php if ($cp == "courses.php" || $cf == "/courses"){ echo "active "; }?> item has-dropdown">
Courses <!-- COURSES -->
<ul class="dropdown">
<li>Courses</li>
<li><label>Locations</label></li>
<li>Oshawa</li>
<li>Bowmanville</li>
</ul>
</li>

Looping through HTML code in PHP/JavaScript

Instead of writing:
<ul class="tabs">
<li>1-50</li>
<li>51-100</li>
<li>101-150</li>
<li>151-200</li>
<li>201-250</li>
<li>251-300</li>
<li>300-350</li>
<li>351-400</li>
<li>401-500</li>
</ul>
until 950-1000 which will be tab 20 - is there a way to using a PHP/JavaScript for loop to create more compact code?
I think this should do it for you:
<ul class="tabs">
<?php
$end_at = 1000;
$group_by = 50;
for($i=0;$i<$end_at/$group_by;$i++) {
echo '<li>', $i * $group_by + 1, '-', ($i+1) * $group_by, "</li>\n";
}
?>
</ul>
Example output
Or this:
<ul class="tabs">
<?php for($i=1;$i<=1000;$i++): ?>
<?php if($i % 50 == 0): ?>
<li><?php echo $i-49 ?>-<?php echo $i; ?></li>
<?php endif; ?>
<?php endfor; ?>
</ul>

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