CodeIgniter Forums
[SORTED] DB results with unknown field names - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: [SORTED] DB results with unknown field names (/thread-11804.html)



[SORTED] DB results with unknown field names - El Forum - 09-24-2008

[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


[SORTED] DB results with unknown field names - El Forum - 09-24-2008

[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.


[SORTED] DB results with unknown field names - El Forum - 09-24-2008

[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.


[SORTED] DB results with unknown field names - El Forum - 09-24-2008

[eluser]meigwilym[/eluser]
Of course.

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

Many thanks,

Mei