...

View Full Version : Random PHP Index content, is there a smarter way to accomplish this?



bradyj
04-06-2004, 07:59 PM
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:


<?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!

sad69
04-06-2004, 08:25 PM
Use include instead of relocating them somehow...?

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

Sadiq.

raf
04-06-2004, 08:47 PM
Was the coder payed at a fixed rate for each line of 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)

miranda
04-06-2004, 08:49 PM
I have two thoughts.
A) instead of redirecting the user to these pages why not strip the

<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

bradyj
04-06-2004, 10:07 PM
Was the coder payed at a fixed rate for each line of 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 :D


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 :thumbsup:, but how do I tell the script to load one table instead of the other, randomly? Do I just keep listing:


<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.

miranda
04-07-2004, 01:37 AM
here is a quick example with 4 different paragraphs and 4 different links


<?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.

sad69
04-07-2004, 02:10 AM
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.

raf
04-07-2004, 02:26 AM
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)


$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'];

bradyj
04-07-2004, 02:30 AM
Ok, so I tried this, adjusting your code (thank you miranda!):


<?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
$Content = "Over 70,000 square feet of exhibits were designed line? What did I do?


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!

Nightfire
04-07-2004, 03:47 AM
There's a missing semi-colon at the end of this line:


$Image = "<img src=\"../styles/images/portfolio_images/ACF221E.jpg\" width=\"216\" height=\"257\" />"

bradyj
04-07-2004, 07:44 PM
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.

miranda
04-07-2004, 10:30 PM
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.

bradyj
04-07-2004, 10:40 PM
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 :o



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum