Image not displaying from table - php

<?php
$connect = mysqli_connect('localhost', 'root', ' ', ' ');
if(mysqli_connect_errno($connect)){
echo 'Failed to connecto to database'.mysqli_connect_error();}
$result= mysqli_query($connect, "SELECT * FROM Products WHERE ProductCategory = 'Electronics'");
?>
<table width="500", cellpadding=5 callspacing=5 border=1>
<tr>
<th>Product Name</th>
<th>Product Price</th>
<th>Product Image</th>
<th>Product Description</th>
<th>Product Category</th>
</tr>
<?php while($rows = mysqli_fetch_array($result)): ?>
<tr>
<td><?php echo $rows['ProductName']; ?></td>
<td><?php echo $rows['ProductPrice']; ?></td>
<td><?php echo '<p<img src="images/'.$row["ProductImage"].'" />'; ?></td>
<td><?php echo $rows['ProductDescription']; ?></td>
<td><?php echo $rows['ProductCategory']; ?></td>
</tr>
<?php endwhile; ?>
</table>
My issue is that the code that I currently have only makes it so that it will run through the database line by line and get the information but when it comes to the images row then it doesn't retrieve anything and my directory is correct as the image is stored in a folder called images that is within the folder the web page is.

<td><?php echo '<p<img src="images/'.$row["ProductImage"].'" />'; ?></td>
You forgot to close the <p and also missing the "s" in rows
this should be <td><?php echo '<p><img src="images/'.$rows["ProductImage"].'" /></p>'; ?></td>
also dont forget to close the <p> tag later

<td><?php echo '<p<img src="images/'.$row["ProductImage"].'" />'; ?></td>
You're missing a closing > on p before img and subsequently a closing p.
Try:
<td><?php echo '<p><img src="images/'.$rows["ProductImage"].'" /></p>'; ?></td>
You can edit the size of your pictures by using CSS, if photoshop is not a solution.

Instead of
<td><?php echo '<p<img src="images/'.$row["ProductImage"].'" />'; ?></td>
Use below code
<td><?php
$productImage = !empty($row["ProductImage"])?$row["ProductImage"]:'no-image.png';
echo "<img src='/images/{$productImage}' />";?>

You're missing the "s" in rows
Try: <td><?php echo '<p><img src="images/'.$rows["ProductImage"].'" /></p>'; ?></td>

Related

How to make sql image clickable on display board

