show images from database with id - php

I have this code :
<?php
$link = mysql_connect("localhost", "root", "");
mysql_select_db("galeria",$link);
$id = $_GET['id'];
$query = "SELECT lowsrc from gallery WHERE id=$id";
$result = mysql_query($query) or die(mysql_error());
$x=0;
while($row = mysql_fetch_array($result))
{
$image = $row['lowsrc'];
echo "<img src='".$image."' /><br />";
}
?>
and in the table I have this one :
echo "<tr>";
echo '<td>' . $row[0] . '</td>';
echo '<td><img src="getImage.php?id=' . $row[0].'" width="300" /></td>';
I cant figure out why this is not showing the images

You are outputting HTML twice.
$image = $row['lowsrc'];
header('Content-type:image/png');
readfile($image);
You'll notice that this will cause the URL getimage.php?id=9 to behave like a PNG image and the read file command will dump the contents of the file.
If this doesn't work, download the image, open it with a text editor and check for PHP errors.

You need to specify full path.
For e.g: src="'http://' . $_SERVER['SERVER_NAME'] .$_SERVER['REQUEST_URI']/getImage.php?id=' . $row[0].'"
echo "<tr>";
echo '<td>' . $row[0] . '</td>';
echo '<td><img src="http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'].'/getImage.php?id=' . $row[0].'" width="300" /></td>';

Related

How can I display images for each data in the table

I have a table where I store some teams. I am displaying them in the page via Materialize cards tag. What I want to do is to display for every team their badge.
Displaying teams
<?php
$link = mysqli_connect("localhost", "root", "", "proiect");
$sql1 = "SELECT id_club, nume, avatar FROM club";
while ($row1 = mysqli_fetch_array($qry1)) {
echo "<div class='w3-card b'>";
echo "<a href='echipa?id=" . $row1['id_club'] . "' _target='blank' title='Vezi echipa'><img src='img/referees.jpg' alt='Person' style='width:100%'>";
echo "<div class='w3-container'>";
echo "<h4><a href='echipa?id=" . $row1['id_club'] . "' _target='blank' title='Vezi echipa'><b>" . $row1['nume'] . "</b></h4>";
echo "</div>";
echo "</div>";
}
Now it is hardcoded the image that is displayed. I want the badge team image to have the same name as the club name from the database.
If the file is stored in img folder with same name then this code will work.
<?php
$link = mysqli_connect("localhost", "root", "", "proiect");
$sql1 = "SELECT id_club, nume, avatar FROM club";
while ($row1 = mysqli_fetch_array($qry1)) {
echo "<div class='w3-card b'>";
echo "<a href='echipa?id=" . $row1['id_club'] . "' _target='blank' title='Vezi echipa'><img src='img/'.$row1['nume'].'.jpg' alt='Person' style='width:100%'>";
echo "<div class='w3-container'>";
echo "<h4><a href='echipa?id=" . $row1['id_club'] . "' _target='blank' title='Vezi echipa'><b>" . $row1['nume'] . "</b></h4>";
echo "</div>";
echo "</div>";
}
?>

Image array repeating/duplicating in PHP/HTML

The problem is as shown in the image I've attached, my first day learning php and sql.
I want the images to be assigned to each particular item, so first item picture be displayed above the first item described rather than all 5 of them above each description. I'm still very new to this sorry. Thank you!
My code:
<?php
$con = mysqli_connect("xxxx", "xxxx",
"xxxx", "xxxx");
$query = "SELECT * FROM MyShop WHERE ID BETWEEN '1' AND '5'";
$result = mysqli_query($con, $query);
$array = array("cp.jpeg", "BV-C.jpeg", "BV-B.jpeg", "ADIY.jpeg", "CDG.jpeg",);
while($person = mysqli_fetch_array($result)) {
foreach( $array as $image ){
echo "<img src='" . $image . "' height='200' width='200'/>";
}
echo "<center><h3>" . $person['Name'] . "</h3><center>";
echo "<center><p>" . $person['Colour'] . "</p><center>";
echo "<center><p class='ex'>" . $person['Description'] . "</p><center>";
echo "<center><p> £" . $person['Price'] . "</p><center>";
}
?>
All five images are getting displayed first because your foreach loop closes after the img tag.
So it is printing each image, and then displaying your item information. Try this and see if it fixes your issue.
$count = 0;
while($person = mysqli_fetch_array($result)) {
echo "<img src='" . $array[$count]. "' height='200' width='200'/>";
echo "<center><h3>" . $person['Name'] . "</h3><center>";
echo "<center><p>" . $person['Colour'] . "</p><center>";
echo "<center><p class='ex'>" . $person['Description'] . "</p><center>";
echo "<center><p> £" . $person['Price'] . "</p><center>";
$count++;
}

