I am trying to assign an image src to a variable, that doesn't seem to work.
I am pretty sure I am missing a "
or a '
but I have just about tried every combination now.
The second issue I have is that I want to use two variables and concatenate doesn't seem to work.
see below: $item.$image
Any help would be appreciated.
<li class="first">
<?php
$image = "<img src="<?php echo base_url() . "assets/" . "menuicon.png" />
if (!empty($pcategory))
{
foreach ($pcategory as $key => $item)
{
echo "<li><a href='" . site_url() . "cat/$key'>$item.$image</a></li>";
}
}
?>
</li>
Thank you Hett and Fabio. Both of you pointed me in the right direction but it didn't work.
Here is the right solution:
<li class="first">
<?php
$image = '<img src="' . base_url() . 'assets/menuicon.png" />';
if (!empty($pcategory)) {
foreach ($pcategory as $key => $item) {
echo "<li><a href='" . site_url() . "cat/$key'>$item.$image</a></li>";
}
}
?>
</li>
I appreciate your help, wouldn't have figured it out without you guys. Thanks!
Try this
$image = '<img src="' . base_url() . '"assets/enuicon.png" />';
if (!empty($pcategory))
{
$site_url = site_url();
foreach ($pcategory as $key => $item) {
echo "<li><a href='{$site_url}cat/{$key}'>{$item}{$image}</a></li>";
}
}
You are opening php tag twice, one inside the other, you should instead concatenate them as follow
<?php
$image = '<img src="'. base_url() . 'assets/menuicon.png" />';
//^ here you need to concatenate
if (!empty($pcategory)) {
foreach ($pcategory as $key => $item) {
echo "<li><a href='" . site_url() . "cat/$key'>$item.$image</a></li>";
}
}
?>
You open the PHP tag twice in a row:
<?php // HERE
$image = "<img src="<?php echo base_url() . "assets/" . "menuicon.png" /> // HERE
Looks like you've been trying to do this in HTML then switched to it all being in PHP (at a guess).
You should always check your PHP error log file, as it's invaluable as a coder - However, it wont produce any error in this case as PHP will simply think you're stating the image is at <?php echo base_url() . "assets/" . "menuicon.png
, which of course it wont be.
You should also be checking (among other things, like your browser source code/output)) your browser page info. This shows you media and images, and you would have seen in the media tab that the link location to this image was <?php echo base_url() . "assets/" . "menuicon.png
, and would have lead you to the issue.
This is the code you need:
<li class="first">
<?php
$image = "<img src='".base_url() . "assets/menuicon.png' />";
if (!empty($pcategory))
{
foreach ($pcategory as $key => $item)
{
echo "<li><a href='" . site_url() . "cat/$key'>$item.$image</a></li>";
}
}
?>
</li>
You don't need to break in and out of the $image
code as much as you did.
i.e. assets and /menuicon.png
are both HTML and not PHP, so no need to break in/out to PHP.