Displaying image path in PHP from MySQL - php

I would like to display multiple images on my index page. Doesn’t matter if they are displayed in any order.
Each of the images have their image path and their unique ID. I have the following code but it is not displaying anything:
<?php
$result = mysql_query("SELECT * FROM my_image", $connection);
while($row = mysql_fetch_array($result))
{
$ID = $row['name'];
$file = $row['file_path'] ;
echo '<img style="margin: -32.5px 0 0 0;" alt="" src="'.$file.'">' ;
}
?>
Anyone know what could be wrong?

you should start to debug your code, as example :
while($row = mysql_fetch_array($result))
{
$file = $row['file_path'] ;
echo $file; continue;
//...
}
and see if you get expected result.
EDIT:
what about var_dump($result) ?

Related

PHP: glob in foreach multiplies with each row?

This is a bit confusing and hard to explain but I will try my best to explain it.
Basically, I have a directory of images (test) which holds all the images for my products.
The images look like this:
999999999Image1.jpg
999999999Image2.jpg
999999999Image3.jpg
999999999Image4.jpg
999999999Image5.jpg
555555555Image6.jpg
555555555Image7.jpg
555555555Image8.jpg
555555555Image9.jpg
555555555Image10.jpg
etc etc...
The numbers before Image is a field in MYSQL database called STOCK.
Now, I'm trying to use glob() to display the images for each item in mysql database like so:
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$STOCK = $row['STOCK'];
foreach(glob('test/'.$STOCK.'*') as $image) {
if($image != ""){
$pic_list2 .= '<img data-src="'.$image.'" class="smallImg" width="57" height="43" data-id="'.$STOCK.'" data-details="'.$ADVERT_DESCRIPTION_main.'" data-name="'.$MAKE2.' '.$MODEL2.' '.$DERIVATIVE2.'" data-price="'.$PRICE2.'" src="'.$image.'" alt="" />';
}
else{
$pic_list2 = '';
}
}
/////rest of my code..../////
}
However, when I run my code, I get a very strange output...
The issue that I have is that with each output of MYSQL result, the images that being found by glob() is being added to the next result too! So, its like the image of the first result are being added to the next result and then the images of first result and the second results are being added to the third result and so on so forth...
I hope this makes sense and someone could point me in the right direction.
Any help would be appreciated.
I suggest:
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$STOCK = $row['STOCK'];
$pic_list2 = ''; // you need this line also here !! only change
foreach(glob('test/'.$STOCK.'*') as $image) {
if($image != ""){
$pic_list2 .= '<img data-src="'.$image.'" class="smallImg" width="57" height="43" data-id="'.$STOCK.'" data-details="'.$ADVERT_DESCRIPTION_main.'" data-name="'.$MAKE2.' '.$MODEL2.' '.$DERIVATIVE2.'" data-price="'.$PRICE2.'" src="'.$image.'" alt="" />';
}
else{
$pic_list2 = '';
}
}
/////rest of my code..../////
}
What about this? This creates for each mysql output a new array key.
<?php
$i = 0;
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$STOCK = $row['STOCK'];
foreach(glob('test/'.$STOCK.'*') as $image) {
if($image != ""){
$pic_list2[$i] .= '<img data-src="'.$image.'" class="smallImg" width="57" height="43" data-id="'.$STOCK.'" data-details="'.$ADVERT_DESCRIPTION_main.'" data-name="'.$MAKE2.' '.$MODEL2.' '.$DERIVATIVE2.'" data-price="'.$PRICE2.'" src="'.$image.'" alt="" />';
}
else{
$pic_list2[$i] = '';
}
}
$i++;
/////rest of my code..../////
}
?>

Displaying data from database using PHP in an ordered fashion

