• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SORTED] DB results with unknown field names

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.



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

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.

Of course.

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

Many thanks,


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.