Question

NEW QUESTION:

I used this codes:

function FetchCategories(){
    // Get categories array
    $categories = $this->dbh->getAll("select id,category,categoryen from ".TABLE_PREFIX."categories order by category,categoryen", DB_FETCHMODE_ASSOC);
    return $categories;
}

These codes order categoryen according to category. Second order array categoryen is not important. I want to order:

if category request, then order by category;

if categoryen request, then order by categoryen.

Is there any way to solve this?


OLD, RESOLVED QUESTION:

I have two category name phrases in the database. Their column titles are "category" and "categoryen". My php code like this:

{if LANGUAGE_ID eq "TR"}
                          <option value=\"{$categories[i].id}\">{$categories[i].category}</option>
{/if}
{if LANGUAGE_ID eq "EN"}
                          <option value=\"{$categories[i].id}\">{$categories[i].categoryen}</option>
{/if}

But I have old function which is support only category phrases

function FetchCategories(){         // Get categories array         $categories = $this->dbh->getAll("select id,category from ".TABLE_PREFIX."categories order by category", DB_FETCHMODE_ASSOC);       return $categories;     }

How can I add "categoryen" support to the this FetchCategories function?

Was it helpful?

Solution

Answer for changed question:

You can pass parameter to the function

function FetchCategories($currentLang){
   $order = 'category';
   if ($currentLang == 'en') {
      $order = 'categoryen';
   }

    // Get categories array
    $categories = $this->dbh->getAll("select id,category,categoryen from ".TABLE_PREFIX."categories order by {$order}", DB_FETCHMODE_ASSOC);
    return $categories;
}

And then you need to run your function fetchCategories($lang) where $lang is your current language

or if you can use LANGUAGE_ID constant in your function you can do it that way:

function FetchCategories(){
   $order = 'category';
   if (LANGUAGE_ID == 'en') {
      $order = 'categoryen';
   }

    // Get categories array
    $categories = $this->dbh->getAll("select id,category,categoryen from ".TABLE_PREFIX."categories order by {$order}", DB_FETCHMODE_ASSOC);
    return $categories;
}

OTHER TIPS

You can do this simple that way:

function FetchCategories(){
    // Get categories array
    $categories = $this->dbh->getAll("select id,categorytr,categoryen from ".TABLE_PREFIX."categories order by categorytr, categoryen", DB_FETCHMODE_ASSOC);
    return $categories;
}

or as alternative:

function FetchCategories(){
    $lang = strtolower(LANGUAGE_ID)   
    // Get categories array
    $categories = $this->dbh->getAll("select id,category{$lang} from ".TABLE_PREFIX."categories order by category{$lang}", DB_FETCHMODE_ASSOC);
    return $categories;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top