PDA

View Full Version : A simple random front page image hack


iaredatsun
08-27-2007, 09:20 AM
Problem: showing a random picture each time the home page is first loaded.
(Of course this should not effect the loading of images from the browse page.)

I know this has been discussed before but no one seemed to come up with a satisfactory answer to this functionality. Usually people suggested some modification of the site structure with index2.php and random folder image method.

My method is a simple one line modification to the index.php file

line 333 of orginal index.php -

change:

$row = sql_array("SELECT * FROM ".$pixelpost_db_prefix."pixelpost WHERE datetime<='$cdate' ORDER BY datetime DESC limit 0,1");

to:

$row = sql_array("SELECT * FROM ".$pixelpost_db_prefix."pixelpost WHERE datetime<='$cdate' ORDER BY rand() asc limit 0,1");

This will only effect the page when it loaded with no PHP arguments - i.e. only when someone first goes to the site and not when things are accessed from the thumbnails page.

Please note that I think this will only work if you are not logged in as admin. I think modifying that would be simple too.

I'm totally new to this but I have tested this a few times and it seems to work fine - at least for me. Any comments?

Mr.Datsun

witty
08-28-2007, 05:14 PM
Hi,

Works well for me too. Although, I have a link on my front page called "Latest Image", which for now is set with a 'href' point to index.php. If I could change that so that the 'href' really did point to the latest image, that would be ideal. Any ideas?

Right now, the URL to my most recent image is:

http://domain.com/index.php?showimage=91

So how can I change the 'href' to be a variable that would use the most recent image and use that for my "Latest Image" link? That way, on the initial page load, visitors would get a random image, but when they clicked on "Latest Image" visitors could start from the beginning of the blog. Thanks!


-Witty

iaredatsun
08-28-2007, 08:33 PM
So how can I change the 'href' to be a variable that would use the most recent image and use that for my "Latest Image" link? That way, on the initial page load, visitors would get a random image, but when they clicked on "Latest Image" visitors could start from the beginning of the blog. Thanks!


There's a line that gets the total image count and stores it in $pixelpost_photonumb. You should be able to use that to create a dynamic link.

My guess is that you put <SITE_PHOTONUMBER> in your template where the latest photo number should go in your url string. Try that.

<SITE_PHOTONUMBER> gets replaced by the number of photos in the database ($pixelpost_photonumb). But I don't know what happens if a photo has been deleted as I don't know if it uses a unique id or a number to retrieve images. That's the current limit of my knowledge.

witty
08-29-2007, 04:56 PM
Hi,

You're definitely on the right track. While <SITE_PHOTONUMBER> is in fact the total number of photos, it is not the unique ID number that is used to identify each photo. The tag we're looking for is <IMAGE_LAST_ID>. I tested it and it works. Thanks iaredatsun!


-Witty

witty
08-30-2007, 12:08 PM
Hi,

I'm not sure why yet, but this hack appears to "break" (including my modification to show <IMAGE_LAST_ID>) when you're logged in as Admin and viewing the blog.... works fine otherwise.


-Witty

iaredatsun
08-31-2007, 03:57 PM
Hi,

I'm not sure why yet, but this hack appears to "break" (including my modification to show <IMAGE_LAST_ID>) when you're logged in as Admin and viewing the blog.... works fine otherwise.


-Witty

I think I know why. But how does it break exactly?

I only hacked the section where the user is not logged in as admin. I figured that as admin it may be better to see the latest photo than to keep getting a random image when you are testing the site.

witty
09-04-2007, 02:35 PM
Hi,

It (talking about the <IMAGE_LAST_ID> tag) leaves off the image number in the following URL:

http://domain.com/index.php?showimage=91

So it looks like this when it breaks:

http://domain.com/index.php?showimage=

and says this:

Not Found
Don't do that! go back to index.php!

And the "randomness" stops being random... only displaying the latest image over and over.

For me, now that I know WHY it breaks, it doesn't really matter. As long as it works for non-admin visitors, that's all I care about.


