I have a question:
I make a pre for news:
Array
(
[0] => Array
(
[name] => tag
[id] => 57
[title] => Article1,Article2,Article3
[views] => 53,54,58
[smallimage] => Koala-08.jpg,Jellyfish-08.jpg,Mountain-08.jpg
[date] => 2014-05-07 09:21:58,2014-05-08 09:24:38,2014-05-08 14:36:40
)
)
How to create the foreach in view to show 1 title 1 views and 1 date...I create an foreach but show first all titles,all views,all dates;
My foreach:
<?php if ($news):?>
<?php foreach($news as $n):?>
<p>Tag:<?php echo $n['name'] ?></p>
<div class="container">
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo $n['smallimage']; ?>" alt="">
<?php echo $n['title'] ?><br/>
<?php echo $n['views'] ?>
<?php endforeach ?>
<?php endif; ?>
With this foreach I get: Article1,Article2,Article3 53,54,58...I want to get Article1 53,Article2 54, Article3 58....Help me please
Try this:
<?php
$news = array(array( "tag",
57,
"Article1,Article2,Article3",
"53,54,58",
"Koala-08.jpg,Jellyfish-08.jpg,Mountain-08.jpg",
"2014-05-07 09:21:58,2014-05-08 09:24:38,2014-05-08 14:36:40"
) );
?>
<?php if ($news){?>
<p>Tag:<?php echo $news[0][0]; ?></p>
<div class="container">
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo $news[0][4]; ?>" alt="">
<?php $views = explode(',',$news[0][3]);?>
<?php $article = explode(',',$news[0][2]);?>
<?php for($i=0;$i<count($article);$i++){
echo $article[$i].'-'.$views[$i];
} ?>
</div>
<?php }?>
I don't know where is "config_item('content_folder')" function result.Try this. And let me know whether it is works or not. Feel free to ask help.
<?php foreach($news as $n):?>
<?php $titles = explode(",", $n['title']); ?>
<?php $smallimages = explode(",", $n['smallimage']); ?>
<?php $views= explode(",", $n['views']); ?>
<p>Tag:<?php echo $n['name'] ?></p>
<div class="container">
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo smallimages[0]; ?>" alt="">
<?php echo title[0] ?><br/>
<?php echo views[0] ?>
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo smallimages[1]; ?>" alt="">
<?php echo title[1] ?><br/>
<?php echo views[1] ?>
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo smallimages[2]; ?>" alt="">
<?php echo title[2] ?><br/>
<?php echo views[2] ?>
<?php endforeach ?>
<?php endif; ?>
I am assuming that for example 'Article1,Article2,Article3' is a string, so you would have to use:
$n['title'] = explode(',',$n['title'])
To make it into an array. Same goes for views, smallimage and date. Here's an example on how to apply that:
<p>Tag:<?php echo $n['name'] ?></p>
<div id="container">
<?php
$n['title'] = explode(',',$n['title'])
$n['views'] = explode(',',$n['views'])
$n['smallimage'] = explode(',',$n['smallimage'])
foreach ($n['title'] as $key=>$value) {
echo "<img src='" . config_item('content_folder') . "news/small" . $n['smallimage'][$key] . "' alt='' />";
echo $n['title'][$key] . "<br />";
echo $n['views'][$key]
}
?>
</div>
You would place this in the existing foreach-loop.
Related
Sorry I feel it an idiot question, but I can't fix it, those characters "> appeared beside the post thumbnail, and I don't know what's wrong there!
<?php
$notification_id = $notification->ID;
$notification_time = strtotime($notification->post_date);
$type = get_post_meta($notification_id, 'dw_notification_type', true);
$custom_type = get_post_meta($notification_id, 'dw_notification_custom_type', true);
$image = get_post_meta($notification_id, 'dw_notification_image', true);
?>
<li id="dw-notif-<?php echo $notification_id;?>" class="<?php echo dwnotif_check_user_read(false, $notification_id)?'read':'unread'; ?> <?php echo $type?$type:''; ?> <?php echo $custom_type?$custom_type:''; ?>" data-time="<?php echo $notification_time;?>">
<a href="<?php echo get_permalink($notification->ID);?>">
<div class="notif-avatar">
<?php if(has_post_thumbnail($notification->ID)): ?>
<?php echo get_the_post_thumbnail($notification->ID, 'post-thumbnail'); ?>
<?php elseif($image): ?>
<img src="<?php echo $image; ?>">
<?php else: ?>
<img src="<?php echo dwnotif_default_image(); ?>">
<?php endif; ?>
</div>
<div><?php echo $notification->post_title; ?></div>
<date class="notif-time"><?php echo human_time_diff( $notification_time, current_time('timestamp') ) . __( ' ago', 'dw-notifications' ); ?></date>
</a>
</li>
I'm triying to retrieve some data from a json file, in this case the json file have a child call "photo" and that child storage several file names, I need to fetch those file names inside an outer foreach. Here is my code:
My JSON file looks like this:
{
"nigiri": [{
"code": "NS-1",
"title": "Maguro",
"description": "6pc tuna",
"price": "$10.00",
"photo": ["HD-21-a.jpg", "HD-21-b.jpg", "HD-21-c.jpg", "HD-21-c.jpg"]
}, {
"title": "Scottish",
"code": "NS-2",
"price": "$9.50",
"photo": ["HD-21-a.jpg", "HD-21-b.jpg", "HD-21-c.jpg", "HD-21-c.jpg"],
"description": "6pc salmon"
}, {
"title": "Buri",
"code": "NS-3",
"price": "$10.00",
"photo": "NS-3.jpg",
"description": "6pc Hamachi"
}]
}
And my PHP file looks like this:
<?php
$getfile = file_get_contents('backend/menu.json');
$jsonfile = json_decode($getfile);
$type = htmlentities($_GET["type"]);
if(empty($_GET['type']))
{
header('Location: index.php');
exit;
}
?>
// This way I only get the first value
<?php foreach ($jsonfile->$type as $index => $obj): ?>
<h2>Name of the plate: <?php echo $obj->title; ?></h2>
<img src="img/plates/thumbs/<?php echo $obj->photo; ?>" alt="<?php echo $obj->title; ?>">
<?php endforeach; ?>
// I want to retrieve the others values from the "photo" child but still inside the previous foreach. I've try this:
<?php foreach ($jsonfile->$type as $index => $obj): ?>
<h2>Name of the plate: <?php echo $obj->title; ?></h2>
<?php foreach ($jsonfile->$type->photo as $index => $photos): ?>
<img src="img/plates/thumbs/<?php echo $photos->photo; ?>" alt="<?php echo $obj->title; ?>">
<?php endforeach; ?>
Sometimes it's best just to stay in PHP rather than going in and out of HTML:
foreach ($jsonfile->$type as $obj) {
echo "<h2>Name of the plate: $obj->title</h2>\n";
$photos = is_array($obj->photo) ? $obj->photo : array($obj->photo);
foreach ($photos as $photo)
echo "<img src=\"img/plates/thumbs/$photo\" alt=\"$obj->title\">\n";
}
Note that since $photo may not be an array, we need to check first before we attempt to iterate it, and if it isn't, make it into one.
Output (for your sample data, with $type = "nigiri"):
<h2>Name of the plate: Maguro</h2>
<img src="img/plates/thumbs/HD-21-a.jpg" alt="Maguro">
<img src="img/plates/thumbs/HD-21-b.jpg" alt="Maguro">
<img src="img/plates/thumbs/HD-21-c.jpg" alt="Maguro">
<img src="img/plates/thumbs/HD-21-c.jpg" alt="Maguro">
<h2>Name of the plate: Scottish</h2>
<img src="img/plates/thumbs/HD-21-a.jpg" alt="Scottish">
<img src="img/plates/thumbs/HD-21-b.jpg" alt="Scottish">
<img src="img/plates/thumbs/HD-21-c.jpg" alt="Scottish">
<img src="img/plates/thumbs/HD-21-c.jpg" alt="Scottish">
<h2>Name of the plate: Buri</h2>
<img src="img/plates/thumbs/NS-3.jpg" alt="Buri">
Your mean is you want output photo if you have a lot photos?
<?php foreach ($jsonfile->$type as $index => $obj): ?>
<h2>Name of the plate: <?php echo $obj->title; ?></h2>
<?php if (is_array($obj->photo)): ?>
<?php foreach ($jsonfile->$type->photo as $index => $photos): ?>
<img src="img/plates/thumbs/<?php echo $photos->photo; ?>" alt="<?php echo $obj->title; ?>">
<?php endforeach; ?>
<?php else ?>
<img src="img/plates/thumbs/<?php echo $obj->photo; ?>" alt="<?php echo $obj->title; ?>">
<?php endif; ?>
<?php endforeach; ?>
You must use nested loops. Something like that:
<?php
$getfile = file_get_contents('backend/menu.json');
$jsonfile = json_decode($getfile);
$type = htmlentities($_GET["type"]);
if(empty($_GET['type']))
{
header('Location: index.php');
exit;
}
?>
<?php foreach ($jsonfile->$type as $index => $obj): ?>
<h2>Name of the plate: <?php echo $obj->title; ?></h2>
<?php if(is_array($obj->photo)): ?>
<?php foreach ($obj->photo as $photo): ?>
<img src="img/plates/thumbs/<?php echo $photo; ?>" alt="<?php echo $obj->title; ?>">
<?php endforeach; ?>
<?php else: ?>
<img src="img/plates/thumbs/<?php echo $obj->photo; ?>" alt="<?php echo $obj->title; ?>">
<?php endif; ?>
<?php endforeach; ?>
I have for each loop return an array
<?php foreach( get_uf_repeater( 'clients' ) as $document_files ): extract( $document_files ) ?>
<div class="ui-grid-a my-breakpoint">
<div class="ui-block-a"><img src="<?php echo $project_image ?>" title="Project image" /><div><?php echo $project_detailes ?></div></div>
<div class="ui-block-b"><img src="<?php echo $project_image ?>" title="Project image" /><div><?php echo $project_detailes ?></div></div>
</div>
<?php endforeach ?>
how can i jump to the next array for the second line "" instead of displaying the same of the first one, I assume I need nested loop but didnt know how do it exactly.
You could use array_chunk
<?php foreach (array_chunk(get_uf_repeater( 'clients' ), 2, true) as $array) { ?>
<div class="ui-grid-a my-breakpoint">
<?php foreach($array as $document_files) { ?>
<?php extract( $document_files ); ?>
<div class="ui-block-a"><img src="<?php echo $project_image ?>" title="Project image" /><div><?php echo $project_detailes ?></div></div>
<?php } ?>
</div>
<?php } ?>
This is a very difficult question to ask but, lets try. I am using this module for joomla i need tocreate an overide for it so that it takes the first result of
<?php foreach( $pages as $key => $list ): ?>
and places it in its own div and carries on with the statement. Please ask if any other information is needed here's the entire code for the override:
<?php
/**
* #package mod_bt_contentslider - BT ContentSlider Module
* #version 1.4
* #created Oct 2011
* #author BowThemes
* #email support#bowthems.com
* #website http://bowthemes.com
* #support Forum - http://bowthemes.com/forum/
* #copyright Copyright (C) 2012 Bowthemes. All rights reserved.
* #license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
*
*/
// no direct access
defined('_JEXEC') or die('Restricted access');
if($modal){JHTML::_('behavior.modal');}
$document = JFactory::getDocument();
if(count($list)>0){?>
<div id="btcontentslider<?php echo $module->id; ?>" style="display:none;" class="span4 bt-cs<?php echo $moduleclass_sfx? ' bt-cs'.$params->get('moduleclass_sfx'):'';?>">
<!--CONTENT-->
<div class="slides_container" style="width:<?php echo $moduleWidth.";".$add_style;?>">
<?php foreach( $pages as $key => $list ): ?>
<div class="slide" style="width:100%;">
<?php foreach( $list as $i => $row ): ?>
<ul class="bt-row <?php if($i==0) echo 'bt-row-first'; else if($i==count($list)-1) echo 'bt-row-last' ?>" style="width:100%" >
<li class="bt-inner">
<?php if( $row->thumbnail && $align_image != "center"): ?>
<a target="<?php echo $openTarget; ?>" class="bt-image-link<?php echo $modal? ' modal':''?>" title="<?php echo $row->title;?>" href="<?php echo $modal?$row->mainImage:$row->link;?>">
<img <?php echo $imgClass ?> src="<?php echo $row->thumbnail; ?>" alt="<?php echo $row->title?>" style="width:<?php echo $thumbWidth ;?>px; float:<?php echo $align_image;?>;margin-<?php echo $align_image=="left"? "right":"left";?>:5px" title="<?php echo $row->title?>" />
</a>
<?php endif; ?>
<?php if( $showTitle ): ?>
<a class="bt-title" target="<?php echo $openTarget; ?>"
title="<?php echo $row->title; ?>"
href="<?php echo $row->link;?>"> <?php echo $row->title_cut; ?> </a><br />
<?php endif; ?>
<?php if( $row->thumbnail && $align_image == "center" ): ?>
<div class="bt-center">
<a target="<?php echo $openTarget; ?>"
class="bt-image-link<?php echo $modal? ' modal':''?>"
title="<?php echo $row->title;?>" href="<?php echo $modal?$row->mainImage:$row->link;?>">
<img <?php echo $imgClass ?> src="<?php echo $row->thumbnail; ?>" alt="<?php echo $row->title?>" style="width:<?php echo $thumbWidth ;?>px;" title="<?php echo $row->title?>" />
</a>
</div>
<?php endif ; ?>
<?php if( $show_intro ): ?>
<div class="bt-introtext">
<?php echo $row->description; ?>
</div>
<?php endif; ?>
<?php if( $showReadmore ) : ?>
<p class="readmore">
<a target="<?php echo $openTarget; ?>"
title="<?php echo $row->title;?>"
href="<?php echo $row->link;?>"> <?php echo JText::_('READ_MORE');?>
</a>
</p>
<?php endif; ?>
</li>
<!--end bt-inner -->
</ul>
<!--end bt-row -->
<!--CONTENT-->
<?php endforeach; ?>
<div style="clear: both;"></div>
</div>
<!--end bt-main-item page -->
<?php endforeach; ?>
</div>
<?php if( $next_back && $totalPages > 1 ) : ?>
<a class="prev" href="#"></a><a class="next" href="#"></a>
<?php endif; ?>
</div>
<!--end bt-container -->
<div style="clear: both;"></div>
<script type="text/javascript">
if(typeof(btcModuleIds)=='undefined'){var btcModuleIds = new Array();var btcModuleOpts = new Array();}
btcModuleIds.push(<?php echo $module->id; ?>);
btcModuleOpts.push({
slideEasing : '<?php echo $slideEasing; ?>',
fadeEasing : '<?php echo $slideEasing; ?>',
effect: '<?php echo $effect; ?>',
preloadImage: '<?php echo $preloadImg; ?>',
generatePagination: <?php echo $paging ?>,
play: <?php echo $play; ?>,
hoverPause: <?php echo $hoverPause; ?>,
slideSpeed : <?php echo $duration; ?>,
autoHeight:<?php echo $autoHeight ?>,
fadeSpeed : <?php echo $fadeSpeed ?>,
equalHeight:<?php echo $equalHeight; ?>,
width: <?php echo $moduleWidth=='auto'? "'auto'":$params->get( 'module_width', 0 ); ?>,
height: <?php echo $moduleHeight=='auto'? "'auto'":$params->get( 'module_height', 0 ); ?>,
pause: 100,
preload: true,
paginationClass: '<?php echo $butlet==1 ? 'bt_handles': 'bt_handles_num' ?>',
generateNextPrev:false,
prependPagination:true,
touchScreen:<?php echo $touchScreen ?>
});
</script>
<?php
// set position for bullet
if($butlet) {
$nav_top = (-1)*(int)$params->get( 'navigation_top', 0 );
$nav_right = (-1)*(int)$params->get( 'navigation_right', 0 )+5;
if(trim($params->get('content_title'))) $nav_top += 13;
$document->addStyleDeclaration(
$modid . ' ' . ($butlet == 1 ? '.bt_handles' : '.bt_handles_num') . '{'.
'bottom: ' . '5% !important;'.
'right: ' . '50% !important'.
'}'
);
}
// set responsive for mobile device
if($moduleWidth=='auto'){
$document->addStyleDeclaration(
'
#media screen and (max-width: 480px){.bt-cs .bt-row{width:100%!important;}}'
);
}
}
else
{
echo '<div>No result...</div>';
} ?>
Any Help Greatly apreciated.
you can do something like this. basicly, set a boolean to only do something different to the first item:
<?php $first = true; ?>
<?php foreach( $pages as $key => $list ): ?>
<?php
if($first) {
/* put this $list in its own div or whatever you need to do */
$first = false;
} else {
... //the default operation/code
?>
...
<?php } ?>
<?php endforeach; ?>
Complete Edit:
Use a counter to know which iteration it is:
<?php $c = 0; // initialize counter
foreach( $pages as $key => $list ):
if ($c++ == 0):
?>
this section will only run for the first element.
<?php endif;?>
so i guess this is pretty easy for most of you, but i can't figure this out.
im trying to make the links dynamic eg: href="linkname(#1 or #2 etc)"
any ideas?
<?php if ($top_fundraisers && is_array($top_fundraisers)): ?>
<?php foreach ($top_fundraisers as $index => $fundraiser): ?>
<a title="" class="fancybox" href="linkname(GENERATE CODE HERE)">
<div class="top-fundraiser">
<div id="newo<?php print htmlentities($index + 1); ?>" class="top-fundraiser-image">
<img src="<?php
if($fundraiser['member_pic_medium']) {
print htmlentities($fundraiser['member_pic_medium']);
} else {
print $template_dir . '/images/portrait_placeholder.png';
}
?>"/>
</div>
</div>
</a>
<?php endforeach;?>
<?php endif; ?>
Suppose below is what you need.
<?php if ($top_fundraisers && is_array($top_fundraisers)): ?>
<?php foreach ($top_fundraisers as $index => $fundraiser): ?>
<a title="" class="fancybox" href="linkname(#<?php echo $index + 1; ?>)">
<div class="top-fundraiser">
<div id="newo<?php print htmlentities($index + 1); ?>" class="top-fundraiser-image">
<img src="<?php
if($fundraiser['member_pic_medium']) {
print htmlentities($fundraiser['member_pic_medium']);
} else {
print $template_dir . '/images/portrait_placeholder.png';
}
?>"/>
</div>
</div>
</a>
<?php endforeach;?>
<?php endif; ?>