SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Zealot Dreamweb Designs's Avatar
    Join Date
    Nov 2005
    Location
    Detroit, MI.
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Random text/image problems (probibly really simple, but im a total noob to php)

    ok, i have this random script generator, which makes words/images show at random on my website. what i need to know, is how i can take this, and make sure it doesnt show the same text/image over and over again when refreshing the page.

    here is the php code:

    PHP Code:
    $random_content="random_content.txt"
    $random_content=file("$random_content");
    $display=rand(0sizeof($random_content)-1);
    echo 
    $random_content[$display]; 
    now, in the random_content.txt file, it looks like this.

    <div style="font:16px georgia; color: #cc0000;"><img src="web1.jpg"><br />this is where the website description will go<br /><br /><a href="#">Link Here</a></div>
    <div style="font:16px trebuchet ms; color: #000066;"><img src="web2.jpg"><br />this is where the website description will go<br /><br /><a href="#">Link Here</a></div>
    i know this is probibly really simple, but i just need to know how to make the php make everything show up, one after the other, instead of multiple times in a row, when refreshing the page.

    thanks to anyone who can help. im a total noob to the php scene, and any help is really appreciated. thanks

    - Robb
    with great sacrifice comes great rewards

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would probably use sessions:

    PHP Code:
    $random_content="random_content.txt";
    $random_content=file($random_content);

    if (isset(
    $_SESSION['display'])) {
        
    $display $_SESSION['display'] + 1;
        if (
    $display >= sizeof ($random_content))
            
    $display 0;
    } else
        
    $display rand(0sizeof($random_content)-1);

    $_SESSION['display'] = $display// remember for next time

    echo $random_content[$display]; 
    Remember to session_start() right at the top of your file, before any character output. The first output will be random, and after that it will cycle.

  3. #3
    SitePoint Zealot Dreamweb Designs's Avatar
    Join Date
    Nov 2005
    Location
    Detroit, MI.
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok so, session_start() should go at the top of the php coding you showed me? or should it go in the file that this will show up in?

    http://www.cre8tiv-vision.com/random/random.php this is where i have the files running.

    thanks for all the help.

    - Robb
    with great sacrifice comes great rewards

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dreamweb Designs
    ok so, session_start() should go at the top of the php coding you showed me? or should it go in the file that this will show up in?

    http://www.cre8tiv-vision.com/random/random.php this is where i have the files running.

    thanks for all the help.

    - Robb
    At the very top of the PHP source, in your case your /random/random.php file:

    PHP Code:
    <?php
        session_start
    ();
    ?>
    <html>
    <head>
    The very first characters in your source must be <?php - even a single blank or a newline is bad.

    Then the rest of the code can go wherever you need it. The call to session_start() must be made while you can still send HTTP headers.

  5. #5
    SitePoint Zealot Dreamweb Designs's Avatar
    Join Date
    Nov 2005
    Location
    Detroit, MI.
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow. that works perfect. thanks. i appreciate it a lot.

    - Robb
    with great sacrifice comes great rewards

  6. #6
    SitePoint Zealot Dreamweb Designs's Avatar
    Join Date
    Nov 2005
    Location
    Detroit, MI.
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, one last thing i noticed with this, is, it brings up the differnet text/images in order of how i have them typed in the random_content.txt file. is there a way that i can get it to randomize which ones are brought up, instead of going in the same order every time?

    thanks for the help. all is appreciated.

    - Robb
    with great sacrifice comes great rewards


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •