473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1943
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: jonathan.beckett | last post by:
Hi All, For the past few months I have been working on an open source Apache/PHP/MySQL content management system - and have recently made it available for download. It's still very much a...
4
by: Altramagnus | last post by:
I have 30 - 40 type of different window. For each type I need about 20 instances of the window. When I try to create them, I get "Error creating window handle" My guess is there is a maximum...
55
by: drhowarddrfine | last post by:
I'm working on a web site that could use some control using js but am concerned about what problems I may have with potential users having their js turned off. Has anyone had any serious problems...
7
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
1
by: jdurden | last post by:
am currently woking on building this site: www.maverick-spirit.com. everything seems to be fine except the press releases page. When you click on one of the press releases in the left column under...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.