Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP random page script!
Old 06-28-2010, 11:57 PM PHP random page script!
aber's Avatar
Average Talker

Posts: 16
Name: Abraham
Trades: 0
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.
aber is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-29-2010, 12:06 AM Re: PHP random page script!
aber's Avatar
Average Talker

Posts: 16
Name: Abraham
Trades: 0
I tryed using the echo statement but no luck
aber is offline
Reply With Quote
View Public Profile
 
Old 06-29-2010, 07:02 AM Re: PHP random page script!
Extreme Talker

Posts: 181
Name: David Jackson
Trades: 0
PHP Code:
<?php

$pagesArray 
= array("random1.php""random2.php""random3.php");

shuffle($pagesArray);

echo 
$pagesArray[0];

?>
__________________

Please login or register to view this content. Registration is FREE
davidj is offline
Reply With Quote
View Public Profile
 
Old 06-29-2010, 09:46 AM Re: PHP random page script!
aber's Avatar
Average Talker

Posts: 16
Name: Abraham
Trades: 0
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.
aber is offline
Reply With Quote
View Public Profile
 
Old 06-29-2010, 09:47 AM Re: PHP random page script!
vectorialpx's Avatar
Extreme Talker

Posts: 249
Name: octavian
Location: Bucharest
Trades: 0
PHP Code:
<?php
$pagesArray 
= array('random1.php''random2.php''random3.php''random4.php''random5.php''random6.php''random7.php');
$randNum rand(0count($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]
__________________
you can
Please login or register to view this content. Registration is FREE

Last edited by vectorialpx; 06-29-2010 at 09:50 AM..
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 06-29-2010, 10:34 AM Re: PHP random page script!
Aquarezz's Avatar
Experienced Talker

Posts: 33
Name: Aquarezz
Trades: 0
Thanks, I was just searching for something like this. Awesome!
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Aquarezz is offline
Reply With Quote
View Public Profile
 
Old 06-29-2010, 10:44 AM Re: PHP random page script!
aber's Avatar
Average Talker

Posts: 16
Name: Abraham
Trades: 0
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.
aber is offline
Reply With Quote
View Public Profile
 
Old 06-29-2010, 10:50 AM Re: PHP random page script!
vectorialpx's Avatar
Extreme Talker

Posts: 249
Name: octavian
Location: Bucharest
Trades: 0
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(0count($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.
__________________
you can
Please login or register to view this content. Registration is FREE
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 06-29-2010, 11:01 AM Re: PHP random page script!
aber's Avatar
Average Talker

Posts: 16
Name: Abraham
Trades: 0
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?
aber is offline
Reply With Quote
View Public Profile
 
Old 06-29-2010, 11:06 AM Re: PHP random page script!
vectorialpx's Avatar
Extreme Talker

Posts: 249
Name: octavian
Location: Bucharest
Trades: 0
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
__________________
you can
Please login or register to view this content. Registration is FREE
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 06-29-2010, 11:13 AM Re: PHP random page script!
aber's Avatar
Average Talker

Posts: 16
Name: Abraham
Trades: 0
<?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.
aber is offline
Reply With Quote
View Public Profile
 
Old 06-29-2010, 11:38 AM Re: PHP random page script!
vectorialpx's Avatar
Extreme Talker

Posts: 249
Name: octavian
Location: Bucharest
Trades: 0
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(0count($pagesArray)-1);
include 
$pagesArray[$randNum];
?>
</div>
... and others...
.....................
__________________
you can
Please login or register to view this content. Registration is FREE
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 06-29-2010, 11:58 AM Re: PHP random page script!
aber's Avatar
Average Talker

Posts: 16
Name: Abraham
Trades: 0
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
aber is offline
Reply With Quote
View Public Profile
 
Old 06-29-2010, 12:08 PM Re: PHP random page script!
aber's Avatar
Average Talker

Posts: 16
Name: Abraham
Trades: 0
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
aber is offline
Reply With Quote
View Public Profile
 
Old 06-29-2010, 12:09 PM Re: PHP random page script!
vectorialpx's Avatar
Extreme Talker

Posts: 249
Name: octavian
Location: Bucharest
Trades: 0
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]
__________________
you can
Please login or register to view this content. Registration is FREE
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 06-29-2010, 02:22 PM Re: PHP random page script!
aber's Avatar
Average Talker

Posts: 16
Name: Abraham
Trades: 0
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!
aber is offline
Reply With Quote
View Public Profile
 
Old 08-02-2010, 06:12 AM Re: PHP random page script!
Novice Talker

Posts: 13
Trades: 0
You should try to check the code if you are unable to see the page. It usually happens. Even my web pages were skipped.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
darylnelson is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP random page script!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.48389 seconds with 12 queries