While loop in while loop for images - php

I have a while loop to get information and within the loop i have another loop in order to get the information for images which are in a different table relation to the main loop...
Gathering and displaying information is fine, getting the images relating to information is working ok accept when there is no image path in the image table.. (ie: if no one has uploaded a picture)
It's not an actual image in Mysql, the the path to the image...
For EG:
Id 1 = No image and shows no image pic
Id 2 = Image and Shows the image
Id 3 = No Image but shows image from Id 2 (Should show no image pic)
Id 4 = Image and Shows the image
Id 5 = Image and Shows the image
Id 6 = No Image but shows image from Id 2 (Should show no image pic)
Id 7 = No Image but shows image from Id 2 (Should show no image pic)
$sql_props = mysqli_query($db_conx, "SELECT `image` FROM `images` WHERE `id`='$id' LIMIT 0,1");
while($lex = mysqli_fetch_array($sql_props)){
$proppic = $lex["image"];
}
$check_pic = "$proppic";
if (file_exists($check_pic)) {
$pr_spic = "<img src=\"$proppic\" width=\"100px\" height=\"100px\" border=\"0\" />";
} else {
$pr_spic = "<img src=\"images/nopimg.png\" width=\"100px\" height=\"100px\" border=\"0\" />";
}
Thank you and hope someone can help with this please :)

The problem is most likely that you define the variable $proppic in one iteration of the while loop and then later on access it again, thinking that the variable should be unset because it appears to be out of scope.
I will try to give you a solution while modifying your code as little as possible.
$sql_props = mysqli_query($db_conx, "SELECT `image` FROM `images` WHERE `id`='$id' LIMIT 0,1");
$num_rows = $sql_props->num_rows;
while($lex = mysqli_fetch_array($sql_props)){
$proppic = $lex["image"];
}
$check_pic = "$proppic"; //bad style
if ((file_exists($check_pic)) && ($num_rows > 0)) {
$pr_spic = "<img src=\"$proppic\" width=\"100px\" height=\"100px\" border=\"0\" />";
} else {
$pr_spic = "<img src=\"images/nopimg.png\" width=\"100px\" height=\"100px\" border=\"0\" />";
}

Related

How do I display several images using URLs saved in phpmyAdmin in a website?

See Table of report data
The table is as displayed in the image above. I wanted to display all the data including several images for which URLs are saved in the imageURL column separated with a semicolon.
The number of image URLs differs in each columns.
The URLs refer to image path in upload folder as shown below:
InvestigatorsReportApi/uploads/KBC589L-1.jpg;
InvestigatorsReportApi/uploads/KBC589L-2.jpg;
InvestigatorsReportApi/uploads/KBC 589L-3.jpg;
How would I display this in a website?
You should simmply make a query about the URL:
$sql = "SELECT url FROM table";
$result = $conn->query($sql);
After that echo it into the <img> tag src attribute:
while($row = $result->fetch_assoc()) {
echo "<img src='".$row["url"]."'>";
}
And there you have it. The sample code above will print every image in the database.
Just try this
$sql = "SELECT url FROM table";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
foreach(explode(';',$row["url"]) as $image) {
echo "<img src='".$image."'>";
}
}
First fix your table. It is bad. You should let the attributes occupy the first row:
id: name: LOA: regNO: rout..., then
first you need to use explode function for separate your image url
$image=explode(";",$imageURL);
then use foreach loop for display image
foreach($image as $value)
{
<img src='".$value."'/>
}

images in phpmyadmin using image path

