How to add a for each loop in php? - php

i have an issues in my code below.
In days and time i want to add a loop, it s because i want to be able to get multiple different operating hours row. I ll add a repeater in my custom field.
And for now it s allow me to get only one row.
<ul>
<?php foreach($locations as $location) {
$title = get_the_title($location);
$address = '';
$address .= get_field('address', $location) . '<br>';
$address .= get_field('city', $location) . ', ';
$address .= get_field('state', $location) . ', ';
$address .= get_field('zip_code', $location);
$hours = get_field('days_open', $location);
$phone = get_field('contact_number', $location);
$email = get_field('contact_email', $location);
?>
<li>
<h3><?php echo $title; ?></h3>
<address><?php echo $address; ?></address>
<?php if($phone) { ?>
<?php echo $phone; ?>
<?php }; ?>
<?php if($email) { ?>
<?php echo $email; ?>
<?php }; ?>
<?php
if($hours && $hours[0]['days']
) {
echo $hours[0]['days'] . ': ';
}
?>
<?php
if($hours && $hours[0]['times']
) {
echo $hours[0]['times'];
}
?>
</li>
<?php
}
?>
</ul>
So i think my solution it s to add a foreach loop, can you help for that?
Thank you in advance cheers!

May be this is what you are looking for?
if(is_array($hours[0]['days'])){
foreach($hours[0]['days'] as $day)
{
echo $day . ': ';
}
}
if(is_array($hours[0]['times'])){
foreach($hours[0]['times'] as $time)
{
echo $time . ': ';
}
}

So I found the solutions it was :
<?php
if(is_array($hours)){
foreach($hours as $day)
{
echo $day['days'] . ': ';
echo $day['times'];
echo '<br />';
}
} ?>

Related

Magento: How to get sub category of current category by level and so on

I have a following code that shows category by level, but I have to show all the sub categories from that specific category.
<?php
$_cat = new Mage_Catalog_Block_Navigation();
$currentCat = $_cat->getCurrentCategory();
$subCats = Mage::getModel('catalog/category')->load($currentCat->getId())- >getChildren();
$subCatIds = explode(',',$subCats);
?>
<?php ////////////////////////level-3///////////////////////////// ?>
<?php $category = Mage::registry('current_category');
$category->getParentCategories();
if ( $category->getLevel() == 3 ) : ?>
<div class="cat_drop_ser_wrap">
<?php $currentCat = Mage::getModel('catalog/category')->load($currentCat- >getId()) ?>
<select class="select_class" onchange="window.location.href=this.value">
<option value="#">-Select</option>
<?php foreach($subCatIds as $subCatId): ?>
<?php $subCat = Mage::getModel('catalog/category')->load($subCatId); ?>
<?php if($subCat->getIsActive()): ?>
<option value="<?php echo $subCat->getUrl() ?>">
<?php echo $subCat->getName(); ?>
</option>
<?php endif; ?>
<?php endforeach; ?>
</select>
</div>
<?php endif; ?><!--if--level-3-->
Thanks in advance.
Ok in case anyone needs it following will show the sub categories and so on:
<?php //////sub////// ?>
<?php
$category_levels = Mage::getModel('catalog/category')->load($category->getId());
$subcategories = $category_levels->getChildrenCategories();
if (count($subcategories) > 0){
foreach($subcategories as $subcategory){
$category_levels_two = Mage::getModel('catalog/category')->load($subcategory->getId());
$subcategoriess = $category_levels_two->getChildrenCategories();
if (count($subcategoriess) > 0){
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_category', 'category_dropdown_label');
$text = $category_levels_two->getCategoryDropdownLabel();
echo '<div class="empty_serch_select_1 cat_drop_ser_wrap 2">';
echo '<label>' . $text . '</label>';
echo '<select disabled="disabled">';
echo '<option value="#">-Select</option>';
echo '<select>';
echo '</div>';
break;
}
}
}
?>
<?php //////sub-sub////// ?>
<?php
$category_levels = Mage::getModel('catalog/category')->load($category->getId());
$subcategories = $category_levels->getChildrenCategories();
echo '<div class="empty_serch_select_2 cat_drop_ser_wrap 3">';
if (count($subcategories) > 0){
foreach($subcategories as $subcategory){
$category_levels_two = Mage::getModel('catalog/category')->load($subcategory->getId());
$subcategoriess = $category_levels_two->getChildrenCategories();
if (count($subcategoriess) > 0){
foreach($subcategoriess as $subcategorys){
$category_levels_three = Mage::getModel('catalog/category')->load($subcategorys->getId());
$subcategoriesss = $category_levels_three->getChildrenCategories();
if (count($subcategoriesss) > 0){
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_category', 'category_dropdown_label');
$text = $category_levels_three->getCategoryDropdownLabel();
echo '<label>' . $text . '</label>';
break;
}
}
}
}
}
echo '<select disabled="disabled">';
echo '<option value="#">-Select</option>';
echo '<select>';
echo '</div>';
?>
<?php //////sub-sub-sub////// ?>
<?php
$category_levels = Mage::getModel('catalog/category')->load($category->getId());
$subcategories = $category_levels->getChildrenCategories();
echo '<div class="empty_serch_select_2 cat_drop_ser_wrap 3">';
if (count($subcategories) > 0){
foreach($subcategories as $subcategory){
$category_levels_two = Mage::getModel('catalog/category')->load($subcategory->getId());
$subcategoriess = $category_levels_two->getChildrenCategories();
if (count($subcategoriess) > 0){
foreach($subcategoriess as $subcategorys){
$category_levels_three = Mage::getModel('catalog/category')->load($subcategorys->getId());
$subcategoriesss = $category_levels_three->getChildrenCategories();
if (count($subcategoriesss) > 0){
foreach($subcategoriesss as $subcategorys){
$category_levels_three = Mage::getModel('catalog/category')->load($subcategorys->getId());
$subcategoriessss = $category_levels_three->getChildrenCategories();
if (count($subcategoriessss) > 0){
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_category', 'category_dropdown_label');
$text = $category_levels_three->getCategoryDropdownLabel();
echo '<label>' . $text . '</label>';
break;
}
}
}
}
}
}
}
echo '<select disabled="disabled">';
echo '<option value="#">-Select</option>';
echo '<select>';
echo '</div>';
?>

