469,950 Members | 2,376 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

Problems Creating the Code to Open Images Within a Template PHP Page

ste
Hi there,

I'm just beginning to learn PHP and MySQL, but I'm finding it difficult! I
wondered if someone could help me out with a problem I'm having, or at least
point me in the right direction? I have setup a MySQL database which
contains the following, though I would like to expand on this in the future
with lots of extra fields:

IMAGES TABLE:
-------------------
imageid (primary key)
imagelocation (url location for image)
imagecaption (caption for image, not used in the code below, but will be
used once I suss this out!)

What I want to do is, from a HTML gallery of thumbnails, be able to open a
larger version of each thumbnail image in a nice pretty formatted HTML page.
Each HTML page would be identical, so that's why I only want to create this
once, as opposed to hard coding them all. As I want to keep this simple and
one step at a time, I'm prepared to create the image gallery and the
appropriate image URL's.

My problem is that I'm unsure of the php/mysql I need to write in order to
open the appropriate picture in the image template. For example, if I hover
over and click 'image 1,' I would like the browser to open the page
www.mywebsite.com/imagetemplate.php?id=1 This would open the image template
page with image 1 visible within it.

So when executing the sql query which selects a particular image from the
database, I would like it to find the record which has an ID equal to the ID
in the URL above (in this case, 1).

Now what I've tried isn't working as I've got it all wrong, but here it is
for interest:

DODGY CODE:
------------------

<?php

/* this is the include file for my database passwords */
include("my_db_login.inc");

