Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Senior Coder
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    2,469
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random PHP Index content, is there a smarter way to accomplish this?

    My company website original had coldfusion as their server-side code, but since it was just a plain old static site, I saw little reason to use that anymore -- turned it over to XHTML (I'm de-tablizing and going all CSS as we speak), and it's all Javascript & PHP (a new design will be coming soon, it's needed). http://www.academystudios.com

    Point of this, I was given a script to randomize the home page (index.php) content:
    Code:
    <?php    
    srand((double)microtime()*1000000); 
    $randomtopic = rand(1,8); 
    
    if  ($randomtopic ==  "1") {   
    header('Location: http://www.academystudios.com/random/index1.html');  
    
    } else if ($randomtopic ==  "2") {   
    header('Location: http://www.academystudios.com/random/index2.html');  
    
    } else if ($randomtopic ==  "3") {   
    header('Location: http://www.academystudios.com/random/index3.html'); 
    
    } else if ($randomtopic ==  "4") {   
    header('Location: http://www.academystudios.com/random/index4.html');    
    
    } else if ($randomtopic ==  "5") {   
    header('Location: http://www.academystudios.com/random/index5.html'); 
    
    } else if ($randomtopic ==  "6") {   
    header('Location: http://www.academystudios.com/random/index6.html');    
    
    } else if ($randomtopic ==  "7") {   
    header('Location: http://www.academystudios.com/random/index7.html'); 
    
    } else if ($randomtopic ==  "8") {   
    header('Location: http://www.academystudios.com/random/index8.html'); 
    
    } else {   
    echo ( "ERROR: There was a problem running the script. ");   
    }   
    ?>
    This is nice and good, but I can forsee problems with google indexing this, and, much less, people bookmarking a random page and then never seeing any further randomized content. How can I create the same effect, in one index.php document? Meaning, how do I just randomize that one table of content in just one home page, instead of using 8 home pages and one php script page? There's gotta be a better way -- anyone have any cool ideas? Thanks all!
    // Art is what you can get away with. <-- Andy Warhol
    ...:.:::: bradyjfrey.com : htmldog : ::::.:...

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Use include instead of relocating them somehow...?

    Just a thought off the top of my head...

    Sadiq.

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Was the coder payed at a fixed rate for each line of code?
    PHP Code:
    $randomtopic rand(1,8); 
    header('Location: http://www.academystudios.com/random/index' $randomtopic  '.html'); 
    would have been enough
    About your actual question: i think it depends on what is inside these 8 files, and what is different beween them.

    Depending on that, you could indeed include, or grab the code/content form an array or db (my favorite)
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #4
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,072
    Thanks
    4
    Thanked 8 Times in 8 Posts
    I have two thoughts.
    A) instead of redirecting the user to these pages why not strip the
    Code:
    <html><head></head><body></body></html>
    tags from the pages leaving content and the tags used for the content only. Then instead of redirecting to a page, include that pages content into your index page.

    B) use php variables on the index page with the values set to the different content, then use the random statement to display which variable is shown.

    Using either way above Google and the other search engines shouldn't have a problem indexing the page.

    Now as to finding that specific content, again there is multiple ways to handle this. 1 would be a dropdown taking the user to the content on selection, another would be to use a paging system allowing the user to jump to the next page. those are just a couple different ideas

  • #5
    Senior Coder
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    2,469
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by raf
    Was the coder payed at a fixed rate for each line of code?
    PHP Code:
    $randomtopic rand(1,8); 
    header('Location: http://www.academystudios.com/random/index' $randomtopic  '.html'); 
    No It was just emailed to me as an option, free of charge

    Quote Originally Posted by miranda
    B) use php variables on the index page with the values set to the different content, then use the random statement to display which variable is shown.
    I like this Idea Miranda , but how do I tell the script to load one table instead of the other, randomly? Do I just keep listing:
    Code:
    <table width="216" border="0" cellpadding="0" align="center">
    <tr>
    <td width="216" background="../styles/images/gif/clearpixel.gif"><img src="../styles/images/portfolio_images/ACF221E.jpg" width="216" height="257" /></td>
    </tr>
    <tr>
    <td width="216" background="../styles/images/gif/clearpixel.gif">
        <p class="featuretext">Over 70,000 square feet of exhibits were designed
        and/or built by Academy Studios for the largest natural history
        museum in the Southeast. </p>
        <p class="featuretext"><a href="../main/portfolio/project3.html">Find
        out more</a> about this exhibit.</p>
    </td>
    </tr>
    </table>
    ...different ones underneath each other and have the code load a specific one? My php is low level so far, I'm still in the learning process and have only created some simple forms, etc.
    // Art is what you can get away with. <-- Andy Warhol
    ...:.:::: bradyjfrey.com : htmldog : ::::.:...

  • #6
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,072
    Thanks
    4
    Thanked 8 Times in 8 Posts
    here is a quick example with 4 different paragraphs and 4 different links

    PHP Code:
    <?php
    //start the random engine

    srand((double)microtime()*1000000); 
    $i rand(1,4); 

    // assign a value to the variables 
    //repeat the process for each content and link
    switch($i){
       case 
    1:
          
    $Content "Over 70,000 square feet of exhibits were designed
             and/or built by Academy Studios for the largest natural history
             museum in the Southeast."
    ;
          
    $Link "<a href=\"../main/portfolio/project3.html\">Find
             out more</a> about this exhibit."
    ;
          break;
       case 
    2:
          
    $Content "This is a random paragraph.";
          
    $Link "<a href=\"http://www.phpfreaks.com\">phpfreaks</a>.";
          break;
       case 
    3:
          
    $Content "This is yet another random paragraph.";
          
    $Link "<a href=\"http://www.codingforums.com\">Coding Forums</a>.";
          break;
       case 
    4:
          
    $Content "And another random paragraph.";
          
    $Link "<a href=\"http://www.dataconnective.net\">Shameless Plug</a>.";
          break;
    }
    ?>
    <table width="216" border="0" cellpadding="0" align="center">
    <tr>
    <td width="216" background="../styles/images/gif/clearpixel.gif"><img src="../styles/images/portfolio_images/ACF221E.jpg" width="216" height="257" /></td>
    </tr>
    <tr>
    <td width="216" background="../styles/images/gif/clearpixel.gif">
        <p class="featuretext"><? echo $Content?></p>
        <p class="featuretext"><? echo $Link?> </p>
    </td>
    </tr>
    </table>

    This is one of the easiest ways to have random content appear on your php page.

  • #7
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The problem you'll have with this method (storing in variables) is that your html files will need to be re-written to be escaped and all that, since it will be in a string. That's why I'd opt for the include method if you can...

    Sadiq.

  • #8
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sad69
    The problem you'll have with this method (storing in variables) is that your html files will need to be re-written to be escaped and all that, since it will be in a string. That's why I'd opt for the include method if you can...

    Sadiq.
    The html should be included is single quotes, so no escaping needed, and you'd also get rd of the useless parsing of them in the current script. Includes have the disadvantages that the content gets spread.

    I'de use an array, like this (sorry about needing to scroll but these codeboxes are to small)
    PHP Code:
    $array_random=array(array('Content' => 'Over 70,000 square feet of exhibits were designed and/or built by Academy Studios for the largest natural history museum in the Southeast.''Link' => '<a href="../main/portfolio/project3.html">Find out more</a> about this exhibit.'), 
          array(
    'Content' => 'This is a random paragraph.''Link' => '<a href="http://www.phpfreaks.com">phpfreaks</a>.'), 
          array(
    'Content' => 'This is yet another random paragraph.''Link' => '<a href="http://www.codingforums.com">Coding Forums</a>'),  
          array(
    'Content' => 'And another random paragraph.''Link' => '<a href="http://www.xxx.net">Shameless Plug</a>.'));

    $upper=count($array_random)-1// make it fully dynamic
    srand((double)microtime()*1000000);  
    $i rand($upper);
    //to display
    echo $array_random[$i]['Content']; 
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #9
    Senior Coder
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    2,469
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, so I tried this, adjusting your code (thank you miranda!):
    PHP Code:
    <?php 
    //start the random engine 

    srand ((double) microtime ()* 1000000 );
    $i rand (1,4);

    // assign a value to the variables 
    //repeat the process for each content and link 
    switch( $i ){ 
        case 
    1:
            
    $Image "<img src=\"../styles/images/portfolio_images/ACF221E.jpg\" width=\"216\" height=\"257\" />"
            
    $Content "Over 70,000 square feet of exhibits were designed 
            and/or built by Academy Studios for the largest natural history 
            museum in the Southeast." 
    ;
            
    $Link "<a href=\"../main/portfolio/project3.html\">Find 
            out more</a> about this exhibit." 
    ;
        break; 
        case 
    2:
            
    $Image "<img src=\"../styles/images/portfolio_images/ACF4D02.jpg\" width=\"216\" height=\"337\" />"
            
    $Content "San Francisco history told through the lives 
            of Mariners in the heart of Fisherman's Wharf." 
    ;
            
    $Link "<a href=\"../main/portfolio/project10.html\">Find 
            out more</a> about this exhibit." 
    ;
        break; 
        case 
    3:
            
    $Image "<img src=\"../styles/images/portfolio_images/ACF1E.jpg\" width=\"216\" height=\"257\" />"
            
    $Content "Through imaginative and educational exhibitry,
            visitors gain a unique perspective of Native American communities
            before and after European contact." 
    ;
            
    $Link "<a href=\"../main/portfolio/project2.html\">Find 
            out more</a> about this exhibit." 
    ;
        break; 
        case 
    4:
            
    $Image "<img src=\"../styles/images/portfolio_images/ACF22C4large.jpg\" width=\"216\" height=\"325\" />"
            
    $Content "Colorful and vibrant interactive exhibits teach
            concepts of historical study to solve mysteries of the past." 
    ;
            
    $Link "<a href=\"../main/portfolio/project5.html\">Find 
            out more</a> about this exhibit." 
    ;
        break; 
        case 
    5:
            
    $Image "<img src=\"../styles/images/portfolio_images/ACF105.jpg\" width=\"210\" height=\"325\" />"
            
    $Content "The new Sea Center replicates a real marine
            sciences laboratory, with testing equipment, sampling equipment
            and other hands-on exhibit activities derived from all areas
            of marine sciences." 
    ;
            
    $Link "<a href=\"../main/portfolio/project8.html\">Find 
            out more</a> about this exhibit." 
    ;
        break; 
        case 
    6:
            
    $Image "<img src=\"../styles/images/portfolio_images/gaareaIntro1.jpg\" width=\"216\" height=\"284\" />"
            
    $Content "Housed in a recently restored 1766 historic
            building, the museum will have significant positive impact
            on the families and children of M&eacute;xico and Latin America." 
    ;
            
    $Link "<a href=\"../main/portfolio/project4.html\">Find 
            out more</a> about this exhibit." 
    ;
        break; 
        case 
    7:
            
    $Image "<img src=\"../styles/images/portfolio_images/ACF4A055.jpg\" width=\"210\" height=\"325\" />"
            
    $Content "The Joe Quinney Winter Sports Center celebrates
            the history and future of winter sports." 
    ;
            
    $Link "<a href=\"../main/portfolio/project7.html\">Find 
            out more</a> about this exhibit." 
    ;
            break; 
        case 
    8:
            
    $Image "<img src=\"../styles/images/portfolio_images/ACFFD45.jpg\" width=\"216\" height=\"324\" />"
            
    $Content "In September, The Henry
            Ford opens a new permanent exhibit, \"Heroes of
            the Sky,\" commemorating the Centennial of Flight
            in 2003." 
    ;
            
    $Link "<a href=\"../main/portfolio/project9.html\">Find 
            out more</a> about this exhibit." 
    ;
        break; 
    }
    ?> 
    <table width="216" border="0" cellpadding="0" align="center"> 
        <tr> 
            <td width="216" background="../styles/images/gif/clearpixel.gif"><?  echo  $Image ;  ?></td> 
        </tr> 
        <tr> 
            <td width="216" background="../styles/images/gif/clearpixel.gif"> 
                <p class="featuretext"> <?  echo  $Content ;  ?> </p> 
                <p class="featuretext"> <?  echo  $Link ;  ?> *</p> 
            </td> 
        </tr> 
    </table>
    But it gave me a PHP error on line 135 http://www.academystudios.com/index2.php
    PHP Code:
            $Content "Over 70,000 square feet of exhibits were designed 
    line? What did I do?

    Quote Originally Posted by sad69
    The problem you'll have with this method (storing in variables) is that your html files will need to be re-written to be escaped and all that, since it will be in a string. That's why I'd opt for the include method if you can...

    Sadiq.
    I don't grasp this , can you explain? Thanks for everyone's help!
    Last edited by bradyj; 04-07-2004 at 02:46 AM.
    // Art is what you can get away with. <-- Andy Warhol
    ...:.:::: bradyjfrey.com : htmldog : ::::.:...

  • #10
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    There's a missing semi-colon at the end of this line:
    PHP Code:
    $Image "<img src=\"../styles/images/portfolio_images/ACF221E.jpg\" width=\"216\" height=\"257\" />" 

  • #11
    Senior Coder
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    2,469
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Nightfire, that did it

    miranda, would you like some line of credit for that in the code? Write something and let me know, I'll pop it in there.
    // Art is what you can get away with. <-- Andy Warhol
    ...:.:::: bradyjfrey.com : htmldog : ::::.:...

  • #12
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,072
    Thanks
    4
    Thanked 8 Times in 8 Posts
    bradyj, I am just glad that I could help.

    I did notice one other piece of the code that you need to change though, that is the line where you set upper limits in your random number

    so change it from this :
    $i = rand (1,4);

    to this
    $i = rand (1,8);

    And for each subsequent addition, you would change the last number in the code. That is where Raf's code actually is more dynamic. In that he uses an array so that if you add some more content to the array there is no need to change any more lines of the code.

    Personally I like the use of the array, I have shied away from them though ever since my days in school where we had to make a video poker game using a multi dimensional array, and I never could quite get it to work like it was supposed to.

  • #13
    Senior Coder
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    2,469
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, thank you then, miranda!

    Yeah, I figured out the 1,8 layout early on; it was similiar to my previous code.

    I liked Raf's idea -- but I'm not at that level yet... I will be, but at the moment I don't fully grasp what it does, and what it can do. Yours is more spelled out for me, and I understand how it structures in the code then. I've got much to learn
    // Art is what you can get away with. <-- Andy Warhol
    ...:.:::: bradyjfrey.com : htmldog : ::::.:...


  •  

    Posting Permissions

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