adding attributes Html::img yii2 - php

I add images to the page, to which I add some attributes
<?php
$data = [
[
'data-z-index' => 1,
'data-width' => 300,
]
];
?>
<?php foreach ($posts as $i => $item) { ?>
<div class="item">
<?php if ($item->img) { ?>
<?= Html::img($item->img->getUrl(), $data[$i]) ?>
<?php } ?>
</div>
<?php } ?>
As a result, on the page all this works for me and I get
<img src="//test.loc/storage/posts-image/1-2.jpg" alt="" data-z-index="1" data-width="300">
Now I also want to add an alt attribute that will come from the database
<?= Html::img($item->img->getUrl(), [$data[$i], 'alt' => $item->img_alt]) ?>
But now the attribute formatting is changing and 0 appears at the beginning
<img src="//test.loc/storage/posts-image/1-2.jpg" alt="post1" 0-data-z-index="1" 0-data-width="300">
What could be the problem?

It's because $data is an array. So you have a nested array as options.
Try to merge the arrays:
array_merge($data[$i], ['alt' => $item->img_alt]);

Related

How to display comma in PHP array?

I am setting this array, and for the most part it works properly in my site. NOTE: This is a Joomla site and a VirtueMart PHP file for the email that the customer receives when ordering a product.
$addressBTOrder = array('email{br}', 'company{br}', 'title', 'first_name', 'last_name{br}', 'address_1{br}', 'address_2{br}', 'city', 'virtuemart_state_id', 'zip{br}', 'virtuemart_country_id{br}', 'phone_1{br}', 'phone_2{br}');
However, I need to add a comma (,) after CITY in the array to actually display a comma in the final output. If I just put the comma after city:
'city,'
... it does not work and the city output just doesn't display at all. I also tried the hex value for comma (%2c) but it didn't work either.
Here is the code that is creating the output:
<?php
foreach ($addressBTOrder as $fieldname) {
$fieldinfo = explode('{',$fieldname);
if (!empty($this->userfields['fields'][$fieldinfo[0]]['value'])) { ?>
<span class="values vm2<?php echo '-' . $this->userfields['fields'][$fieldinfo[0]]['name'] ?>" ><?php echo $this->escape($this->userfields['fields'][$fieldinfo[0]]['value']) ?></span> <?php
if (isset($fieldinfo[1]) && $fieldinfo[1] == 'br}') { ?>
<br class="clear" /> <?php
}
}
}
?>
What code do I need to put there to display a comma in the final output? For instance, I am making the line break work by using {br}. Thanks!
What I'm getting from your code is that you want to specify some sort of suffix after each output of the corresponding userfield value.
A differentapproach is to make use of your CSS classes. This means you can properly style your content without adding extra elements.
For example, going back to your original array without anything extra
$addressBTOrder = ['email', 'company', 'title', 'city', ...];
foreach ($addressBTOrder as $field) :
$userField = $this->userfields['fields'][$field];
if (!empty($userField['value'])) : ?>
<span class="values vm2-<?= $userField['name'] ?>">
<?= $this->escape($userField['value']) ?>
</span>
<?php endif; endforeach ?>
and, since you're already adding CSS classes which I'm guessing are like vm2-email, vm2-company, etc...
.vm2-email, .vm2-company {
display: block; /* same as adding a newline */
}
.vm2-city:after {
content: ",";
}
Original answer here...
I would recommend using a more succinct data format. For example
$br = '<br class="clear" />';
$addressBTOrder = [[
'key' => 'email',
'suffix' => $br
], [
'key' => 'company',
'suffix' => $br
], [
'key' => 'title',
'suffix' => ''
], /* etc */ [
'key' => 'city',
'suffix' => ','
]];
then you can iterate like this...
<?php foreach ($addressBTOrder as $field) :
$userField = $this->userfields['fields'][$field['key']];
if (!empty($userField['value'])) : ?>
<span class="values vm2-<?= $userField['name'] ?>">
<?= $this->escape($userField['value']), $field['suffix'] ?>
</span>
<?php endif; endforeach ?>
It's more clean and easy to design the view exactly you want instead of try to do this with code. For example something like this:
<?php if(!empty($this->userfields['fields']['email']['value'])): ?>
<span class="values vm2-email">
<?= $this->escape($this->userfields['fields']['email']['value']) ?></span>
</span>
<br class="clear" />
<?php endif; ?>
<?php if(!empty($this->userfields['fields']['city']['value'])): ?>
<span class="values vm2-city">
<?= $this->escape($this->userfields['fields']['city']['value']) ?>, </span>
</span>
<?php endif; ?>
<?php if(!empty($this->userfields['fields']['virtuemart_state_id']['value'])): ?>
<span class="values vm2-virtuemart_state_id">
<?= $this->escape($this->userfields['fields']['virtuemart_state_id']['value']) ?></span>
</span>
<?php endif; ?>
The problem is that you're expecting everything before { in the array element to be a key in the $this->userfields['fields'] array. But when you have a comma there, it doesn't match the array keys. So you need to remove the comma before using it as a key. And if it has a comma, you need to add that to the output.
foreach ($addressBTOrder as $fieldname) {
$fieldinfo = explode('{',$fieldname);
$name = $fieldinfo[0];
if ($name[-1] == ",") {
$comma = ",";
$name = substr($name, 0, -1);
} else {
$comma = "";
}
if (!empty($this->userfields['fields'][$name]['value'])) { ?>
<span class="values vm2<?php echo '-' . $this->userfields['fields'][$name]]['name'] ?>" ><?php echo $this->escape($this->userfields['fields'][$name]['value']) . $comma; ?></span> <?php
if (isset($fieldinfo[1]) && $fieldinfo[1] == 'br}') { ?>
<br class="clear" /> <?php
}
}
}

