Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2002
    Location
    Texas
    Posts
    353

    PHP Challenge #2

    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!

  2. #2
    Join Date
    Jan 2002
    Location
    Kuwait
    Posts
    679
    Answer:

    PHP Code:
    <?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 Alhashemi
    PHP, Apache, C, Python, Perl, SQL
    18 related BrainBench certificates

  3. #3
    Join Date
    Jan 2002
    Location
    Kuwait
    Posts
    679
    This will also work, but doens't use concatenation, no print calls at all:

    PHP Code:
    <a href="<?=$url?>"><?=$name?></a> -- <?=$description?>
    Ahmad Alhashemi
    PHP, Apache, C, Python, Perl, SQL
    18 related BrainBench certificates

  4. #4
    Join Date
    Mar 2002
    Location
    Texas
    Posts
    353
    Good job. Another thing you can do is declare your date variable outside of your switch statement.

    PHP Code:
    $daynum=date("w"); 
    and then use
    PHP Code:
    switch($daynum

  5. #5
    Join Date
    May 2001
    Location
    Dayton, Ohio
    Posts
    4,962
    PHP Code:
    <?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

    [edit]Just added one more comment[/edit]
    -Mat Sumpter
    Director, Product Engagement
    Penton Media

  6. #6
    Join Date
    Mar 2002
    Location
    Texas
    Posts
    353
    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
    PHP Code:
    print ("<a href=\"$url[$num]\">".$name[$num]."</a>: ".$comment[$num]); 
    or an echo

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

  7. #7
    Join Date
    Jan 2002
    Location
    Kuwait
    Posts
    679
    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

    [edit]Just added one more comment[/edit]
    This is actually how I fist thought about it
    Ahmad Alhashemi
    PHP, Apache, C, Python, Perl, SQL
    18 related BrainBench certificates

  8. #8
    Join Date
    May 2001
    Location
    Dayton, Ohio
    Posts
    4,962
    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

Posting Permissions

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