SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    what to do with a number...

    I have a database where I store the weekdays that someone works...
    monday = 1
    tuesday = 2
    ...et cetera

    However, I want to print 1 as monday and 2 as tuesday when I collect the workingdays from someone in the database.
    How do I this? Getting a number as a value and print it as a word.

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use this:

    Code:
    // This code assumes the number is called "$number"
      $week = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
    echo $array[$number + 1];
    Hope that helps.

  3. #3
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx... what about the following?
    PHP Code:
                switch($Daynr){
                  case 
    1: print "<tr><td>Monday &nbsp;</td><td>$Time </td></tr>"; break;
                  case 
    2: print "<tr><td>Tuesday &nbsp;</td><td> $Time </td></tr>"; break;
                  case 
    3: print "<tr><td>Wednesday &nbsp;</td><td> $Time </td></tr>"; break;
                  case 
    4: print "<tr><td>Thursday &nbsp;</td><td> $Time </td></tr>"; break;
                  case 
    5: print "<tr><td>Friday &nbsp;</td><td> $Time </td></tr>"; break;
                  case 
    6: print "<tr><td>Saturday &nbsp;</td><td> $Time</td></tr>"; break;
                  case 
    7: print "<tr><td>Sunday &nbsp;</td><td> $Time</td></tr>"; break;
                } 
    ....


  4. #4
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, that could work, but obviously it's a lot more code than this:

    Code:
      $week = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
      print "<tr><td>" . $array[$Daynr + 1] . " &nbsp;</td><td>$Time </td></tr>";

  5. #5
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does that also work when people only work on 2 days or 4?

  6. #6
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm afraid I don't understand. If they have a number in the database corresponding to a day of the week that is no lower than 1, and no higher than 7, it will print out the appropriate day of the week.

    Are you saying you need to print this out for ALL days, rather than just one individually?

    If this is the case, then I recommend using a function, like this. It's more efficient than the switch statement, because, as you can see, you re-use a lot of the same code:

    Code:
      function getDay($n) {
        switch($n) {
          case 1: $day = "Monday"; break;
          case 2: $day = "Tuesday"; break;
          case 3: $day = "Wednesday"; break;
          case 4: $day = "Thursday"; break;
          case 5: $day = "Friday"; break;
          case 6: $day = "Saturday"; break;
          case 7: $day = "Sunday"; break;
        }
        return $day;
      }
    Then, when calling it, just use this:

    Code:
    print "<tr><td>" . getDay($Daynr) . " &nbsp;</td><td> $Time</td></tr>";


Bookmarks

Posting Permissions

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