For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > February 2005 > help with previous and next buttons on gallery









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author help with previous and next buttons on gallery
DaRemedy

2005-02-03, 8:55 pm

I've created a gallery which pulls out the records from the database using
the variables $folder (galleryID) and &pID (pictureID), for example,
picviewer.php?folder=1&picID=1.

All is working, only problem is that when the picviewer.php page displays
the image, I would like a previous and next buttons so that all the records
within folder=1 will display in sequence depending on whether the forward or
backward button is clicked upon.

so far, I have tried,
<? $folderref = $row_tblGalleryNames['gID'];
<? if ($folderref==$folder) { ?><a href="viewer2.php?folder=<? echo $folder;
?>&pid=<?php echo $row_rsPics['picID']+1; ?><? } ?>
?>

But it doesn't work!

Please help.

Many thanks.


Oli Filth

2005-02-03, 8:55 pm

DaRemedy wrote:
> I've created a gallery which pulls out the records from the database using
> the variables $folder (galleryID) and &pID (pictureID), for example,
> picviewer.php?folder=1&picID=1.
>
> All is working, only problem is that when the picviewer.php page displays
> the image, I would like a previous and next buttons so that all the records
> within folder=1 will display in sequence depending on whether the forward or
> backward button is clicked upon.
>
> so far, I have tried,
> <? $folderref = $row_tblGalleryNames['gID'];
> <? if ($folderref==$folder) { ?><a href="viewer2.php?folder=<? echo $folder;
> ?>&pid=<?php echo $row_rsPics['picID']+1; ?><? } ?>
> ?>
>
> But it doesn't work!


It would help if you could say in what way it doesn't work. What does it
do instead?

P.S. Going in and out of PHP with <? ?> all the time makes it very hard
to read. For trivial things like this, why not just use echo?

P.P.S. Whenever you want an ampersand (&) in HTML, you should write
& (ampersand character, "amp", semi-colon), even inside href URIs.


--
Oli
DaRemedy

2005-02-04, 3:56 pm

Many thanks for the tips, Oli

Right, when trying to click on the next button, it does go to the next
picture, but as soon as all the pictures are over in that particular
gallery, and the folderref in the database changes, it still moves to the
next set of gallery pics where the $folder=2. I don't know if this is making
any sense??

basically, I click on a link on the gallery page, for example, family pics.
The family pics gallery has a folderref of 1, when I click on the next
buttons it moves to the next picture in the database but as soon as the
folderref changes, for example, folderref=2, in the database, the next
button can still be clicked when it shouldn't be able to!

Any ideas?


"Oli Filth" <oli_filth@eatspam.coldmail.com> wrote in message
news:gHyMd.1102$7R1.11@newsfe5-gui.ntli.net...
> DaRemedy wrote:
using[color=darkred]
displays[color=darkred]
records[color=darkred]
forward or[color=darkred]
$folder;[color=darkred]
>
> It would help if you could say in what way it doesn't work. What does it
> do instead?
>
> P.S. Going in and out of PHP with <? ?> all the time makes it very hard
> to read. For trivial things like this, why not just use echo?
>
> P.P.S. Whenever you want an ampersand (&) in HTML, you should write
> & (ampersand character, "amp", semi-colon), even inside href URIs.
>
>
> --
> Oli



Oli Filth

2005-02-04, 3:56 pm

DaRemedy wrote:
> Many thanks for the tips, Oli
>
> Right, when trying to click on the next button, it does go to the next
> picture, but as soon as all the pictures are over in that particular
> gallery, and the folderref in the database changes, it still moves to the
> next set of gallery pics where the $folder=2. I don't know if this is making
> any sense??
>
> basically, I click on a link on the gallery page, for example, family pics.
> The family pics gallery has a folderref of 1, when I click on the next
> buttons it moves to the next picture in the database but as soon as the
> folderref changes, for example, folderref=2, in the database, the next
> button can still be clicked when it shouldn't be able to!
>
> Any ideas?
>
>
> "Oli Filth" <oli_filth@eatspam.coldmail.com> wrote in message
> news:gHyMd.1102$7R1.11@newsfe5-gui.ntli.net...
>
>
> using
>
>
> displays
>
>
> records
>
>
> forward or
>
>
> $folder;
>
>
>
>


