Jump to content

Recommended Posts

Hi there.

I'm new here and have got a big problem, and I'll tell you the whole situation.

 

I've got a blog powered by Wordpress, with header photos I randomly use that other people have taken.

Due to the Wordpress file system, I've got the header.php archive, where I call (with include( )) a random.php file (which has the function rand( ) in it). I have no problem to insert a random file name, so different header images are displayed every time page is refreshed.

The problem is that I need to put the credits of the images (this Creative Commons thing...) in the footer.php file. What can I do to use the same randomly generated number in these two different files and include( ) them both in the index.php file that Wordpress require?

 

I've tried to include( ) random.php in header.php as in footer.php, but it generates two different random numbers, so image doesn't correspond with credits of that image.

 

Any help will be greatly appreciated.

Thanks in advance!

— Aldeasim.

Link to comment
https://forums.phpfreaks.com/topic/63742-include-and-rand/
Share on other sites

Thanks drewbee, but I have done that. I save this random value to $hd, and the name of the author of the picture and a link to his page and saved it to $hdinfo, but it still doesn't work in footer.php. As a proof, I called $hdinfo from the header.php file, and it displays the result.

 

Maybe with "save the name value to a variable" you mean something I can catch...  ???

 

— Aldeasim.

Link to comment
https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317631
Share on other sites

Unless you're saving that information to a variable within a function, the scope of the variable will be every page that is called with include().  The issue is that the information is probably being called in some GetRandomPic() function.  Try declaring them as global variables.

Link to comment
https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317641
Share on other sites

Now I feel really dumb.

 

<?php

$hd = rand(1, 4); // header

function GetRandImages() {

global $hd; 

switch($hd) {
  case 1: ($hdn = "1"); ($hdperma = "http://www.sxc.hu/profile/hawkida"); ($hdname = "Hawkida"); break;
  case 2: ($hdn = "2"); ($hdperma = "http://www.sxc.hu/profile/reuben4eva"); ($hdname = "Audrey Johnson"); break;
  case 3: ($hdn = "3"); ($hdperma = "http://www.imageafter.com/"); ($hdname = "ImageAfter"); break;
  case 4: ($hdn = "4"); ($hdperma = "http://www.sxc.hu/profile/dsiis"); ($hdname = "Israel Jiménez"); break;
  }

$hdinfo = "<a href=\"" . $hdperma . "\" target=\"_blank\">" . $hdname . "</a>" ;

  }
?>

 

That's my random.php file.

 

In my header.php file I have this:

 

<?php include_once (TEMPLATEPATH . '/random.php'); ?>

<style>
#top {background: url(recursos/superior_bg<?php GetRandImages(); echo $hdn; ?>.jpg) center repeat-x; }
</style>

 

And in my footer.php I have:

<?php GetRandImages(); ?>
header image by <?php echo $hdinfo; ?>

 

Any idea? The best result I've reached is having two different random numbers, but the header and footer were looking correctly (but with wrong data).

 

Thanks a lot in advance!

— Aldeasim.

Link to comment
https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317677
Share on other sites

The only variable you're defining as global is $hd, not $hdn or $hdinfo.  Make them global and your problem should disappear.

 

Hmm. Actually, pyrodude, it didn't :-[

It appears I'm doing something bad, but the code looks just fine...

 

— Aldeasim.

 

PS: The code now is:

<?php

$hd = rand(1, 4); // header

function GetRandImages() {

global $hd, $hdn, $hdinfo; 

switch($hd) {
  case 1: ($hdn = "1"); ($hdperma = "http://www.sxc.hu/profile/hawkida"); ($hdname = "Hawkida"); break;
  case 2: ($hdn = "2"); ($hdperma = "http://www.sxc.hu/profile/reuben4eva"); ($hdname = "Audrey Johnson"); break;
  case 3: ($hdn = "3"); ($hdperma = "http://www.imageafter.com/"); ($hdname = "ImageAfter"); break;
  case 4: ($hdn = "4"); ($hdperma = "http://www.sxc.hu/profile/dsiis"); ($hdname = "Israel Jiménez"); break;
  }

$hdinfo = "<a href=\"" . $hdperma . "\" target=\"_blank\">" . $hdname . "</a>" ;

  }
?>

Link to comment
https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317765
Share on other sites

