2 arrays in a 1 function. Ordering array items by selected array in Smarty

StackOverflow https://stackoverflow.com/questions/23669630

  •  23-07-2023
  •  | 
  •  

Вопрос

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?

Это было полезно?

Решение

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;
}

Другие советы

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;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top