You have the line
$folderref = $row_tblGalleryNames['gID'];

What does gID hold? This is important, because you're using $folderref
to check whether or not to print the <A href=...>.

Also, why do you need to pass the pic ID *and* the folder ID in the URI
for the next button? Surely the picID on its own is enough information
to identify a particular picture, assuming picID is set as a unique
field within the database table.

--
Oli
DaRemedy

2005-02-04, 3:56 pm



"Oli Filth" <oli_filth@eatspam.coldmail.com> wrote in message
news:ajJMd.255$8G5.51@newsfe1-gui.ntli.net...
> DaRemedy wrote:
the[color=darkred]
making[color=darkred]
pics.[color=darkred]
>
> You have the line
> $folderref = $row_tblGalleryNames['gID'];
>
> What does gID hold? This is important, because you're using $folderref
> to check whether or not to print the <A href=...>.
>
> Also, why do you need to pass the pic ID *and* the folder ID in the URI
> for the next button? Surely the picID on its own is enough information
> to identify a particular picture, assuming picID is set as a unique
> field within the database table.
>
> --
> Oli


$row_tblGalleryNames['gID']; holds the gallery ID, so:
gID gName
1 Family Pics

the gID is the same as folderref

tblPics
picID picPath folderRef
1 image1.jpg 1

I've called out both picID and folderID, just in case I have two images with
the same name but in different folders


Oli Filth

2005-02-04, 3:56 pm

DaRemedy wrote:
>
> $row_tblGalleryNames['gID']; holds the gallery ID, so:
> gID gName
> 1 Family Pics
>
> the gID is the same as folderref
>
> tblPics
> picID picPath folderRef
> 1 image1.jpg 1
>


So $folderref is the gallery ID, but then so is $folder.
So ($folderref==$folder) will always be TRUE, so you'll always print the
<A href=...>.

> I've called out both picID and folderID, just in case I have two images with
> the same name but in different folders


But you're identifying the pictures by ID, not by name. And IDs should
be unique within the tblPics table, i.e. no two pictures should have the
same value of picID.


--
Oli
DaRemedy

2005-02-04, 3:56 pm

Oh, I remember why I used the $folder as well as the $picID, the folderref
forms part of the image path:

e.g. images/gallery/$folder/$pic

How would you suggest I handle the previous and next button issue?

I bet you're thinking "what a noob!", hehe

"Oli Filth" <oli_filth@eatspam.coldmail.com> wrote in message
news:LcKMd.327$hQ1.270@newsfe1-win.ntli.net...
> DaRemedy wrote:
>
> So $folderref is the gallery ID, but then so is $folder.
> So ($folderref==$folder) will always be TRUE, so you'll always print the
> <A href=...>.
>
with[color=darkred]
>
> But you're identifying the pictures by ID, not by name. And IDs should
> be unique within the tblPics table, i.e. no two pictures should have the
> same value of picID.
>
>
> --
> Oli



Oli Filth

2005-02-04, 3:56 pm

DaRemedy wrote:
> Oh, I remember why I used the $folder as well as the $picID, the folderref
> forms part of the image path:
>
> e.g. images/gallery/$folder/$pic
>
> How would you suggest I handle the previous and next button issue?
>


I would have the table structure as so:

pics
====
id, folderId, name, etc...

folders
=======
id, name, etc...

where id is set as an autoincrement unique field in each table.

Here is a snippet of (untested) code that should sort out the "next" button:

====== START PHP CODE ======

if (!isset($_GET["picID"]))
{
die("No picture selected.");
}

$picID = (int)$_GET["picID"];

$query = mysql_query("SELECT pics.*, folders.name AS foldername"
. "FROM pics, folders "
. " WHERE pics.id = " . $picID
. " AND pics.folderId = folders.id");

