Web Hosting Talk







View Full Version : PHP Challenge #2


DoobyWho
08-09-2002, 11:06 AM
This one moves forward a little and deals with arrays. Good luck.

TODO - Write a script to print to the browser a different URL each day of the week using the switch/case statement and comparing to the numeric day of the week (0-6), not the textual (Sunday-Saturday) day of the week. Use only one print() function to do this and make these urls active hyperlinks with the url title hyperlinked and the url description not hyperlinked. Employ the use of concatenation to create the single print function instead of enclosing in interpreted double quotes to demonstrate your understanding of concatenating strings.

Good Luck!

Ahmad
08-09-2002, 12:44 PM
Answer:


<?php

switch (intval(date('w'))) {
case 0:
$url = 'http://www.google.com/';
$name = 'Google';
$description = 'Great search engine.';
break;
case 1:
$url = 'http://www.google.com/';
$name = 'Google';
$description = 'Great search engine.';
break;
case 2:
$url = 'http://www.google.com/';
$name = 'Google';
$description = 'Great search engine.';
break;
case 3:
$url = 'http://www.google.com/';
$name = 'Google';
$description = 'Great search engine.';
break;
case 4:
$url = 'http://www.google.com/';
$name = 'Google';
$description = 'Great search engine.';
break;
case 5:
$url = 'http://www.google.com/';
$name = 'Google';
$description = 'Great search engine.';
break;
case 6:
$url = 'http://www.google.com/';
$name = 'Google';
$description = 'Great search engine.';
break;
}

printf( "<a href='". $url ."'>" . $name . "</a> -- " . $description );

?>

Ahmad
08-09-2002, 12:46 PM
This will also work, but doens't use concatenation, no print calls at all:


<a href="<?=$url?>"><?=$name?></a> -- <?=$description?>

DoobyWho
08-09-2002, 01:18 PM
Good job. Another thing you can do is declare your date variable outside of your switch statement.


$daynum=date("w");

and then use

switch($daynum)

The Prohacker
08-09-2002, 03:07 PM
<?php

/*
There are several ways to do the same thing, and this is by no means the best...

Data Info:
$url[] //Holds the URL of the site
$name[] //Holds the name of the site
$comment[] //Containts all comments for the site...
*/

//Sundays Info
$url[0] = "http://www.webhostingtalk.com/";
$name[0] = "WebHostingTalk";
$comment[0] = "Here!";
//Mondays Info
$url[1] = "http://www.vbulletin.com/";
$name[1] = "vBulletin";
$comment[1] = "vBulletin’s Home Page";
//Tuesdays Info
$url[2] = "http://www.php.net/";
$name[2] = "PHP.net";
$comment[2] = "PHP’s Home Page";
//Wednesdays Info
$url[3] = "http://www.hostinginvestigator.com/";
$name[3] = "HostingInvestigator";
$comment[3] = "Points to WHT";
//Thursdays Info
$url[4] = "http://www.vbulletin.org/";
$name[4] = "vBulletin.ORG";
$comment[4] = "Place for vB hacks";
//Fridays Info
$url[5] = "http://www.linux.org/";
$name[5] = "Linux";
$comment[5] = "Linux’s Home Page";
//Saturdays Info
$url[6] = "http://www.cpanel.net/";
$name[6] = "Cpanel.NET";
$comment[6] = "Cpanel’s Home Page";

//Select number for the day of the week..
$num = intval(date('w'));

//Print it all nice and neat out...
print "<a href='" . $url[$num] . "'>" . $name[$num] . "</a>: " . $comment[$num];
?>


Switch works great for this, but since its already been done once, I had to think of another way to do the same thing :D

Just added one more comment

DoobyWho
08-09-2002, 03:30 PM
Good thinking. You dont need the intval when your doing the date. It would have worked fine with $num=date("w")

Its a good practice to use the intval , but its not required.

Also, instead of doing the print command the way you did, you could do

print ("<a href=\"$url[$num]\">".$name[$num]."</a>: ".$comment[$num]);


or an echo


echo "<a href=\"$url[$num]\">".$name[$num]."</a>: ".$comment[$num];

Ahmad
08-09-2002, 06:20 PM
Originally posted by The Prohacker



Switch works great for this, but since its already been done once, I had to think of another way to do the same thing :D

Just added one more comment

This is actually how I fist thought about it :)

The Prohacker
08-09-2002, 06:27 PM
Originally posted by Ahmad


This is actually how I fist thought about it :)


I took a few minutes trying to think of another way to do it, than the one posted above...

Thats the best I could think of :D