How to check the values from the array in the loop - php

<?php $callout = array(
'1',
'3',
'5'
); ?>
<ul>
<?php foreach($callout as $call) { ?>
<li>
<h3><?php echo get_the_title($call); ?></h3>
<p>different text goes here...</p>
</li>
<?php } ?>
</ul>
Is it possible - if $call=1 then echo some static text, if 2 echo something else, and so on?

Problem solved. Thanks to #swapnesh
http://codepad.org/0mZIkJuB
<?php $callout = array(1,2,3); ?>
<ul>
<?php foreach($callout as $call) { ?>
<li>
<?php
if ('1' == $call)
echo "a";
elseif ('2' == $call)
echo "b";
elseif ('3' == $call)
echo "c";
else
echo "d";
?>
</li>
<?php } ?>
</ul>

Related

Syntax error, Unexpected else T_ELSE [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I got this error from my product.tpl, inserted some codes to manipulate my price and stocks, im looking to end my code using this } absolutely but i got no luck.
<?php if($price == "₱0.00" && $stock == "In Stock" ) { ?><?php echo "Customizable"; ?>
<?php } else { ?>
<?php if($price == "₱0.00" && $stock == "For inquiry") { ?><?php echo "For inquiry"; ?>
<?php } else { ?>
<?php if($price) { ?><?php echo $price; ?></h4>
<?php } ?>
Originally the code is this and i added to a little bit of code to the highlighted code
<ul class="list-unstyled">
<?php if (!$special) { ?>
<li>
<h4><?php if($price) { ?><?php echo $price; ?></h4>
</li>
<?php } else { ?>
<li><span style="text-decoration: line-through;"><?php echo $price; ?>
</span>
<h4><?php echo $special; ?></h4></li>
<?php } ?>
<?php if ($tax) { ?>
<li><?php echo $text_tax; ?><?php echo $tax; ?></li>
<?php } ?>
<?php if ($points) { ?>
<li><?php echo $text_points; ?> <?php echo $points; ?></li>
<?php } ?>
<?php if ($discounts) { ?>
<?php foreach ($discounts as $discount) { ?>
<li><?php echo $discount['quantity']; ?><?php echo $text_discount; ?>
<?php echo $discount['price']; ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
Formatting your code properly really helps. Does the code below work the way you want it to?
<?php if(!$special) { ?>
<ul class="list-unstyled">
<li>
<?php if($price == "₱0.00" && $stock == "In Stock" ) {
echo "Customizable";
} else if($price == "₱0.00" && $stock == "For inquiry") {
echo "For inquiry";
} else if($price) { ?>
<h4><?=$price;?></h4>
<?php } else { ?>
<span style="text-decoration: line-through;"><?=$price;?></span>
<h4><?=$special;?></h4>
<?php } ?>
</li>
<?php if($tax) { ?>
<li><?=$text_tax.' '.$tax;?></li>
<?php } ?>
<?php if($points) { ?>
<li><?=$text_points.' '.$points;?></li>
<?php } ?>
<?php if(count($discounts)) { foreach ($discounts as $discount) { ?>
<li><?=$discount['quantity'].' '.$text_discount.' '.$discount['price'];?></li>
<?php } } ?>
</ul>
<?php } ?>
Use proper format, too many tags of php, use codes look like this.
<?php
if($price == "₱0.00" && $stock == "In Stock" ) {
echo "Customizable";
} else if($price == "₱0.00" && $stock == "For inquiry") {
echo "For inquiry";
} else if($price) {
echo $price;
}
?>
The if elseif is used like below:
<?php if($condition) { //action } elseif($condition2) { //action2 } ?>
I think you want this:
<ul class="list-unstyled">
<?php if(!$special) { ?>
<li>
<h4><?php echo $price; ?></h4>
</li>
<?php }else{ ?>
<li>
<span style="text-decoration: line-through;"><?php echo $price; ?></span>
<h4><?php echo $special; ?></h4>
</li>
<?php if($tax) { echo '<li>'.$text_tax.$tax.'</li>';} ?>
<?php if($points) { echo '<li>'.$text_points.$points.'</li>';} ?>
<?php
if($discounts){
foreach ($discounts as $discount) {
echo '<li>'.$discount["quantity"].$text_discount'<br>'.$discount['price'].'</li>';
}
}
?>
<?php } ?>
</ul>

Add third level to php menu in WP?

I'm attempting to add a for loop to a php menu that will add a tertiary level to a currently existing WP menu. Unfortunately, the only editable amount of content is that posted below, so please bear with me on this one.
I'm just wondering, before I attempt to implement this code, if anyone can tell me if it will actually work, of if you assume it'll break everything.
What I've done is to take the same semantic forloop that was written for the secondary (or 'sub_nav') menu items, and repeat it in attempts to dynamically generate third level (or 'tert_nav') menu items.
<div class="main-menu-sub-navigation">
<?php
$main_nav_items = wp_get_nav_menu_items(2);
$sub_nav = array();
foreach ($main_nav_items as $item) {
if($item->menu_item_parent !== '0'){
$parent_id = $item->menu_item_parent;
$sub_nav[$parent_id][] = array('item' => $item);
}
}
$tert_nav = array();
foreach ($sub_nav_items as $item) {
if($item->menu_item_parent !== '0'){
$parent_id = $item->menu_item_parent;
$tert_nav[$parent_id][] = array('item' => $item);
}
}
?>
<div class="sub-nav-inner">
<?php foreach ($sub_nav as $key => $value) { ?>
<ul class="nav sub-nav" data-parent="<?php print $key; ?>">
<?php foreach ($value as $item) { ?>
<?php if(isset($_GET['archive'])): ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->title == 'Archives') ? 'current-menu-item' : 'menu-item'; ?>"><a href="<?php print $item['item']->url; ?>"><?php print $item['item']->title; ?>
<?php foreach ($tert_nav as $key => $value) { ?>
<ul class="nav tert-nav" data-parent="<?php print $key; ?>">
<?php foreach ($value as $item) { ?>
<?php if(isset($_GET['archive'])): ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->title == 'Archives') ? 'current-menu-item' : 'menu-item'; ?>"><?php print $item['item']->title; ?></li>
<?php else: ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->object_id == get_the_ID()) ? 'current-menu-item' : 'menu-item'; ?>"><?php print $item['item']->title; ?></li>
<?php endif; ?>
<?php } ?>
</ul>
<?php } ?>
</a></li>
<?php else: ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->object_id == get_the_ID()) ? 'current-menu-item' : 'menu-item'; ?>"><a href="<?php print $item['item']->url; ?>"><?php print $item['item']->title; ?>
<?php foreach ($sub_nav as $key => $value) { ?>
<ul class="nav tert-nav" data-parent="<?php print $key; ?>">
<?php foreach ($value as $item) { ?>
<?php if(isset($_GET['archive'])): ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->title == 'Archives') ? 'current-menu-item' : 'menu-item'; ?>"><?php print $item['item']->title; ?></li>
<?php else: ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->object_id == get_the_ID()) ? 'current-menu-item' : 'menu-item'; ?>"><?php print $item['item']->title; ?></li>
<?php endif; ?>
<?php } ?>
</ul>
<?php } ?>
</a></li>
<?php endif; ?>
<?php } ?>
</ul>
<?php } ?>
</div>
Any help would be so very greatly appreciated. Thank you!!