How to give hyperlink to items in CodeIgniter

I need give some hyperlink to my items category in codeigniter.
$this->data['itemdata'] = array(
array(
'title' => 'Printers / Accessories',
'items' => ['Printers', 'Printer Cartridges', 'Compatible Toners'],
'brands' => ['hp', 'cannon', 'brother', 'toshiba', 'sharp']
),
how can I create it?
Updated
view file
<div class="items">
<ul class="floated">
<?php foreach ($item['items'] as $key => $value): ?>
<li><?php echo $value; ?></li>
<?php endforeach; ?>
</ul>
</div>
You have to do it something like below:-
<li><?php echo $value;?></li>
Note:- I hope through URL you want to send user to some fruitful link. So change controller/function/uri to corresponding values.
Reference:-CodeIgniter - Correct way to link to another page in a view
Note:- if you want to remove link then do like below:-
<li><?php echo $value;?></li>

Unset first element of array that don't know first item's key value

I'm getting list of image using foreach, but I don't want to display first image of array, but I don't know how to make it works. Here my code:
$portfolio_gallery_image = get_post_meta( get_the_ID(), "portfolio_gallery_image", true );
<div class="folio-gallery grid-masonry clearfix">
<?php
foreach ( $portfolio_gallery_image as $key => $image ){
?>
<a class="folio-item col-3 ndSvgFill grid-item" href="<?php echo esc_url($image) ?>">
<img src="<?php echo esc_attr($image) ?>" alt="portfolio">
</a>
<?php } ?>
</div>
This is array
Array
(
[2098] => http://dione.thememove.com/wp-content/uploads/2016/04/11.jpg
[2097] => http://dione.thememove.com/wp-content/uploads/2016/04/10.jpg
[2062] => http://dione.thememove.com/wp-content/uploads/2016/06/f_08.jpg
[2084] => http://dione.thememove.com/wp-content/uploads/2016/06/10.jpg
[2096] => http://dione.thememove.com/wp-content/uploads/2016/04/9.jpg
[2082] => http://dione.thememove.com/wp-content/uploads/2016/06/08.jpg
[2094] => http://dione.thememove.com/wp-content/uploads/2016/04/7.jpg
)
Really appreciate your help. Thank you.
You can do like this,
<?php
function custom_function(&$arr)
{
list($k) = array_keys($arr);
$r = array($k => $arr[$k]);
unset($arr[$k]);
return $r;
}
custom_function($portfolio_gallery_image);
foreach ($portfolio_gallery_image as $key => $image) {
?>
<a class="folio-item col-3 ndSvgFill grid-item" href="<?php echo esc_url($image) ?>">
<img src="<?php echo esc_attr($image) ?>" alt="portfolio">
</a>
<?php } ?>
array_shift Shift an element off the beginning of array.
Try below code to remove first key/value from array before for each in your code.
$new_image_array = array_shift($portfolio_gallery_image);

MySQL to PHP output HTML with grouped content (categories)

i have problem that i googled allot to try and figure out, but all solutions are variations on mine.
So i have output like this
<div class="row">
<?php $cat= '';?>
<div class="blank">
#foreach($posts as $post)
#if($cat!= $post->cat_id)
</div><div class="col-sm-3">
<p class="alert alert-info"><strong>{{$post->category_name}}</strong></p>
<?php $cat= $post->cat_id; ?>
#endif
</ul><ul>
<li>
{{$post->title}}
</li>
#endforeach
So basically i wont output that category name is in and then all the posts of that category within that div in ul li, and then close the DIV.
But i managed to make it only with this blank div, and closing ul and div before open, but that gives me invalid html, but its sorted good.
Is there any smart way to get this done?
Thanks
here is a working example. I think the problem was the place where you were opening and closing the foreach.
...
(I replaced the blade statements for plain php for clarity, but you should be able to change it back.)
EDIT:
I was not that far..
<?php $posts = [
['cat_id' => 1, 'cat_name' => 'a name', 'title' => 'title'],
['cat_id' => 1, 'cat_name' => 'a name2', 'title' => 'title2'],
['cat_id' => 2, 'cat_name' => 'another name', 'title' => 'title3'],
['cat_id' => 2, 'cat_name' => 'another name2', 'title' => 'title4'],
]; ?>
<div class="col-sm-3">
<?php $cat= '';?>
<?php foreach($posts as $post): ?>
<?php if($post['cat_id'] != $cat): ?>
<p class="alert alert-info">
<strong><?php echo $post['cat_name'];?></strong>
</p>
<ul>
<?php endif; ?>
<li> <?=$post['cat_name'];?> </li>
<?php if($post['cat_id'] == $cat): ?>
</ul>
<?php endif; ?>
<?php $cat= $post['cat_id']; ?>
<?php endforeach; ?>
</div>
Here I edited my previous answer, a few recommendations tho:
The point is not to just write code that works, but also maintainable and readable. That will help you or whoever has to change some functionality in the future.
Try to be as clear as you can be when asking, and provide an example of your desired output if needed (just like in the comment).
You could probably format the data before passing it to the view, so you can print it easily, for example, transforming the array I used in the above code to something like:
$posts = [
['cat_id' => 1, 'cat_name' => 'a name', 'posts' => [
['title' => 'a title','content' => 'whatever'],
['title' => 'another title','content' => 'whatever2'],
]
];
That way it would be waaay easier to show it, and it'll give you ways to
write clearer code.
Best regards!
So after extensive trying, i got what i wanted. Hope this is correct way of doing it.
<?php
$cat= '';
foreach($posts as $post){
if($cat != $post['cat_id']){
echo $cat!= '' ? '</ul></div>' : '';
echo '<div class="col-sm-4 col-md-3">
<p class="alert alert-info"><strong>'.$post['cat_name'].'</strong></p>
<ul>';
$cat= $post['cat_id'];
}
echo '<li>'.$post['title'].'</li>';
}
echo '</ul>'; //close last opened ul
echo '</div>'; //close last opened div
?>

How to get in php foreach value from other array?

I get images in a array then i read it with a foreach this are images, but how can i get also image text, my example is here:
$slideImages = array('image/path/image1', 'image/path/image2', 'image/path/image13');
$slideText = array('image text 1', 'image text 2', 'image text 3');
if($slideImages) :
echo '<ul>';
foreach($slideImages as $slideImage) :
?>
<li>
<div>IMAGE TEXT</div>
<img src="<?php echo $slideImage; ?>" />
</li>
<?php
endforeach;
echo '</ul>';
endif;
I need to embed also image text from the true position, i know how to do it with for but so is not efficient... it's something like this possible?
Assuming the keys are the same, use the $key in the foreach:
foreach($slideImages as $key => $slideImage) :
?>
<li>
<div>IMAGE TEXT</div>
<img src="<?php echo $slideImage; ?>" />
<?php echo $slideText[$key]; ?>
</li>
<?php
endforeach;
You could also change the way you store the info and keep them in one multidimensional array.
$slides = array(
array( image => "image/path/image1", text => "image text 1"),
array( image => "image/path/image2", text => "image text 2"),
array( image => "image/path/image3", text => "image text 3")
);
Then you would iterate through the $slides array and just pull out the image and text for each:
foreach($slides as $slide)
{
echo "Text:".$slide["text"];
echo "Image Path:".$slide["image"];
}
This organizes your code a little better so you don't wind up with something crazy going on should your two arrays wind up out of sync.
simplified example:
foreach($slideImages as $key=>$var){
echo $var;
echo $slideText[$key];
}

Categories