CodeIgniter Forums

Full Version: [SORTED] DB results with unknown field names
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

I've got a table with columns for each language (en and cy).

I get the data like this

function getdata($lang){

  $sql = "SELECT title_".$lang.", text_".$lang." FROM table ; ";

  $query = $this->db->query($sql);

  foreach ($query->result() as $row){

# etc etc

but then I'm stuck as I either need to call $row->text_cy or $row->text-en.



El Forum

[eluser]GSV Sleeper Service[/eluser]
you may be able to use something like this.
foreach($query->result_array() as $row){
    echo $row[0]; //title_xx
    echo $row[1]; //text_xx
I've not tested this though...

[edit] xwero's alias idea is a much neater solution.

El Forum

Or you could use an alias
$sql = "SELECT title_".$lang." title, text_".$lang." text FROM table ; ";
This way it doesn't matter which language is used and you don't have to use numbers for the fields.

El Forum

Of course.

I haven't thought about fieldname aliases in this context before.

Many thanks,