Working on a project, am pretty new to PHP and don't know how to do this in a better way.
$item_thumbnails = array(
"https://brkcdn.com/v2/images/shop/thumbnails/f7dd8e95-551f-5e1f-92f1-2ea6c29e61ca.png",
"https://brkcdn.com/v2/images/shop/thumbnails/9fd1ea9d-cfd0-525a-963f-0c3438c5dc34.png"
);
$item_names = array(
"brick-luke's Fedora",
"Golden Bok"
);
$item_urls = array(
"https://www.brick-hill.com/shop/20/",
"https://www.brick-hill.com/shop/10267/"
);
$item_values = array(
"30.000",
"100.000"
);
$item_demands = array(
"High",
"Low"
);
foreach ($item_urls as $key => $item_url) {
foreach ($item_thumbnails as $key => $item_thumbnail) {}
foreach ($item_names as $key => $item_name) {}
foreach ($item_values as $key => $item_value) {}
foreach ($item_demands as $key => $item_demand) {}
echo ('
<div class="card">
<a class="image" href="'.$item_url.'">
<img src="'.$item_thumbnail.'" onerror="this.onerror=null;this.src="'.$storageUrl.'/error.png"">
</a>
<div class="content">
<a class="header">'.$item_name.'</a>
<div class="ui divider"></div>
Value: <div class="ui right floated red horizontal label">'.$item_value.'</div>
<div class="ui divider"></div>
Demand: <div class="ui right floated green horizontal label">'.$item_demand.'</div>
</div>
</div>
');
}
It only shows the latest item in the array on all tabs (if there are more).
How to fix this? Help is appreciated.
Something like this should work:
$item_thumbnails = array(
"https://brkcdn.com/v2/images/shop/thumbnails/f7dd8e95-551f-5e1f-92f1-2ea6c29e61ca.png",
"https://brkcdn.com/v2/images/shop/thumbnails/9fd1ea9d-cfd0-525a-963f-0c3438c5dc34.png"
);
$item_names = array(
"brick-luke's Fedora",
"Golden Bok"
);
$item_urls = array(
"https://www.brick-hill.com/shop/20/",
"https://www.brick-hill.com/shop/10267/"
);
$item_values = array(
"30.000",
"100.000"
);
$item_demands = array(
"High",
"Low"
);
// iterate over one array and get values from
// other arrays under the same key `$key`
foreach ($item_urls as $key => $item_url) {
echo ('
<div class="card">
<a class="image" href="'.$item_url.'">
<img src="' . $item_thumbnails[$key] . '" onerror="this.onerror=null;this.src="'.$storageUrl.'/error.png"">
</a>
<div class="content">
<a class="header">' . $item_names[$key] . '</a>
<div class="ui divider"></div>
Value: <div class="ui right floated red horizontal label">' . $item_values[$key] . '</div>
<div class="ui divider"></div>
Demand: <div class="ui right floated green horizontal label">' . $item_demands[$key] . '</div>
</div>
</div>
');
}
First of all you should improve your array structure.
$items = array(
[
"thumbnail" => "https://brkcdn.com/v2/images/shop/thumbnails/f7dd8e95-551f-5e1f-92f1-2ea6c29e61ca.png"
"name" => "brick-luke's Fedora",
"url" => "https://www.brick-hill.com/shop/20/",
"value" => "30.000",
"demand" => "High"
],
[
"thumbnail" => "https://brkcdn.com/v2/images/shop/thumbnails/9fd1ea9d-cfd0-525a-963f-0c3438c5dc34.png",
"name" => "Golden Bok",
"url" => "https://www.brick-hill.com/shop/10267/",
"value" => "100.000",
"demand" => "Low"
]
);
After that you can foreach $items variable and take data from it. See below:
<?php
foreach ($items as $item) {
?>
<div class="card">
<a class="image" href="<?php echo $item['url']; ?>">
<img src="<?php echo $item['thumbnail']; ?>" onerror="this.onerror=null;this.src=<?php echo $storageUrl; ?>/error.png">
</a>
<div class="content">
<a class="header"><?php echo $item['name']; ?></a>
<div class="ui divider"></div>
Value: <div class="ui right floated red horizontal label"><?php echo $item['value']; ?></div>
<div class="ui divider"></div>
Demand: <div class="ui right floated green horizontal label"><?php echo $item['demand']; ?></div>
</div>
</div>
<?php
}
?>
Enjoy!
Related
I have a view which list multiple photo galleries.
so from the menu when I try to access the gallery view the action is
site/gallery
Now in my controller action I am trying like this to show the latest photo-gallery added as first.
This is my code in controller:
public function actionGallery($id = null)
{
//$this->layout = "#app/themes/frontend/main";
$activities = Activities::find()
->where(['status' => 1])
->orderBy(['id' => SORT_DESC])
->all();
$model = Activities::find()->where(['id' => $id])->one();
if ($id == null) {
$gallery = Gallery::find()
->where(
[
'status' => 'active',
'activity_name' => max(['id'])
]
)->all();
} else {
$gallery = Gallery::find()
->where(
['status' => 'active', 'activity_name' => $id]
)->all();
}
return $this->render(
'gallery',
[
'gallery' => $gallery,
'activities' => $activities,
'model' => $model
]
);
}
I also tried like:
$gallery = Gallery::find()->where(
[
'status' => 'active',
'activity_name' => max(['activity_name'])
]
)->all();
the code for view:
<?php
$this->title = 'gallery';
?>
<div class="container">
<div class="col-lg-2 col-md-2 col-sm-12 col-xs-12" style='background-color:#fafafa'>
<h3>Activities</h3>
<?php
foreach ($activities as $activity) {
?>
<!-- <a href="<?php // Yii::getAlias('#web');?>/site/gallery?activity_name=<?php //$Categories['id'];?>"> -->
<a href="<?php Yii::getAlias('#web');?>/site/gallery/?id=<?php echo $activity['id']; ?>">
<br><h5 class="text-center"><?php echo $activity['activity_name']; ?></h5>
</a>
<?php
}
?>
</div>
<div class="col-lg-10 col-md-10 col-sm-12 col-xs-12">
<header class="section-header">
<div>
<h3 class="text-center">
<?php echo $model['activity_name'] ?>
</h3>
<h5 class="text-center">
<?php echo $model['activity_description'] ?>
</h5>
<br>
</div>
</header>
<div id="grid-container" class="cbp-l-grid-projects">
<ul>
<?php foreach ($gallery as $galleries) {?>
<div class="cbp-item" style="width:320px;">
<a href="<?php echo Yii::getAlias('#web'); ?>/web/gallery/<?php echo $galleries['id']; ?>/<?php echo $galleries['image']; ?>" data-lightbox="portfolio" data-title="<?php echo $galleries['title']; ?>" title="Preview" class="cbp-lightbox">
<picture>
<source srcset ="<?php echo Yii::getAlias('#web'); ?>/web/gallery/<?php echo $galleries['id']; ?>/<?php echo basename($galleries['image_thumb'], '.jpg') . '.webp'; ?>" type="image/webp">
<source srcset ="<?php echo Yii::getAlias('#web'); ?>/web/gallery/<?php echo $galleries['id']; ?>/<?php echo $galleries['image_thumb']; ?>" type="image/jpeg">
<img src="<?php echo Yii::getAlias('#web'); ?>/web/gallery/<?php echo $galleries['id']; ?>/<?php echo $galleries['image_thumb']; ?>" style ="width:100%;" class="img-fluid" alt="">
</picture>
</a>
</div>
<?php
}
?>
</ul>
</div>
</div>
</div>
I think I have resolved it using the below code.
public function actionGallery($id=null) {
//$this->layout = "#app/themes/frontend/main";
$activities = Activities::find()->where(['status' => 1])->orderBy(['id'=>SORT_DESC])->all();
$model = Activities::find()->where(['id' => $id])->one();
$latestActivity = Activities::find()->max('id');
if($id==null){
$gallery = Gallery::find()->where(['status' => 'active','activity_name'=>$latestActivity])->all();
}else{
$gallery = Gallery::find()->where(['status' => 'active','activity_name'=>$id])->all();
}
return $this->render('gallery', ['gallery' => $gallery,'activities'=>$activities,'model'=>$model]);
}
I have seen similar questions but I feel mine is different, I have a 2 column page which needs 2 users per row, issue is when I have an array that has an odd length, the last element does not come out here is my logic
<?php for($i = 0; $i < count($team); $i++){
echo "Looping ".$i;
?>
<div class="row">
<?php for($z = $i; $z < 2; $z++){
echo "inner Looping ".$z?>
<div class="col s12 m6 grid">
<figure class="effect-honey">
<img src="<?php echo $team[$z]['image']; ?>" alt="<?php echo $team[$z]['fname']. ' '.$team[$z]['lname']; ?>"/>
<figcaption>
<h2><?php echo $team[$z]['fname']; ?><span><?php echo $team[$z]['lname']; ?></span> <i><?php echo $team[$z]['position']; ?></i></h2>
</figcaption>
</figure>
</div>
<?php $i = $z;} ?>
</div>
<?php } ?>
</div>
A sample of the output of an array with 3 items
Use array_chunk() on $team with 2 as the chunk size parameter. Then you can use an outer foreach() to iterate the generated pairs and an inner foreach() to display the subarrays.
Assuming you don't need to write empty html elements for the missing 2nd element in odd-count scenarios, here is an implementation (there are a few ways to do this).
Code (Demo)
$team = [
['image' => 'A.jpg', 'fname' => 'B', 'lname' => 'C', 'position' => 'D'],
['image' => 'E.jpg', 'fname' => 'F', 'lname' => 'G', 'position' => 'H'],
['image' => 'I.jpg', 'fname' => 'J', 'lname' => 'K', 'position' => 'L']
];
$pairs = array_chunk($team, 2);
foreach ($pairs as $pair) {
echo "<div class=\"row\">";
foreach ($pair as $player) {
echo "<div class=\"col s12 m6 grid\">";
echo "<figure class=\"effect-honey\">";
echo "<img src=\"{$player['image']}\" alt=\"{$player['fname']} {$player['lname']}\"/>";
echo "<figcaption>";
echo "<h2>{$player['fname']}<span>{$player['lname']}</span> <i>{$player['position']}</i></h2>";
echo "</figcaption>";
echo "</figure>";
echo "</div>";
}
echo "</div>";
}
Output:
<div class="row">
<div class="col s12 m6 grid">
<figure class="effect-honey">
<img src="A.jpg" alt="B C"/>
<figcaption>
<h2>B<span>C</span> <i>D</i></h2>
</figcaption>
</figure>
</div>
<div class="col s12 m6 grid">
<figure class="effect-honey">
<img src="E.jpg" alt="F G"/>
<figcaption>
<h2>F<span>G</span> <i>H</i></h2>
</figcaption>
</figure>
</div>
</div>
<div class="row">
<div class="col s12 m6 grid">
<figure class="effect-honey">
<img src="I.jpg" alt="J K"/>
<figcaption>
<h2>J<span>K</span> <i>L</i></h2>
</figcaption>
</figure>
</div>
</div>
If you require an empty (space-holding) portion of html when the "right-side" player is missing, please clarify your question / desired result.
I'm wondering what is the best practice to embed tags in a foreach loop?
I have an associative array which has one entry with the following keys:
$portfolio = [
'title' => '',
'technology' => '',
'description' => '',
'link' => '',
];
What I am trying to do is echo the div tag with the specific elements inside, but each div tag is then duplicated:
<div class="container">
<div class="row">
<div class="col-md-6 col-lg-4"><div class="card">\n<div class="car img
top"></div>
</div>
</div><div class="col-md-6 col-lg-4"><div class="card"><div class="car
img top"></div>
</div>
</div><div class="col-md-6 col-lg-4"><div class="card"><div class="car
img top"></div>
</div>
</div><div class="col-md-6 col-lg-4"><div class="card"><div class="car
img top"></div>
</div>
</div> </div><!-- row end -->
</div><!-- container end -->
My foreach loop is:
I have commented some out, as I'm in the process of debugging as when the page is loaded a white page appears, which the error is within the commented out code, which I will fix.
foreach($portfolio as $value){
echo '<div class="col-md-6 col-lg-4">' . '\n';
echo '<div class="card">' . '\n';
echo '<div class="car img top">';
//echo "<img src='img/" . "$value['image']'" . ">";
//echo "</div>";
//echo "<div class='card-body'>";
// echo "<div class='card-body'>";
// echo "<h3 class'card-title'><a class='text-secondary' href='#'>See Project</a></h3>";
// echo "<h6 class='card-subtitle mb-2 text-muted'>$value['description']</h6>";
echo "</div>". "\n";
echo "</div>" . "\n";
echo "</div>";
}
Any help would be appreciated, but I think my best aim is to do a multidimensional array?
Thanks,
James
If your problem is only a formatting and legibility one, try to use this syntax instead.
<?php foreach($portfolio as $value):?>
<div class="col-md-6 col-lg-4">
<div class="card">
<div class="car img top">
<img src="img/<?php echo $value['image'];?>" />
</div>";
<div class="card-body">
<div class="card-body">
<h3 class='card-title'>
<a class='text-secondary' href='<?php echo $value['link']?>'>
<?php echo $value['title']?>
</a>
</h3>
<h6 class='card-subtitle mb-2 text-muted'><?php echo $value['description']?></h6>
</div>
</div>
</div>
</div>
<?php endforeach;?>
Notice that you had mistakes on your quotes and you had forgotten the closing div tag. This format makes it easier to read and catch mistakes.
Note in my example, I assume that your $portfolio variable is a multi-dimensional array as such:
$portfolio = [
[
'title' => '',
'technology' => '',
'description' => '',
'link' => ''
],
[
'title' => '',
'technology' => '',
'description' => '',
'link' => ''
],
// etc
];
I am not able to see any changes after editing opencart 2.0.3.1, I am not getting the images and also edited the files from category.php (catlog>controller>product) and category.tpl(catlog>view>theme>*>template>product) and replaced
$data['categories'][] = array(
and
<?php if ($categories) { ?>
with
if ($result['image']) {
$image = $this->model_tool_image- >resize($result['image'], 100, 100);
} else {
$image = $this->model_tool_image- >resize('placeholder.png', 100, 100);
}
$data['categories'][] = array(
'name' => $result['name'] . ($this->config- >get('config_product_count') ? ' (' . $this->model_catalog_product- >getTotalProducts($filter_data) . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url),
'thumb' => $image
);
and
<?php if ($categories) { ?>
<div class="col-sm-12">
<div>
<h3><?php echo $text_refine; ?></h3>
</div>
<div style="text-align: center;">
<div class="row">
<?php foreach ($categories as $category) { ?>
<div class="col-sm-2 col-xs-6" style="margin-bottom: 20px;">
<img src="<?php echo $category['thumb']; ?>" alt="<?php echo $category['name']; ?>" />
<?php echo $category['name']; ?>
</div>
<?php } ?>
</div>
</div>
</div>
<?php } ?>
respectively but not able to get any thing! Help highly appreciated
Not sure how will i able to explain this, but here we go
I have an array as below:
$arr = array(
array('id' => 1,
'content' => '<h1>This is a simple Content</h1>',
'vars' => null,
'path' => 'holders/row.php',
'sub-page' => array(
'id' => 2,
'content' => '<h1>Sample Body Here</h1>',
'vars' => '{"title":"Sample Title Here"}',
'path' => 'holders/box.php',
'sub-page' => array(
'id' => 3,
'vars' => '{"title":"Sample Title NUmber 3 Here"}',
'content' => '<h1>Again Sample Body Here</h1>',
'path' => 'holders/box.php'
)
)
)
);
What i am looking for is
array should go to the deepest sub-page and json_decode the vars key and extract as variables and include the file.
Once this is done, the output is passed to the parent's content key and parse it again
This is basically for creating dynamic content
the holders/row.php contains:
<div class="row">
<?= $content; ?>
</div>
and holders/box.php contains:
<div class="box lite">
<div class="box-title">
<h4><?= $title; ?></h4>
<div class="tools">
<a href="javascript:;" class="collapse">
<i class="fa fa-chevron-up"></i>
</a>
</div>
</div>
<div class="box-body">
<?= $content; ?>
</div>
</div>
and the desired output is:
<div class="row">
<div class="box lite">
<div class="box-title">
<h4>Sample Title Here</h4>
<div class="tools">
<a href="javascript:;" class="collapse">
<i class="fa fa-chevron-up"></i>
</a>
</div>
</div>
<div class="box-body">
<h1>Again Sample Body Here</h1>
<div class="box lite">
<div class="box-title">
<h4>Sample Title NUmber 3 Here</h4>
<div class="tools">
<a href="javascript:;" class="collapse">
<i class="fa fa-chevron-up"></i>
</a>
</div>
</div>
<div class="box-body">
<h1>Again Sample Body Here</h1>
</div>
</div>
</div>
</div>
</div>
What have i tried so far
I am trying to loop around the array as below:
foreach ($arr as &$page) {
if (isset($page[$keyToParse])) {
$subMenu = $page[$keyToParse];
unset($page[$keyToParse]);
$page['content'] = $page['content'] . $this->parsePage($keyToParse, $subMenu);
return $page['content'];
} else {
$params = strlen($page['vars']) > 0 ? json_decode($page['vars'], true) : [];
$elementPath = $page['path'];
$params = array_merge($params, array('content' => $page['content']));
$page['content'] = callback(function () use ($params, $elementPath) {
extract($params);
include($elementPath);
});
return $page['content'];
}
and the function callback is as:
function callback($userfunc)
{
ob_start();
$userfunc();
$return = ob_get_contents();
ob_end_clean();
return $return;
}
Please help as i am stuck for a long time now
All you should have to do is something like this.
function renderNode($node){
ob_start();
if (isset($node["sub-page"]) && $node["sub-page"]){
$node["content"] = renderNode($node["sub-page"]);
}
extract($node);
include ($node["path"]);
return ob_get_clean();
}
and then invoke it like:
echo renderNode($arr[0]);