sort output result in php array ksort? - php

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;?>

Related

Display MySQL request in PHP

I am trying to make a basic todolist but i want to display a list with task undone at the top and the done at the bottom.
Every time i try i have a blank page
<h1 class="header">To do.</h1>
<?php if (!empty($items)): ?>
<ul class="items">
<?php foreach ($items as $item): ?>
<?php if ( $item['done'] == 0 ) {
echo '<li>';
echo '<span class=\"item'?><?php echo $item['done'] ? ' done' : '' ?>"> <?php echo $item['name']; c</span>
<?php if (!$item['done']): ?>
Mark as done
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p> No tasks</p>
I have a table items with a field Done (0,1)
I have tryed to put if condition in the foreach but it failed.
Thanks for the help
At the end of your line which begins with echo '<span class=\"item'?> you set a PHP open tag but without closing it and then open another one on the next line:
<?php echo $item['name']; c</span>
<?php if (!$item['done']): ?>
It looks like you meant to close the PHP tag on the previous line with ?>.
As a side suggestion, if you use a good editor or IDE it will show you errors on front of you as you type, saves a lot of time.
Viewing your PHP error logs is a must as a developer, as it tells you of problems as you work, and this would have shown a fatal error and the line number etc.
A white page is also a sign that you have a fatal error as PHP crashes before it will output any content to the screen.
When i remove the if
The code is working
<?php if (!empty($items)): ?>
<ul class="items">
<?php foreach ($items as $item): ?>
<li>
<span class="item<?php echo $item['done'] ? ' done' : '' ?>"> <?php echo $item['name']; ?> </span>
<?php if (!$item['done']): ?>
Mark as done
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p> You have no task</p>

Error parsing JSON with 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; ?>

Magento get CMS pages based on layout

So I'm building a custom breadcrumb nav to list all fundraising options. The pages are using a unique layout called "fundraising_page." Is there a way to grab the pages only if they have the "fundraising_page" layout? So far I have this, which is grabbing every page regardless of the template it is using.
So what I need is only to list the pages that are using the "fundraising_page" template.
<?php $collection = Mage::getModel('cms/page')->getCollection()->addStoreFilter(Mage::app()->getStore()->getId());?>
<?php $collection->getSelect()->where('is_active = 1'); ?>
<ul>
<?php foreach ($collection as $page): ?>
<?php $PageData = $page->getData(); ?>
<?php if($PageData['identifier']!='no-route'){ ?>
<li>
<?php echo $PageData['title'] ?>
</li>
<?php } ?>
<?php endforeach; ?>
Here's some well formatted code and using proper methods.
<?php
$collection = Mage::getModel('cms/page')->getCollection()
->addStoreFilter(Mage::app()->getStore()->getId())
->addFieldToFilter('is_active', 1)
->addFieldToFilter('root_template', 'fundraising_page');
?>
<?php foreach ($collection as $page): ?>
<?php if ($page->getIdentifier() != 'no-route'): ?>
<li>
<?php echo $page->getTitle() ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
Instead of if($PageData['identifier']!='no-route') try
if($PageData['root_template']=='fundraising_page')

Loop with a Loop including an explode()

I have an associative array returning a SQL query that pulls questions from my database. The tags field is stored as a delimited text VarChar. I want to loop through all of my database entries while using explode to create an array and then loop through the tag array to create a UL with the tags as well unfortunately it has not worked very well. This is what I have so far:
<?php foreach($questionRow as $questionShow) { ?>
<?php echo ($questionShow['netvotes']) ;?>
<?php echo ($questionShow['views']) ;?>
<?php echo ($questionShow['q_answer_count']) ;?>
<?php echo ($questionShow['title']) ;?>
Tags:
<ul style="display: inline">
<?php
$tagname = explode(",",$questionShow['tags']);
foreach ($tagname as $tagList) { ?>
<li class="label label-inverse" style="margin-left: 5px"><?php echo($tagList) ;?></li>
<?php }; ?>
</ul>
<?php }; ?>
The results were very strange
I also tried by using the below to run my outer loop
<?php while($questionShow =$questionResult>fetch_assoc() ) { ?>
That caused the main recordset results to not actually loop. Hopefully I have explained this properly and someone could help. Thank you in advance!
well this could be very simple:
<?php
$query = mysql_query("SELECT * FROM `questions` LIMIT 30"); // replace this with your query
while($data=mysql_fetch_array($query)){
echo $data['netvotes'];
echo $data['views'];
echo $data['q_answer_count'];
echo $data['title']) ;?>
?>
Tags:
<ul style="display: inline">
<?php
foreach(explode(",",$data['tags']) as $tag){
?>
<li class="label label-inverse" style="margin-left: 5px"><?php echo $tag; ?></li>
<?php
}
?>
</ul>
<?php
}
?>

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