CodeIgniter Forums
$query->row_array() problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: $query->row_array() problem (/showthread.php?tid=35732)



$query->row_array() problem - El Forum - 11-09-2010

[eluser]blorriman[/eluser]
I have a poll with a radio group showing the answers (from a db table 'poll_answer.

When the user selects one of the answers I return the radio button number (ie: 1,2,3, etc), which corresponds to the row of the answer in the table.

I am trying to use $query->row_array() - with the radio button number inside the brackets to find the row in the table.

$uri_3 is the radio button number
$pq_id is the id of the question the answers are associated with.

With the following code:
Code:
$uri_3 = '3';
$pq_id = '15';

    $query = $this->db->get_where('poll_answer', array('pq_id' => $pq_id));
    foreach ($query->result() as $row) {
        echo $row->id . ' - ';
        echo $row->answer . '<br>';
    }

I get all the answers as they are in the table (the number is their id), so I know it is working:
Quote:29 - I think so
30 - I'm not sure
31 - I don't think so

However, since I only want the one answer that corresponds to the radio button selected, I am trying to use the following code:
Code:
$uri_3 = '3';
$pq_id = '15';

    $query = $this->db->get_where('poll_answer', array('pq_id' => $pq_id));

    $row = $query->row_array($uri_3);
    echo $row['id'] . ' - ';
    echo $row['answer'] . '<br>';
But when I change the $uri_3 I get the following:
Quote:$uri_3 = 1 - "30- I'm not sure"
$uri_3 = 2 - "31 - I don't think so"
$uri_3 = 3 - "29 - I think so"

So it's completely out of order. $uri_3 = 1 should give the first row, but instead returns the second row, $uri_3 = 2 should be the second row, but instead returns the third row, and $uri_3 = 3 returns the first row.

Any suggestions?


$query->row_array() problem - El Forum - 11-09-2010

[eluser]InsiteFX[/eluser]
Code:
$this->db->order_by("pq_id", "asc");

InsiteFX


$query->row_array() problem - El Forum - 11-09-2010

[eluser]blorriman[/eluser]
Thanks InsiteFX,
I tried ordering by both asc and desc and it doesn't change the results.


$query->row_array() problem - El Forum - 11-09-2010

[eluser]vitoco[/eluser]
you just need to add another "where" condition to get the row that you want
Code:
$uri_3 = '3';
$pq_id = '15';

    $query = $this->db->get_where('poll_answer', array('pq_id' => $pq_id , 'id' => $uri_3 ));

    // GET THE ROW AS OBJECT
    $row   = $query->row();
    echo $row->id . ' - ';
    echo $row->answer . '<br>';

    // GET THE ROW AS ARRAY
    $row   = $query->row_array();
    echo $row['id'] . ' - ';
    echo $row['answer'] . '<br>';


Saludos


$query->row_array() problem - El Forum - 11-09-2010

[eluser]blorriman[/eluser]
Thanks vitoco,
But $uri_3 is the radio button number that was selected and does not correspond to the poll_answer id.


$query->row_array() problem - El Forum - 11-09-2010

[eluser]vitoco[/eluser]
i don't use row_array() method with the optional parameter, but i think it's zero based, so

1 => 0
2 => 1
3 => 3

so .....
Code:
$uri_3 = '3';
    $pq_id = '15';
    
    $query = $this->db->get_where('poll_answer', array('pq_id' => $pq_id));

    // $uri_3 = 3 -> $uri_3 - 1
    $row = $query->row_array($uri_3 - 1);
    echo $row['id'] . ' - ';
    echo $row['answer'] . '<br>';

Saludos


$query->row_array() problem - El Forum - 11-09-2010

[eluser]blorriman[/eluser]
Brilliant . . . thanks, it works like a charm !