1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

q. How to set up a PHP comand to show different content on page refresh

Discussion in 'PHP' started by saturn100, May 3, 2011.

  1. #1
    is there any way to get up a php statement to show different HTML code on page reload and refresh

    PHP or JS would be fine

    Thanks
     
    saturn100, May 3, 2011 IP
  2. Projekt.Gopher

    Projekt.Gopher Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What do you mean... you just want to display something new and random on each page load?
     
    Projekt.Gopher, May 3, 2011 IP
  3. saturn100

    saturn100 Well-Known Member

    Messages:
    465
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    111
    #3
    yes
    Show different content on page load
    but HTML not just text
     
    saturn100, May 4, 2011 IP
  4. Mak3MyDay

    Mak3MyDay Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you can use AJAX to load different content
     
    Mak3MyDay, May 4, 2011 IP
  5. samie

    samie Member

    Messages:
    269
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #5
    Yep :) You can do anything with PHP. You'd probably need to have the different content/html stored in a database though. Then you could just have the content pulled out of the database at random whenever the page is refreshed.

    Like for example you've probably seen Wordpress widgets/plugins where it shows on the right side of the site "Random Posts". The script is just randomly pulling posts to be displayed in that widget.

    My guess would be to look into using the "rand" function in php.



    Edit: I may have misunderstood the question too. Sounds like you want maybe want the page to always show specific content when you first go to it, but change if you refresh the page in any way. If so, you could maybe also have it setup so a session is created whenever accessing that page, and in the script have it setup so if the session exists(if it's already been accessed before), then it will different content or whatever you want.
     
    Last edited: May 4, 2011
    samie, May 4, 2011 IP
  6. Mak3MyDay

    Mak3MyDay Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    samie, there's no need in storing html files in a database. You can create html file which contains the content to be shown, then you can load specific content on the html file with specific id or class. You can do this in jquery, here's a link: http://api.jquery.com/load/
     
    Mak3MyDay, May 4, 2011 IP
  7. littlejohn199

    littlejohn199 Peon

    Messages:
    42
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You can use the following code to display content of a page randomly. This code can be used to display a portion/part of a webpage randomly.

    <?php

    $content_path = "content/";
    $content_array = array(
    1 => "test1.html",
    2 => "test2.html",
    3 => "test3.html",
    4 => "test4.html"
    );

    $num = rand (1, 4);

    require_once($content_path.$content_array[$num]);

    ?>

    So in the code above, let's say I have 4 webpages that I want to display each time a page is loaded (test1.html, test2.html, test3.html, test4.html). These webpages are located under folder "content" which is defined in $content_path variable. You can change this variable to anything to fit your needs.

    The next variable is content_array which is used to defined where your webpages/contents are.

    The rand function is a built-in php function which is used to generate a random number within the range you specified. In this case, I use rand(1,4) meaning that I
    want a random number within the range 1-4 everytime a page is load. Notice the range 1-4 corresponds to the number of pages that are defined in $content_array. So, if
    you have 5 webpages instead of 4, then the rand function should be changed to rand(1,5)

    Hope this helps!
    Little John
     
    littlejohn199, May 5, 2011 IP
  8. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    heres something I found, try this.

    <?php 
    $rotate[] = "<a href='LOCATION_URL'><img src='IMAGE_URL' alt='ALTERNATIVE_TEXT' /></a>"; 
    $rotate[] = "<a href='LOCATION_URL'><img src='IMAGE_URL' alt='ALTERNATIVE_TEXT' /></a>"; 
    $rotate[] = "<a href='LOCATION_URL'><img src='IMAGE_URL' alt='ALTERNATIVE_TEXT' /></a>"; 
    $rotate[] = "<a href='LOCATION_URL'><img src='IMAGE_URL' alt='ALTERNATIVE_TEXT' /></a>"; 
    $number = rand(0, sizeof($rotate) - 1); 
    
    echo $rotate[$number]; 
    ?>  
    
    
    Code (markup):
     
    srisen2, May 5, 2011 IP