SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast Yonko's Avatar
    Join Date
    Jun 2001
    Location
    Panama
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SELECT CASE help

    Is there any way of handling options like SELECT CASE does in other languages?

    I need to set optional queries for two different values of the $sid variable (status of the article, marked or active), but I keep getting the same result (the first query), how can I do it?

    PHP Code:
    // The basic SELECT statement
    $select "SELECT DISTINCT News.ID, Text";
    $from   " FROM News";
    $where  " WHERE News.ID > 0";

    if (
    $aid != "") { // An author is selected
      
    $where .= " AND AID=$aid";
    }

    if (
    $cid != "") { // A category is selected
      
    $from .= ", Categories";
      
    $where .= " AND Categories.ID=News.CID AND Categories.ID=$cid";
    }

    if (
    $sid "1") { // The article is active
      
    $where .= " AND News.SID =1";
    } else { 
    // The article is marked
      
    $where .= " AND News.SID =3";
    }

    if (
    $searchtext != "") { // Search text specified
      
    $where .= " AND Text LIKE '%$searchtext%'";


  2. #2
    SitePoint Addict lveale's Avatar
    Join Date
    Jun 2001
    Location
    Dublin
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    switch

    Try the switch operator:

    http://www.php.net/manual/en/control...res.switch.php

    PHP Code:

    switch($sid)
    {
      case 
    1:
         
    $where .= " AND News.SID =1";
         break;
      case 
    3:
         
    $where .= " AND News.SID =3";
         break;

    // etc. etc.....
    //............

      
    default:
         
    $where .= " AND News.SID =1";
         break;
    }
    //end switch 


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
  •