error while using if statement inside image in php - php

i have a codeigniter php website, in the image tag I have given an if statement to determine the source, the code is like below:
<img class="st_img" <?php if(str_replace(' ', '',$val['unique_tblkey'])=='entertainment'){?> src=" <?php echo ADMIN_IMG.strtolower(str_replace(' ', '',$val['unique_tblkey'])).'s/'.$val['image'][0]['btp_image'];?>"<?php} else { ?> src=" <?php echo ADMIN_IMG.strtolower(str_replace(' ', '',$val['unique_tblkey'])).'/'.$val['image'][0]['btp_image'];?> " <?php } ?>/>
I am getting
unexpected '}'
this error is coming although there isn't any unwanted brackets, can anyone please tell me what is wrong in my code, thanks in advance

OK, I finished rewriting it. Not completely sure if it will work, because I cannot test it.
<?php
$tableKey = strtolower(str_replace(' ', '', $val['unique_tblkey']));
$url = ADMIN_IMG .
$tableKey .
($tableKey == 'entertainment' ? 's/' : '/') .
$val['image'][0]['btp_image'];
echo '<img class="st_img" src="'.$url.'" />';
There's no repetition, multiple lines, and no mixing of PHP and HTML. This all makes for more readable code.

Related

cant print images (.svg) of folder using matrix

just print the square but not the image, dont know what is wrong, doesnt throw mistake.
Thanks
echo "<img src=\"img/$numero[$i].svg\" alt=\"$numero[$i]\" title=\"$numero[$i]\" width=\"140\" height=\"140\">\n";
Nothing wrong in your code. Check if you have correct svg image or not at particular location. (inspect using chrome developer tool)
Check using Object tag or if your browser support or not. https://www.w3schools.com/html/html5_svg.asp
I would recommend to not mix strings with code vars. Always do proper concatenation:
$str = '<img src="/img/' . $numero[$i] . '.svg"';
$str .= ' alt="' . $numero[$i] . '"';
$str .= ' title="' . $numero[$i] . '"';
$str .= ' width="140" height="140">'. "\n";
echo $str;
your code seem right, try checking if the svg image is in the correct image path you placed
i tested with this and is working well
<?php
$numero = array('imagename', 'image alt', 'title');
echo "<img src=\"Images/$numero[0].svg\" alt=\"$numero[1]\" title=\"$numero[2]\" width=\"140\" height=\"140\">\n";
?>
thank you all, I discovered that it happened
I was missing a bar in front of the img
echo "<img src=\"/img/$numero[$i].svg\" alt=\"$numero[$i]\" title=\"$numero[$i]\" width=\"140\" height=\"140\">\n";
instead of
echo "<img src=\"img/$numero[$i].svg\" alt=\"$numero[$i]\" title=\"$numero[$i]\" width=\"140\" height=\"140\">\n";
I do not see any sense

How can I echo a Link to a wp page from a specific directory?

This is the code I am working with:
echo '' . $post_type->labels->singular_name . '' .$markup;
It currently links correctly but shows the entire http string before the link.
Example: http://www.blah.com/blah/blahPortfolio. When it should be just "Portfolio".
Fresh eyes on this would be so helpful.
There are two errors that I can see:
<?php bloginfo('template_directory'); ?> If you are using echo it means <?php tag is already open so use only bloginfo('template_directory')
There are two closing tag for a tag (i.e. />) You use only one (i.e. >)
So the code will be like this:
echo '' . $post_type->labels->singular_name . '';
Try this code chunk and let me know --
echo '' . $post_type->labels->singular_name . '';

Apparent PHP Syntax Error

Basically, I've been trying to make a simple Wordpress widget that displays a QR code with the URL of the current page. I'm using a modififed version of the simple text widget that parses PHP too.
function the_qrcode($permalink = '', $title = '') {
if($permalink && $title == '') {
$permalink = 'http://eternityofgamers.com/forums';
$title = 'Forums';
}
echo '<img src="http://api.qrserver.com/v1/create-qr-code/?data=' .$permalink. '" alt="QR: ' .$title. '"/>;
}
Can someone tell me what's wrong with this? I get a 500 error when I add it to functions.php.
Look at StackOverflow's syntax highlighting. You're missing a closing single quote ' on your string at the end of the function's last line:
echo '<img ...' .$title. '"/>;
^
Close the last single quote on the echo line to be:
echo '<img src="http://api.qrserver.com/v1/create-qr-code/?data=' .$permalink. '" alt="QR: ' .$title. '"/>';

Insert function name with php

I'm trying to do a simple task, insert a second function name under "onmouseover" but probably something is wrong with the syntax.
echo '<div onmouseover="changeText(); <?php if($title==""){echo changeViz();}; ?> ">';
I probably need to escape some quotes and add some, but i can't figure out the correct solution.
Thanks
Nothing it's working. Let me give you the complete code...:
echo '<div class="frame" onmouseover="changeText(\''.$text[$i].'\'); <?php if($title[$i]==""){echo changeViz();}; ?>">';
You are nesting <?php ?> inside existing php code, which is a syntax error. Instead, concatenate in the javascript function changeViz() as a quoted string.
This version uses a ternary operator to duplicate the if() statement you had originally.
echo '<div onmouseover="changeText(); ' . ($title == '' ? 'changeViz();' : '') . '">';
The ternary operation here will concatenate changeViz(); onto the echo string if $title == "", or otherwise just concatenate on an empty string.
Update after seeing full code:
You have the quote escaping correct in the first part.
echo '<div class="frame" onmouseover="changeText(\'' . $text[$i] . '\'); ' . ($title == '' ? 'changeViz();' : '') . '">';
You can make your code much more readable if you do not try to do everything in one line:
$onmouseover_action = "changeText(); ";
if($title==""){
$onmouseover_action .= "changeViz(); ";
}
echo '<div onmouseover="'.$onmouseover_action.'">';
It makes your code easier to maintain, and you have less need to comment it, because it describes itself quite much better.
Try this:
echo '<div class="frame" onmouseover="changeText(\''.$text[$i].'\'); '. ($title[$i]=="")?'changeViz();':'').'">';

if statement on one line if poss

I am printing an image using an ID which is generated. however i wanted to do a check to see if this image exists and if it doesnt print no-image.jpg instead...
<img src="phpThumb/phpThumb.php?src=../public/images/'.$row["id"].'/th.jpg&w=162" alt="" width="162" />
It would be great if this could be kept on one line is possible. Any help would be appreciated.
What Kristopher Ives says, and file_exists:
echo (file_exists("/path/file/name/here") ? "web/path/goes/here" : "no_image.jpg")
btw, your snippet is unlikely to work, as you seem to be combining plain HTML output and PHP without putting the PHP into <? ?>
My recommendation would actually be to abstract the decision making from the html tag itself, in a separate block of php logic that is not outputting html...here is an abbreviated example that assumes you are not using a template engine, or MVC framework.
<?php
$filename = 'th.jpg';
$filePath = '/public/images/' . $row['id'] '/';
$webPath = '/images/' . $row['id'] . '/';
//Logic to get the row id etc
if (!file_exists($filePath . $filename)) {
$filename ='no-image.jpg';
$webPath = '/images/';
}
?>
<img src="<?php echo $webpath . $filename;?>" />
Wow, this question gets asked and answered a lot:
http://en.wikipedia.org/wiki/Ternary_operation
You could do it by:
<img src="<?php ($row['id'] != 0) ? "../public/{$row['id']}.jpeg" : 'no_image.jpg'; ?> >

Categories