if ($row = mysql_fetch_assoc($query))
{
/* ... show picture and whatever ... */

// now deal with "next" button
$query = mysql_query("SELECT id FROM pics"
. " WHERE id > " . $picID
. " AND folderId = " . $row["folderId"]
. " LIMIT 1");

if ($row2 = mysql_fetch_assoc($query))
{
echo "<A href=\"gallery.php?picId=" . $row2["id"] . "\">Next</A>";
}
}
else
{
die("Couldn't find specified picture.");
}

======= END PHP CODE =======

The first MySQL query joins the pics and folders tables, extracting the
corresponding folder-name for a particular picture id. So in row, you'll
have:

- id (picture ID)
- folderId (folder ID)
- name (picture file-name)
- foldername (folder name)

and any other details that you decide to put in the pics table.

After using this to create the HTML for the <IMG> and whatever else, the
second MySQL query is used to pull out the ID of the next picture in the
same folder. If this query returns no results (i.e. there are no more
pictures whose folderId matches) then the <A href="...">Next</A> won't
be printed.

This code might not be exactly correct, but it should give you the
general idea of what I was thinking.

> I bet you're thinking "what a noob!", hehe


We all had to learn how to do this (or something similar) at some point
in our PHP "careers"!! ;)



--
Oli
DaRemedy

2005-02-04, 3:56 pm

Thanks very much for your help, Oli.

I shall try your untested piece of code out tonight, and hopefully all will
be fine. You really seem to know your stuff and if you don't mind me asking,
how long have you been a php programmer?

Kindest Regards.

DR


"Oli Filth" <oli_filth@eatspam.coldmail.com> wrote in message
news:_kLMd.335$hQ1.64@newsfe1-win.ntli.net...
> DaRemedy wrote:
folderref[color=darkred]
>
> I would have the table structure as so:
>
> pics
> ====
> id, folderId, name, etc...
>
> folders
> =======
> id, name, etc...
>
> where id is set as an autoincrement unique field in each table.
>
> Here is a snippet of (untested) code that should sort out the "next"

button:
>
> ====== START PHP CODE ======
>
> if (!isset($_GET["picID"]))
> {
> die("No picture selected.");
> }
>
> $picID = (int)$_GET["picID"];
>
> $query = mysql_query("SELECT pics.*, folders.name AS foldername"
> . "FROM pics, folders "
> . " WHERE pics.id = " . $picID
> . " AND pics.folderId = folders.id");
>
> if ($row = mysql_fetch_assoc($query))
> {
> /* ... show picture and whatever ... */
>
> // now deal with "next" button
> $query = mysql_query("SELECT id FROM pics"
> . " WHERE id > " . $picID
> . " AND folderId = " . $row["folderId"]
> . " LIMIT 1");
>
> if ($row2 = mysql_fetch_assoc($query))
> {
> echo "<A href=\"gallery.php?picId=" . $row2["id"] . "\">Next</A>";
> }
> }
> else
> {
> die("Couldn't find specified picture.");
> }
>
> ======= END PHP CODE =======
>
> The first MySQL query joins the pics and folders tables, extracting the
> corresponding folder-name for a particular picture id. So in row, you'll
> have:
>
> - id (picture ID)
> - folderId (folder ID)
> - name (picture file-name)
> - foldername (folder name)
>
> and any other details that you decide to put in the pics table.
>
> After using this to create the HTML for the <IMG> and whatever else, the
> second MySQL query is used to pull out the ID of the next picture in the
> same folder. If this query returns no results (i.e. there are no more
> pictures whose folderId matches) then the <A href="...">Next</A> won't
> be printed.
>
> This code might not be exactly correct, but it should give you the
> general idea of what I was thinking.
>
>
> We all had to learn how to do this (or something similar) at some point
> in our PHP "careers"!! ;)
>
>
>
> --
> Oli



Oli Filth

2005-02-04, 3:56 pm

DaRemedy wrote:
> Thanks very much for your help, Oli.
>


No problem :)

> I shall try your untested piece of code out tonight, and hopefully all will
> be fine. You really seem to know your stuff and if you don't mind me asking,
> how long have you been a php programmer?


I've been using PHP about 6 months, but I've been doing C/C++/Basic for
years, and I'm learning Java at the moment.




P.S. In the code I posted, in the first mysql_query() call, there should
be a space before FROM, i.e.:

