By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,313 Members | 1,113 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,313 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.