Php beginner here. I have php loop on page to display images (basically a gallery) and each image has a title displayed below. Those titles are links to second page where only one image is shown.
The problem is that every link has a different name (value of name is specific /ID of image).
<form action="image_link.php" method="post">
<?php>
$sql_select = "SELECT * FROM images ORDER BY data DESC";
$sql_table_selected = mysql_query($sql_select);
while ($table_row = mysql_fetch_array($sql_table_selected)){
echo "<div class=\"galery-1-3\"><figure>";
echo "<img src=\"" . $table_row['file_path'] . "\" />";
echo "<figcaption>" . $table_row['tytul'] . "</figcaption></figure></div>";
}
?>
I'm getting a single image displayed but no matter what title I click, I always get last image from the table. How can I catch a specific ID on image_link.php, or should I use some flexible kind of address like /image_link.php?id=34. I don't know where to start.
image_link.php contains basically the same code without the loop:
$sql_select = "SELECT * FROM images WHERE ID = $SomehowGetID";
$sql_table_selected = mysql_query($sql_select);
$table_row = mysql_fetch_array($sql_table_selected);
echo "<div class=\"galery-big\"><figure>";
echo "<img src=\"" . $table_row['file_path'] . "\" />";
echo "<figcaption>" . $table_row['tytul'] . "</figcaption></figure></div>";
Thanks in advance for help.
No need for a form
first script :
<?php
$sql_select = "SELECT * FROM images ORDER BY data DESC";
$sql_table_selected = mysql_query($sql_select);
while ($table_row = mysql_fetch_array($sql_table_selected)){
echo "<div class=\"galery-1-3\"><figure>";
echo "<img src=\"" . $table_row['file_path'] . "\" />";
echo "<figcaption><a href=\"image_link.php?id=" . $table_row['ID']
. "\" name=\"" . $table_row['ID']
. "\">" . $table_row['tytul'] . "</a></figcaption></figure></div>";
}
?>
second script
$SomehowGetID=$_GET['id'];
$sql_select = "SELECT * FROM images WHERE ID = $SomehowGetID";
$sql_table_selected = mysql_query($sql_select);
$table_row = mysql_fetch_array($sql_table_selected);
echo "<div class=\"galery-big\"><figure>";
echo "<img src=\"" . $table_row['file_path'] . "\" />";
echo "<figcaption>" . $table_row['tytul']
. "</figcaption></figure> </div>";
I'd advice you though to use prepared statement instead of simple query constructions.
Change query to:
$sql_select = "SELECT * FROM images WHERE ID =" $_GET['id'];
echo
"<figcaption>
<a href='image_link.php?id=$table_row[ID]' name='$table_row[ID]'>"
.$table_row['tytul'] ."
</a>
</figcaption>
</figure></div>";
You need to pass the id as a parameter in your href with $table_row[ID] as value.
In image_link.php
Use:
$sql_select = "SELECT * FROM images WHERE ID = '$_GET[ID]'";
You had the right idea by saying that you want to pass the id of the image.
your code should looks like this :
<form action="image_link.php" method="post">
<?php>
$sql_select = "SELECT * FROM images ORDER BY data DESC";
$sql_table_selected = mysql_query($sql_select);
while ($table_row = mysql_fetch_array($sql_table_selected)){
echo "<div class=\"galery-1-3\"><figure>";
echo "<img src=\"" . $table_row['file_path'] . "\" />";
echo "<figcaption>" . $table_row['tytul'] . "</figcaption></figure></div>";
}
?>
image_link.php:
$sql_select = "SELECT * FROM images WHERE ID =" . mysql_real_escape_string($_GET['id']);
$sql_table_selected = mysql_query($sql_select);
$table_row = mysql_fetch_array($sql_table_selected);
echo "<div class=\"galery-big\"><figure>";
echo "<img src=\"" . $table_row['file_path'] . "\" />";
echo "<figcaption>" . $table_row['tytul'] . "</figcaption></figure></div>";
Please note that the use of mysql_* function is highly deprecated, you should use mysqli extention instead
Related
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>";
}
?>
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++;
}
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
?>
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].'">
I have done a little bit of research on this, but currently I'm stuck.
My situation:
My database has a serverName, and serverBanner for each entry. When I do this following code:
function listBanner() {
include("mysql.php");
$votes = "serverVotes";
$results = mysql_query("SELECT * FROM toplist ORDER BY $votes ASC");
while($row = mysql_fetch_array($results)){
echo " " . "<img src=" . $row['serverBanner'] . " height=60 width=460 />";
}
}
If you noticed, it is pulling all the info from toplist table. I need to pull all the info from toplist table, but make it so that I can put each on if them in a table row. Right now if I did that, it would put each banner in the same table row.
Also, how would I go about implementing this into a table?
Do you mean:
<?php
echo "<tr>";
while($row = mysql_fetch_array($results)){
echo "<td>" . $row['serverName'] . "</td>";
echo "<td>" . "<img src=" . $row['serverBanner'] . " height=60 width=460 />"."</td>";
}
echo "</tr>"
?>