$query = mysql_query("SELECT pics.*, folders.name AS foldername"
. " FROM pics, folders "
. " WHERE pics.id = " . $picID
. " AND pics.folderId = folders.id");

--
Oli
DaRemedy

2005-02-07, 3:57 pm

The problem I have with this code is that I want the pics sorted by the
image name ascending, not the ID. By sorting the database by the image name,
the ID's aren't ascending and therefore I don't think that the statement
'WHERE id > " . $picID' will work because on some of the pics, the next ID
may be lower than the one displayed.

Any thoughts?

Thanks.

> // now deal with "next" button
> $query = mysql_query("SELECT id FROM pics"
> . " WHERE id > " . $picID
> . " AND folderId = " . $row["folderId"]
> . " LIMIT 1");
>
> if ($row2 = mysql_fetch_assoc($query))
> {
> echo "<A href=\"gallery.php?picId=" . $row2["id"] . "\">Next</A>"; }
> }
> else
> {
> die("Couldn't find specified picture.");
> }



liquidice

2005-02-07, 3:57 pm

DaRemedy.
I too am after something similar but cannot seem to sort it out. Please let
me know if you've managed to find the solution to this rather complicated
problem.

TIA

"DaRemedy" <demonnet@{nospam}btinternet.com> wrote in message
news:420736e4$0$19164$cc9e4d1f@news-text.dial.pipex.com...
> The problem I have with this code is that I want the pics sorted by the
> image name ascending, not the ID. By sorting the database by the image
> name, the ID's aren't ascending and therefore I don't think that the
> statement 'WHERE id > " . $picID' will work because on some of the pics,
> the next ID may be lower than the one displayed.
>
> Any thoughts?
>
> Thanks.
>
>
>



Geoff Berrow

2005-02-07, 3:57 pm

I noticed that Message-ID:
<420736e4$0$19164$cc9e4d1f@news-text.dial.pipex.com> from DaRemedy
contained the following:

>The problem I have with this code is that I want the pics sorted by the
>image name ascending, not the ID. By sorting the database by the image name,
>the ID's aren't ascending and therefore I don't think that the statement
>'WHERE id > " . $picID' will work because on some of the pics, the next ID
>may be lower than the one displayed.



Well that's nothing to do with next and previous, or WHERE for that
matter. You need to investigate ORDER BY
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Oli Filth

2005-02-07, 3:57 pm

DaRemedy wrote:
> The problem I have with this code is that I want the pics sorted by the
> image name ascending, not the ID. By sorting the database by the image name,
> the ID's aren't ascending and therefore I don't think that the statement
> 'WHERE id > " . $picID' will work because on some of the pics, the next ID
> may be lower than the one displayed.
>
> Any thoughts?
>
> Thanks.
>


Yup, just change the query that forms the "next" button (assuming you're
using something similar to what I posted a few days ago) to:

$query = mysql_query("SELECT id FROM pics"
. " WHERE name > " . $row["name"]
. " AND folderId = " . $row["folderId"]
. " LIMIT 1");

so it's finding the next picture along by name, not id. I haven't fully
thought this through, but try it and see if it works...

--
Oli
Oli Filth

2005-02-07, 3:57 pm

Oli Filth wrote:
> $query = mysql_query("SELECT id FROM pics"
> . " WHERE name > " . $row["name"]
> . " AND folderId = " . $row["folderId"]
> . " LIMIT 1");
>


Oh yeah, and you'll need a "ORDER BY name" at the end of that query.

--
Oli
liquidice

2005-02-08, 3:56 pm

Hi Oli,

I tried to use 'WHERE name > " . $row["name"]' but it gave me a strange
error saying something about the filename not being a table, I can't
remember the exact error message but it wasn't parsing it.

I then tried to use 'WHERE id > " . $row["id"] again, which seems to work
but only pulls out the next record if the ID is greater then the current
one. Is the '>' sign considered as 'greater than' in SQL?

I've also tried using a loop. I don't know exactly, I thought it may help,
but obviously, it didn't!:
<?
for($i=0; $i<$row_rsPics; $i++)
<?

