2

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>
3
  • What issue you facing with above code ? Can you be more clear ? Commented Mar 25, 2014 at 7:12
  • have you any error? what you getting after return result try with remove comma after <li>'); Commented Mar 25, 2014 at 7:13
  • Why you not return if($query->num_rows > 0 ){ return $query->result_array();} and use foreach() for this result array in your controller/view Commented Mar 25, 2014 at 7:19

2 Answers 2

3

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;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

I need to pass the li elements to front end
0

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

Comments

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.