In PHP use 'img src' as part of data attribute - php

I am creating a table using both Bootstrap and PHP, which makes use of Popovers. I would ideally like my popover to include a JPG but as I am already using echo to create my html table I am unsure how to incorporate it. I have tried to include the usual 'img src' as shown below but it's just printing out the code.
echo "<td colspan='$newlength' id='example' rel='popover' data-content='<img src='imagetouse.jpg' /> This is my content.' data-title=' This is my Title'>$name . $time2</td>";
The above code terminates the tags after the image src. I am unable to use "" around my image because they surround the whole tags for the echo therefore again terminating the code.
Having attempted nietonfir's suggestion:
echo '<td colspan="' . $newlength . '" id="example" rel="popover" data-content="<img src=\'imagetouse.jpg\' /> This is my content." data-title="This is my Title">' . $name . $time2 . '</td>';
The content of my popover now reads as opposed to displaying the image:
<img src='imagetouse.jpg' /> This is my content.

For one I think that you should move the image to the content attribute. And I couldn't find a description to "original-title", so I assume you just meant title. According to the documentation you can insert HTML into the popover by setting an attribute. See this jsBin for a demo.
<div id="foo" data-html="true" data-content="<img src='http://placehold.it/200x100' /> Content" data-title="Title!">Click</div>
[edit]
Concerning your wrong output statement, the following snippet might fix your issue (please be aware that I didn't incorporate the necessary data-html="true" change for the popover to work):
echo '<td colspan="' . $newlength . '" id="example" rel="popover" data-content="<img src=\'imagetouse.jpg\' /> This is my content." data-title="This is my Title">' . $name . $time2 . '</td>';
Imho you should always terminate your strings and concatenate them. In the case of DOM output I'd even terminate the parser and output the variables individually like
?>
<td colspan="<?php echo $newlength; ?>" id="example" rel="popover" data-content="<img src='imagetouse.jpg' /> This is my content." data-title="This is my Title"><?php echo $name . $time2;?></td>
This way it is easier to spot mistakes for one. And on the other side your editor can highlight your code as what it is: HTML.

Related

PHP: Echo IMG src= php echo

I have the following code working in another document
<img src="<?php echo $option_upload_url . '/' . $shortname . '_banner1_imagen.'.$get_banner1_imagen_ext; ?>" alt="<?php bloginfo('name'); ?>" Style="width:100%" />
but now i took to next expression:
echo "<img src=\"$option_upload_url/$shortname_banner1_imagen$get_banner1_imagen_ext\" >";
but seems like the parth isnt correctly implemented, tried diferent things i found on the web but couldnt manage to find a solution...
EDIT:
The image isnt loading when i use the second code.
Try it like this, php isn't understanding which are your variable names, and you forgot the . between _imagen and $get_banner1_imagen_ext
echo "<a href=\"".stripcslashes($get_ads_code_one)."\" title=\"".bloginfo('name')."\">
<img src=\"$option_upload_url/{$shortname}_banner1_imagen.{$get_banner1_imagen_ext}\" /></a>";
I would prefer going with the single ' at the place of escaping ", keep " in the html like this:
echo '<a href="'.stripcslashes($get_ads_code_one).'" title="'.bloginfo('name').'">
<img src="'.$option_upload_url.'/'.$shortname.'_banner1_imagen.'.$get_banner1_imagen_ext.'" /></a>';
You need to have your PHP variables be outside the quotes for them to not be taken as literals.
echo "<img src='" . $option_upload_url . "/" . $shortname_banner1_imagen . $get_banner1_imagen_ext . "' >";

Outputting dynamic PHP code to insert into a dynamically created PHP page

Hey guys got a question on outputting a dynamic PHP block for a dynamically created PHP page. In my code I am looking for a string in an HTML page thats been uploaded. Once found I am replacing the string with a block of PHP code, the HTML page will be saved as a PHP page to be used on the project. So as I am looping through the HTML I am replacing the string with this ($i is replaced with the number in the loop so I can use them in my array.)
$phpCodeNoLink = '<span id="Title'.$i.'"><?php echo $sl_result['.$i.'][2]; ?></span>
<a href="editor.php?<?php echo "vfSID=" . $sl_result['.$i.'][0] . "&vfSection=2&vfSLink=" . $sl_result['.$i.'][4] . "&vfOrderID=" . $sl_result['.$i.'][5] . "&vfID=" . $vfID; ?>" target="_parent">
<img src="images/btn_edit.gif" border="0" id="SL_editButton'.$i.'" class="editButton" />
</a>';
The problem is it is not outputting what I need, example of what it should look like
<span id="Title1"><?php echo $sl_result[1][2]; ?></span>
<a href="editor.php?<?php echo "vfSID=" . $sl_result[1][0] . "&vfSection=2&vfSLink=" . $sl_result[1][4] . "&vfOrderID=" . $sl_result[1][5] . "&vfID=" . $vfID; ?>" target="_parent">
<img src="images/btn_edit.gif" border="0" id="SL_editButton1" class="editButton" />
</a>
This is what I get in the PHP page once it's generated
<span id="Title0"><?php echo $sl_result[0][2]; ?></span>
<a href="editor.php?<?php%20echo%20%20" vfsid=" . $sl_result[0][0] . " .>" target="_parent">
<img src="images/btn_edit.gif" border="0" class="editButton"></a>
The PHP tags are being replaced and I am missing a whole block of code. Am I missing something any help would be much appreciated.
Figured it out, the PHP code was being parsed and removed by my inline CSS converter moving it above all the other parsing resolved it issue...

Advice on cleaner way to write this While statement

So the following code works, it is doing everything i want it to do. However, as i step back it seems like an overly convoluted approach to what is arguably one of the most common tasks in php.
I know enough about php to figure out what most things are doing when i see them, and to create some rather ugly code like you will see below; however, the finer points evade me.
I was hoping that if someone had some free time, he/she could look this over and show me a more concise way to approach this.
<?php
$result = mysql_query('SELECT * FROM events');
$i = 1;
while ($row = mysql_fetch_assoc($result)) {
echo '<div id="item_gallery_s'.$i .'"'. 'class="fluid profileImgWrap goldDiagGrad">' .
'<div class="profile_name">' . $row['name'] . '<br /><span class="profile_date">' .
'<a href="http:#"
target="_blank"
title="some title">' . $row['place'] .
'</a></span></div><!-- DCD Diva Name -->' .
'<a rel="events[events]"
href="#">' .
'<div class="profile_banner">Custom Banner</div><!-- Banner -->' .
'<img src='.'"img/upload/'.$row['icon'].
'"' .
'alt="image description |'.$row['name'].
'"/>' .
'<!-- Photo --></a></div><!-- END #item_gallery_s'.$i .'-->';
$i++;
}?>
The loop itself is fine but you'll find varying opinions on the HTML-in-strings. For the past seven years I've encouraged my team to either use HTML with php tags or we rely on a full templating system:
<?php while ($row = mysql_fetch_assoc($result)): ?>
<div><?= $row['something'] ?></div>
<?php endwhile ?>
Though we have short tags enabled for even cleaner code. The benefit of this is that it's cleaner - less quotes, escaping problems, and IDEs will be able to syntax highlight the html. Most treat the html as string when it's inside quotes.
That's as "concise" as it gets.
You could not use an echo inside the while. And use php short tags.
while ($row = mysql_fetch_assoc($result)) {
?>
<?=$row['place'];?>
<?php
}
?>
Another way to "clean up", would be to use a template engine, but once again that would be just for the HTML part.
{place}
Good coding!
You can clean this up a bit by interspersing actual HTML, rather than simply echoing it:
<?php
$result = mysql_query('SELECT * FROM events');
$i = 1;
while ($row = mysql_fetch_assoc($result)) {
?>
<div id="item_gallery_s<?php echo $i; ?>" class="fluid profileImgWrap goldDiagGrad">
<div class="profile_name">
<?php echo $row['name']; ?>
<br />
<span class="profile_date"><?php echo $row['place']; ?></span>
</div><!-- DCD Diva Name -->
<a rel="events[events]" href="#"><div class="profile_banner">Custom Banner</div><!-- Banner -->
<img src="img/upload/<?php echo $row['icon']; ?>" alt="image description |<?php echo $row['name']; ?>"/><!-- Photo --></a>
</div><!-- END #item_gallery_s<?php echo $i; ?> -->
<?php
$i++;
}?>
Another option, depending on how much work like this you have to do, would be a full-blown template engine such as Smarty.
Here's how I would probably format this code (as a matter of personal style):
<?php
$result = mysql_query('SELECT * FROM events');
$i = 1;
while ($row = mysql_fetch_assoc($result)) {
echo
'<div id="item_gallery_s'.$i.'" class="fluid profileImgWrap goldDiagGrad">
<div class="profile_name">' . $row['name'] . '<br /><span class="profile_date">
<a href="http:#" target="_blank" title="some title">' . $row['place'] .
'</a></span>
</div><!-- DCD Diva Name -->
<a rel="events[events]" href="#">
<div class="profile_banner">Custom Banner</div><!-- Banner -->
<img src="img/upload/' . $row['icon']. '"
alt="image description |' . $row['name']. '"/>
<!-- Photo -->
</a>
</div><!-- END #item_gallery_s'.$i .'-->';
$i++;
}
?>
Try also to use consistent indentation to make it easy to tell what matches up with what. By the way, a <div> (block element) inside an <a> (inline element) is bad form. Did you mean to use a <span>? Learn to use the W3C validator to pick up this stuff.

Link PHP inside an echo tag

I am having a problem with this code
<?php
echo '<div class="post_note2">
<b>'.$lang['RENEW_SUCCESS'].'</b></div><br /><span class="orange"><b>HOME|VIEW AD</b></span>';
}
}?>
for some reason when the VIEW AD link is clicked it doesn't build it properly and still contains the php code in the link rather than the link to the actual ad page. is it an issue with an echo in an echo ?
I'm sure this isn't quite difficult to solve but I have been trying for far to long on my own and cant get it.
Thanks, any help would be great.
You actually had it right in the first part of your string. You can't have and echo statement inside of another echo statement. Use concatenation throughout your string:
<a href="' . $adurl . '"
You have two extra brackets at the end and php text inside your echo.
<?php
echo '
<div class="post_note2">
<b>'.$lang['RENEW_SUCCESS'].'</b>
</div>
<br />
<span class="orange">
<b>
HOME | VIEW AD
</b>
</span>';
?>
All fixed given that $adurl is defined.
This
<?php echo $adurl; ?>
Should be
' . $adurl . '
i.e.
echo '<div class="post_note2"><b>'.$lang['RENEW_SUCCESS'].'</b></div><br /><span class="orange"><b>HOME|<a href="'.$adurl.'>VIEW AD</a></b></span>';