Im trying to add images to my database by using an image path, under the Image column, i have used varchar as the the type and i have inserted images/canin.jpg as the value.
When im trying to display this image using php it doesn't work, this is what i have :
$sql = "SELECT dog_id, dogbreed, image, sellercontact, location FROM dog";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Dog Breed</th><th>Image</th><th>Seller and Contact number</th><th>Location</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["dog_id"]."</td><td>".$row["dogbreed"]." <td>".$row["image"]."</td><td>".$row["sellercontact"]." </td><td>".$row["location"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
All the other information such as dog it, dog breed etc is being retrieved, but its not bringing back the image.
any ideas?
You need to use the HTML img tag to display the image.
Replace <td>".$row["image"]."</td> with <td><img src=\"".$row["image"]."\"/></td>
This should produce <td><img="images/canin.jpg"/></td> when executed

I am using Chrome and cant get image to show from mysql using php

Firstly I know I shouldn't put the image in a DB but I am still learning.
I want to show the image, where am I going wrong?
In my database I have a column Image that is a BLOB. Small sized image. And I've checked an image array is there.
After I connect the db here is the php
$result = mysql_query("SELECT Image FROM table WHERE ID = 1");
$row = mysql_fetch_array($result);
echo $row["image"];
Any help appreciated.
All I want is just the image to show. Nothing fancy as I will expand on it once I get an image up on the screen.
I think I need an extra step between fetch array to split a variable that contains just the image and to display that. This is where I get lost.
Cheers.
This is the code I use to update the DB (from a form)
$myphoto = $_FILES['MyPhoto'];
$query = mysql_query("UPDATE table SET Image = '$myphoto' WHERE ID = 1") or die(mysql_error());
You need to echo an HTML <image> tag with the src set to your query results to render an image. Use this code:
$result = mysql_query("SELECT Image FROM TABLE WHERE ID = 1");
$row = mysql_fetch_array($result);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'"/>';

Only the last image displays from database

I asked this question before but no one could help me unfort. I have images and headings coming from database now the problem is that only one image is displaying(the last image) i need both to display.
here is my revised code
$query = "SELECT page_title, page_image FROM pages WHERE id='$page'";
$result = mysqli_query($connection, $query);
confirm_query($result);
while ($page_fetch = mysqli_fetch_assoc($result)) {
$page_title = $page_fetch['page_title'];
$images = $page_fetch['page_image'];
echo "<div class=\"content \">";
echo "<h3 class=\"words\"> $page_title </h3>";
echo "<img src='pics/" . $images . "' width=\"340\" height=\"252\" alt=\"\" />";
echo "</div>"; //end box
} // close while loop
here is my database for pages
page_id id page_image page_title
1 1 ocean.jpg have a look at the ocean
2 1 house.jpg The house
just some extra info the images must display dynamically , as they coming in from a form to db to this page
Your are returning your images from your query.
Check the view source page, You will find your error their.
Your returing image name will not match with the image which you got in the folder. (For the second one)

Display image on php mysql blog based on username of post

Thanks in advance!
I have a simple mysql and php blog that I built based on a tutorial I found online. What I would like to be able to do, but have no idea how to go about it, is this:
I would like a picture (avatar) to be displayed with each comment on each post. The picture that is chosen would be based off of the name in the Posted By: area of the comment. So for instance: Let's say me, the admin, leaves a comment on the thread. My name is automatically pulled in via a '$_SESSION' variable so I don't have to worry about entering that each time. When the comment is displayed on the blog thread page, it shows Commented on By: Admin. This name is stored in the db and pulled in with the a php echo statement.
So what I want this avatar code to be able to do is
1) look at the area where the Commented on By: text is
2) read the text
3) see that it says Admin and display the admin.png image next to it. If it sees anything other than Admin in the Commented on By: area, then it will display something like guest.png
Here is a snippet of code I found in my stackoverflow and google searches. It works but it pulls in the guest image 6 times, then the actual admin.png image, and then the guest image 3 more times. And it displays this way on EACH comment on EACH thread! And when I add a new thread and a new comment to that thread, it adds the guest image again at the end of the multiple images being displayed on each comment. Did I set it up wrong?
<?
$sql = "SELECT comment_user FROM comments";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) != 0) {
$counter = $starting + 1;
$pathImg = "images/";
while ($row = mysql_fetch_array($result)) {
//calculate url image
$pathFile = $pathImg . $row['comment_user'] . ".png";
if (!file_exists($pathFile)) {
$pathFile = $pathImg . "guest.png";
}
?>
<img src="<?=$pathFile?>" alt="<?=$row['comment_user']?>">
</p>
<?
$counter++;
}
}
?>
This displays out as (Guest Image)(Guest Image)(Guest Image)(Guest Image)(Guest Image)(Guest Image)(Admin Image)(Guest Image)(Guest Image)(Guest Image).
Any help on throwing something together would be great! Trying to keep it simple to!
EDIT:
This is how the comments are displayed, along with the code from FlyingGuy's answer.
<?php
foreach ($post['comments'] as $comment){
$commentCount = 0 ;
$sql = "SELECT comment_user FROM comments";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$commentCount++ ;
$pathImg = "images/";
$pathFile = $pathImg . $row['comment_user'] . ".png";
if (!file_exists($pathFile)) {
$pathFile = $pathImg . "guest.png";
}
echo "<img src=\"". $pathFile ."\" alt=\"". $row['comment_user'] ."\"\><br>";
}
?>
<h4>By <?php echo $comment['user']; ?> on <?php echo $comment['date']; ?></h4>
<p><?php echo $comment['body']; ?></p>
<hr />
<?php
}
?>
This is how the functions look for displaying and adding comments:
function get_comments($pid){
$pid = (int)$pid;
$sql = "SELECT `comment_body` AS `body`, `comment_user` AS `user`, DATE_FORMAT(`comment_date`, '%m/%d/%Y') AS`date` FROM `comments` WHERE `post_id` = {$pid}";
$comments = mysql_query($sql);
$return = array();
while (($row = mysql_fetch_assoc($comments)) !== false){
$return[] = $row;
}
return $return;
}
// adds a comment
function add_comment($pid, $user, $body){
if (valid_pid($pid) === false){
return false;
}
$pid = (int)$pid;
$user = mysql_real_escape_string(htmlentities($user));
$body = mysql_real_escape_string(nl2br(htmlentities($body)));
mysql_query("INSERT INTO `comments` (`post_id`, `comment_user`, `comment_body`, `comment_date`) VALUES ({$pid}, '{$user}', '{$body}', NOW())");
return true;
}
?>
Look what you are trying to do is select the image that matches the name of the user in the current row of your result set. So you will set your image file variable as appropriate for each row and you are sending that to the browser.
For starters and can see the probability of case issues here. Are all user names forced to lower case and all image names forced to lower case? If this is on a linux box that is a land mine on windows not so much, but this should be taken into account.
It will set an image name for each row of your queries result set so it will look like:
[image] [comments]
[image] [comments]
[image] [comments]
if you have three rows in your result set.'
Personally I avoid all of the turning php on and off all over the place. Concat a single string and then simply echo it out for each row. So I would code it like so:
<?
$commentCount = 0 ;
$sql = "SELECT comment_user FROM comments";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$commentCount++ ;
$pathFile = $pathImg . $row['comment_user'] . ".png";
if (!file_exists($pathFile)) {
$pathFile = $pathImg . "guest.png";
}
echo "<img src=\"". $pathFile ."\" alt=\"". $row['comment_user'] ."\"\><br>";
}
So I have eliminated a lot of things from your code example like counters etc. You don't really need to check and see if there are rows since the while loop simply will not execute of there are no rows so you will simply have a question of comment with no subordinate comments and it will only send the image link if there are comments.
No if it were me doing this I would create an avatar file name is the user table and store the path to those as part of the system configuration which would be part of the global set of variables that are always present. Your query would then join in the users table and the image name or guest image would be in your result set. A bit more complex but much cleaner and it simplifies your code.
One of the reasons I don;t like dynamic typing. $row was being mutated to an array of ALL the rows..

Categories