I am trying to build a web page that has 6 rows of images. Each row has 4 images and each image has a caption right below it. So it goes something like this:
IMAGE IMAGE IMAGE IMAGE
TEXT TEXT TEXT TEXT
IMAGE IMAGE IMAGE IMAGE
TEXT TEXT TEXT TEXT
and so on.
This is my code:
<?php
$resultSet = $db->query("SELECT * FROM Articles");
if ($resultSet->num_rows != 0) {
while ($rows = $resultSet->fetch_assoc()) {
$image = $rows["image"];
$text = $rows["text"];
echo "<img class=images src=$image> <p class=texts>$text</p>";
}
}
?>
Both classes are set to display: inline-block. The code prints out each image next to the text. The text should be below the image. I am trying to think of ways of displaying the images and the texts in the proper format, but I can't seem to think of any solutions at the moment. Anyone can give me some insight?
Set only the wrapper class with the display: inline-block rule
<?php
$resultSet = $db->query("SELECT * FROM Articles");
if ($resultSet->num_rows != 0) {
while ($rows = $resultSet->fetch_assoc()) {
$image = $rows["image"];
$text = $rows["text"];
echo "<div class='wrapper'>";
echo "<img class=images src=$image> </br> <p class=texts>$text</p>";
echo "</div>";
}
}
?>
You should be able to achieve this using divs.
The following code will add in a row ever
<?php
$resultSet = $db->query("SELECT * FROM Articles");
if ($resultSet->num_rows != 0) {
$count = 0;
while ($rows = $resultSet->fetch_assoc()) {
$num_in_row = 4; // Number of items you want in each row
if($count % $num_in_row == 0){
echo '<div class="row">'; // if the row already has 4 items, add a new row.
}
$image = $rows["image"];
$text = $rows["text"];
echo "<span class='wrapper'>";
echo '<img class="images" src='.$image.'> <div class="texts">'.$text.'</p>';
echo "</span>";
if($count % $num_in_row == ($num_in_row-1)){
echo '</tr>';
}
$count++;
}
}
?>
That should give you a result that looks like the jsfiddle

Replace Static image with DB image PHP

I have a div of 40 images(nopreview.png), What I am trying to do is to replace the nopreview.png with the images from db, so if I have 10 images in DB, so out of 40 nopreview.png will be replaced with images from DB keeping the 30 nopreview.png as it is.
HTML
<div class="holder">
<img src="nopreview.png"/>
<img src="nopreview.png"/>
</div>
PHP
$uid="XXXXX";
$check = "SELECT rqid FROM users WHERE fbid = $uid LIMIT 0,250";
$rs = mysqli_query($con,$check);
if(mysqli_num_rows($rs)>0):
while($row = mysqli_fetch_assoc($rs)):
$reqid= $row['rqid'];
$requests = explode(',',$reqid);
foreach(array_unique($requests) as $request_id) {
echo $request_id."<br>";
echo"<img src='https://graph.facebook.com/$request_id/picture?width=78&height=78' />";
echo "<hr>";
}
endwhile;
endif;
Stuck in where to put the images div?
You're trying to work with PHP like Javascript.
Javascript is for DOM manipulation.
PHP is embedded into HTML, and creates HTML (most of the time).
Sccrap your HTML file. Your file "images.php" will look sth like:
<div class="holder">
<?php
$uid="XXXXX";
$check = "SELECT rqid FROM users WHERE fbid = $uid LIMIT 0,250";
$rs = mysqli_query($con,$check);
$imagecount=0;
while($row = mysqli_fetch_assoc($rs)) {
$reqid= $row['rqid'];
$requests = explode(',',$reqid);
foreach(array_unique($requests) as $request_id) {
echo"<img src='https://graph.facebook.com/$request_id/picture?width=78&height=78' />";
$imagecount++;
}
}
for(;$imagecount<40;$imagecount++) {
echo("<img src=\"nopreview.png\" />");
}
?>
</div>
So you will always have your 40 images, starting with the available ones and filling at the end with nopreview.png if required.

just return last row bold in mssql_fetch_array