Counter to add seperator on foreach

I have an foreach to add terms to a post on Wordpress Which is working great. Code below:
<h3>
<?php foreach($terms as $term) {?>
<?php echo $term->name;?>
<?php } ?>
</h3>
However I need to add a counter so that if there is more than one term in the <h3> it adds a / between them. For instance:
<h3>Term Name</h3>
<h3>Term Name / Term Name / Term Name</h3>
This is the code I have so far however its not working.
<?php
$i = 1;
foreach($terms as $term) {
if($i == 1){
echo ' / '.$term->name;
} else {
echo $term->name;
}
$i++;
} ?>
You don't need to use a counter. Just put each $term->name in to an array, and implode it:
echo implode(' / ', array_map(function($term) { return $term->name; }, $terms));
Here's a demo
Please try below code.
<?php $terms = array_values($terms);
if( sizeof($terms) > 1){ ?>
<h3><?php echo implode(' / ', array_map(function($term) { return $term->name; }, $terms)); ?></h3>
<?php }else{ $term = $terms[0]; ?>
<h3>echo $term->name;</h3>
<?php } ?>
<?php
$i = 1;
foreach($terms as $term) {
if($i > 1) {
echo ' / '.$term->name;
} else {
echo $term->name;
$i++;
}
}
?>
Here is the working solution :)
<?php
$terms = array("Term One","Term Two","Term Three","Term Four");
$i = 1;
$result="";
foreach($terms as $term)
{
if($i==1)
{
$result=$result.$term.' ';
$i++;
}
else
{
$result=$result."/".' ';
$result=$result.$term.' ';
}
}
echo $result;
?>

Only display elements 5 days old or younger?

