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
Share on other sites

Your question wasn't that clear, but if I understand what you are asking, then yes you can do that. Did you try it out before asking?

 

Please use code tags when posting code in the futre. Its the # button above the little smilies when you are typing your post.

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.