I have a php code similar to below. I want to make just last row bold. Is it possible?
<table border = "5">
<?php
if (isset($startdate) && isset($enddate) ){
$query = "storedprocedure '$startdate','$enddate','field1'";
$result = mssql_query($query);
while ($rows = mssql_fetch_array($result) ) {
echo "<tr><td>".$rows[0]." <td>".$rows[1]."
<td>".$rows[2]." <td>".$rows[3]."
<td>".$rows[4]." <td>".$rows[5]." ";
}
}
</table>
You can use mssql_num_rows() to get the total number of rows returned by the query before you loop through them, and then (with the use of a counter), see if the current row is the last one and then apply any styling you want:
<table border = "5">
<?php
if (isset($startdate) && isset($enddate) ){
$query = "storedprocedure '$startdate','$enddate','field1'";
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
if ($numRows > 0) {
$counter = 0;
while ($rows = mssql_fetch_array($result) ) {
$style = ($counter++ == ($numRows - 1)) ? ' style="font-weight: bold;"' : '';
?>
<tr>
<td<?=$style;?>><?=$rows[0];?></td>
<td<?=$style;?>><?=$rows[1];?></td>
<td<?=$style;?>><?=$rows[2];?></td>
<td<?=$style;?>><?=$rows[3];?></td>
<td<?=$style;?>><?=$rows[4];?></td>
</tr>
<?
}
}
}
?>
</table>
I find that copying the data to an array (or better yet object) is much better, then outputting the data at the end makes these sorts of things easier.
in this case, if you drop the data into an array, you can then use lovely things like count() to see how many rows there are etc. This lets you do a simple check in the display loop to see if it is the last element in the array.
This should work:
<?php
if (isset($startdate) && isset($enddate))
{
$res=array();
$query = "storedprocedure '$startdate','$enddate','field1'";
$result = mssql_query($query);
while ($rows = mssql_fetch_array($result) )
{
$res[]=$rows;
}
}
?>
<table border = "5" id="1">
<?php
$numberRows=count(%res);
for($i=0;$i<$numberRows;$i++)
{
$boldStart="";
$boldEnd="";
if($i==($numberRows-1))
{
$boldStart="<B>";
$boldEnd="</B>";
}
echo "<tr><td>".$boldStart.$rows[$i][0].$boldEnd." </td><td>".$boldStart.$rows[$i][1].$boldEnd."
</td><td>".$boldStart.$rows[$i][2].$boldEnd." </td><td>".$boldStart.$rows[$i][3].$boldEnd."
</td><td>".$boldStart.$rows[$i][4].$boldEnd." </td><td>".$boldStart.$rows[$i][5].$boldEnd." </td>";
}
?>
</table>
Just use CSS:
t1 tr:last-child{font-weight:bold;}

.load into DIV with PHP content

I am trying to .load a script called 'refreshImages.php'. Inside that script is a while loop pulling from the database. I have got it to load a single echo function but it wont load anything inside the while loop I have on the script... this is what the php file has...
<?php
include 'includes/config.php';
$pimages = mysql_query("SELECT * FROM property_images WHERE pid='$pid'");
//Cant Post Images So Leaving The Echo Content Out//
while($img = mysql_fetch_array($pimages)){
$image = $img['image'];
$image_alt = $img['image_alt'];
echo "<li>$img</li>";
}?>
I am using .load('refreshImages.php') on the page I need it to show up on.
Any explanation I am not seeing?
Your $img is an array, not a string. You will get output like <li>Array</li>, if you have stuff coming from the database. Is that what you mean? Or are you getting an empty result?
If empty - what does your mysql_num_rows tell you when ran against the result resource?
try changing this:
echo "<li>$img</li>";
to
echo "<li><img src=\"{$image}\" alt=\"{$image_alt}\" /></li>";
You may not be getting any results from the database. Try using this code which will display a message if there is something wrong with your sql query.
<?php
include 'includes/config.php';
$pimages = mysql_query("SELECT * FROM property_images WHERE pid=" . $pid );
if (mysql_num_rows($pimages) > 0) { // checks to see if you are getting results from db
while($img = mysql_fetch_array($pimages)){
$image = $img['image'];
$image_alt = $img['image_alt'];
echo '<li><a class="thumb" href="{$image}"><img src="{$image}" width="50px" height="50px" alt="{$image_alt}"></a></li>';
}
} else {
echo "no results returned from database";
} // end of mysql_num_rows check
?>
You might be better off concatenating all the images and then echo-ing it out rather than echo-ing each one e.g
$htmlOutput = '';
while($img = mysql_fetch_array($pimages)){
$image = $img['image'];
$image_alt = $img['image_alt'];
$htmlOutput .= "<li><img src=\"{$image}\" alt=\"{$image_alt}\" /></li>";
}
echo $htmlOutput ;

Categories