Welcome Guest, Not a member yet? Register   Sign In
[SORTED] DB results with unknown field names
#1

[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
#2

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

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

[eluser]meigwilym[/eluser]
Of course.

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

Many thanks,

Mei




Theme © iAndrew 2016 - Forum software by © MyBB