I've tried everything I can think of and I'm not getting very far :(

So now, I've gone back to square one and created the following three queries
again in the hope that I can clear my head and work out a correct and
logical way of extracting the data I need.

<?php
mysql_select_db($database_website, $website);
$query_tblGalleryNames = "SELECT * FROM tblGalleryNames WHERE
tblGalleryNames.gID = '$folder'";
$tblGalleryNames = mysql_query($query_tblGalleryNames, $website) or
die(mysql_error());
$row_tblGalleryNames = mysql_fetch_assoc($tblGalleryNames);
$totalRows_tblGalleryNames = mysql_num_rows($tblGalleryNames);

mysql_select_db($database_website, $website);
$query_rsPics = "SELECT * FROM tblGalleryPics WHERE picID = '$pid' AND
folderRef = '$folder' ORDER BY tblGalleryPics.picThumb";
$rsPics = mysql_query($query_rsPics, $website) or die(mysql_error());
$row_rsPics = mysql_fetch_assoc($rsPics);
$totalRows_rsPics = mysql_num_rows($rsPics);

mysql_select_db($database_website, $website);
$query_rsPics2 = "SELECT * FROM tblGalleryPics WHERE picID > ".$pid." AND
folderRef = '$folder' ORDER BY tblGalleryPics.picThumb LIMIT 1";
$rsPics2 = mysql_query($query_rsPics2, $website) or die(mysql_error());
$row_rsPics2 = mysql_fetch_assoc($rsPics2);
$totalRows_rsPics2 = mysql_num_rows($rsPics2);

?>

I do apologise if I am being a constant pain.

Regards.

"Oli Filth" <oli_filth@eatspam.coldmail.com> wrote in message
news:FONNd.1197$XV1.723@newsfe3-win.ntli.net...
> Oli Filth wrote:
>
> Oh yeah, and you'll need a "ORDER BY name" at the end of that query.
>
> --
> Oli



Oli Filth

2005-02-08, 3:56 pm

liquidice wrote:
> Hi Oli,
>
> I tried to use 'WHERE name > " . $row["name"]' but it gave me a strange
> error saying something about the filename not being a table, I can't
> remember the exact error message but it wasn't parsing it.
>


Ah, yes, you need single-quotes around $row["name"], and use
mysql_real_escape_string(), as well, as in:

$query = mysql_query("SELECT id FROM pics"
. " WHERE name > '"
. mysql_real_escape_string($row["name"]) . "'"
. " AND folderId = " . $row["folderId"]
. " ORDER BY name LIMIT 1");


This demonstrates why one shouldn't necessarily assume the code that
people post here is perfect! After all, I did say that this wasn't
tested or fully thought-through. :)

--
Oli
liquidice

2005-02-08, 3:56 pm


>
> Ah, yes, you need single-quotes around $row["name"], and use
> mysql_real_escape_string(), as well, as in:
>
> $query = mysql_query("SELECT id FROM pics"
> . " WHERE name > '"
> . mysql_real_escape_string($row["name"]) . "'"
> . " AND folderId = " . $row["folderId"]
> . " ORDER BY name LIMIT 1");


Cool, it might just work then, will give it a bash tonight.

The loop I created, for $i=0... that was a useless attempt then, I take it?
:)

>
> This demonstrates why one shouldn't necessarily assume the code that
> people post here is perfect! After all, I did say that this wasn't tested
> or fully thought-through. :)


You did indeed, my friend. :)


Oli Filth

2005-02-08, 3:56 pm

liquidice wrote:
> The loop I created, for $i=0... that was a useless attempt then, I take it?
> :)
>


Oh, sorry, I didn't read the rest of your other post. I'm guessing with
the loop you wanted to extract all entries from the database table, and
then use PHP to work out which picture should be linked by the "next"
button? That method would work, but it's not as efficient, as MySQL has
to do more work by returning multiple rows rather than just one.

> Is the '>' sign considered as 'greater than' in SQL?


Yup. You can use it for most data-types, including strings.



--
Oli
liquidice

2005-02-08, 3:56 pm


