after the div not echoing php - 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.

Related

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.

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

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.

PHP+HTML Syntax

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; ?>

Concrete5 show fancybox from file set on link click?

I don't know exactly where to start to display files from the file set in order to show a fancybox gallery on click. I would like to have the gallery open from a link. On click show the gallery, aka images that have the same rel but are set to display:none (easily controlled by my css). I can do it from selecting one image but am not sure how to pass the images from the file set into the view (I'm assuming I need to create some kind of function in my controller to get the fsID, just not sure how). I just need the first image to display on the page (thumbnail image), then click the link and it shows more full size images.
Basically, if you know Concrete5, I would like it to be like the image block, except that the administrator can choose a fileset instead of one image.
here is my view.php
$picture = $this->controller->getPicture();
if ($picture) {
$bigPicture = $image->getThumbnail($picture,600,600)->src;
$smallPicture = $image->getThumbnail($picture,200,200)->src;
echo "<img src=" . $smallPicture . " alt=" . $imageTitle . " title=" . $imageTitle . "/>";//thumbnail picture
echo "<div id=\"image-modal\">";
echo "{$linkText}";//open fancybox from link
echo "<div class=\"hiddenGallery\" style=\"display:none;\">";//hidden images
echo "<a href=\"images/pattern/t-103-n.jpg\" class=\"fancybox-thumb\" rel=" . $title . " title=" . $imageTitle . ">";
echo "<img src=\"images/pattern/t-103-n.jpg\" class=\"fancybox-thumb\" />";
echo "</a>";
echo "</div>";
echo "</div>";
}
my controller.php
function getPicture() {
if ($this->fIDpicture > 0) {
return File::getByID($this->fIDpicture);
}
return null;
}
my add.php
$al = Loader::helper('concrete/asset_library');
echo $al->image('ccm-b-image', 'fIDpicture', t('Choose File'),
$this->controller->getPicture());
echo '</div>';
Any and all help is much appreciated.
Well, two things :
You have to set the class="fancybox-thumb" AND the rel attribute to the <a> tag!!, not to the <img /> tag.
If you are planing to hide the rest of the elements of the gallery, don't set a display: none; css property to each of them, but rather wrap them in a hidden <div> container like :
<div style="display: none;">
<a class="fancybox-thumb" rel="gallery" href="images/02.jpg"></a>
<a class="fancybox-thumb" rel="gallery" href="images/03.jpg"></a>
<a class="fancybox-thumb" rel="gallery" href="images/04.jpg"></a>
... etc
</div>
I am using the rendered html, which is what it matters.
I have some code which handles the entire back-end (add/edit/controller) part of this equation:
https://github.com/jordanlev/c5_designer_gallery
Here's a tutorial that explains how to use it (with the example of the FlexSlider, but if you know how Fancybox works then it shouldn't be hard to understand what's going on):
http://c5blog.jordanlev.com/blog/2011/12/build-a-slideshow-block/

Categories