after the div not echoing php

i have a problem with echoing or even retrieving a value after the div popup can anyone help the problem is that $r is not displaying after the divpopup.
or rather that the fetch does not iterate and it displays the first record only..
thanks in advance
<?php
$con = mysql_connect('****', 'root','****') or die('Error connecting to MySQL server.');
mysql_select_db("dreschema", $con) or die("cannot select DB");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<link href="styles2.css" rel="stylesheet" type="text/css" />
<link href="styles.css" rel="stylesheet" type="text/css" />
<link href="styleshref.css" rel="stylesheet" type="text/css" />
<script>
</script>
</head>
<body>
<div id="container1">
<div align="center"></div>
<div id="mainContent1">
<?php
$text = $_GET["searchtext"];
echo "Results displayed for ". $text;
echo '</br>';
$query4 = "SELECT * from products WHERE ProductName LIKE '%$text%'";
$data = mysql_query($query4, $con);
if (!mysql_query($query4, $con)){
print mysql_error();
exit;
}
while ($row = mysql_fetch_array($data)) {
echo $row['ProductName'];
if($row['Stock']== 0 OR $row['Stock']== "")
{
echo "(SOLDOUT)";
echo '<input type="hidden" name="prod" value="' . $row['Image'] . ' " " id = "prod">';
echo '<img id = "imageid" src="' . $row['Image'] . '" alt="' . $row['Image'] . '" width="80" height="80" style="margin-left:1.5em;margin-top:1.5em;"/>';
echo $row['Price'] . "PhP";
?>
<div id="blanket" style="display:none;"></div>
<div id="popUpDiv" style="display:none;">
<font size =" 20">x</font><br>
</iframe>';?>
</div>
Click to Open CSS Pop Up<br>
<?php
}
else
{
echo '<input type="hidden" name="prod" value="' . $row['Image'] . ' " " id = "prod">';
echo '<a href="orders2.php?image=' . $row['Image'] . '">';
echo '<img id = "imageid" src="' . $row['Image'] . '" alt="' . $row['Image'] . '" width="80" height="80" style="margin-left:1.5em;margin-top:1.5em;"/></a>';
echo $row['Price'] . "PhP";
$r = $row['Image'];
echo '<br>';
?>
<div id="blanket" style="display:none;"></div>
<div id="popUpDiv" style="display:none;">
<?php
echo $r;
?>
<font size =" 20">x</font><br>
</iframe>';?>
</div>
Click to Open CSS Pop Up<br>
<?php
}
}
?>
<!-- end #mainContent --></div>
<!-- end #container --></div>
</body>
</html>
This may or may not be an answer, but it wouldn't fit in the comments section so I wanted to expand it here. Looking over the code it's hard to tell if the problem is that (a) MySQL only returned one row; (b) The PHP loop is hitting a problem; or (c) everything is working fine but your HTML markup is so messed up that the browser is simply unable to render it as you think it should.
To eliminate the first issue, please add this right after your query:
$num_rows = mysql_num_rows($data);
if (!$num_rows){die('no rows');}
echo "<p>$num_rows rows returned</p>";
This way you can see how many rows are being returned. Also, don't do this: if (!mysql_query($query4, $con)){. Do this if(!$data){.
----- ASIDE ----
Better yet switch to PDO or mysqli, as your current setup WILL be hacked and your data will get messed up). *On the MySQL
front, at the very least, you should set $text =
mysql_real_escape_string($_GET["searchtext"]); to avoid the simplest
SQL injection attack, and for g*d's sake don't use the root user for your site. Set up MySQL users with limited permissions and use them... also, to avoid an XSS injection you should also strip_tags($_GET["searchtext"]) when echoing the text to the screen.*
----- /ASIDE ----
If the problem isn't that the query is returning a single response, look at the source code. Are you sure you're not seeing multiple loops-worth of code? You have A LOT of markup problems. I've highlighted a few:
echo '<input type="hidden" name="prod" value="' . $row['Image'] . ' " " id = "prod">'; - What are all those "s doing there? There's a floating " just sitting there, which can mess up how the browser interprets the tag. Also, you often have leading or trailing spaces in attribute values... get rid of those, especially in value attributes.
Same line (as well as others): You've statically coded an element ID (prod). IDs are supposed to be unique, so if the code is successfully looping, you'll end up with lots of elements with the same ID (this shouldn't cause the issue you're facing, but there's javascript included that you're not showing us, so the JS could be removing duplicate IDs).
<iframe src="orders2.php?image=' . $row['Image'] . '"style= position:absolute;width:500px;height:500px;"></iframe> - The style attribute is merged into the src attribute. You need a space there or the browser may not know what to do with either attribute.
Same line, the style attribute has no opening " but it has a closing " - more of the same.
<a href="orders2.php?image=' . $row['Image'] . '"> - You have no opening " but you have a closing "... you can omit quotes altogether, but you can't do both.
There are more issues, as well as some general coding standards you should clean up, but you should have a look at the resulting code and make sure that it passes muster.
Lastly, make sure that the image is supposed to be displaying. In the first if statement the image is never told to display (presumably it's in the iframe) and in the else statement the image is echoed into a div that has style="display:none", so we wouldn't expect to see it. If the problem is just that the isn't displaying the image, make sure that the iframe src is correct (and you might want to urlencode the image URI when you're passing it as a _GET variable.)
If you post the resulting HTML code (and confirm that it's not just that MySQL is returning one row) we might be able to help further.

Categories