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
?>
Related
I have this PHP code, which fetches data from my SQL database called "comments".
This code prints out every comment in the table:
<?php
$sql = "SELECT id,name,email,number,text FROM comments";
$result = $conn->query($sql);
if($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<strong>ID:</strong><br> " . $row["id"] . "<br>";
echo "<strong>Navn:</strong><br> " . $row["name"] . "<br>";
echo "<strong>Email:</strong><br> " . $row["email"] . "<br>";
echo "<strong>Nummer:</strong><br> " . $row["number"] . "<br>";
echo "<strong>Melding:</strong><br> " . $row["text"] . "<br><br><br>";
}
echo '<div class = "white_line_comments"></div>';
} else {
echo "0 results";
}
This has worked fine so far, everything prints as it's supposed to.
Then I decided I wanted a way to give each individual comment some sort of identification to make them unique. I tried putting each single comment into its own div, using the SQLtable row id as id for the div.
However, when I try to access my webpage now, it tells me the website doesn't work (HTTP Error 500).
<?php
$sql = "SELECT id,name,email,number,text FROM comments";
$result = $conn->query($sql);
if($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
<div class='$row[' id'].'>";
echo "<strong>ID:</strong><br> " . $row["id"] . "<br>";
echo "<strong>Navn:</strong><br> " . $row["name"] . "<br>";
echo "<strong>Email:</strong><br> " . $row["email"] . "<br>";
echo "<strong>Nummer:</strong><br> " . $row["number"] . "<br>";
echo "<strong>Melding:</strong><br> " . $row["text"] . "<br><br><br>";
echo '</div>';
}
echo '
<div class="white_line_comments"></div>';
} else {
echo "0 results";
}
Any ideas on this? I guess I must've done something wrong when including the div, but I can't figure out what!
You have an error in this line after starting while loop:
echo "<div class ='$row['id'].'>";
It should be
echo "<div class ='". $row['id'] ."'>";
You should also configure your web server/hosting/localhost to throw a PHP error.
Read this if you are on localhost or your own server:
How can I make PHP display the error instead of giving me 500 Internal Server Error
Read this if you are using shared hosting: How do I displaying details of a PHP internal server error?
echo "<div class ='$row['id'].'>";
First line in while loop should look like this
echo "<div class = '".$row['id']."'>";
or
echo "<div class = '$row['id']'>"
You have mixed different apostrophe, try this:
echo "<div class ='" . $row['id'] . "'>";
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
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++;
}
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>"
?>