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

[eluser]meigwilym[/eluser]
I've got a table with columns for each language (en and cy).

I get the data like this

Code:
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.

Thanks,

Mei

El Forum

[eluser]GSV Sleeper Service[/eluser]
you may be able to use something like this.
Code:
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

[eluser]xwero[/eluser]
Or you could use an alias
Code:
$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

[eluser]meigwilym[/eluser]
Of course.

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

Many thanks,

Mei