I am trying to make the fetched images on a sql database clickable. They display fine but no-one can click them or download them and I'd like them to be able to. The script is below.
<?php
include_once 'connect.php';
$result = mysqli_query($conn,"SELECT * FROM Leaderboard");
?>
<?php
if (mysqli_num_rows($result) > 0) {
?>
<p>Leaderboard and guest photos as of October 20, 2020. Our apologies to our prior guests.</p>
<table class='table table-bordered table-striped'>
<tr>
<td>Date</td>
<td>Photo</td>
<td>Team Name</td>
<td># of Players</td>
<td>Escape Room</td>
<td>Time</td>
<td>Success</td>
<td>Game Master</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["date"]; ?></td>
<td><?php echo "<img src='/teamphotos/".$row['photo']." ' width='250' height='200'>"; ?>. </td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["players"]; ?></td>
<td><?php echo $row["room"]; ?></td>
<td><?php echo $row["time"]; ?></td>
<td><?php echo $row["win"]; ?></td>
<td><?php echo $row["master"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
<?php
}
else{
echo "No result found";
}
?>
Probably you want that if the user left clicks the image, a download of that image starts. For that you only need to wrap an anchor tag <a> around the <img> with the attributes src and download.
<td><?php echo ''; ?></td>
This is related to HTML and not to PHP. Here you can read more about it:
https://www.w3schools.com/howto/howto_html_download_link.asp

show multiple rows for each result value

I have two tables one for products and another for items.
I want to show all items details for each product in one table like this:
I have tried to make a nested while loops but the result not as I want.
<table border="1">
<thead>
<th>Product No.</th>
<th>Product Name</th>
<th>T.Qty</th>
<th>Item No.</th>
<th>Item Name</th>
<th>Qty </th>
</thead>
<tbody>
<tr>
<td><?php echo $Product_no ?></td>
<td><?php echo $Product_name?></td>
<td><?php echo $TQty ?></td>
<?php
// my problem is here
$Items= $connect->prepare("Query Statment?");
$Items->execute();
$res = $Items->get_result();
while($GetItems = $res->fetch_assoc()){
?>
<td><?php echo GetItems['Item_no'];?></td>
<td><?php echo GetItems['Item_name']; ?></td>
<td><?php echo GetItems['Qty']; ?></td>
<?php } ?>
</tr>
</tbody>
</table>
but the items displayed beside each other not below.
You have a problem in your html table code. You should close the tag for each row you have, and, in case it is not the first line for that element, insert 3 cells with no data:
<tbody>
<tr>
<td><?php echo $Product_no ?></td>
<td><?php echo $Product_name?></td>
<td><?php echo $TQty ?></td>
<?php
// my problem is here
$Items= $connect->prepare("Query Statment?");
$Items->execute();
$res = $Items->get_result();
$i=0;
while($GetItems = $res->fetch_assoc()){
if ($i!=0){
echo "<td></td><td></td><td></td>";
}
?>
<td><?php echo GetItems['Item_no'];?></td>
<td><?php echo GetItems['Item_name']; ?></td>
<td><?php echo GetItems['Qty']; ?></td>
<?php
echo "</tr>";
$i++;
} ?>
</tbody>

How to stop php 'for loop' from creating multiple table headers

I am creating a table that fetches data from an SQL database using a PDO method. The data loads fine but the issue I'm having is that the 'for loop' I'm using is multiplying a (table header) after every (table row).
I am wondering what a possible solution the the issue could be.
Any help is appreciated, thanks!
Here is the code:
<?php
for($i=0; $row = $result->fetch(); $i++){
?>
<table id="eventstable">
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
</table>
<?php }
?>
Up at the very top is the connection file that creates a connection to my local database and a statement that brings in the information I want to display from the database like so:
$result = $conn->prepare("SELECT * FROM events");
$result->execute();
Few possible solutions are
i) Put the header part and the table opening and closing tag outside the for loop. This will give you an empty table with headers if there is no data.
ii) Put an if condition and print headers only when i = 0, and put table tags outside the loop. This will give you an empty table with nothing if there is no data.
Edit: Method II (since you are learning)
<table id="eventstable">
<?php
for($i=0; $row = $result->fetch(); $i++){
if($i == 0){
?>
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<?php }//if statment ends here ?>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
<?php }
?>
I would also suggest since you are learning, use better ways than for loop. Look at the php PDO manuals and see the use of while or foreach. It will help more.
Put in loop only, what should be looped, everything else should be outside of loop.
For example, your code could look like this
<table id="eventstable">
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<?php
for($i=0; $row = $result->fetch(); $i++){
?>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
<?php
}
?>
</table>
try this :
<table id="eventstable">
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<?php
foreach($result->fetch() as $row){
?>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
<?php }
?>
</table>

php table not displaying contents and only creating table boxes

I would like the table to display all the values inside of it, but it is currently not displaying the values and only creating an empty table
[what the table is displayed as (image)][1]
$result = mysqli_query( $conn,'SELECT * FROM Pictures ');
$conn->close();
Html
<html>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;"
<thead>
<tr>
<th>id</th>
<th>Name</th>
<th>image</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
if( $result != null){
Result is not empty
while( $row1 = mysqli_fetch_assoc( $result ) ){
foreach ($row1 as $row){
?>
<tr>
<td><?php $row['id'] ?></td>
<td><?php $row['hname'] ?></td>
<td><?php $row['himage'] ?></td>
<td><?php $row['hdesc'] ?></td>
</tr>
<?php
}
}
}else{
echo "Something went wrong with the result";
}
?>
</tbody>
</table>
<?php //mysqli_close($conn); ?>
</body>
</html>
changed the out put to match the answer you gave but the output came out as picture 2 while my table is actually picture 3 any ideas
output:
table im trying to display
Display the data in this way:
<td><?php echo $row['id']; ?></td>
Try changing
<td><?php $row['id'] ?></td>
<td><?php $row['hname'] ?></td>
<td><?php $row['himage'] ?></td>
<td><?php $row['hdesc'] ?></td>
to
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['hname']; ?></td>
<td><?php echo $row['himage']; ?></td>
<td><?php echo $row['hdesc']; ?></td>
or the shorthand variant
<td><?= $row['id']; ?></td>
<td><?= $row['hname']; ?></td>
<td><?= $row['himage']; ?></td>
<td><?= $row['hdesc']; ?></td>
And as Samuel pointed out in a comment, are you sure there is a need for the extra foreach considering you're already looping with the while?
Update: OP have you tried the following?
while( $row = mysqli_fetch_assoc( $result ) ){
//removed foreach()
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['hname']; ?></td>
<td><?php echo $row['himage']; ?></td>
<td><?php echo $row['hdesc']; ?></td>
</tr>
<?php
}
}else{
Update 2 OP wishes to have the image load instead of showing the raw URL.
To do this, we need to use an actual image tag and insert the url into the src.
I assume that this line is the image URL
<td><?php echo $row['himage']; ?></td>
So change it to
<td> <img src="<?php echo $row['himage']; ?>" > </td>

PHP show MySQL table data PDO

I'm a PHP noob, I keep getting this error:
Parse error: syntax error, unexpected end of file...
The line it points to is at the very last line. My 'header.php' declares the !DOCTYPE html and my 'config.php' holds the database properties.
Any help would be greatly appreciated.
<?php
include 'config.php';
include 'header.php';
?>
<div id='wrapper'>
<div style='height:50em;width:100%;z-index:10;margin-top:1em;text-align:left;'>
<table id='mainDPUtable'>
<th class='cen'>NUM</th>
<th class='cen'>TYP</th>
<th class='cen'>LVL</th>
<th class='cen'>Job No.</th>
<th class='cen'>Responsible</th>
<th class='cen'>Rep</th>
<th class='cen'>Initiated</th>
<th class='cen'>Age</th>
<th class='cen'>Part Number</th>
<th class='cen'>Qty</th>
<th class='cen'>Description</th>
<th class='cen'>Location</th>
<th class='cen'>Complete</th>
<?php
$db = new PDO("mysql:host=localhost;dbname=".$dbname.",".$user.",".$pswd);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$result = $db->prepare("SELECT NUM, TYP, LVL, JOBNO, RESP, REP, DATE_INITIATED, AGE, PARTNO, QTY, DESCRIPTION, LOC FROM ".$dbtable);
$result->execute();
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
$num=$row["NUM"];
$typ=$row["TYP"];
$lvl=$row["LVL"];
$jobNo=$row["JOBNO"];
$resp=$row["RESP"];
$rep=$row["REP"];
$date_initiated=$row["DATE_INITIATED"];
$age=$row["AGE"];
$partNo=$row["PARTNO"];
$qty=$row["QTY"];
$description=$row["DESCRIPTION"];
$loc = $row["LOC"];
$comp=$row["COMP"];
?>
<tr>
<td><?php echo $num; ?></td>
<td><?php echo $typ; ?></td>
<td><?php echo $lvl; ?></td>
<td><?php echo $jobNo; ?></td>
<td><?php echo $resp; ?></td>
<td><?php echo $rep; ?></td>
<td><?php echo $date_initiated; ?></td>
<td><?php echo $age; ?></td>
<td><?php echo $partNo; ?></td>
<td><?php echo $qty; ?></td>
<td><?php echo $description; ?></td>
<td><?php echo $loc; ?></td>
<td><input type='button' id=<?php echo 'btn'.$num; ?> value='Complete'/></td>
</tr>
<?php}?>
</table>
</div>
</div>
</body>
</html>
Missing spaces:
<?php}?>
should be
<?php } ?>
^-^--

Categories