/* this is the code to make the connection to the database */
$connection = mysql_connect($host,$user,$password) or die ("couldn't connect
to server");
$db = mysql_select_db($database,$connection) or die ("Couldn't select
database");

/* this query SHOULD be requesting all records from My Database where the
ImageID is equal to the ImageID listed in the URL as mentioned above */
$query = "SELECT * FROM my_database WHERE imageid =
\"{$_POST['imageid']}\"";
$result = mysql_query($query) or die ("Couldn't execute query.");
/* the line of code below is actually the code from the table cell which
SHOULD insert the image location url for the image where the id is equal to
the one requested in the url above */
echo "<img src=\"{$row['imagelocation']}\" width=\"500\" border=\"0\" />";

?>

I'd be grateful for any help with this as I'm obviously doing something (or
many things!) wrong.

Thanks,

Ste
Apr 28 '06 #1
6 1698
ste wrote:
Hi there,

I'm just beginning to learn PHP and MySQL, but I'm finding it difficult! I
wondered if someone could help me out with a problem I'm having, or at least
point me in the right direction? I have setup a MySQL database which
contains the following, though I would like to expand on this in the future
with lots of extra fields:

IMAGES TABLE:
-------------------
imageid (primary key)
imagelocation (url location for image)
imagecaption (caption for image, not used in the code below, but will be
used once I suss this out!)

What I want to do is, from a HTML gallery of thumbnails, be able to open a
larger version of each thumbnail image in a nice pretty formatted HTML page.
Each HTML page would be identical, so that's why I only want to create this
once, as opposed to hard coding them all. As I want to keep this simple and
one step at a time, I'm prepared to create the image gallery and the
appropriate image URL's.

My problem is that I'm unsure of the php/mysql I need to write in order to
open the appropriate picture in the image template. For example, if I hover
over and click 'image 1,' I would like the browser to open the page
www.mywebsite.com/imagetemplate.php?id=1 This would open the image template
page with image 1 visible within it.

So when executing the sql query which selects a particular image from the
database, I would like it to find the record which has an ID equal to the ID
in the URL above (in this case, 1).

Now what I've tried isn't working as I've got it all wrong, but here it is
for interest:

DODGY CODE:
------------------

<?php

/* this is the include file for my database passwords */
include("my_db_login.inc");

/* this is the code to make the connection to the database */
$connection = mysql_connect($host,$user,$password) or die ("couldn't connect
to server");
$db = mysql_select_db($database,$connection) or die ("Couldn't select
database");

/* this query SHOULD be requesting all records from My Database where the
ImageID is equal to the ImageID listed in the URL as mentioned above */
$query = "SELECT * FROM my_database WHERE imageid =
\"{$_POST['imageid']}\"";
$result = mysql_query($query) or die ("Couldn't execute query.");
/* the line of code below is actually the code from the table cell which
SHOULD insert the image location url for the image where the id is equal to
the one requested in the url above */
echo "<img src=\"{$row['imagelocation']}\" width=\"500\" border=\"0\" />";

?>

I'd be grateful for any help with this as I'm obviously doing something (or
many things!) wrong.

Thanks,

Ste


You're close. mysql_query() returns a result object; you need to retrieve the
actual data (into $row in your example).

After the mysql_query() add the following:

$row = mysql_fetch_array($result);
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 28 '06 #2
ste

"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:G5******************************@comcast.com. ..
<snip>
You're close. mysql_query() returns a result object; you need to retrieve
the actual data (into $row in your example).

After the mysql_query() add the following:

$row = mysql_fetch_array($result);
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================


Hi Jerry,

Thanks for getting back to me - I'm close? I'm astonished as I thought I
would be way out!

I *think* I've put your line of code in the right place, but this still
doesn't work I'm afraid. When the page opens, there's no image - if I look
at the HTML, it is basically returning <img src=""> instead of <img
src="images/image1/jpg">. Perhaps the query is right but my code to insert
the field name (which contains the image URL, it's called 'imagelocation')
isn't?

Here's the code again with the extra line - did I put it in the right place?

<?php
include("my_db_login.inc");

$connection = mysql_connect($host,$user,$password) or die ("couldn't connect
to server");
$db = mysql_select_db($database,$connection) or die ("Couldn't select
database");

$query = "SELECT * FROM my_database WHERE imageid =
\"{$_POST['imageid']}\"";
$result = mysql_query($query) or die ("Couldn't execute query.");
$row = mysql_fetch_array($result);

echo "<img src=\"{$row['imagelocation']}\" width=\"500\" border=\"0\" />";
?>

Thanks,

Ste
Apr 28 '06 #3
ste wrote:
"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:G5******************************@comcast.com. ..
<snip>
You're close. mysql_query() returns a result object; you need to retrieve
the actual data (into $row in your example).

After the mysql_query() add the following:

$row = mysql_fetch_array($result);
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Hi Jerry,

Thanks for getting back to me - I'm close? I'm astonished as I thought I
would be way out!

I *think* I've put your line of code in the right place, but this still
doesn't work I'm afraid. When the page opens, there's no image - if I look
at the HTML, it is basically returning <img src=""> instead of <img
src="images/image1/jpg">. Perhaps the query is right but my code to insert
the field name (which contains the image URL, it's called 'imagelocation')
isn't?

Here's the code again with the extra line - did I put it in the right place?

<?php
include("my_db_login.inc");

$connection = mysql_connect($host,$user,$password) or die ("couldn't connect
to server");
$db = mysql_select_db($database,$connection) or die ("Couldn't select
database");

$query = "SELECT * FROM my_database WHERE imageid =
\"{$_POST['imageid']}\"";
$result = mysql_query($query) or die ("Couldn't execute query.");
$row = mysql_fetch_array($result);

echo "<img src=\"{$row['imagelocation']}\" width=\"500\" border=\"0\" />";
?>

Thanks,

Ste


Ste,

I have no idea if it is 'imagelocation' or not. It's the name of the MySQL
column containing the information you want.

What happens if you do:

echo "<pre>\n";
print_r($row);
echo "</pre>\n";

This will give you the contents (including keys) of $row.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 28 '06 #4
ste

"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:rp********************@comcast.com...
<snip>
Ste,

I have no idea if it is 'imagelocation' or not. It's the name of the
MySQL column containing the information you want.

What happens if you do:

echo "<pre>\n";
print_r($row);
echo "</pre>\n";

This will give you the contents (including keys) of $row.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Hi Jerry,

There is a field called imagelocation in my database, so this is okay.

I entered your code above, and like the other piece of code I had, it
displayed nothing, besides some HTML tags <pre></pre>.

This made me examine the actual query again and made me wonder if anything
was being passed from the URL at all. I've got a PHP and MySQL book dor
dummies (I couldn't find myself a more basic one, so had to settle for this!
:-)), so out of interest and by chance, I looked up the $_POST command. In
the same chapter, I came across the $_GET command too, and although I still
don't know what they mean, the following sentence rang out to me: 'Contains
all the variables passed from a previous page as part of the URL.' Of
course, that still means nothing, except for the bit about the URL.

So I changed $_POST to $_GET, and it worked! I'm amazed, but I wouldn't
have even got this far without your help on this, so thank you. I'm working
by trial and error it has to be said, and I've lost count of the number of
times I've uploaded and tested php pages with my FTP browser recently! :-)

For info, the following piece of code is the code that works:

<?php
include("my_db_login.inc");

$connection = mysql_connect($host,$user,$password) or die ("couldn't connect
to server");
$db = mysql_select_db($database,$connection) or die ("Couldn't select
database");

$query = "SELECT * FROM my_database WHERE imageid = \"{$_GET['imageid']}\"";
$result = mysql_query($query) or die ("Couldn't execute query.");
$row = mysql_fetch_array($result);

echo "<img src=\"{$row['imagelocation']}\" width=\"500\" border=\"0\" />";
?>

I've even tested it by adding in other fields from the database, and that
works great too!

If you have any suggestions on how any of the above can be tidied up, please
don't hesitate to let me know, though it works so that's the main thing.

Finally, do let me know if you know of any good onlie php/mysql tutorials
for creating image galleries and the like.

Thanks again,

Ste
Apr 28 '06 #5
Rik
ste wrote:
I
looked up the $_POST command. In the same chapter, I came across the
$_GET command too, and although I still don't know what they mean,
the following sentence rang out to me: 'Contains all the variables
passed from a previous page as part of the URL.' Of course, that
still means nothing, except for the bit about the URL.


A bit simplefied:
$_GET contains values that are set in the url:
index.php?page=something
then $_GET['page'] is "something"
$_POST contains posted variables, usually by a form, that aren't in the url.

Have fun coding,
--
Rik Wasmus
Apr 28 '06 #6
ste

"Rik" <lu************@hotmail.com> wrote in message
news:e2**********@netlx020.civ.utwente.nl...
ste wrote:
I
looked up the $_POST command. In the same chapter, I came across the
$_GET command too, and although I still don't know what they mean,
the following sentence rang out to me: 'Contains all the variables
passed from a previous page as part of the URL.' Of course, that
still means nothing, except for the bit about the URL.


A bit simplefied:
$_GET contains values that are set in the url:
index.php?page=something
then $_GET['page'] is "something"
$_POST contains posted variables, usually by a form, that aren't in the
url.

Have fun coding,
--
Rik Wasmus


Thanks for that Rik, much clearer - you should write books for dummies! :-)

Ste
Apr 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by jonathan.beckett | last post: by
4 posts views Thread by Altramagnus | last post: by
55 posts views Thread by drhowarddrfine | last post: by
7 posts views Thread by Nathan Sokalski | last post: by
1 post views Thread by jdurden | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.