SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need switch for multiple database fields

    Imagine a database table with the fields URL, PX, IVR, ER, FW, JB and SM. URL contains people's names. Each cell in each of the other fields can contain one of two values - the name of the field or NULL:

    Code:
    URL | PX | IVR | ER | FW | JB | SM
    
    (1) Joe_White | NULL | NULL | NULL | NULL | NULL | SM
    (2) Cat_Smith | PX | IVR | ER | FW | JB | SM
    (3) Dan_White | PX | NULL | NULL | NULL | NULL | NULL
    (4) Joe_Cool | NULL | IVR | NULL | NULL | NULL | NULL
    (5) Tom_Brown | NULL | NULL | ER | FW | NULL | NULL
    There's one more key to the puzzle: The script I want to write will vary depending on the local website. For example, let's say we're visiting http://www.SM.com, identified by $MySiteID = 'SM'.

    Now suppose you have a query, followed by the following echo statements:

    $PX (equals the contents of the cell on field PX that correlates with the name in Field URL)

    $IVR (displays the contents of the cell on field IVR that correlates with the URL)

    $ER, $FW, $JB & $SM follow the same pattern.

    Can someone show me how to write a switch(es) or IF statement(s) that does the following?:

    Code:
    1. Display "SM" if the field SM contains the value SM (rows 1 and 2 in the example above)
    
    2. If SM is NULL, and there's just one entry in another field, display that value (e.g. "PX" in the third row in the above example or "IVR" in the next row.
    
    3. If there are values in two or more fields, none of which equal $MySiteID ( SM, in this example), display "Multiple Choice" (the last row in the example above).
    Thanks.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    while ($row mysql_fetch_array($query_result)) {

      
    //Check if the column corresponding to the local website is not empty
      
    if (!empty($row[$MySiteID])) {
        echo 
    $MySiteID;
      } else {
        
        
    //That column was empty, so look for values in other columns of the row

        
    $foundFields = array();
        
    $possibleFields = array('PX''IVR''ER''FW''JB''SM');
        foreach (
    $possibleFields as $field) {
          if (!empty(
    $row[$field])) {
            
    $foundFields[] = $row[$field];
          }
        }

        if (
    count($foundFields) == 0) {
          echo 
    'None found';
        } elseif (
    count($foundFields) == 1) {
          echo 
    $foundFields[0];
        } else {
          echo 
    'Multiple Choices: ' implode(', '$foundFields);
        }

      }


    You have a very strange database.
    Last edited by Dan Grossman; Jun 24, 2007 at 03:49.

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Dan.

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    You have a very strange database.
    That's the one thing I like about MySQL; if you can't figure out the right way to do something, you can always use the bigger hammer approach. I wish PHP was that forgiving.


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
  •