Page 1 of 1

Code problems..

Posted: Thu Jul 10, 2003 7:42 am
by seeker2921
I'm using a online tutorial to learn php and at each end of the chapter it tells you to do soemthing before you go on.. I'm having problems getting this one to work.. Heres the objective..


Write a script to print to the browser a different url every day of the month with a different category of url for each day of the week using a multi-dimensional array and no conditional statement.

Heres my code.. Thank you for your help..

Code: Select all

<?
// Set date varibles
$month=(date("j")-1);
$week=date("W");
$today=date("n/j/y");
$end=("$main[$week][$month]");
//Set arrays
$main=array($games,$web_hosts,$search_engines,$isp);

$games=array("www.blizzard.com",
			 "www.thesims.com",
             "www.simcity.com",
             "www.diablo.com",
             "www.blackandwhite.com",
             "www.quake.com",
             "www.maxpayne.com");

$web_hosts=array("www.alien-web-networks.com",
				 "www.kytec.com",
                 "www.hostforweb.com",
                 "www.planethosting.com",
                 "www.powweb.com",
                 "www.dellhost.com",
                 "www.alienwebhost.com");

$search_engines=array("www.yahoo.com",
					  "www.google.com",
                      "www.ask.com",
                      "www.msn.com",
                      "www.hotbot.com",
                      "www.updated.com",
                      "www.altavista.com");

$isp=array("www.aol.com",
		   "www.cdsnet.net",
           "www.internetcds.com",
           "www.cvc.com",
           "www.charter.com",
           "www.fireserve.net",
           "www.msn.com",
           "www.juno.com");

//print to screen
echo("Today is $today");
echo(" So that means that todays link is $end");

?>

Posted: Thu Jul 10, 2003 7:51 am
by Johnm
What is not working? Did you get any error codes? A little more info would help us help you.


John M

Posted: Thu Jul 10, 2003 8:14 am
by mudkicker

Code: Select all

<? 
// Set date variables 
$day=date("w"); 
$week=rand(1,4); 
$today=date("n/j/y"); 
//Set arrays 
$main=array(

		"1" => array("www.blizzard.com", 
          "www.thesims.com", 
             "www.simcity.com", 
             "www.diablo.com", 
             "www.blackandwhite.com", 
             "www.quake.com", 
             "www.maxpayne.com"), 

		"2" => array("www.alien-web-networks.com", 
             "www.kytec.com", 
                 "www.hostforweb.com", 
                 "www.planethosting.com", 
                 "www.powweb.com", 
                 "www.dellhost.com", 
                 "www.alienwebhost.com"), 

		"3" => array("www.yahoo.com", 
                 "www.google.com", 
                      "www.ask.com", 
                      "www.msn.com", 
                      "www.hotbot.com", 
                      "www.updated.com", 
                      "www.altavista.com"), 

		"4" => array("www.aol.com", 
         "www.cdsnet.net", 
           "www.internetcds.com", 
           "www.cvc.com", 
           "www.charter.com", 
           "www.fireserve.net", 
           "www.msn.com", 
           "www.juno.com"),

); 
//print to screen 

echo("Today is $today"); 
echo(" So that means that todays link is ".$main&#1111;"$week"]&#1111;$day]); 

?>

Posted: Thu Jul 10, 2003 8:19 am
by mudkicker
maybe this code would help?

Posted: Thu Jul 10, 2003 5:03 pm
by seeker2921
Thanx mudkicker.. That does waht I need it to.. One question..

What exactly does this do.. $week=rand(1,4);
And how is it usedi n my code.. (just so that I undersatnd becuase I've never seen it before..

Posted: Thu Jul 10, 2003 5:16 pm
by bionicdonkey
rand() generates a random number. it also has optional parameters to set min and max values ( first one being min and second one being max). it then sets $week to that value, which is used in relation to $main array to randomly pick which array to get the link from

Posted: Fri Jul 11, 2003 7:18 am
by Johnm
seeker2921,
Keep in mind that rand cannot truly generate a random number only a quasi random number. Computers, by nature, cannot generate true random numbers. That should not make any difference in this application but it is definitely worth noting.

John M