"Oli Filth" <oli_filth@eatspam.coldmail.com> wrote in message
news:vv5Od.147$Dr.81@newsfe5-win.ntli.net...
> liquidice wrote:
>
> Oh, sorry, I didn't read the rest of your other post. I'm guessing with
> the loop you wanted to extract all entries from the database table, and
> then use PHP to work out which picture should be linked by the "next"
> button? That method would work, but it's not as efficient, as MySQL has to
> do more work by returning multiple rows rather than just one.
>

Yep, but now I realise that 150+ entries from the pic table would definately
not be an efficient way of doing things


liquidice

2005-02-09, 8:55 am


"Oli Filth" <oli_filth@eatspam.coldmail.com> wrote in message
news:h95Od.143$Dr.21@newsfe5-win.ntli.net...
> liquidice wrote:
>
> Ah, yes, you need single-quotes around $row["name"], and use
> mysql_real_escape_string(), as well, as in:
>
> $query = mysql_query("SELECT id FROM pics"
> . " WHERE name > '"
> . mysql_real_escape_string($row["name"]) . "'"
> . " AND folderId = " . $row["folderId"]
> . " ORDER BY name LIMIT 1");
>
>
> This demonstrates why one shouldn't necessarily assume the code that
> people post here is perfect! After all, I did say that this wasn't tested
> or fully thought-through. :)
>
> --
> Oli


Oli,

I don't believe it, it worked....WooooHoooo... I owe you a drink mate.

Now to get back to the previous image, I've created the following two
queries and need sort of a second opinion on them. I know that we have to
determine which row we are on in the database so that we can go back to the
previous one, so I've created a query to determine our current position and
one for moving back a row:

<?php
$query_rsPosition = "SELECT * FROM tblGalleryPics WHERE ".$pid." =
".$row_rsPics['picID']." ORDER BY picThumb";
$rsPosition = mysql_query($query_rsPosition, $demonnet) or
die(mysql_error());
$row_rsPosition = mysql_fetch_assoc($rsPosition);
$totalRows_rsPosition = mysql_num_rows($rsPosition);

$query_previous = "SELECT * from tblGalleryPics WHERE
".$row_rsPosition['picThumb']." < ".$row_rsPics['picThumb']." AND folderRef
= $folder ORDER BY picThumb";
$rsPrevious = mysql_query($query_rsPrevious, $demonnet) or
die(mysql_error());
$row_rsPrevious = mysql_fetch_assoc($rsPrevious);
$totalRows_rsPrevious = mysql_num_rows($rsPrevious);
?>

This however brings up a 'query was empty' error for some reason, can you
see anything wrong with the two queries?

Cheers Mate.


Oli Filth

2005-02-09, 3:56 pm

liquidice wrote:
> Now to get back to the previous image, I've created the following two
> queries and need sort of a second opinion on them. I know that we have to
> determine which row we are on in the database so that we can go back to the
> previous one, so I've created a query to determine our current position and
> one for moving back a row:
>
> <?php
> $query_rsPosition = "SELECT * FROM tblGalleryPics WHERE ".$pid." =

^
^
This should presumably be WHERE id = ... At the moment you're
substituting in a value on the left-hand-side of the equals sign, so the
query will look something like:

SELECT * FROM tblGalleryPics WHERE 5 = 10 ORDER BY picThumb

I believe this *is* valid SQL, but will return no results!

> ".$row_rsPics['picID']." ORDER BY picThumb";
> $rsPosition = mysql_query($query_rsPosition, $demonnet) or
> die(mysql_error());
> $row_rsPosition = mysql_fetch_assoc($rsPosition);
> $totalRows_rsPosition = mysql_num_rows($rsPosition);


There should be a maxiumum of one table row with a given ID, so the
mysql_num_rows() call is redundant. This also eliminates the need for
"ORDER BY picThumb", as there's not much point ordering one result!

Actually, why do you need to perform this first query? You already know
the current picID, so why do you need to pull the details out again?

As for the second query:

> $query_previous = "SELECT * from tblGalleryPics WHERE
> ".$row_rsPosition['picThumb']." < ".$row_rsPics['picThumb']." AND folderRef

^
Same problem as before; you're substituting in a value where you
shouldn't be.