I am using SimplePie to display the RSS results from multiple sources, however I'm finding that there's several I don't need to see. How do I only show the results that are 5 days old or younger?
Here's my current PHP code:
foreach ($feed->get_items(0) as $item):
$url = $item->get_permalink();
$title = $item->get_title();
$date = $item->get_date('F j, Y - g:i a');
$description = $item->get_description();
if (strpos($url,'craigslist') !== false) {
echo ' <div id="linkCell" style="width: 100%;">';
echo ' <div id="vAlign">';
echo ' <p class="linkTitle">';
echo ' '.$title.'';
echo ' </p><br />';
echo ' <span class="date">'.$date.'</span><br>';
echo ' <p class="description">'.$description.'</p>';
echo ' </div>';
echo ' </div>';
}
endforeach;
Thanks in advance!
Something like this should work. Need to convert date to unix timestamps, find the difference, and then convert this to days.
foreach ($feed->get_items(0) as $item):
$url = $item->get_permalink();
$title = $item->get_title();
$date = $item->get_date('F j, Y - g:i a');
$datestrtotime = strtotime($date);
$now = time();
$datediff = $now - datestrtotime;
$daysdifference = floor($datediff/(60*60*24));
$description = $item->get_description();
if($daysdifference>5)
{
if (strpos($url,'craigslist') !== false) {
echo ' <div id="linkCell" style="width: 100%;">';
echo ' <div id="vAlign">';
echo ' <p class="linkTitle">';
echo ' '.$title.'';
echo ' </p><br />';
echo ' <span class="date">'.$date.'</span><br>';
echo ' <p class="description">'.$description.'</p>';
echo ' </div>';
echo ' </div>';
}
}
endforeach;

Database results by month and year

I have a Joomla 3.1 site that is going to show reports about contributions made. The reports should be divided into groups by month and year. I have a code that pulls data from database but cannot make it put each month into a separate <div> and each year into a parent <div> to its months. How is it done? Here is my code.
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array($db->quoteName('contribution_date'), $db->quoteName('contribution_amount'), $db->quoteName('source')));
$query->from($db->quoteName('contribution'));
$db->setQuery($query);
$results = $db->loadObjectList();
?>
<table class='financial-reports'><th>aaa</th><th>bbb</th><th>ccc</th>
<?php
foreach ( $results as $result) {
echo '<tr>' . '<td class="fin-rep-date">' . JFactory::getDate($result->contribution_date)->format('d M, Y') . '</td>' .
'<td class="fin-rep-sum">' . $result->contribution_amount . ' ' .'руб.' . '</td>' .
'<td class="fin-rep-source">' . $result->source . '</td>' . '</tr>';
}
?>
</table>
I think the easies way would be convert results into multi-dimensional array like this:
foreach ($results as $result) {
$resultsArray[JFactory::getDate($result->contribution_date)->format('Y')][JFactory::getDate($result->contribution_date)->format('M')][] = $result;
}
And then you can use it like that:
<?php foreach ($resultsArray as $year) : ?>
<div>
<?php foreach ($year as $month) : ?>
<div>
<?php foreach ($month as $item) : ?>
<div>YOUR OUTPUT <?php echo $item->contribution_date; ?></div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>

codeigniter avoiding html div's

Is there a proper syntax to avoid div's in codeigniter?
I don't really like opening and closing tags all the time...
<div class="theForm">
<?php
echo form_open('edit/links');//this form uploads
echo "Enter the Name: ". form_input('name','name');
echo "Enter the Link: ". form_input('url','url');
echo " ".form_submit('submit', 'Submit');
echo form_close();
if (isset($linksQuery) && count($linksQuery)){
foreach($linksQuery as $link){
echo anchor($link['link'], $link['name'].".", array("class" => "links"));
echo form_open('edit/links',array('class' => 'deleteForm'));
echo form_hidden('name',$link['name']);
echo " ".form_submit('delete','Delete');
echo form_close();
echo br(2);
}
}
?>
</div>
You could write a small helper like this:
<?php
function div_open($class = NULL, $id = NULL)
{
$code = '<div ';
$code .= ( $class != NULL ) ? 'class="'. $class .'" ' : '';
$code .= ( $id != NULL ) ? 'id="'. $id .'" ' : '';
$code .= '>';
return $code;
}
function div_close()
{
return '</div>';
}
echo div_open('some_class', 'some_id');
echo 'some content...';
echo div_close();
?>
will produce:
<div class="some_class" id="some_id" >some content...</div>

Categories