While loop and reusing fetch_assoc()

For a project I am building a flash game website, and I would like to know how to reuse this piece of code:
$result = $conn->query("SELECT DISTINCT `category` FROM beoordeling");
<?php
echo "<div class='row list jumbotron'>";
while($data = $result->fetch_assoc()) {
echo "<div class='col-md-3'><a href='category.php?id=" . $data['category'] . "' class='thumbnail'><h4>" . ucfirst($data['category']) . " games</h4>";
echo "<img src='" . $imgLocation . $data['category'].".jpg' class='img-rounded' alt='" . $data['category'] . "' width='304' heigth='182'>";
echo "</a></div>";
}
echo "</div>";
?>
I need to be able to use the $data['category'] again for dynamicly filling my menu with all of the games categories. If I just try to use the while loop again but then only one will work. The other one stays empty. Thanks alot!
You need to adjust the result pointer to point to the beginning of the result set so that you could use it again. Make use of mysqli_result::data_seek() method for this.
Here's the reference:
mysqli_result::data_seek()
So your code should be like this:
<?php
$result = $conn->query("SELECT DISTINCT `category` FROM beoordeling");
echo "<div class='row list jumbotron'>";
while($data = $result->fetch_assoc()) {
echo "<div class='col-md-3'><a href='category.php?id=" . $data['category'] . "' class='thumbnail'><h4>" . ucfirst($data['category']) . " games</h4>";
echo "<img src='" . $imgLocation . $data['category'].".jpg' class='img-rounded' alt='" . $data['category'] . "' width='304' heigth='182'>";
echo "</a></div>";
}
echo "</div>";
// adjust the result pointer to point to the beginning of the result set
$result->data_seek(0);
// now you can use the result set again
// for example, you can iterate through it using while loop again
?>

How to call images in a database into 10 images per row?

<?php
include 'dbconnect.php';
$query = mysql_query("SELECT * FROM champicons") or die("Error: " . mysql_error());
echo "<table border='10' width='100%' cellpadding='10' >";
echo "<tr>";
while($row = mysql_fetch_array($query)) {
$image = $row[2];
echo "<td>";
echo '<img src="data:image/png;base64,' . base64_encode($image) . '" />';
echo "<td>";
}
echo "</tr>";
echo "</table>";
?>
What I'm trying to achieve is for 10 images to be put in 1 row before dropping a row below and making another 10 images appear, right now the more images I put into the database the wider and smaller the current row gets and I can't see all the images.
It's hard to get it to create a new row at 10 images as the images are called on a while loop.
Add a counter before the loop and if in the loop.
$i = 1;
while($row = mysql_fetch_array($query)) {
$image = $row[2];
echo "<td>";
echo '<img src="data:image/png;base64,' . base64_encode($image) . '" />';
echo "</td>";
if($i++%10 == 0) echo '</tr><tr>';
}
I think you just need to count your iteration and insert new row according
$i=1;
while($row = mysql_fetch_array($query)){
$image = $row[2];
echo "<td>";
echo '<img src="data:image/png;base64,' . base64_encode($image) . '" />';
echo "</td>";
$i++;
if($i==10){
$i=1;
echo "</tr><tr>";
}
}
As side note your </td> wasn't closed correctly

How can I open a link into a div that has data from mysql?

I am building a site that has a page where I display some products that I have on my database. The products are displayed by small divs that are next to each other. I have put inside each div a link that I want to have the information of each product. The information will all have the same structure. I thought about having an
<a href="'.$row[id].".php".'">
and have an id.php file for each of the products. But that doesn t seem to make sense.
Here it is a bit of my code
<?php
mysql_select_db("myshop",$con);
$result = mysql_query('SELECT * FROM products',$con);
while($row = mysql_fetch_array($result))
{
$myimage = '<img src="'.$row['image'].'" />';
echo "<div id='appear'>" . $myimage . '<br />' . $row['title'] .
"<br />" . "<p style='color:red;' >" . "value " . $row['price'] . "€" .
"</p>". <a href="'.$row[id].".php".'">. "details" . "<a>".
</div>";
}
mysql_close($con);
?>
Create an image.php...
<?php
mysql_select_db("myshop",$con);
id = (int)$_GET['id'];
$result = mysql_query("SELECT * FROM products WHERE id=$id",$con);
print_r($result)
?>
And instead of this:
<a href="'.$row[id].".php".'">
Do this:
<a href="image.php?id='.$row[id].'">

Categories