vini
vini

Reputation: 4722

How to pass sql rows to array in codeigniter

How do I make an array of the sql rows please help I am new to PHP?

$data = null;
if ($query->num_rows() > 0) {
  foreach ($query->result() as $row) {
    $SurveyTitle = $row->SurveyTitle;
    $SurveyId = $row->SurveyId;
    $data =array('lidata' => '<li id=' . $SurveyId . '><a href=' . $SurveyTitle . '>' . $SurveyTitle . '</a><li>',);
  }
  return $data;
}
else
{
  return $data;
}

EDIT: I need to pass the lidata to my view

<ul class="nav nav-sidebar">
  <li class="active"><a href="#">Home</a></li>
  <li><a href="#">Add new Survey</a></li>
  <?php echo $lidata;?>
</ul>

Upvotes: 2

Views: 169

Answers (2)

Mohan
Mohan

Reputation: 4829

instaead if $query->result() use $query->result_array()

your function will be something like this

$data = null;
    if ($query->num_rows() > 0) {
        $data = $query->result_array();
        return $data;
    }
    else
    {
        return $data;
    }

Upvotes: 3

Littlebobbydroptables
Littlebobbydroptables

Reputation: 3731

If you want just to store mysql results into an array you need to do this:

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

Thats all, just check it:

var_dump($allResults);

More information about Codeigniter database helper you can find here: http://ellislab.com/codeigniter/user-guide/database/results.html

Upvotes: 0

Related Questions