2

I'm having trouble generating my query result.

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

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

Will this work?

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

foreach ($query->result_array() as $row)
{
   echo $row['0'];
   echo $row['1'];
   echo $row['2'];
}
1
  • Perhaps we need to know what realistic problem you are actually solving. I mean, you could use a key map to translate indexes to keys, or call array_values() on each row, or maybe even use one of the printf() family functions. Offering the best advice requires better understanding your scenario. I can say, generally, that there should be no echoing in the model, and no querying in the view. Commented Sep 30 at 5:38

2 Answers 2

1

As Edward mentioned, the array returned is an associative array. While there is no standard way to index into assoc array using integers, you could do it this way:

$resultarray = $query->result_array();
// get an array of keys in result
$keys = array_keys($resultarray[0]);

foreach ($resultarray as $row)
{
   echo $row[$keys[0]];
   echo $row[$keys[1]];
   echo $row[$keys[2]];
}
Sign up to request clarification or add additional context in comments.

Comments

1

$query->result_array() generates an associative array. You can't access it's elements by index.

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.