0

How to concatenate string in image source. I try to set base path dynamic in image source but it does not work. I am also using in fore each loop. any body can help me, give me some suggestion. I am new in php.

Here is my php code-

$base_path  =   'https://odesk.com';

$listing = '';
$count=1;
if(sizeOf($entry['contents'])>0){   
foreach($entry['contents'] as $child) {
    $count++;
    $cp = $child['path'];
    $cn = basename($cp);
    $image='';

    $cp = htmlspecialchars($cp);
    $link = getPath("?path=".htmlspecialchars($cp));

    if ($child['is_dir']) 
    {

    $image ='<img src="'@$base_path'/images/folder.png" width="20">';// i want here add base path
    $cn .= '/';
    }else{
    $image ='<input type="checkbox" value="'.$child['rev'].'" id="'.$child['rev'].'" name="checkbox_value[]" onclick="saveimages('.@$_SESSION['products_id'].',`'.@$path.'`,`'.@$cn.'`,this.value,event)" class="checked">&nbsp;<img src="https://odesk.com/images/image_dropbox.png" width="15">';
    }


    if($count%2==0){
    $listing .= "<div class='white'>$image&nbsp;<a id='a1' style='text-decoration: none;vertical-align:super;color:black;' href='$link'>$cn</a></div>";
    }else{
    $listing .= "<div class='white'>$image&nbsp;<a id='a1' style='text-decoration: none;vertical-align:super;color:black;' href='$link'>$cn</a></div>";
    }

}
}else{
    $listing = "<div align='center' class='white'><span style='color:red; text-align:center;'>No folders & files are available.</span></div>";
}

3 Answers 3

2

If you want to parse variables in your strings you have to use double quotes. This is described in the Variable parsing section in the Strings documentation.

The following code will work:

$image = "<img src='$base_path/images/folder.png' width='20'>";
Sign up to request clarification or add additional context in comments.

Comments

1

We use . for concatenating values in PHP.

Replace following line

$image ='<img src="'@$base_path'/images/folder.png" width="20">';/

with

$image ='<img src="'.$base_path.'/images/folder.png" width="20">';/

Comments

0

You should go for sprintf http://php.net/manual/en/function.sprintf.php

$image = sprintf('<img src="%s/images/folder.png" width="20">', $base_path);

Comments

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.