Forum Moderators: coopster

Message Too Old, No Replies

Repeating Random Record Selection

         

inveni0

9:42 pm on Jan 28, 2006 (gmt 0)

10+ Year Member



I currently have a page that randomly selects and displays records from a table on my database. It selects one at a time and shows a photo with the record. The photo is named using the record ID as a reference. For instance, [ID].jpg.

Now, I need an idea on how to create a conditional statement, because not every record has a photo on the server. I need a statement that will select a row at random, check to see if the file exists, and if it doesn't, try another random row.

Is this possible, or practical? Currently, when it loads a row with no image, it shows a generic, "no image available" photo, but this looks really crumby on the page. Which is why I only want to show rows with photos.

jatar_k

9:57 pm on Jan 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



only problem is what happens when it keeps getting a row with no image. It could get caught up for quite a while.

Do you have anything in the actual table that says whether there is an attributed photo or not? This could be an option as it would then allow you to select randomly from rows where havepic='yes'.

inveni0

10:00 pm on Jan 28, 2006 (gmt 0)

10+ Year Member



Well, the problem is that different people upload photos whenever they feel like it. I really have no way to track who has and who has not. The user has no access to the database, just the ability to upload photos.

jatar_k

10:07 pm on Jan 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



another thought would be to use a set of funny pics that say "no image available". Might make it a little more interesting.

inveni0

10:11 pm on Jan 28, 2006 (gmt 0)

10+ Year Member



Well, that's what I have right now, a generic "no image available" photo, but photos that the users place look 100% better than any generic I could add. I also don't want to have a generic that looks good enough that it's misleading to website browsers in its purpose.

If there's no other way, I suppose I can call upon my graphic design training, but I hate that crap...

jatar_k

10:14 pm on Jan 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hehe, me too.

another thought is, grab 10 random rows instead of just 1. that way php can do the selection and you won't have to keep going back to the database. 10 should give you a very high chance of getting 1 or more with images. Then at least the odds of having to show one with no photo are very reduced.

You may want to start with 5 rows and see how that goes. If you find there are still too many with no photos then you could increase it 1 by 1. I would think 10 would be a good cieling number.

inveni0

10:18 pm on Jan 28, 2006 (gmt 0)

10+ Year Member



That's a good idea. How do you think I should go about filtering? How would I grab the first record with a photo and display it only, even if others have photos? Worse, how would I select a record if none have photos?

jatar_k

3:58 am on Jan 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



how are you testing for the existence of the photo now?

instead of however you output it now, you put it in a loop based on your test for the photo, if you find one with a photo then you break [php.net] out of the loop