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
Related
I'm trying to take the following xml from this url http://r7j8v4x4.map2.ssl.hwcdn.net/NOD_R.xml and make a table with the data. I've verified I can pull the data by using print_r($xml); but I can't get the data to dump into the table. This is what I've got so far, which is probably wrong. Can anyone help me out with the proper code to use?
<?php
$url = "http://r7j8v4x4.map2.ssl.hwcdn.net/NOD_R.xml";
$xml = simplexml_load_file($url);
?>
<table>
<thead>
<tr>
<col><span style="font-weight:bold">Day</span></col>
<col><span style="font-weight:bold">Time(Eastern)</span></col>
<col><span style="font-weight:bold">Reservoir Elev. (behind dam)*</span</col>
<col><span style="font-weight:bold">Tailwater Elev. (below dam)*</span></col>
<col><span style="font-weight:bold">Avg Hourly Discharge*</span></col>
</tr>
</thead>
<tbody>
<?php foreach ($xml->RESULTSET->ROW as $obs) :?>
<tr>
<td><?php echo $obs->obs_day; ?></td>
<td><?php echo $obs->obs_hr; ?></td>
<td><?php echo $obs->upstream_elev; ?></td>
<td><?php echo $obs->downstream_elev; ?></td>
<td><?php echo $obs->avg_hourly_discharge; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
As the XML has 2 <RESULTSET> elements, the RESULTSET->ROW was just picking the first one. So change that to RESULTSET[1]->ROW and you will get the data your after.
You also need to ensure that you use the same case for each element name...
<?php foreach ($xml->RESULTSET[1]->ROW as $obs) :?>
<tr>
<td><?php echo $obs->OBS_DAY; ?></td>
<td><?php echo $obs->OBS_HR; ?></td>
<td><?php echo $obs->UPSTREAM_ELEV; ?></td>
<td><?php echo $obs->DOWNSTREAM_ELEV; ?></td>
<td><?php echo $obs->AVG_HOURLY_DISCHARGE; ?></td>
</tr>
<?php endforeach; ?>
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>
Using this code, I do search from my database and display the result. Now its show all the result of my table first.
Then when I search for a new Item then its show the item. I do not want to display all result first. I want to display only searched Item.
<table class="table table-striped">
<tr>
<th>No</th>
<th>Teacher Name</th>
<th>Gender</th>
<th>Date of Birth</th>
<th>Place of Birth</th>
<th>Degree</th>
<th>Salary</th>
<th>Married</th>
<th>Phone</th>
<th>E-mail</th>
</tr>
<?php
$key="";
if(isset($_POST['searchtxt']))
$key=$_POST['searchtxt'];
if($key !="")
$sql_sel=mysql_query("SElECT * FROM teacher_tbl WHERE f_name like '%$key%' or l_name like '%$key%'");
else
$sql_sel=mysql_query("SELECT * FROM teacher_tbl");
$i=0;
while($row=mysql_fetch_array($sql_sel)){
$i++;
?>
<tr >
<td><?php echo $i;?></td>
<td><?php echo $row['f_name']." ".$row['l_name'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['dob'];?></td>
<td><?php echo $row['pob'];?></td>
<td><?php echo $row['degree'];?></td>
<td><?php echo $row['salary'];?></td>
<td><?php echo $row['married'];?></td>
<td><?php echo $row['phone'];?></td>
<td><?php echo $row['email'];?></td>
</tr>
<?php
}
?>
</table>
Using the following code, the search will be carried out and displayed only when you have submitted a search key:
<?php
$key="";
if(isset($_POST['searchtxt']) && !empty($_POST['searchtxt'])) {
$key=$_POST['searchtxt'];
$sql_sel=mysql_query("SElECT * FROM teacher_tbl WHERE f_name like '%$key%' or l_name like '%$key%'");
$i=0;
while($row=mysql_fetch_array($sql_sel)) {
$i++;
?>
<tr>
<td><?php echo $i;?></td>
<td><?php echo $row['f_name']." ".$row['l_name'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['dob'];?></td>
<td><?php echo $row['pob'];?></td>
<td><?php echo $row['degree'];?></td>
<td><?php echo $row['salary'];?></td>
<td><?php echo $row['married'];?></td>
<td><?php echo $row['phone'];?></td>
<td><?php echo $row['email'];?></td>
</tr>
<?php
}
}
?>
You just have to loop through the results only if $_POST['searchtxt'] is set.
<?php
$key=$_POST['searchtxt'];
if($key !="")
$sql_sel=mysql_query("SElECT * FROM teacher_tbl WHERE f_name like '%$key%' or l_name like '%$key%'");
else
$sql_sel=mysql_query("SELECT * FROM teacher_tbl");
$i=0;
if(isset($_POST['searchtxt']) && !empty($_POST['searchtxt']))
{
while($row=mysql_fetch_array($sql_sel)){
$i++;
?>
<tr >
<td><?php echo $i;?></td>
<td><?php echo $row['f_name']." ".$row['l_name'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['dob'];?></td>
<td><?php echo $row['pob'];?></td>
<td><?php echo $row['degree'];?></td>
<td><?php echo $row['salary'];?></td>
<td><?php echo $row['married'];?></td>
<td><?php echo $row['phone'];?></td>
<td><?php echo $row['email'];?></td>
</tr>
<?php
}
}
?>
</table>
<?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>
I am working on a xml to table project which would use PHP.
<? $xml = new SimpleXMLElement('http://example.com/genXML.php?product=388', 0, TRUE);
?>
<table>
<thead>
<tr>
<th>Symbol</th>
<th>Name</th>
<th>Price</th>
<th>Change</th>
<th>Percentage</th>
<th>High</th>
<th>Low</th>
</tr>
</thead>
<tbody>
<?php foreach ($xml->product as $check) :?>
<tr>
<td><?php echo $check->symbol; ?></td>
<td><?php echo $check->name->chinese; ?></td>
<td><?php echo $check->price; ?></td>
<td><?php echo $check->change; ?></td>
<td><?php echo $check->pct_change; ?></td>
<td><?php echo $check->high; ?></td>
<td><?php echo $check->low; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
As product will have their own unique code, I would like the programe to show all the details in one table.
Is it possible to use MYSQL Database to store the product code that i need to ref. and show them out on a single web page? Thanks!