473,406 Members | 2,387 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,406 software developers and data experts.

Problems outputting to a 3 column table in order to crate an image gallery

ste
Hi there,

I'm trying to query a MySQL database (containing image data) and to output
the results in a HTML table of 3 columns wide (and however many rows it
takes) in order to create a basic thumbnail gallery.

I can query and output the data as a single column table, but I'm having
problems filling up a 3 column table (with different images, looping
continuously until the end).

I would appreciate it if someone could take a look at the code below and
point me in the right direction or suggest any extra lines of code which
would finish the job?

The code below puts an image on each row, instead of filling up 3 columns on
each row with a different image.

I open this gallery by going to
mywebsite.com/gallery.php?imagetype=landscape (i.e. to access the landscape
gallery).

Thank you for any help,

Ste
<?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");

$title = "Gallery";
$query = "SELECT * FROM mydatabase WHERE imagetype =
\"{$_GET['imagetype']}\"";
$result = mysql_query($query) or die ("Couldn't execute query.");

echo "<h2>$title</h2>";
echo "<table>";
echo "<tbody>";

while ($row = mysql_fetch_array($result))

{
extract($row);
echo "<tr>";
echo "<td><img src=\"$imagelocation\" />\n
</td>\n";
}

echo "</tr>";
echo "</table>\n";
?>
Apr 29 '06 #1
10 2215
Rik
Why don't I see the bumber three anywhere in the code? Have you tried making
it 3 rows? How?
But I'm in a generous mood tonight:
echo "\n<table>";
$i = 3;
while ($row = mysql_fetch_assoc($result))
{
if($i==3) echo "\n\t<tr>";
echo "\n\t\t<td><img src=\".$row['imagelocation']." /></td>";
$i--;
if($i==0) {
echo "\n\t<tr>";
$i = 3;
}
}
if($i!=3) echo "\n\t\t<td colspan=\"$i\"></td>\n\t</tr>";
echo "\n</table>";

Grtz,
--
Rik Wasmus
Apr 29 '06 #2
ste

"Rik" <lu************@hotmail.com> wrote in message
news:e2**********@netlx020.civ.utwente.nl...
Why don't I see the bumber three anywhere in the code? Have you tried
making
it 3 rows? How?
But I'm in a generous mood tonight:
echo "\n<table>";
$i = 3;
while ($row = mysql_fetch_assoc($result))
{
if($i==3) echo "\n\t<tr>";
echo "\n\t\t<td><img src=\".$row['imagelocation']." /></td>";
$i--;
if($i==0) {
echo "\n\t<tr>";
$i = 3;
}
}
if($i!=3) echo "\n\t\t<td colspan=\"$i\"></td>\n\t</tr>";
echo "\n</table>";

Grtz,
--
Rik Wasmus


Hi Rik,

I had two main outcomes when trying to write the code for this - the one you
saw in my post (which posted 1 image per table row), and another one which
did post 3 images per row, but each row contain identical images (i.e. it
was just duplicating code, as opposed to inserting the next record/image).
As I've not yet grasped enough skills with PHP yet, and in my frustration of
nearly seeing what I was hoping for, I completely overlooked the fact that
the number 3 should be in my code at some point! :-S

Thank you for being in such a generous mood and posting the above code, it's
appreciated! :-)

When I first ran it, I had a message saying "Parse error: parse error,
unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or
T_NUM_STRING" on the line containing the code 'imagelocation' above.

I googled this message and found the solution was to remove the apostrophe's
surrounding ['imagelocation'], and this now works great! I also inserted a
\ in front of the quotation mark after imagelocation, but I'm not sure if
this made any difference at all.

Thanks again, I'd have *never* have gotten there with this assistance.

Ste
Apr 30 '06 #3
Rik
ste wrote:
echo "\n\t\t<td><img src=\".$row['imagelocation']." /></td>";

When I first ran it, I had a message saying "Parse error: parse error,
unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or
T_VARIABLE or T_NUM_STRING" on the line containing the code
'imagelocation' above.

It should offcourse be:
echo "\n\t\t<td><img src=\"".$row['imagelocation']."\" /></td>";

--
Rik Wasmus
Apr 30 '06 #4
ste

"Rik" <lu************@hotmail.com> wrote in message
news:e3**********@netlx020.civ.utwente.nl...
ste wrote:
echo "\n\t\t<td><img src=\".$row['imagelocation']." /></td>";