> = $folder ORDER BY picThumb";
> $rsPrevious = mysql_query($query_rsPrevious, $demonnet) or
> die(mysql_error());
> $row_rsPrevious = mysql_fetch_assoc($rsPrevious);
> $totalRows_rsPrevious = mysql_num_rows($rsPrevious);


Again, there should only be one relevant result, so you can do away with
this mysql_num_rows() call. Just add "LIMIT 1" to the end of the MySQL
query (this tells MySQL to return a maximum of one row).

Hope this helps!

--
Oli
liquidice

2005-02-10, 8:56 am

It took me a few hours to get my head around what the sql was doing, finally
got it working in the end by echoing the queries. I had to make picThumb <
the picThumb from rsPics and order the picThumb DESC instead of ASC.

Thanks for all your help, Oli. I owe you one.

Kindest Regards.

"Oli Filth" <oli_filth@eatspam.coldmail.com> wrote in message
news:S3sOd.5368$Z%2.2445@newsfe3-gui.ntli.net...
> liquidice wrote:
> ^
> ^
> This should presumably be WHERE id = ... At the moment you're substituting
> in a value on the left-hand-side of the equals sign, so the query will
> look something like:
>
> SELECT * FROM tblGalleryPics WHERE 5 = 10 ORDER BY picThumb
>
> I believe this *is* valid SQL, but will return no results!
>
>
> There should be a maxiumum of one table row with a given ID, so the
> mysql_num_rows() call is redundant. This also eliminates the need for
> "ORDER BY picThumb", as there's not much point ordering one result!
>
> Actually, why do you need to perform this first query? You already know
> the current picID, so why do you need to pull the details out again?
>
> As for the second query:
>
> ^
> Same problem as before; you're substituting in a value where you shouldn't
> be.
>
>
> Again, there should only be one relevant result, so you can do away with
> this mysql_num_rows() call. Just add "LIMIT 1" to the end of the MySQL
> query (this tells MySQL to return a maximum of one row).
>
> Hope this helps!
>
> --
> Oli



liquidice

2005-02-15, 3:57 am


>
> Ah, yes, you need single-quotes around $row["name"], and use
> mysql_real_escape_string(), as well, as in:
>
> $query = mysql_query("SELECT id FROM pics"
> . " WHERE name > '"
> . mysql_real_escape_string($row["name"]) . "'"
> . " AND folderId = " . $row["folderId"]
> . " ORDER BY name LIMIT 1");


Cool, it might just work then, will give it a bash tonight.

The loop I created, for $i=0... that was a useless attempt then, I take it?
:)

>
> This demonstrates why one shouldn't necessarily assume the code that
> people post here is perfect! After all, I did say that this wasn't tested
> or fully thought-through. :)


You did indeed, my friend. :)


Oli Filth

2005-02-15, 3:57 am

liquidice wrote:
> The loop I created, for $i=0... that was a useless attempt then, I take it?
> :)
>


Oh, sorry, I didn't read the rest of your other post. I'm guessing with
the loop you wanted to extract all entries from the database table, and
then use PHP to work out which picture should be linked by the "next"
button? That method would work, but it's not as efficient, as MySQL has
to do more work by returning multiple rows rather than just one.

> Is the '>' sign considered as 'greater than' in SQL?


Yup. You can use it for most data-types, including strings.



--
Oli
liquidice

2005-02-16, 3:57 am


"Oli Filth" <oli_filth@eatspam.coldmail.com> wrote in message
news:h95Od.143$Dr.21@newsfe5-win.ntli.net...
> liquidice wrote:
>
> Ah, yes, you need single-quotes around $row["name"], and use
> mysql_real_escape_string(), as well, as in:
>
> $query = mysql_query("SELECT id FROM pics"
> . " WHERE name > '"
> . mysql_real_escape_string($row["name"]) . "'"
> . " AND folderId = " . $row["folderId"]
> . " ORDER BY name LIMIT 1");
>
>
> This demonstrates why one shouldn't necessarily assume the code that
> people post here is perfect! After all, I did say that this wasn't tested
> or fully thought-through. :)
>
> --
> Oli


Oli,

I don't believe it, it worked....WooooHoooo... I owe you a drink mate.

