Echo an Array Key.. how? - php

Im looking to echo the value of my array key out into the view for this controller. I cant quite figure it out. So for the first feed I want to echo mmafighting into the of the view. Then it would loop through the rest.
Here is my controller code:
function index()
{
$this->load->library('simplepie');
$feeds = array('mmafighting' => 'http://www.mmafighting.com/rss/current',
'bbc' => 'http://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml',
'bloodyelbow' => 'http://www.bloodyelbow.com/rss',
'ufc' => 'http://www.ufc.com/rss/news',
'hackernews' => 'http://news.ycombinator.com/rss',
'msnbc' => 'http://www.msn.com/rss/news.aspx',
'msnbc2' => 'http://www.msn.com/rss/msnmoney.aspx',
'msnbc3' => 'http://www.msn.com/rss/msnshopping_top.aspx'
);
foreach ($feeds as $site=>$url)
{
$data['feed'][$site] = new SimplePie();
$data['feed'][$site]->set_feed_url($url);
$data['feed'][$site]->set_cache_location(APPPATH.'cache');
$data['feed'][$site]->set_cache_duration(300);
$data['feed'][$site]->init();
$data['feed'][$site]->handle_content_type();
}
$this->load->view('feedsview', $data);
}
Here is my view code:
<?php
$feedCount = 0;
$rowCount = 0;
?>
<?php foreach ($feed as $site): ?>
<?php if ($site->data): ?>
<div class="feed">
<h2><?php // echo original array key here ?></h2>
<ul>
<?php $items = $site->get_items(0, 5); ?>
<?php foreach ($items as $item): ?>
<li><?php echo $item->get_title(); ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<?php $feedCount++; ?>
<?php if ($feedCount === 3) {
echo '<div style="clear:both;"> </div>';
$feedCount = 0;
$rowCount++;
if ($rowCount === 2) {
echo '<div style="width:1000px; margin-bottom:20px;height:100px; border:1px solid #252525; float:left;">images</div>';
}
} ?>
<?php endforeach; ?>

I'm not sure i understand but what about doing this :
<?php foreach ($feed as $key => $site): ?>
<?php if ($site->data): ?>
<div class="feed">
<h2><?php echo $key ?></h2>
<ul>

Related

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!!

Opencart Display subcategories

