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)
Related
I have created a members.php page that connects to a database table. The table has the following fields: id, username, profile image.
The following PHP code displays the profile image for each user in rows of 6 and allows each image to be clickable.
<?php
// The code below will display the current users who have created accounts with: **datemeafterdark.com**
$result=mysql_query("SELECT * FROM profile_aboutyou");
$row1 = mysql_fetch_assoc($result);
$id = $row1["id"];
$_SESSION['id'] = $id;
$profileimagepath = $row1["profileimagepath"];
$_SESSION['profileimagepath'] = $profileimagepath;
$count = 0;
while($dispImg=mysql_fetch_array($result))
{
if($count==6) //6 images per row
{
print "</tr>";
$count = 0;
}
if($count==0)
print "<tr>";
print "<td>";
?>
<center>
<img src="<?php echo $dispImg['profileimagepath'];?>" width="85px;" height="85px;">
</center>
<?php
$count++;
print "</td>";
}
if($count>0)
print "</tr>";
?>
This is all great, however, when I click on the image that loads it re-directs me to: viewmemberprofile.php which is what it is supposed to do. But it always displays the same image with the same id value (i.e.) 150 no matter which image I click. What I would like to have happened is. If I click on an image with id 155 etc... it will display content for that image data field not consistently the same image data regardless of which image I click.
Your help and guidance would be greatly appreciated. Thank you in advance.
One thing that I forgot to mention is that I do use sessions so... when I am re-directed to the viewmemberprofile.php page I use the following code to aide in getting the data that I need from the table.
<?php
$id = $_SESSION['id'];
echo($id);
?>
<?php
echo('<br>');
?>
<?php
$profileimagepath = $_SESSION['profileimagepath'];
?>
<img src="<?php echo($profileimagepath);?>" width="50px;" height="50px;">
I have yet to impliment the suggested solution.
You need to pass the ID of the row to viewmemberprofile.php, e.g.:
<a href="viewmemberprofile.php?id=<?= $dispImg['profileimagepath'] ?>">
And viewmemberprofile.php needs to select that row from the DB:
SELECT * FROM profile_aboutyou WHERE id = $_GET['id']
The above SQL statement is pseudo-code; you need to write actual code to accomplish what it is describing, preferably using parameterized queries.
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\" />";
}
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."'/>
}
hii am amateur in php and making a website in which i am uploading an image,storing it in database(with an autoincrement variable) and displaying it on page in descending order so it may seem as latest at top and latter below it,,i need to add a textbox for comment and username(cuz i haven't made login page as m not gonna host it somewhere)
i knwo how to add it for upload image's section as only 1 image is gonna get uploaded
problem is how to display it, i have used
while ($image = mysqli_fetch_assoc($sql))
{
echo image code....
}
Now i in the div to display image i'll incorporate 2 textboxes and evertime after while loop ill display values from dB accordingly but when i want to edit the textbox after uploading more than 1 image,how to know which textbox is being edited as every textbox would have same textbox name
should i compare the image contents ?as the image iteself is stored in binary format? but that wud be a slow process i guess
plz suggest som idea or method to do so basically its like instagram(but very basic)....
First create images table:
images table
image_id | url
Then creare seperate table for images comments:
images_comments table
comment_id | image_id | user | text | date
Where:
comment_id: integer , Auto Increment
image_id: integer referring to id in images table
user: should be user id referring to separate users table, but because you do not have login system you can store user name here as varchar.
text: varchar
date: datetime
To show images + comments:
$sql = "SELECT * FROM images";
while ($image = mysqli_fetch_assoc($sql))
{
$imageUrl = $image["url"];
$imageID = $image["image_id"];
//Show image:
echo "<img src='$imageUrl ' />";
//Get comments:
$sql2 = "SELECT * FROM images_comments WHERE image_id=$imageID";
while ($comment = mysqli_fetch_assoc($sql2))
{
$text = $comment["text"];
$userName = $comment["user"];
$date = $comment["date"];
//show username and comment date
echo $userName . " " .$date . "<br>";
//Show comment text:
echo $text;
}
}
To add a comment to images
if you want a comment form under each image then in the While loop we used before add the following code:
while ($image = mysqli_fetch_assoc($sql))
{
.......... previous code goes here .........
//in the form we store imageID in hidden input field
//so that we can know to which image the form belongs
echo "<form method='post' action='page.php'>
<input type='text' name='username'/>
<textarea name='commentText'></textarea>
<input type='hidden' name='imageID' value='$imageID'/>
<input type='submit' name='submitComment' value='Submit'>
</form>";
}
then in page.php
if(isset($_POST["submitComment"]))
{
$username = $_POST["username"];
$text = $_POST["commentText"];
$imageID = $_POST["imageID"];
$date = date("Y-m-d H:i:s");
$sql = "INSERT INTO images_comments (image_id,user,text,date) VALUES
($imageID,$username,$text,$date)";
mysqli_query($con,$sql);
}
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..