Error parsing JSON with PHP - php

I have a problem parsing my JSON result with specific term.
This my JSON sample result : http://bit.ly/1FbZbde
If in "curriculum" I have "__class": "chapter", I'm looking for a result of:
<h2>Title from chapter</h2>
and if I have "__class": "lecture", I'm looking for a result of:
<li>Title from lecture</li>
My code:
<?php foreach($json['curriculum'] as $item) { ?>
<li><i class="fa fa-play-circle"></i> <?php echo $item['title']; ?></li>
<?php } ?>
My results include all titles from chapters and lectures.

I hope I understood your question right.
So if the __class is lecture you want the title in a <li> and if the _class is a chapter, you want the title in a <h2>.
<?php foreach($json['curriculum'] as $item): ?>
<?php if ($item['__class'] == 'chapter'): ?>
<h2><i class="fa fa-play-circle"></i>
<?php echo $item['title']?>
</h2>
<?php elseif ($item['__class'] == 'lecture'): ?>
<li><i class="fa fa-play-circle"></i>
<?php echo $item['title'] ?>
</li>
<?php endif; ?>
<?php endforeach; ?>

Related

How to split Contao variable into several HTML Elements

I´ve got this template from my Contao CMS:
<?php $helper = new \ContaoBootstrap\Navbar\Helper\NavigationHelper($this); ?>
<?php foreach ($this->items as $item) : ?>
<?php
$currentLevelStringParts = explode('_', $this->level);
$currentLevelNumber = $currentLevelStringParts[1];
?>
<?php $itemHelper = $helper->getItemHelper($item); ?>
<li class="<?php echo str_replace(array('_'), array('-'), $itemHelper->getItemClass());
if (!empty($item['subitems'])) {echo ' subnav';} ?>
">
<<?= str_replace(array('strong'), array('a'), $itemHelper->getTag()); ?>
<?= str_replace(array('dropdown-item', 'dropdown-toggle', 'nav-link', 'data-toggle="dropdown"'), array(' ', ' ', ' ', ' '), $itemHelper) ?>>
<?php if ($item['isActive']) : ?>
<?= $item['link']?>
<?php else : ?>
<span itemprop="name"><?= $item['link']?></span>
<?php endif; ?>
</<?= str_replace(array('strong'), array('a'), $itemHelper->getTag()); ?>>
<?php if (!empty($item['subitems'])) { ?>
<span class="subnav-pull-down hidden-lg-up <?php if ($item['isTrail']) : ?>active<?php endif; ?>"></span>
<div class="subnav-container <?php if ($item['isTrail']) : ?>open<?php endif; ?>">
<div class="relative">
<ul class="nav sub-nav level-<?php echo (int)$currentLevelNumber+1 ?>">
<?= $item['subitems'] ?>
<div class="clear"></div>
</ul>
</div>
</div>
<?php } ?>
</li>
<?php endforeach; ?>
My problem is, I need a HTML structure like:
<ul>
<li><a></a></li>
<li><a></a></li>
<li>
<ul>
<li><a></a></li>
</ul>
</ul>
To change link () and description for my navigation. I tried to work the the bootstrap API but I´m kind of new to PHP and Bootstrap.
I hope you guys can help me.

sort output result in php array ksort?

I need help, sort for this output result
bellow my website code :
<?php
foreach ($this->system->produk as $pr_key=>$pr_val):
if ($pr_val->status == 'on'):?>
<li>
<?php echo $pr_val->nama;?>
</li>
<?php endif;?>
<?php endforeach;?>
any idea how to sort this output ?
Did you try ?
<?php
sort($this->system->produk);
foreach ($this->system->produk as $pr_key=>$pr_val):
if ($pr_val->status == 'on'):?>
<li>
<?php echo $pr_val->nama;?>
</li>
<?php endif;?>
<?php endforeach;?>

PHP ifelse display white screen of death

