Code problems..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
seeker2921
Forum Contributor
Posts: 120
Joined: Sat Mar 22, 2003 7:10 pm
Location: Wiesbaden Germany
Contact:

Code problems..

Post 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");

?>
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

What is not working? Did you get any error codes? A little more info would help us help you.


John M
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post 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]); 

?>
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

maybe this code would help?
seeker2921
Forum Contributor
Posts: 120
Joined: Sat Mar 22, 2003 7:10 pm
Location: Wiesbaden Germany
Contact:

Post 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..
bionicdonkey
Forum Contributor
Posts: 132
Joined: Fri Jan 31, 2003 2:28 am
Location: Sydney, Australia
Contact:

Post 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
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post 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
Post Reply