How to split each value of foreach statement with different classes PHP

I need to split up each return $val with different class. Let's say the value has 7 value inside and I wish to have each value have different classes.
foreach ($arrNum as $val) {
?>
<li class="col-15"><?php echo $val; ?></li>
<?php } ?>
i try amend with below code , its seems not working :
foreach ($arrNum as $i => $val) {
?>
<?php if($i == 0): ?>
<div class="col-03">
<?php elseif($i == 1): ?>
<div class="col-28">
<?php elseif($i == 2): ?>
<div class="col-15">
<?php elseif($i == 3): ?>
<div class="col-48">
<?php elseif($i == 4): ?>
<div class="col-02">
<?php elseif($i == 5): ?>
<div class="col-46">
<?php elseif($i == 6): ?>
<div class="col-33">
<?php endif; ?>
Rest of html
</div>
<!-- <li class="col-15"><?php echo $val; ?></li> -->
<?php } ?>
Do you looking for something like this PHP-Code
<?php
// Create array
$arrNum = array();
$arrNum['03'] = 'Rest of html';
$arrNum['28'] = 'Rest of html';
$arrNum['15'] = 'Rest of html';
$arrNum['48'] = 'Rest of html';
$arrNum['02'] = 'Rest of html';
$arrNum['46'] = 'Rest of html';
$arrNum['33'] = 'Rest of html';
foreach ($arrNum as $i=>$val) {
echo '<div class="col-'.$i.'">'.$val.'</div>';
}
?>
this will return HTML-Code
<div class="col-03">Rest of html</div>
<div class="col-28">Rest of html</div>
<div class="col-15">Rest of html</div>
<div class="col-48">Rest of html</div>
<div class="col-02">Rest of html</div>
<div class="col-46">Rest of html</div>
<div class="col-33">Rest of html</div>
if your text ("Rest of html") is every time the same you can simplify the code
<?php
$arrNum = array('03', '28', '15', '48', '02', '46', '33');
foreach ($arrNum as $i) {
echo '<div class="col-'.$i.'">Rest of html</div>';
}
?>
You try this
`$no=1;
foreach ($arrNum as $val) {
?>
<li class="col-<?php echo $no;?>"><?php echo $val; ?></li>
<?php $no++; } ?>`

