Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Building an index for next/last in a photo album.

by davidj (Priest)
on Jul 26, 2004 at 20:34 UTC ( [id://377569]=note: print w/replies, xml ) Need Help??


in reply to Building an index for next/last in a photo album.

Sounds rather complicated to me. If it's not too much of a hassle, I would suggest that you redesign your database to use img_id as the primary key (assuming that it is unique). You could then easily calculate the before and after img_id values and select them from the database in 1 SQL query. (It would also be very quick since primary keys are indexed)
my ($img_id) = $str =~ m/img_id=(\d+)/; my $prev_id = $img_id - 1; my $next_id = $img_id + 1;
Then the SQL query (something like):
"select WHATEVER from TABLE where img_id in ($prev_id, $img_id, $next_ +id)"
Hope this helps,
davidj

Replies are listed 'Best First'.
Re^2: Building an index for next/last in a photo album.
by Seumas (Curate) on Jul 26, 2004 at 20:46 UTC
    The problem is that this application will serve infinite users with infinite albums, so while img_id is unique and sequential, it doesn't gaurantee that any two images belong to the same person or album.
      Ok, this may or may not be a good solution, depending you whether or not you want to mess with your database design, but it will work.

      Somewhere in the table put another column for a sequence number. So, instead of
      img_id | album_id | uid | name | ext -------+----------+-----+--------------+------ 38 | 14 | 89 | mypetrat | jpg 39 | 14 | 89 | mypetlemur | jpg 40 | 2 | 12 | vacation_01 | jpg 41 | 14 | 89 | mypetvulture | jpg 42 | 2 | 12 | vacation_02 | jpg 43 | 2 | 12 | vacation_03 | jpg 44 | 14 | 89 | mypetcow | jpg
      you have something like
      img_id | album_id | seq_id | uid | name | ext -------+----------+--------+-----+--------------+------ 38 | 14 | 1 | 89 | mypetrat | jpg 39 | 14 | 2 | 89 | mypetlemur | jpg 40 | 2 | 1 | 12 | vacation_01 | jpg 41 | 14 | 3 | 89 | mypetvulture | jpg 42 | 2 | 2 | 12 | vacation_02 | jpg 43 | 2 | 3 | 12 | vacation_03 | jpg 44 | 14 | 4 | 89 | mypetcow | jpg
      You could then grab the seq_id to the corresponding img_id
      select seq_id from TABLE where img_id = $img_id
      Then 1) calculate prev and next seq_id and 2) grab the img_ids you want be referencing the seq_id
      my $p_seq_id = $seq_id - 1; my $n_seq_id = $seq_id + 1; "select img_id from TABLE where seq_id in ($p_seq_id, $seq_id, $n_seq_ +id)"
      You could easily keep the current album seq_id value in another table and reference it when adding new images. As I said, it may be a hassle modifying your database, but it may be worthwhile in the long run as it more easily does what you want than the other options.

      Note: This may be a good "teachable moment" as educators like to call them.

      Perhaps the most important thing in designing an application that uses a database is to know thoroughly what you want from the application before you even begin designing the database. Too often, people design a database based on a less than thorough examination of the application requirements and end up having to redo it (the database.) :)

      davidj

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://377569]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-19 19:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found