When I first ran it, I had a message saying "Parse error: parse error,
unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or
T_VARIABLE or T_NUM_STRING" on the line containing the code
'imagelocation' above.

It should offcourse be:
echo "\n\t\t<td><img src=\"".$row['imagelocation']."\" /></td>";

--
Rik Wasmus

Thanks Rik, I've now updated it.

Now when I open this page: mywebsite.com/gallery.php?imagetype=landscape
it opens all the images from my database which are tagged with the category
of landscape.

Likewise, going to the url: mywebsite.com/gallery.php?imagetype=portrait
it opens all the images from my database which are tagged with the category
of portrait.

Is there a way to open the gallery showing ALL pictures, without having to
re-write the sql query? i.e. is there a tag I can enter in a URL which
displays images where the imagetype could literally be anything?
mywebsite.com/gallery.php?imagetype=anything

If there is such a term, I haven't been able to find it, but thought I'd ask
anyway in case you knew otherwise.

Thanks,

Ste
Apr 30 '06 #5
Rik
ste wrote:
Likewise, going to the url:
mywebsite.com/gallery.php?imagetype=portrait
it opens all the images from my database which are tagged with the
category of portrait.

Is there a way to open the gallery showing ALL pictures, without
having to re-write the sql query? i.e. is there a tag I can enter in
a URL which displays images where the imagetype could literally be
anything? mywebsite.com/gallery.php?imagetype=anything

just don't set the imagetype, so the url would be mywebsite.com/gallery.php,
and use this code:

//create database $connection

if(get_magic_quotes_gpc()){
$_GET['imagetype'] = stripslashes($_GET['imagetype']);
}
$imagetype = mysql_real_escape_string( $_GET['imagetype'], $connection);
$where = (isset($_GET['imagetype'])) ? "WHERE imagetype =
'".$_GET['imagetype']."'" : '';
$query = "SELECT * FROM mydatabase ".$where;

Grtz,
--
Rik Wasmus
Apr 30 '06 #6
Rik
Rik wrote:
$where = (isset($_GET['imagetype'])) ? "WHERE imagetype =

'".$_GET['imagetype']."'" : '';

Should be:
$where = (isset($_GET['imagetype'])) ? "WHERE imagetype = '".$imagetype."'"
: '';
--
Rik Wasmus
Apr 30 '06 #7
ste

"Rik" <lu************@hotmail.com> wrote in message
news:e3**********@netlx020.civ.utwente.nl...
Rik wrote:
$where = (isset($_GET['imagetype'])) ? "WHERE imagetype =

'".$_GET['imagetype']."'" : '';

Should be:
$where = (isset($_GET['imagetype'])) ? "WHERE imagetype =
'".$imagetype."'"
: '';
--
Rik Wasmus

Thanks for this Rik. If I'm understanding this correctly, adding the above
code to the existing gallery script will allow all images in the database to
be displayed when a user opens the gallery.php (as it was currently
displaying a blank page), or, it can still be used to open specific
galleries when it is used with the extension of
gallery.php?imagetype=landscape

I have added your code to the existing code, and also removed the previous
line of code which contained the old $query line.

