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++;
}
Related
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
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>';
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 use str_replace with $row link
now my link is showing like this
example.com/view.php?category=laptop&model=dell i5
and i want like this
example.com/view.php?category=laptop&model=dell_i5
this is a link code
<a href=\"detail.php?category=" . $row['category'] . "&model=" . $row['model'] ."\" >
so how can i use str_replace with the link " . $row['model'] ."
please help me to solve this issue
Complete code
<?php
//connect to database
mysql_connect('localhost','user','password');
mysql_select_db('newalldata');
$page = (empty($_GET['page'])) ? 1 : $_GET['page'];
$max_results = 6;
$from = (($page * $max_results) - $max_results);
if(empty($_POST)) {
$query = "SELECT * FROM alldata LIMIT $from, $max_results";
}
$result = mysql_query("SET NAMES utf8"); //the main trick
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_num_rows($result);
$count=0;
while($row = mysql_fetch_array($result))
{
if($count%6==0)
{
echo "<tr/>";
echo "<tr>";
}
echo "<td><hr><div class='style99' align='center'><img src='/media/image.php?width=200&height=210&image=/media/" . $row['photo'] . "' title=". $row['price'] ." alt=". $row['model'] ." style='FILTER: alpha(opacity=100);-moz-opacity: 1.0; opacity: 1.0;' onmouseover=BeginOpacity(this,100,40) onmouseout=EndOpacity(this,100)><p><font color='#3366FF'>" . $row['category'] . "</font></p><p><font color='#3366FF'>" . $row['model'] . "</font></p><p><font color='#336600'>" . $row['price'] . "</font></p><div></td>";
$count++;
}
?>
$var = str_replace(" ","_",$row['model']);
<a href=\"detail.php?category=" . $row['category'] . "&model=" . $var ."\" >;
should do it
You may check http://php.net/manual/de/function.str-replace.php
The first parameter is what you search ( in this case the " ") , the second is what u want to heave instead ("_") and then which string is checked.
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].'">