Forum Moderators: coopster
I now have a PHP page which contains nicely ordered images all extracted from a mySQL db... I was trying to figure a way where if I click on any of these images (call them thumbnails really) then it loads a new page which looks the same except that a column on the page now displays a bigger version of the image plus the various other (text) fields from the db. In essence, I want to click on any thumbnail and get the details of that product load with a bigger image on a new page (or even the same page if possible).
I played with some code that I'd used previously for a different purpose- this was for making actual changes to the db, whereas this is just grabbing and displaying the data- I was trying to work with the thumbnail as a "select" i.e firing off a bit of code to grab the relevant record from the db- but couldn't get it working:
The original code that displays the thumbnails on their page (three per row) is:
//code
while ($row= mysql_fetch_array($result)) {
$title = $row["Image"];
echo '<td><img src="images/products/' . $title . '.jpg"></td>';
if ( ($count % 3) == 0 ) {
echo '</tr><tr>';
}
$count++ ;
}
//end code
And the changed code I attempted to make in order to make clicking the thumbnail grab the details of the relevant field:
//code
while ($row= mysql_fetch_array($result)) {
$title = $row["Image"];
echo '<td><input type="image" src="images/products/' . $title . '.jpg" name="showdetails"></td>';
echo '<td><input name="_Image" size="30" type="hidden" value=$title /></td>';
if ( ($count % 3) == 0 ) {
echo '</tr><tr>';
}
$count++ ;
}
//end code
I tried using the "hidden" input as a way of storing $title so it could be used in the showdetails.php called by the input, which is as follows:
//code
<?php
if($_POST['showdetails'])
{
$title =@$_POST['_Image'];
include ('inc/dbconnect.php');
$query = "SELECT * FROM trios WHERE Image = '$title'";
}
if($result = mysql_query($query))
{
header('location: showdetails.php');
exit;
}
else
{
echo "ERROR: ".mysql_error();
}
?>
//end code
The idea was that I could then output the record fields with something like the following:
//code
while ($row= mysql_fetch_array($result)) {
$title = $row["Image"];
$title2 = $row["Text1"];
$title3 = $row["Text2"];
echo "<form action=\"display.php\" method=\"post\" name=\"display\">
<table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">
<tr>
<td width=\"200\">Image" . $title . "</td>
<td><input name=\"_Image\" size=\"30\" value='$title' type=\"hidden\" /></td>
</tr>
<tr>
<td>Details " . $title2 . "</td>
<td><input name=\"_Text1\" size=\"30\" value='$title2' type=\"hidden\" /></td>
</tr>
<tr>
<td>Price</td>
<td><textarea name=\"_Price\" cols=\"40\" rows=\"5\" value='$title3'>$title3</textarea></td>
</tr>
</table><br />
</form>";
$count++ ;
}
//end code
I'm sure it can be done much more simply (not that this works anyway!)...?
if so the logic is as such
use the unique identifier to create a link on your image. something like this if you have an id
<a href="/getbigimganddetail.php?img=12345"><img src="someimg.jpg" width="80" height="80"></a>
or maybe this for a string
<a href="/getbigimganddetail.php?img=my_cool_image"><img src="someimg.jpg" width="80" height="80"></a>
the getbigimganddetail.php would then grab the $_GET parameter, validate it and use it to select the appropriate data from your database.
get that portion working and then worry about display as it is fairly straight forward after that
//code
while ($row= mysql_fetch_array($result)) {
$title = $row["Image"];
$itemId = $row['itemId'];
echo '<td><a href="display.php?itemId=$itemId"><img src="images/products/' . $title . '.jpg"></a></td>';
if ( ($count % 3) == 0 ) {
echo '</tr><tr>';
}
$count++ ;
}
//end code
//code
while ($row= mysql_fetch_array($result)) {
$title = $row["Image"];
$Reference = $row['Reference'];
echo '<td><a href="display.php?Reference=$Reference"><img src="images/products/' . $title . '.jpg"></a></td>';
if ( ($count % 3) == 0 ) {
echo '</tr><tr>';
}
$count++ ;
}
//end code
Then on display.php, to test (not sure if this is ok) this PHP:
<?php
if($_POST['display'])
{
// Get the search variable from URL
$Reference =@$_POST['Reference'];
include ('inc/dbconnect.php');
// Build SQL Query
$query = "SELECT * FROM trios WHERE Reference = '$Reference'"; // specify the table and field names for the SQL query
}
if($result = mysql_query($query))
{
header('location: display.php');
exit;
}
else
{
echo "ERROR: ".mysql_error();
}
?>
But I'm getting this error on the page: ERROR: Access denied for user 'username'@'localhost' (using password: NO)
However the dbconnect.php is definitely ok (includes username and password), and I can access the database with the specified password without any problem.
Any ideas?
Any ideas?
I changed the code slightly as follows:
On the first page
//code
while ($row= mysql_fetch_array($result)) {
$title = $row["Image"];
$refnumber = $row['Reference'];
echo '<td><a href="display.php?Reference=$refnumber"><img src="images/products/' . $title . '.jpg"></a></td>';
if ( ($count % 3) == 0 ) {
echo '</tr><tr>';
}
$count++ ;
}
On the display.php:
//code
<?php
include ('inc/dbconnect.php');
$refnumber =@$_GET['Reference'];
// Build SQL Query
$query = "select * from trios WHERE Reference like $refnumber";
if($result = mysql_query($query))
{
//go to the new member confirmation page
header('location: display.php');
exit;
}
else
{
echo "ERROR: ".mysql_error();
}
?>
This is what I've got to so far:
<?php
include ('inc/dbconnect.php');
// Get the search variable from URL
$refnumber =@$_GET['Reference'];
// Build SQL Query
$query = "select * from trios WHERE Reference = '$refnumber'"; // specify the table and field names for the SQL query
//Now I try to display the results on the page
if($result = mysql_query($query))
{
$title = $row["Image"];
$title2 = $row["Title"];
$title3 = $row["Reference"];
echo "<table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">
<tr>
<td width=\"200\">Image " . $title . "</td>
</tr>
<tr>
<td>Title " . $title2 . "</td>
</tr>
<tr>
<td>Reference " . $title3 . "</td>
</tr>
</table>";
}
else
{
echo "ERROR: ".mysql_error();
}
?>
But it's not displaying...
// Build SQL Query
$query = "select * from trios WHERE Reference = '$refnumber'"; // specify the table and field names for the SQL query
$result = mysql_query($query);//if valid query (rows found)
if(mysql_num_rows($result)){
{$row = mysql_fetch_assoc($result);
$title = $row["Image"];
$title2 = $row["Title"];
$title3 = $row["Reference"];
<?php
include ('inc/dbconnect.php');// Get the search variable from URL
$refnumber = $_GET['Reference'];// Build SQL Query
$query = "select * from trios WHERE Reference = '$refnumber'"; // specify the table and field names for the SQL query
$result = mysql_query($query);
if(mysql_num_rows($result)) {$row = mysql_fetch_assoc($result);
$title = $row["Image"];
$title2 = $row["Title"];
$title3 = $row["Reference"];echo "<table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">
<tr>
<td width=\"200\">Image " . $title . "</td>
</tr>
<tr>
<td>Title " . $title2 . "</td>
</tr>
<tr>
<td>Reference " . $title3 . "</td>
</tr>
</table>";
}
else {
echo "ERROR: ".mysql_error();
}
?>
Also the display.php displays in the browser with the URL display.php?Reference=$refnumber
Is this correct? Should it not show the particular product ID (Reference in this case)e.g Reference=TRS1
"select * from trios WHERE Reference like 'TS9'" (and other product references)
- and it works. But it doesn't like the variable $refnumber for some reason, though I have no idea why. Any ideas? The code on the preceding page, where the variable is generated, hasn't been changed.
I changed the query to just "select * from trios" and this, intertestingly, brings up just the first record in the table as a result (no matter which graphic I click on).
Also the display.php displays in the browser with the URL display.php?Reference=$refnumber
Is this correct? Should it not show the particular product ID (Reference in this case)e.g Reference=TRS1
This is not correct. It should display the value of $refnumber, instead of the variable name. It is the way you code the link where PHP does not parse the variable.
rewrite this::
while ($row= mysql_fetch_array($result)) {
$title = $row["Image"];
$Reference = $row['Reference'];
echo '<td><a href="display.php?Reference=$Reference"><img src="images/products/' . $title . '.jpg"></a></td>';
while ($row= mysql_fetch_assoc($result)) {
$title = $row['Image'];
$Reference = $row['Reference'];
echo '<td><a href="display.php?Reference='.$Reference.'"><img src="images/products/' . $title . '.jpg"></a></td>';
But the fields themselves from the appropriate record still don't display on the page for some reason. The code I now have for display.php is:
//start code
<?php
include ('inc/dbconnect.php');
// Get the search variable from URL
$refnumber =@$_POST['Reference'];
// Build SQL Query
$query = "select * from trios WHERE Reference like '$refnumber'"; // specify the table and field names for the SQL query
$result = mysql_query($query) or die("The System is undergoing maintenance at the moment and will be available shortly");
if($result = mysql_query($query))
{
$row = mysql_fetch_assoc($result);
$title = $row["Image"];
$title2 = $row["Title"];
$title3 = $row["Reference"];
echo "<table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">
<tr>
<td>Page " . $title . "</td>
</tr>
<tr>
<td>Paragraph Number '$title2' </td>
</tr>
<tr>
<td>Reference '$title3' </td>
</tr>
</table>";
}
else
{
echo "ERROR: ".mysql_error();
}
?>
//end code
All that displays on the page is the text from inside the td's. The query presumably runs because it doesn't generate an error, but it isn't picking anything up from the db. However as before, if I substitute the $refnumber in the query for an actual value such as TR1, it works.
?