Unfortunately (and I've probably done something wrong here), the code below
(which is how I've incorporated your latest piece of code into the existing
code) doesn't quite do this - when I open gallery.php, it's not opening all
the images, but is instead opening all images which have a blank value for
imagetype (which would normally be none, but I've just added an image to the
database to test, which is why I know this is what's happening). When I
open gallery.php?imagetype=landscape, this works as it did previously so no
problems here. Have I added it correctly?

<?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");

if(get_magic_quotes_gpc()){
$_GET['imagetype'] = stripslashes($_GET['imagetype']);
}

$imagetype = mysql_real_escape_string( $_GET['imagetype'], $connection);
$where = (isset($_GET['imagetype'])) ? "WHERE imagetype = '".$imagetype."'":
'';
$query = "SELECT * FROM mydatabase ".$where;
$result = mysql_query($query) or die ("Couldn't execute query.");

echo "\n<div id=\"content\">";
echo "\n<h2>Sample Gallery</h2>";
echo "\n<table id=\"thumbgallery\" cellSpacing=\"0\" cellPadding=\"10\"
width=\"400\" border=\"0\">";
echo "\n<tbody>";
$i = 3;
while ($row = mysql_fetch_assoc($result))
{
if($i==3) echo "\n\t<tr>";
echo "\n\t\t<td valign=\"top\" width=\"113\" id=\"thumbs\"><img
src=\"".$row['imagelocation']."\" width=\"113\" border=\"0\" /><p
class=\"captionref\">Ref: ".$row['imageid']."</p><p
class=\"caption\">".$row['imagecaption']."</p></td>";
$i--;
if($i==0) {
echo "\n\t<tr>";
$i = 3;
}
}
if($i!=3) echo "\n\t\t<td colspan=\"$i\"></td>\n\t</tr>";
echo "\n</tbody>";
echo "\n</table>";

?>
Thanks,

Ste
Apr 30 '06 #8
Rik
The only thing I can think of is that the following code sets
$_GET['imagetype'], allthough it didn't previsously exist.

if(get_magic_quotes_gpc()){
$_GET['imagetype'] = stripslashes($_GET['imagetype']);
}

Try echoing your query, is should be "SELECT * FROM mydatabase" and nothing
more.
Else try:

$where = (isset($_GET['imagetype'])&&$_GET['imagetype']!='') ? "WHERE
imagetype = '".$imagetype."'":'';

Grtz,
--
Rik Wasmus
Apr 30 '06 #9
ste

"Rik" <lu************@hotmail.com> wrote in message
news:e3**********@netlx020.civ.utwente.nl...
The only thing I can think of is that the following code sets
$_GET['imagetype'], allthough it didn't previsously exist.

if(get_magic_quotes_gpc()){
$_GET['imagetype'] = stripslashes($_GET['imagetype']);
}

Try echoing your query, is should be "SELECT * FROM mydatabase" and
nothing
more.
Else try:

$where = (isset($_GET['imagetype'])&&$_GET['imagetype']!='') ? "WHERE
imagetype = '".$imagetype."'":'';

Grtz,
--
Rik Wasmus

Hi Rik,

I replaced the existing $where line with the one above as per your
suggestion, and this now works perfectly! :-)

Old: $where = (isset($_GET['imagetype'])) ? "WHERE imagetype =
'".$imagetype."'": '';
New: $where = (isset($_GET['imagetype'])&&$_GET['imagetype']!='') ? "WHERE
imagetype = '".$imagetype."'":'';

I wouldn't have a clue why one works and one doesn't (well, they both worked
of course, but 1st one returned only blank fields, and the 2nd one returned
all fields which is what I wanted), but I've got a few books to work through
now so I'll revisit this post in a few months (if I'm lucky!) and let you
know! :-)

Thanks for all your help Rik, it's appreciated.

Ste
May 1 '06 #10
Rik
ste wrote:
The only thing I can think of is that the following code sets
$_GET['imagetype'], allthough it didn't previsously exist.

if(get_magic_quotes_gpc()){
$_GET['imagetype'] = stripslashes($_GET['imagetype']);
}

I replaced the existing $where line with the one above as per your
suggestion, and this now works perfectly! :-)
I wouldn't have a clue why one works and one doesn't


Like I said above, $_GET['imagetype'] gets set by the code, even if it
doesn't exist, as an empty string.

Grtz,
--
Rik Wasmus
May 1 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Oliver | last post by:
Hi, I have a strange problem and I am not sure where to look for the core reason. I have a homepage in php which is working fine since ages and I have now a new server with a fresh Suse 9.1...
8
by: CAFxX | last post by:
i'm writing a program that executes some calculations on a bitmap loaded in memory. these calculation ends up with pixel wth values far over 255, but i need them to be between 0 and 255 since i...
2
by: me | last post by:
I would like to add an Identity to an existing column in a table using a stored procedure then add records to the table and then remove the identity after the records have been added or something...
2
by: robert | last post by:
I've made a gallery site for an artist and it works well -- the gallery page has a central large image which is swapped when thumbnail images are clicked. I'm using Dreamweaver's "SwapImage"...
6
by: ste | last post by:
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...
2
by: teddymeu | last post by:
Hi Guys, this is kinda complicated but ill do my best to explain. I have two tables. products and categories. Products holds product info and an image, its primary key is ProductID. Category table...
4
tjc0ol
by: tjc0ol | last post by:
Hi guys, I'm a newbie in php and I got error in my index.php which is: 1054 - Unknown column 'p.products_id' in 'on clause' select p.products_image, pd.products_name, p.products_id,...
4
by: saunders1989 | last post by:
Hi, my goal is to create a website with an image gallery. i have about 6 buttons at the bottom of the page whcih when clicked will take you to a different album of photos. i have created the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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.