on each products page I currently have displayed the products subcategory, but only one level deep. So if in categories I have Brother > MFC > B4564 > TN450.... and I am on the TN450's product page, down below it will only show BN4564 . I need it also show The mfc and the brother part. I am not sure how to do this.. any ideas? here is my current code.
.TPL file
<!-- Display product categories -->
<?php foreach ($catprod as $catp)
{
?><?php echo $catp['name']; ?> <?php
}`
controller .php file
$data['catprod'] = array();
$product_category = $this->model_catalog_product->getCategories($this->request->get['product_id']);
foreach ($product_category as $prodcat)
{
$category_info = $this->model_catalog_category->getCategory($prodcat['category_id']);
if ($category_info)
{
$data['catprod'][] = array('name' => $category_info['description'],'href' => $this->url->link('product/category', 'path=' . $category_info['category_id']));
}
}`
which version of opencart?
this should work for version 1:
open catalog/view/theme/default/template/module/category.tpl
remove its contents and insert:
<div class="box" id="categories-menu">
<div class="box-heading"><?php echo $heading_title; ?></div>
<div class="box-content">
<ul class="box-category" id="box-category">
<?php foreach ($categories as $category) { ?>
<li>
<?php if ($category['category_id'] == $category_id) { ?>
<a href="<?php echo $category['href']; ?>" class="active list-group-item" ><?php echo $category['name']; ?></a>
<?php } else { ?>
<?php echo $category['name']; ?>
<?php } ?>
<?php if ($category['children']) { ?>
<ul class="submenu">
<?php foreach ($category['children'] as $child) { ?>
<li>
<?php if ($child['category_id'] == $child_id) { ?>
- <?php echo $child['name']; ?>
<?php } else { ?>
- <?php echo $child['name']; ?>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>

What is the best way to rewrite this repeated if statement?

I'm thinking there is an easier way to write this code, but not sure on what approach to take. Basically, I want to check if each variable exists, and if so - add the appropriate markup to the page.
Any suggestions would be great.
<?php
$words = get_field('words');
$photography = get_field('photography');
$architect = get_field('architect');
?>
<div class="panel">
<?php if( $words ): ?>
<div>
<p><span>Words</span><span><?php echo $words;?></span></p>
</div>
<?php endif ;?>
<?php if( $photography ): ?>
<div>
<p><span>Photography</span><span><?php echo $photography;?></span></p>
</div>
<?php endif; ?>
<?php if( $architect ): ?>
<div>
<p><span>Architect</span><span><?php echo $architect;?></span></p>
</div>
<?php endif; ?>
</div>
You can use array & loop -
<?php
$fields = array();
$fields['words'] = get_field('words');
$fields['photography'] = get_field('photography');
$fields['architect'] = get_field('architect');
?>
<div class="panel">
<?php foreach($fields as $key => $value):
if($value)
?>
<div>
<p><span><?php echo ucwords($key);?></span><span><?php echo $value;?></span></p>
</div>
<?php
endif;
endforeach;?>
</div>
Create a field array with their appropriate label if you want to have your own labels.
<div class="panel">
<?php
$fields = array (
'words' => 'Words',
'photography' => 'Photography',
'architect' => 'Architect'
);
foreach ( $fields as $field => $label ) {
$value = get_field ( $field );
if (!empty($value)) {
echo '<div>
<p><span>' . $label . '</span><span>' . $value . '</span></p>
</div>';
}
}
?>
</div>
<?php
$required_fields = array("words","photography","architect");
$data = array();
foreach($required_fields as $field)
{
$data[$field] = get_field($field);
}
?>
<div class="panel">
<?php foreach($data as $data_field=>$data_value):
if($data_value)
?>
<div>
<p><span><?=$data_field?></span><span><?=$data_value?></span></p>
</div>
<?php
endif;
endforeach ;?>
</div>
You can create a function like this and then iterate the data that it returns to output your content
<?php
//define a function to fetch data in specific fields
function fetch_data($keys=array()){
$data = array();
foreach($keys as $key){
$val = get_field($key);
if(!empty($val)){
$data[$key] = $val;
}
}
return $data;
}
//now you can easily add or change your fields
$data = fetch_data(array('words','photography','architect'));
?>
<div class="panel">
<?php
foreach($data as $field){
if(isset($data[$field])){
$val = $data[$field];
?>
<div>
<p><span><?php echo ucfirst($field); ?></span><span><?php echo $val; ?></span></p>
</div>
<?php
}
}
?>

Tabular Input Yii

I have to input several strings into table in db. Its tabular input yii. Cannot understand, how to do it this way, if i have such controller:
public function actionCreate()
{
$model1=new Section;
$model2=new SectionI18n;
if(isset($_POST['SectionI18n'])){
foreach ($model2 as $i => $m1) {
foreach($_POST['SectionI18n'] as $i => $m1)
{
$m1=new SectionI18n;
$m1->attributes=$_POST['SectionI18n'][$i];
$my[]=$m1->attributes;
}
}
and some layout:
<?php foreach ($model2 as $i=>$m1):?>
<?php ?>
<div class="row">
<?php echo $form->labelEx($model2,'Название'); ?>
<?php echo $form->textField($model2,'[$i]title',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model2,'title'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model2,'Описание'); ?>
<?php echo $form->textArea($model2,'[$i]description',array('size'=>60,'maxlength'=>200)); ?>
<?php echo $form->error($model2,'description'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model2,'Язык'); ?>
<?php echo $form->dropDownList($model2,'[$i]lang',array('ru'=>'ru','en'=>'en','ua'=>'ua')); ?>
<?php echo $form->error($model2,'lang'); ?>
</div>
<?php ?>
<?php endforeach;?>
Help please
It would be something similar to this:
foreach ( $_POST[Model Name] as $i => $y ){
$var name[$i] = new Model Name;
$var name[$i]->title = $_POST[Model Name][$i][title]
...
}
var_dump the post ($_POST), object makes it easier to see.

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