-1

I am getting fatal error any one can help me what is wrong with code.? please help.

public function index(){


                $t_name = $this->input->post('t_name');
                $row_id = $this->input->post('change_id');

                if($t_name == ''){
                    $t_name = 'table1';                 
                }
                if($row_id == '')
                {
                    $row_id = 1;
                    //Setup a database connection returning a single row value
                    $this->db->select("*");
                    $this->db->where("Q_Id", $row_id);
                    $query = $this->db->get('table1',1,0);

                    if($query->num_rows() > 0) {
                        $variable = $query->row("Q_Id");
                        $qu['res'] = $variable;

                    } else {

                    }
                }

                $this->load->view('admin/question_paper_alter' , $qu);
            }

view code. I want to print my row in this table field. any one can help me.

<?php foreach($res->result_array() as $row):?>
<tr>
    <td><?php echo $row['rrid']?></td>
                    <td><?php echo $row['name']?></td>
                </tr>
                <?php endforeach; ?>                
4
  • what is $res? or do you mean $qu['res']->result_array()? Commented Mar 22, 2017 at 11:03
  • @bansi yes $res comes from controller $qu['res'] I am new to codeiniter so help me. Commented Mar 22, 2017 at 11:05
  • 1
    Have you tried this stackoverflow.com/questions/18347028/… Commented Mar 22, 2017 at 11:07
  • looks like you need $qu['res'] = $query;. I may be wrong (never worked with codeigniter) Commented Mar 22, 2017 at 11:12

2 Answers 2

1

write result_array(); in query itself.

$data= $this->db->get()->result_array();
$i=0;
foreach ($data as $row);
{
       echo $row[$i]->title;
       echo $row[$i]->name;
       $i++;
}

result_array returns result with array with stdobject

Sign up to request clarification or add additional context in comments.

Comments

0

Check https://www.codeigniter.com/userguide3/database/results.html

result_array() is a method of $query

$query = $this->db->query("YOUR QUERY");

foreach ($query->result_array() as $row)
{
        echo $row['title'];
        echo $row['name'];
        echo $row['body'];
}

try do something like Codeigniter result_array() returning one row

3 Comments

I do this same but not working. my $query is $res comes from controller.
try $qu['res'] = $query;
It give me error Parse error: syntax error, unexpected '=' when I try to use $qu['res']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.