Jump to content

using a case function


project3

Recommended Posts

I have a function using case. Can I loop through the case's and fill that in with the db loop.

 

function &getSortScheme($sortBy){

        switch($sortBy){

            case 'PriceHiLo':

              return 'myPrice desc';

              break;

 

            case 'PriceLoHi':

              return 'myPrice asc';

              break;

 

            case '':

              return 'sort_order asc';

              break;

 

              default;

              return 'sortOrder, pName, myPrice';

              break;

        }

    }

 

Now what i want to do is have a sql statement and fill in more of the case like.

 

$query = "SELECT DISTINCT ds_products.pID, ds_products.foodcat, ds_product_categories.pID, ds_product_categories.catID FROM ds_products, ds_product_categories WHERE (ds_products.pID = ds_product_categories.pID) AND ds_product_categories.catID = '$c' GROUP BY foodcat";

 

$result = mysql_query($query) or die(mysql_error());

  while($row = mysql_fetch_array($result, MYSQL_ASSOC))

 

  { 

 

      $foodcat = $row['foodcat'];

 

      case "$foodcat":

      return "$foodcat";

      break;

 

 

 

}

 

 

   

Link to comment
https://forums.phpfreaks.com/topic/94195-using-a-case-function/
Share on other sites

Yes I tried it out it doesn't work. I get an error. Parse error: syntax error, unexpected T_CASE

 


function &getSortScheme($sortBy){
         switch($sortBy){
            case 'PriceHiLo':
               return 'myPrice desc';
               break;

            case 'PriceLoHi':
               return 'myPrice asc';
               break;

             case '':
               return 'sort_order asc';
               break;

  
           case "Rolls":
               return "Rolls";
               break;

  
   

    
$c = $_REQUEST["c"];
  




$query = "SELECT DISTINCT ds_products.pID, ds_products.foodcat, ds_product_categories.pID, ds_product_categories.catID FROM ds_products, ds_product_categories WHERE (ds_products.pID = ds_product_categories.pID) AND ds_product_categories.catID = '$c' GROUP BY foodcat";
   
$result = mysql_query($query) or die(mysql_error());
   while($row = mysql_fetch_array($result, MYSQL_ASSOC))

   {  

       $foodcat = $row['foodcat'];

      case "$foodcat:";
      return "$foodcat";
      break;



}

Link to comment
https://forums.phpfreaks.com/topic/94195-using-a-case-function/#findComment-482489
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.