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...
Related
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>';
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.
I am trying to get a webpage to display four divs that will hold an img and a description. I would like to use a loop because I will have other pages with many of these divs. Here is the code I am using now:
for ($i=0;$i<4;$i++)
{
echo '<div class="item">
<img src="IMGs\\' . $items[$i]["ImgFilename"] . '" />
<h6 class="panel">Description</h6>
</div>';
}
I believe the problem is that I am not escaping the correct way. I have been searching for a while but cannot find the right combination. Files are stored in IMGs\file.jpg where file.jpg is pulled from the array.
Your escaping seems fine to me. However, I think the problem is with the double backslash. Eg, remove the \\ and replace it with / So that line becomes:
<img src="IMGs/' . $items[$i]["ImgFilename"] . '" />
U dont need to escape this.
change this:
<img src="IMGs\\' . $items[$i]["ImgFilename"] . '" />
to <img src="IMGs/' . $items[$i]["ImgFilename"] . '" />
You can lay that code out a little better by breaking in/out of PHP as required, here's a quick example:-
<?php for($index = 0; $index < 4; $index++): ?>
<div class="item">
<img src="IMGs/<?php echo $items[$index]["ImgFilename"]; ?>" />
<h6 class="panel">Description</h6>
</div>
<?php endfor; ?>
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.
Insead of having to alter values for every item in the list as so
<li class="item drawing">
<a class="fancybox" rel="group" href="images/portfolio/skateboard_l.jpg">
<img src="images/portfolio/skateboard.jpg" alt="skateboard"/>
<h3>Skateboard</h3>
</a>
</li>
Is it possible to have something like
item="Skateboard
<li class="item drawing">
<a class="fancybox" rel="group" href="images/portfolio/[item-lowercase][if "item"_l.jpg exists, echo"_l"].jpg">
<img src="images/portfolio/[item-lowercase].jpg" alt="[item-lowercase]"/>
<h3>[item]</h3>
</a>
</li>
So I can just change the item variable for each item in the list rather than all the seperate entries. I assume this would be done using PHP or JS?
Thanks.
You want to use templates I guess.
If you want to do it client side in JS:
underscore
Mustache.js
Handlebars.js
The best solution that I can think of is to build an array (I would use PHP)... then use a while loop to build your list... put it all in a function and call it wherever you need it...
For example:
<?php
function itemList(){
$items=array ("skateboard","rollerskates","scooter","rollerblades");
reset($items);
while (list(, $value) = each($items)) {
echo '<li class="item drawing">';
echo '<a class="fancybox" rel="group" href="images/portfolio/' . $value . '_l.jpg">';
echo '<img src="images/portfolio/' . $value . '.jpg" alt="' . $value . '"/>';
echo '<h3>' . $value . '</h3>'; echo '</a>';
echo '</li>';
}
}
?>
Then in your HTML file (where you want the list to be displayed):
<ul><?php itemList(); ?></ul>
If you put the function in a separate .php file, you have to include it in the HTML document:
<?php include ('/url/of/list.php'); ?>
You can do this in PHP using echo.
For instance, if the image name was stored in $images["myImage"] and the alt was $imageAlts["myImage"
<img src="images/portfolio/<?php echo $images["myImage"]; ?>.jpg" alt="<?php echo $imageAlts["myImage"]; ?>"/>
Looks like AngularJS would help as well. It lets you do stuff like this:
<!doctype html>
<html ng-app>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
</head>
<body>
<div>
<label>Name:</label>
<input type="text" ng-model="yourName" placeholder="Enter a name here">
<hr>
<h1>Hello {{yourName}}!</h1>
</div>
</body>
</html>
Smarty is a good template system for PHP. If you'd like to use PHP for templating over JavaScript, just start with one of the tutorials there (or look into other PHP template systems).
A classic way to do this in PHP would be to create an array of items in PHP and then iterate over the array, creating the HTML list items with the appropriate [item-lowercase] entries.
I would consider doing the [if item exists] as part of the process that builds your data array so you don't have to do anything complicated when you build your html. Then, just loop through your array and display whatever is in $theItem.
This is, of course, a simplification.
foreach($itemList as $key=>$theItem){
?>
<li class="item drawing">
<a class="fancybox" rel="group" href="images/portfolio/<?php echo $theItem ?>
<img src="images/portfolio/[item-lowercase].jpg" alt="[item-lowercase]"/>
<h3>[item]</h3>
</a>
</li>
<?php
}