Now to get back to the previous image, I've created the following two
queries and need sort of a second opinion on them. I know that we have to
determine which row we are on in the database so that we can go back to the
previous one, so I've created a query to determine our current position and
one for moving back a row:

<?php
$query_rsPosition = "SELECT * FROM tblGalleryPics WHERE ".$pid." =
".$row_rsPics['picID']." ORDER BY picThumb";
$rsPosition = mysql_query($query_rsPosition, $demonnet) or
die(mysql_error());
$row_rsPosition = mysql_fetch_assoc($rsPosition);
$totalRows_rsPosition = mysql_num_rows($rsPosition);

$query_previous = "SELECT * from tblGalleryPics WHERE
".$row_rsPosition['picThumb']." < ".$row_rsPics['picThumb']." AND folderRef
= $folder ORDER BY picThumb";
$rsPrevious = mysql_query($query_rsPrevious, $demonnet) or
die(mysql_error());
$row_rsPrevious = mysql_fetch_assoc($rsPrevious);
$totalRows_rsPrevious = mysql_num_rows($rsPrevious);
?>

This however brings up a 'query was empty' error for some reason, can you
see anything wrong with the two queries?

Cheers Mate.


Oli Filth

2005-02-16, 3:57 am

liquidice wrote:
> Now to get back to the previous image, I've created the following two
> queries and need sort of a second opinion on them. I know that we have to
> determine which row we are on in the database so that we can go back to the
> previous one, so I've created a query to determine our current position and
> one for moving back a row:
>
> <?php
> $query_rsPosition = "SELECT * FROM tblGalleryPics WHERE ".$pid." =

^
^
This should presumably be WHERE id = ... At the moment you're
substituting in a value on the left-hand-side of the equals sign, so the
query will look something like:

SELECT * FROM tblGalleryPics WHERE 5 = 10 ORDER BY picThumb

I believe this *is* valid SQL, but will return no results!

> ".$row_rsPics['picID']." ORDER BY picThumb";
> $rsPosition = mysql_query($query_rsPosition, $demonnet) or
> die(mysql_error());
> $row_rsPosition = mysql_fetch_assoc($rsPosition);
> $totalRows_rsPosition = mysql_num_rows($rsPosition);


There should be a maxiumum of one table row with a given ID, so the
mysql_num_rows() call is redundant. This also eliminates the need for
"ORDER BY picThumb", as there's not much point ordering one result!

Actually, why do you need to perform this first query? You already know
the current picID, so why do you need to pull the details out again?

As for the second query:

> $query_previous = "SELECT * from tblGalleryPics WHERE
> ".$row_rsPosition['picThumb']." < ".$row_rsPics['picThumb']." AND folderRef

^
Same problem as before; you're substituting in a value where you
shouldn't be.

> = $folder ORDER BY picThumb";
> $rsPrevious = mysql_query($query_rsPrevious, $demonnet) or
> die(mysql_error());
> $row_rsPrevious = mysql_fetch_assoc($rsPrevious);
> $totalRows_rsPrevious = mysql_num_rows($rsPrevious);


Again, there should only be one relevant result, so you can do away with
this mysql_num_rows() call. Just add "LIMIT 1" to the end of the MySQL
query (this tells MySQL to return a maximum of one row).

Hope this helps!

--
Oli
Maximilian Hohenheim

2005-02-22, 3:57 pm


"DaRemedy" <demonnet(dont spam me)@btinternet.com> schrieb im Newsbeitrag
news:1107523493.761348@smtp-1.griffin.com...
>
>
> "Oli Filth" <oli_filth@eatspam.coldmail.com> wrote in message
> news:ajJMd.255$8G5.51@newsfe1-gui.ntli.net...
> the
> making
> pics.
the[color=darkred]
it[color=darkred]
hard[color=darkred]
>
> $row_tblGalleryNames['gID']; holds the gallery ID, so:
> gID gName
> 1 Family Pics
>
> the gID is the same as folderref
>
> tblPics
> picID picPath folderRef
> 1 image1.jpg 1
>
> I've called out both picID and folderID, just in case I have two images

with
> the same name but in different folders
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com