aldeasim Posted August 7, 2007 Share Posted August 7, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/ Share on other sites More sharing options...
drewbee Posted August 7, 2007 Share Posted August 7, 2007 You need to save the name value to a variable, then reference that variable in the footer.php Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317616 Share on other sites More sharing options...
aldeasim Posted August 7, 2007 Author Share Posted August 7, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317631 Share on other sites More sharing options...
pyrodude Posted August 7, 2007 Share Posted August 7, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317641 Share on other sites More sharing options...
aldeasim Posted August 7, 2007 Author Share Posted August 7, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317677 Share on other sites More sharing options...
drewbee Posted August 7, 2007 Share Posted August 7, 2007 Interesting. By scope both calls should have access to it. In your footer, echo out the variable "$hd" and see if it outputs the random number. Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317689 Share on other sites More sharing options...
aldeasim Posted August 7, 2007 Author Share Posted August 7, 2007 Drewbee, it doesn't. — Aldeasim. PS: With the code I've sent, I neither get $hdn, nor $hdinfo, so I have a blank header and no link to any author. Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317699 Share on other sites More sharing options...
pyrodude Posted August 7, 2007 Share Posted August 7, 2007 The only variable you're defining as global is $hd, not $hdn or $hdinfo. Make them global and your problem should disappear. Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317762 Share on other sites More sharing options...
aldeasim Posted August 7, 2007 Author Share Posted August 7, 2007 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>" ; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317765 Share on other sites More sharing options...
hcdarkmage Posted August 7, 2007 Share Posted August 7, 2007 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>" ; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317777 Share on other sites More sharing options...
aldeasim Posted August 7, 2007 Author Share Posted August 7, 2007 The problem remains, hcdarkmage. (Sorry by my code syntax ) Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317796 Share on other sites More sharing options...
hcdarkmage Posted August 7, 2007 Share Posted August 7, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317834 Share on other sites More sharing options...
aldeasim Posted August 7, 2007 Author Share Posted August 7, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317856 Share on other sites More sharing options...
hcdarkmage Posted August 7, 2007 Share Posted August 7, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317893 Share on other sites More sharing options...
aldeasim Posted August 7, 2007 Author Share Posted August 7, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317915 Share on other sites More sharing options...
hcdarkmage Posted August 7, 2007 Share Posted August 7, 2007 I think I am going to have to bow out on this one. I did what I could. Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317922 Share on other sites More sharing options...
aldeasim Posted August 7, 2007 Author Share Posted August 7, 2007 Thanks anyway, hcdarkmage. Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317932 Share on other sites More sharing options...
aldeasim Posted August 7, 2007 Author Share Posted August 7, 2007 Hmmm. Proofing I realized that the code is good, but Wordpress has something that blocks the right behaviour of the script . I think this is not the right place to solve WP problems, so thanks a lot for all your efforts!! — Aldeasim. Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-317950 Share on other sites More sharing options...
pyrodude Posted August 8, 2007 Share Posted August 8, 2007 If you're done with this thread, be sure to mark it as solved Quote Link to comment https://forums.phpfreaks.com/topic/63742-include-and-rand/#findComment-318020 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.