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 !

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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

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.