Jump to content

loading a db table into Multidimentional array


Xajel

Recommended Posts

I have a small table for word translating, the table includes category ( wich specify in wich category this pharse is used, duo to some dublicates in name ), the pharse name, and translation in both Arabic & English. so the coulmns are id, category, name, ar, en

 

the problem I have is I use this fuction too much, and it will take too many queries if I choosed to make a query every time I needed a single word only !!

 

that's why I'm planning to load the whole table into multi-dimentional array then I'll just put the array as I want for example

 

// example
$trans["category"]["name"]["ar"] // for arabic translation...
$trans["category"]["name"]["en"] // for english translation...

// real example
$trans["hw"]["cpu"]["ar"] // open's Hardware category, the pharse is 'cpu', and the lang is Arabic
$trans["sw"]["os"]["en"] // open's Software category, the pharse is 'os' and the lang is English

 

the proble is with loading the table into this multidimentional array, I may use some while and if or something like that, I'm currently working on one, but I want some ideas or maybe just another effective way of doing this...

 

just to note,

- categories may not be in order in the db and they don't have specific number, they maybe 5 or 10.

- pharses are not fixed in number, some categories may have only two pharses, and some other may have 14 pharses or more.

- currenly only two languages, Arabic and English, but in not the near future we may have more.

 

the array will be filled one time only ( per page or per the whole script or per session, I don't know how PHP is handling variables and arrays ), so only one query will be required rather than a query for every pharse !

<?php
function getTransArray()
{
    $sql = "SELECT category, name, language, phrase FROM translationTable
            ORDER BY category, name, language";
            
    $res = mysql_query($sql) or die (mysql_error());

    $trans = array();
    while (list ($cat, $name, $lang, $phr) = mtsql_fetch_row($res))
    {
        $trans[$cat][$name][$lang] = $phr;
    }
    return $trans;
}

?>

Thanks Barand, ofcourse after some changes

 

the original table has both Arabic & English translation in the same row, and there's no Pharse column

 

so I changed the code to

 

if (!is_array($trans)) {
//function getTransArray() {
    $sql = "SELECT `category`, `name`, `ar`, `en` FROM `translate` ORDER BY category, name";

    $res = mysql_query($sql) or die (mysql_error());

    $trans = array();
    while (list ($cat, $name, $ar, $en) = mysql_fetch_row($res))
    {
        $trans[$cat][$name]["ar"] = $ar;
        $trans[$cat][$name]["en"] = $en;
    }
}

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.