My code doesnt work properly and it does now shows any error or any clue, what am I missing here ? any hints ? TIA
<?php if(isset($_SESSION['user_session_organizer']))
{ ?>
<li>VIEW TRIPS</li>
<?php } ?>
<?php
elseif(isset($_SESSION['user_session']))
{
?>
<li>VIEW TRIPS</li>
<?php }else{ ?>
<li><a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a></li>
<?php } ?>
One unnecessary <?php and else if is in wrong way, do like below:-
<?php if(isset($_SESSION['user_session_organizer'])){ ?>
<li>VIEW TRIPS</li>
<?php } elseif(isset($_SESSION['user_session'])){?>
<li>VIEW TRIPS</li>
<?php }else{ ?>
<li><a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a></li>
<?php } ?>
Note:-
Always add
error_reporting(E_ALL);ini_set('display_errors',1);
on top of your each php page to prevent yourself from a situation like:-"White Screen Of Death"
(It will on the error reporting setting for all type of errors and every error will displayed on the page if any occur).
Thanks
Try this, your else if is wrong ?> is a close tag and then the compiler dont understand the elseif, better this:
<?php if(isset($_SESSION['user_session_organizer']))
{ ?>
<li>VIEW TRIPS</li>
<?php }elseif(isset($_SESSION['user_session']))
{
?>
<li>VIEW TRIPS</li>
<?php }else{ ?>
<li><a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a></li>
<?php } ?>
This is easier to read and to debug:
<?php if (isset($_SESSION['user_session_organizer'])) : ?>
<li>
VIEW TRIPS
</li>
<?php elseif (isset($_SESSION['user_session'])) : ?>
<li>
VIEW TRIPS
</li>
<?php else : ?>
<li>
<a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a>
</li>
<?php endif; ?>
Check about control structures.
try this, its neat and simple code.
<?php if (isset($_SESSION['user_session_organizer'])): ?>
<li>VIEW TRIPS</li>
<?php elseif (isset($_SESSION['user_session'])): ?>
<li>VIEW TRIPS</li>
<?php else: ?>
<li><a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a></li>
<?php endif; ?>

K2_content module rating

I've been reconstructing the standard K2 rating in the category_item.php to view ratings from to show as stars to show as number.
What I did was, I replaced this code:
<?php if($this->item->params->get('catItemRating')): ?>
<div id="catItemRatingBlock">
<div class="itemRatingForm">
<ul class="itemRatingList">
<li class="itemCurrentRating" id="itemCurrentRating<?php echo $this->item->id; ?>" style="width:<?php echo $this->item->votingPercentage; ?>%;"></li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</div>
</div>
<?php endif; ?>
with this code:
<?php if($this->item->params->get('catItemRating')): ?>
<div id="catItemRatingBlock">
<div class="itemRatingForm">
<?php
$rating_sum=0;
$rating_cont=0;
$db =& JFactory::getDBO();
$query='SELECT * FROM #__k2_rating WHERE itemID='. $this->item->id;
$db->setQuery($query);
$votes=$db->loadObject();
$rating_sum = intval($votes->rating_sum);
$rating_count = intval($votes->rating_count);
$evaluate = ($rating_count==0) ? "0" : number_format($rating_sum/$rating_count,1);
$evaluate = str_replace('.0', '', $evaluate);
$output=" Rating: ". $evaluate."/5";
echo $output;
?>
</div>
</div>
<?php endif; ?>
And what I want is for it to work on the K2 module as well. I tried to use the same code that I wrote above here to achieve it in k2 content module but that doesn't work at all.
Anyone know how to pull it off?
Replace
<?php if($this->item->params->get('catItemRating')): ?>
with:
<?php if($params->get('catItemRating')): ?>

Magento sort Categories in template

I'm searching for a way to sort the frontend display of categories in my navigation.
This is the code for my navigation:
<div id="menu-accordion" class="accordion">
<?php
foreach ($this->getStoreCategories() as $_category): ?>
<?php $open = $this->isCategoryActive($_category) && $_category->hasChildren(); ?>
<h3 class="accordion-toggle"><?php print $_category->getName();?></h3>
<div class="accordion-content">
<ul>
<?php foreach ($_category->getChildren() as $child): ?>
<li>
<span class="ui-icon ui-icon-triangle-1-e vMenuIconFloat"></span>
<?php print $child->getName();?>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach ?>
</div>
I tried using asort() to sort $this->getStoreCategories(), but it resolved to an error 500, so I guess that it's not an array, but an object (which seems obvious for the objectoriented programming of magento). I tried finding a solution for object, but failed and now I'm a bit stuck.
Thanks for your help.
The call to $this->getStoreCategories() does not return an array. But you can build up your own array and use the key of the array as the element to sort on (assuming you want to sort by name of the category):
foreach ($this->getStoreCategories() as $_category)
{
$_categories[$_category->getName()] = $_category;
}
ksort($_categories);
Now instead of iterating over $this->getStoreCategories() you iterate over the $_categories array. So your code would look something like:
<div id="menu-accordion" class="accordion">
<?php
$_categories = array();
foreach ($this->getStoreCategories() as $_category)
{
$_categories[$_category->getName()] = $_category;
}
ksort($_categories);
foreach ($_categories as $_category): ?>
<?php $open = $this->isCategoryActive($_category) && $_category->hasChildren(); ?>
<h3 class="accordion-toggle"><?php print $_category->getName();?></h3>
<div class="accordion-content">
<ul>
<?php foreach ($_category->getChildren() as $child): ?>
<li>
<span class="ui-icon ui-icon-triangle-1-e vMenuIconFloat"></span>
<?php print $child->getName();?>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach ?>
</div>

Categories