Try this:

<?php

$hd = rand(1, 4); // header

function GetRandImages() {

global $hd, $hdn, $hdinfo; 

switch($hd) {
  case 1: 
       $hdn = '1';
       $hdperma = 'http://www.sxc.hu/profile/hawkida';
       $hdname = 'Hawkida';
       break;
  case 2:
       $hdn = '2';
       $hdperma = 'http://www.sxc.hu/profile/reuben4eva';
       $hdname = 'Audrey Johnson';
       break;
  case 3:
       $hdn = '3';
       $hdperma = 'http://www.imageafter.com/';
       $hdname = 'ImageAfter';
       break;
  case 4:
       $hdn = '4';
       $hdperma = 'http://www.sxc.hu/profile/dsiis';
       $hdname = Israel Jiménez';
       break;
  }

$hdinfo = "<a href=\"" . $hdperma . "\" target=\"_blank\">" . $hdname . "</a>" ;

  }
?>

Link to comment
https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317777
Share on other sites

Maybe you should try a new approach:

<?php

$hd = rand(1, 4); // header

$img = ''.$hd.'':

switch($img) {
  case 1: 
       $hdn = '1';
       $hdperma = 'http://www.sxc.hu/profile/hawkida';
       $hdname = 'Hawkida';
       break;
  case 2:
       $hdn = '2';
       $hdperma = 'http://www.sxc.hu/profile/reuben4eva';
       $hdname = 'Audrey Johnson';
       break;
  case 3:
       $hdn = '3';
       $hdperma = 'http://www.imageafter.com/';
       $hdname = 'ImageAfter';
       break;
  case 4:
       $hdn = '4';
       $hdperma = 'http://www.sxc.hu/profile/dsiis';
       $hdname = Israel Jiménez';
       break;
  }

$hdinfo = "<a href=\"" . $hdperma . "\" target=\"_blank\">" . $hdname . "</a>" ;

?>

 

Then in the header.php file:

<?php include_once (TEMPLATEPATH . '/random.php'); ?>

<style>
#top {background: url(recursos/superior_bg[var.$hdn].jpg) center repeat-x; }
</style>

 

And in the footer.php file:

<?php include_once (TEMPLATEPATH . '/random.php'); ?>
header image by [var.$hdinfo]

 

See how that works for you.

Link to comment
https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317834
Share on other sites

Well, it works for the header.php file. The problem is that footer.php doesn't return any value to $hdinfo. Now, include(random.php) in footer.php only makes the script execute twice, and the random number not corresponding with the header.php one.

 

— Aldeasim.

Link to comment
https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317856
Share on other sites

Maybe you should try this in the footer.php file:

header image by [var.$hdinfo]

 

Try it that way.  It seams that if you already have the include once in the file, then it should not have to be repeated.

 

<?php

$hd = rand(1, 4); // header

$img = ''.$hd.'':

switch($img) {
  case 1: 
       $hdn = '1';
       $hdperma = 'http://www.sxc.hu/profile/hawkida';
       $hdname = 'Hawkida';
       break;
  case 2:
       $hdn = '2';
       $hdperma = 'http://www.sxc.hu/profile/reuben4eva';
       $hdname = 'Audrey Johnson';
       break;
  case 3:
       $hdn = '3';
       $hdperma = 'http://www.imageafter.com/';
       $hdname = 'ImageAfter';
       break;
  case 4:
       $hdn = '4';
       $hdperma = 'http://www.sxc.hu/profile/dsiis';
       $hdname = 'Israel Jiménez';
       break;
  }

$hdinfo = $img("<a href=\"" . $hdperma . "\" target=\"_blank\">" . $hdname . "</a>");

?>

 

NOTE: Code not tested, but try along these lines and see what you get.

Link to comment
https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317893
Share on other sites

It's the same, hcdarkmage. The new random.php you send hasn't now the little errors it had, but I had solved them all before proofing it.

Now it doesn't show anything (before it showed a name, a wrong one, but a name anyway).

 

I'm observing other thing now. I have a javascript in <head></head> (that's header.php file) but the code isn't executing in footer.php. The problem is similar. I don't want to seek here solutions for this js problem, but I only wanted to point it out. For the record.

 

— Aldeasim.

Link to comment
https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317915
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.