2

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>   
6
  • ?? missing "." after $image = "<img src=" Commented Feb 15, 2014 at 13:36
  • Would you mind elaborating on that? Commented Feb 15, 2014 at 13:36
  • so does it work as $image = "<img src=" . <?php echo base_url() . "assets/" . "menuicon.png" /> Commented Feb 15, 2014 at 13:37
  • 1
    If you have a problem with quotation marks, then why don't you simply look at the finished html markup you output? Commented Feb 15, 2014 at 13:37
  • arkascha, the page just goes blank and doesn't load otherwise I would. Commented Feb 15, 2014 at 13:39

4 Answers 4

2

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>"; 
    } 
}

?>
Sign up to request clarification or add additional context in comments.

1 Comment

$image = "<img src=". base_url() . "assets/" . "menuicon.png />" = PHP Parse error: syntax error, unexpected end of file
1

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>"; 
    } 
}

Comments

0

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!

Comments

0

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.

1 Comment

Thank you man. I really appreciate your help. Will keep it in mind in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.