Wordpress Themes - WP Forum at BFA

Wordpress Themes - WP Forum at BFA (http://forum.bytesforall.com/index.php)
-   Sidebars & Widgets (http://forum.bytesforall.com/forumdisplay.php?f=14)
-   -   Image rotation PHP script needs a tweak (http://forum.bytesforall.com/showthread.php?t=2033)

paulae Jun 13, 2009 03:33 PM

Image rotation PHP script needs a tweak
 
This is what I want to do: I want a sidebar image rotation of a different image each day of the month, clicking through to different sponsor for each image. I found a PHP script that does the random image each day part, but I can't figure out how to include the link code for each image. Here is the script that works so far, and if anybody can show me how to put in the URL for each image to link to, I will be so happy! I've put in 2 images to test it. There will be up to 30 eventually. These are pictures of homes for sale, each one linking to an individual real estate agent's website. I tried sticking the URL right after the image code, but that made the whole thing break.

Code:

<?php

$totalPics="2";                    // Total photos in folder

$photoOfTheDay = Array (          // Add up to 31 photos below

  '1' => '/ads/weichert/0615Mary.jpg' ,    // Name of or Path to image
  '2' => '/ads/weichert/0619Janet.jpg' ,    // Name of or Path to image
 
);
//  published at: scripts.tropicalpcsolutions.com

// Place day of month in variable 'x'
//$x=date("d");
// A website vistor made a suggestion to change the date function to use j
// instead of d. This causes the date to be read without leading zeros.
$x=date("j");  //Thanks for the suggestion Andrew

// If value of date exceeds the amount of photos then pick a random photo to display
if($x > $totalPics) { $x=rand(1,$totalPics); }

// Display image
echo <<<EOC
<center><img src="$photoOfTheDay[$x]"></center>
EOC;

?>


juggledad Jun 14, 2009 05:52 AM

Paulae,
What you need to do is put the links in an array also. Try this
HTML Code:

<?php

$totalPics="2";                    // Total photos in folder

$photoOfTheDay = Array (          // Add up to 31 photos below

  '1' =>
'/ads/weichert/0615Mary.jpg' ,    // Name of or Path to image
  '2' => '/ads/weichert/0619Janet.jpg' ,    // Name of or Path to image
 
);

$linkOfTheFDay = Array (

  '1' => 'http://www.link1.com' ,    // Name of or Path to image
  '2' => 'http://www.link2.com' ,    // Name of or Path to image


//  published at: scripts.tropicalpcsolutions.com

// Place day of month in variable 'x'
//$x=date("d");
// A website vistor made a suggestion to change the date function to use j
// instead of d. This causes the date to be read without leading zeros.
$x=date("j");  //Thanks for the suggestion Andrew

// If value of date exceeds the amount of photos then pick a random photo to display
if($x > $totalPics) { $x=rand(1,$totalPics); }

// Display image
echo <<<EOC
<center>
<a href="$linkOfTheDay[$x]"><img src="$photoOfTheDay[$x]"></a></center>
EOC;

?>

Paul

paulae Jun 14, 2009 06:41 AM

Thanks. But that didn't do the trick. At first, I pasted in just what you gave me, and the image disappeared. Then I noticed the
Code:

);
was missing after your new array, so I moved it down. That didn't help. So I moved it back after the image array and added one after the link array. Now the image is back but not clickable.

This is what's there now:
Code:

<?php

$totalPics="2";                    // Total photos in folder

$photoOfTheDay = Array (          // Add up to 31 photos below

  '1' => '/ads/weichert/0615Mary.jpg',    // Name of or Path to image
  '2' => '/ads/weichert/0619Janet.jpg' ,    // Name of or Path to image
 
);
$linkOfTheFDay = Array (

  '1' => 'http://www.cnn.com' ,    // Name of or Path to image
  '2' => 'http://www.nytimes.com' ,    // Name of or Path to image
);


//  published at: scripts.tropicalpcsolutions.com


$x=date("j");  //Thanks for the suggestion Andrew

// If value of date exceeds the amount of photos then pick a random photo to display
if($x > $totalPics) { $x=rand(1,$totalPics); }

// Display image
echo <<<EOC
<center><img src="$photoOfTheDay[$x]"></center>
EOC;

?>


juggledad Jun 14, 2009 09:49 AM

Paulae,

I found another typo, the second array is '$linkOfTheFDay'. Get rid of the extra 'F', sigh the problems you get when you don't have your first cup of coffee. Here is the updated version
HTML Code:

<?php

$totalPics="2";                    // Total photos in folder

$photoOfTheDay = Array (          // Add up to 31 photos below

  '1' =>
'/ads/weichert/0615Mary.jpg',    // Name of or Path to image
  '2' => '/ads/weichert/0619Janet.jpg' ,    // Name of or Path to image
 
);

$linkOfTheDay = Array (

  '1' => 'http://www.cnn.com' ,    // Name of or Path to image
  '2' => 'http://www.nytimes.com' ,    // Name of or Path to image

);

//  published at: scripts.tropicalpcsolutions.com

// Place day of month in variable 'x'
//$x=date("d");
// A website vistor made a suggestion to change the date function to use j
// instead of d. This causes the date to be read without leading zeros.
$x=date("j");  //Thanks for the suggestion Andrew

// If value of date exceeds the amount of photos then pick a random photo to display
if($x > $totalPics) { $x=rand(1,$totalPics); }

$ad_image = '';
$ad_image = '<a href=' . $linkOfTheDay[$x] . '><img src=' . $photoOfTheDay[$x] . '></a>';

// Display image
echo "potd=" . $photoOfTheDay[$x] . "<br>";
echo "lotd=" . $linkOfTheDay[$x] . "<br>";
echo "x=" . $x . "<br>";
echo  "<<<EOC <center> " . $ad_image . " </center> EOC" ;

?>

Note the extra 'echo' statements so you can check out the variables, comment them out when your done testing

paulae Jun 14, 2009 09:57 AM

Almost there! By "extra echo statements," you mean which ones?

http://www.larchmontgazette.com

Scroll to the very bottom of the right sidebar. Image is there, link works. Part of the code is showing. I'm not sure which part to comment out.

juggledad Jun 14, 2009 10:49 AM

These three lines
echo "potd=" . $photoOfTheDay[$x] . "<br>";
echo "lotd=" . $linkOfTheDay[$x] . "<br>";
echo "x=" . $x . "<br>";
were just for displaying the variables so I/(you) would know it was working.
You probably want to change the last line from:
echo "<<<EOC <center> " . $ad_image . " </center> EOC" ;
to
echo "<center> " . $ad_image . " </center>" ;
I thought the <<<EOC was something related to a plugin or something

paulae Jun 14, 2009 11:03 AM

OK, that did the trick!

Oddly enough, though, and I don't think this has a thing to do with your work, the image has changed back today to the one from yesterday. It's rotating images more than once a day. Can you see anything in the code that would explain it?

juggledad Jun 14, 2009 12:22 PM

The way the code is written, it gets todays DAY, today is the 14th. It then checks to see if the array has 14 or more images (it doesn't, there are only 2) so it picks a random image from teh array and displays it.

You need an image for the 1st
an image for the 2nd
...
an image for the 31st
then it will stay with that image all day

paulae Jun 14, 2009 12:33 PM

Ah. OK. I'll try putting more images in there.

By the way, I made a donation specifically to you. Thanks for the help!

juggledad Jun 14, 2009 12:41 PM

You're welcome and thanks for the donation!


All times are GMT -6. The time now is 02:11 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.