-Witty

Marx
01-17-2008, 06:30 AM
Just wondering.. does this also work with V1.7.1?
(cant find the code used here)

Marx
01-21-2008, 01:02 PM
In V1.7.1 at line 424 I see
$row = sql_array("SELECT * FROM ".$pixelpost_db_prefix."pixelpost WHERE datetime<='$cdate' ORDER BY ".$cfgrow['display_sort_by']." ".$display_order." limit 0,1");
Could some one tell me what I need to adjust to get it workin?
Thanks.

jaywilliams
01-22-2008, 12:00 AM
In V1.7.1 at line 424 I see
$row = sql_array("SELECT * FROM ".$pixelpost_db_prefix."pixelpost WHERE datetime<='$cdate' ORDER BY ".$cfgrow['display_sort_by']." ".$display_order." limit 0,1");
Could some one tell me what I need to adjust to get it workin?
Thanks.
In v1.7.1 its on Line # 423

Marx
01-22-2008, 06:55 AM
Dang.. must get more sleep ;-)
Thanks, I've got it. It works.

fredxeric
03-02-2008, 08:36 PM
oups didn't read the complete tread I found the answer to my question that was with line for version 1.7.1... sorry

kevincrafts
03-02-2008, 11:16 PM
Here's a random front page image that does not require any hacking...

http://www.pixelpost.org/extend/addons/random-front-page-image/

AlanC
03-28-2009, 08:05 AM
Kevin - I think I've been able to misinterpret your simply instruction to put <RANDOMFRONT> into the first line of image_template.html. Do you mean the literal first line:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

or some other? I get either a 404 or no randomization depending on where I insert it. The 404 clears when I turn off Randomization in admin panel. Will you be kind enough to reply with a line that has the tag correctly inserted?

Thanks!

Alan

AlanC
03-28-2009, 07:19 PM
I'm following up on my last post with some lessons learned en route to getting random images on the opening page to work correctly. I found the original 08-27-2007 post by iaredatsun that had a 1-line index.php hack that worked perfectly for me in 1.7.1. The full text is below to save you looking around, but I modified line 428 instead of 333. To find the line, I searched index.php for the string

$row = sql_array("SELECT * FROM

and replaced the first occurrence of the entire row containing that partial statement with

$row = sql_array("SELECT * FROM ".$pixelpost_db_prefix."pixelpost WHERE datetime<='$cdate' ORDER BY rand() asc limit 0,1");

FWIW, I use a customized badarrow template, Ajax Categories Manager, External App Upload (for LR) and ImageCycle. All coexist happily with this hack. I saw it in the thread and got the same results in that randomization does not work while I'm logged in as admin, but starts immediately after I log out. I'm happy with that and am not going to look for the line that is called when I'm logged in.

Thanks iaredatsun!

****Original Post Text***

Old 08-27-2007, 05:20 AM
iaredatsun Offline
forum loafer

Join Date: Aug 2007
Posts: 6
A simple random front page image hack
Problem: showing a random picture each time the home page is first loaded.
(Of course this should not effect the loading of images from the browse page.)

I know this has been discussed before but no one seemed to come up with a satisfactory answer to this functionality. Usually people suggested some modification of the site structure with index2.php and random folder image method.

My method is a simple one line modification to the index.php file

line 333 of orginal index.php -

change:

$row = sql_array("SELECT * FROM ".$pixelpost_db_prefix."pixelpost WHERE datetime<='$cdate' ORDER BY datetime DESC limit 0,1");

to:

$row = sql_array("SELECT * FROM ".$pixelpost_db_prefix."pixelpost WHERE datetime<='$cdate' ORDER BY rand() asc limit 0,1");

This will only effect the page when it loaded with no PHP arguments - i.e. only when someone first goes to the site and not when things are accessed from the thumbnails page.

Please note that I think this will only work if you are not logged in as admin. I think modifying that would be simple too.

I'm totally new to this but I have tested this a few times and it seems to work fine - at least for me. Any comments?

Mr.Datsun