Php empty result for language table

I am trying to add multiple language options to the database. I created a table as follows.

enter image description here

And my PHP function:

    public function Languages(){
     $query=mysqli_query($this->db,"
SELECT  * FROM languages") or die(mysqli_error($this->db)); 
                while($row=mysqli_fetch_array($query,MYSQLI_ASSOC)) {
                 $data[]=$row;
              }
             if(!empty($data)) {
                // Store the result into array
                return $data;
              } 
            }

      

So, I used the following code to show the result, but I get an empty result:

<?php 
  $language = $ReSult->Languages();
   $userLanguage = 'english';
   echo  $language['your_family'][$userLangauge];
?>

      

I know if I use $language['1'][$userLanguage];

, then I get the result, but I don't want to use id here. Is there a way to do this in order to show results without using IDs?

+3


source to share


3 answers


You can store the results in an associative array:

while ($row = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
    $data[$row['lang_key']] = $row;
}

      



This way you can access data directly using your key:

echo $language['your_family'][$userLangauge];

      

+2


source


check it out



function getIndex($language  , $key)
{
  foreach ($language as $row) 
  {
    if (strcmp($row["lang_key"] , $key)==0) {
      return $row["id"]
    }  
  }
  return null;
}

...

echo $language[getIndex($language , "your_family")][$userLanguage];

      

+1


source


You can try this way

while ($row = mysqli_fetch_assoc($query)) {
    $data[$row['tagKey']] = $row;
}

      

+1


source







All Articles