 |
|
|
06-28-2010, 11:57 PM
|
PHP random page script!
|
Posts: 16
Name: Abraham
|
Hey guys -
I'm a newbie to PHP. I'm trying to create a random page with each browser refresh using php.
This is what I have some far:
<?php
$pagesArray = array("random1.php", "random2.php", "random3.php");
$randNum = rand(0, count($pagesArray)-1);
include($pagesArray[$randNum]);
?>
when I refresh the browser, random1.php will be on top of random3.php instead of replacing it. Can someone please peruse the script?
Thanks in advance guys.
|
|
|
|
06-29-2010, 12:06 AM
|
Re: PHP random page script!
|
Posts: 16
Name: Abraham
|
I tryed using the echo statement but no luck 
|
|
|
|
06-29-2010, 07:02 AM
|
Re: PHP random page script!
|
Posts: 181
Name: David Jackson
|
PHP Code:
<?php
$pagesArray = array("random1.php", "random2.php", "random3.php");
shuffle($pagesArray);
echo $pagesArray[0];
?>
|
|
|
|
06-29-2010, 09:46 AM
|
Re: PHP random page script!
|
Posts: 16
Name: Abraham
|
Thanks David - that looks likes what I'm looking for, but I think am placing in the wrong place. It only outputs a string on the top left corner. I put it before my Doctype, and between the head tags - got the same results. Where do I insert it?
Thanks in advance.
|
|
|
|
06-29-2010, 09:47 AM
|
Re: PHP random page script!
|
Posts: 249
Name: octavian
Location: Bucharest
|
PHP Code:
<?php $pagesArray = array('random1.php', 'random2.php', 'random3.php', 'random4.php', 'random5.php', 'random6.php', 'random7.php'); $randNum = rand(0, count($pagesArray)-1); echo $pagesArray[$randNum]; ?>
Looks OK to me. At a refresh you'll get a random value, and I thing this is faster than davidj's version. You can have a problem with the cache, so you can send a no-cache header [via apache or PHP]
// edit // for your question... davidj gave you just an echo (as I did) just for debugging. You have to use your include [require]
Last edited by vectorialpx; 06-29-2010 at 09:50 AM..
|
|
|
|
06-29-2010, 10:34 AM
|
Re: PHP random page script!
|
Posts: 33
Name: Aquarezz
|
Thanks, I was just searching for something like this. Awesome! 
|
|
|
|
06-29-2010, 10:44 AM
|
Re: PHP random page script!
|
Posts: 16
Name: Abraham
|
Thanks for your help, but I'm very sorry. I'm new a newbie to to PHP. The include or require would that go after the variables? In the parentheses of the 'include' statement do I enter the path the file I want to display first? I placed the script before my doctype, is that okay?
<?php
$pagesArray = array('page1.php', 'page2.php', 'page3.php');
$randNum = rand(0, count($pagesArray)-1);
include (path/to/file);
?>
Thanks in advance.
|
|
|
|
06-29-2010, 10:50 AM
|
Re: PHP random page script!
|
Posts: 249
Name: octavian
Location: Bucharest
|
well... it's your code, you know what to do with it
Your text it's OK
PHP Code:
<?php $pagesArray = array("random1.php", "random2.php", "random3.php"); $randNum = rand(0, count($pagesArray)-1); include($pagesArray[$randNum]); ?>
so, it should work. The only thing I did was to get rid of double quotes and add single quotes [it's recommended unless you need double-quote functionality] AND add some array values, to make sure the random is OK.
so, what's the problem? Do you have an error, or something?
add
PHP Code:
<?php error_reporting(E_ALL); // before all... ... ?>
to be sure you don't have errors.
|
|
|
|
06-29-2010, 11:01 AM
|
Re: PHP random page script!
|
Posts: 16
Name: Abraham
|
Thanks for your quick responses Vectorialpx - the script is working now. My problem reverted back to when I posted the initial post. It cycles through the pages, but it displays them on top of each other, instead of replacing the page. Like random one page will be at the top and when I scroll down the page I see random three below it.
Is it the placement of the script that is causing this?
|
|
|
|
06-29-2010, 11:06 AM
|
Re: PHP random page script!
|
Posts: 249
Name: octavian
Location: Bucharest
|
you can post here the script context [2-3 lines before and after it] to understand what's the problem. In this moment, you'll just include a PHP page... I cannot know where 
|
|
|
|
06-29-2010, 11:13 AM
|
Re: PHP random page script!
|
Posts: 16
Name: Abraham
|
<?php
$pagesArray = array("page1.php", "page2.php", "page3.php");
$randNum = rand(0, count($pagesArray)-1);
include($pagesArray[$randNum]);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tansitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
I also use include() throughout the document for header, navigation, footer. I should of mention that earlier. Is there a conflict?
Thanks for the help, I appreciate it.
|
|
|
|
06-29-2010, 11:38 AM
|
Re: PHP random page script!
|
Posts: 249
Name: octavian
Location: Bucharest
|
about the include, it's OK, unless you don't rewrite variables
so, your included-page is included above everything, so you'll get an invalid document (for w3c standards) if you make any output and everything will be on top [this is where you have it]
If it's a page that outputs something, you can make a DIV into your layout, and place it there. SO, you can also control the DIV via CSS with a class.
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tansitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> ............... <body> <div class="myPage"> <?php $pagesArray = array("page1.php", "page2.php", "page3.php"); $randNum = rand(0, count($pagesArray)-1); include $pagesArray[$randNum]; ?> </div> ... and others... .....................
|
|
|
|
06-29-2010, 11:58 AM
|
Re: PHP random page script!
|
Posts: 16
Name: Abraham
|
Thanks It works - just the page I need to display doesn't work? It display a new page after every browser refresh; one of the pages is a videos page. It skips that page, you have an idea of why it might skip that page? I have some javascript before the </body> tag that pertains to the video players of that video page. It just doesn't display that video page.
Thanks
|
|
|
|
06-29-2010, 12:08 PM
|
Re: PHP random page script!
|
Posts: 16
Name: Abraham
|
vectorialpx thanks for all your help. I added few more pages to the $pagesArray variable. Now it cycles through all the pages. Thanks a lot man. If you have an idea, can you please elaborate - if not thanks man!
Have a good day 
|
|
|
|
06-29-2010, 12:09 PM
|
Re: PHP random page script!
|
Posts: 249
Name: octavian
Location: Bucharest
|
what do you mean by "it skips that page"
You can't see the video, or what ?
javascript is client-side, it has no problems with the include.
The javascript is executed after PHP echo, so you have to check the final output [view source, and see what's wrong]
You can use firebug to check javascript errors, or post here that videoPage [inot a code or PHP area]
|
|
|
|
06-29-2010, 02:22 PM
|
Re: PHP random page script!
|
Posts: 16
Name: Abraham
|
Yeah I understand about Javascript being a client side script. What I meant by 'skip' was that on every refresh I would see all the pages from the array except for that one particular video page. Everything works now, thanks for your time and help man. It probably don't make sense - it works now though. Thanks again! 
|
|
|
|
08-02-2010, 06:12 AM
|
Re: PHP random page script!
|
Posts: 13
|
You should try to check the code if you are unable to see the page. It usually happens. Even my web pages were skipped.
|
|
|
|
|
« Reply to PHP random page script!
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|