I want to limit this foreach() loop in PHP to 2 outputs

This is the loop I want to limit this to 2 outputs and create another loop for further outputs please help.. I am a beginner
Enter code here
<?php if ($informations) { ?>
<div class="column">
<h3>
<?php echo $text_information; ?>
</h3>
<ul>
<?php foreach ($informations as $information){ ?>
<li><a href="<?php echo $information['href']; ?>">
<?php echo $information['title']; ?>
</a></li>
<?php } ?>
</ul>
</div>
<?php } ?>
Try this. .
<?php if ($informations) { ?>
<div class="column">
<h3><?php echo $text_information; ?></h3>
<ul>
<?php
$i=1;
foreach ($informations as $information){
if($i==2)
{
break;
}
?>
<li><?php echo $information['title']; ?></li>
<?php
$i++;
} ?>
</ul>
</div>
<?php } ?>
For other values . .
<?php if ($informations) { ?>
<div class="column">
<h3><?php echo $text_information; ?></h3>
<ul>
<?php
$i=1;
foreach ($informations as $information){
if($i<2)
{
continue;
}
?>
<li><?php echo $information['title']; ?></li>
<?php
$i++;
} ?>
</ul>
</div>
<?php } ?>
Bit vague but I guess you are looking for array_chunk,
$new_array = array_chunk($informations, 2, true);

Change Magento Menu Subcategory Sort Order

I am using the following code to display Categories and Subcategories in a drop down menu, however I want the subcategories to be sorted alphabetically... how can I do this?
<ul id="custom-menu">
<li><b>Home</b></li>
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul style="width:940px;">
<?php foreach($_categories as $_category): ?>
<li>
<b><a class="drop" href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a></b>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<div class="dropdown_1column" style="width:235px;">
<?php foreach($_subcategories as $_subcategory): ?>
<div class="col_1">
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</div>
<?php endforeach; ?>
<div class="clr" clear="all"></div>
</div>
<?php endif; ?>
</li>
<?php endforeach; ?>
<li class="normal"><b>Sale</b></li>
<li class="normal"><b>Help</b></li>
<li class="normal"><b>Open Account</b></li>
</ul>
<?php endif; ?>
What I've always done as our clients don't usually have categories that change frequently is dragged and dropped them in alphabetical order in the admin backend: Catalog > Categories > Manage Categories.
If that's not your style, you can always take some advice from this answer to first run through the categories, place them in a new array, and then key sort the array and finally output: https://stackoverflow.com/a/4273912/823549
First backup your topmenu.phtml then replace the following code in your new topmenu.phtml file
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php
function array_sort($array, $on, $order=SORT_ASC){
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
?>
<?php
$layer = Mage::getSingleton('catalog/layer');
$_category = $layer->getCurrentCategory();
$currentCategoryId= $_category->getId();
?>
<div class="nav-container">
<ul id="nav">
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0){ ?>
<?php foreach($_categories as $_category){ ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<li><span><?php echo $_category->getName(); ?></span>
<?php $catList = array();?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php foreach($_subcategories as $_subCategory){ ?>
<?php $catList[] = array('name' => $_subCategory->getName(), 'url' => $_subCategory->getUrl(), 'id' => $_subCategory->getId());?>
<?php } ?>
<?php $catList = array_sort($catList, 'name', SORT_ASC);?>
<ul>
<?php if (count($catList) > 0){ ?>
<?php $subcat=0?>
<?php foreach($catList as $_subCategory){ ?>
<li><span><?php echo $_subCategory['name'] ?></span>
<?php $subCatList = array();?>
<?php $_subSubCat = Mage::getModel('catalog/category')->load($_subCategory['id']);
$_subSubCategories = $_subSubCat->getChildrenCategories();?>
<?php foreach($_subSubCategories as $_subSubCategory){ ?>
<?php $subCatList[] = array('name' => $_subSubCategory['name'], 'url' => $_subSubCategory['url']);?>
<?php } ?>
<?php $subCatList = array_sort($subCatList, 'name', SORT_ASC);?>
<?php if (count($subCatList) > 0){ ?>
<ul>
<?php foreach($subCatList as $_subSubCat){ ?>
<li><span><?php echo $_subSubCat['name'] ?></span>
<?php } ?>
</li>
</ul>
<?php } ?>
</li>
<?php } ?>
<?php } ?>
</ul>
</li>
<?php } ?>
<?php } ?>
</ul>
</div>

Categories