We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

foreach

Development and Programming

rhino18's avatar
rhino18
7 posts
15 years ago
rhino18's avatar rhino18

Hello, I tried looking around but couldn’t find anything on this subject. I have v2.1.0 build 20100810 and have a temple (called from an embed tag) with PHP allowed on input. I ran a query and wanted to use a foreach loop with the data, but when I do so, the temple fails to process EE template tags. As soon as I remove the foreach statement, the template then is able to use the tags. Is this because there is something wrong with the foreach? Or is there a setting I am missing?

Thank you!

       
Greg Aker's avatar
Greg Aker
6,022 posts
15 years ago
Greg Aker's avatar Greg Aker

can you show some code you’re using? You get any errors? how does it fail to process template tags?

       
rhino18's avatar
rhino18
7 posts
15 years ago
rhino18's avatar rhino18

OK, I did some other checking around–it fails because my results array is empty. Tested a few queries I selected at random, like: $this->EE->db->query(“SELECT message_id FROM exp_message_data”);

And they always return something like:

CI_DB_mysql_result Object
(
    [conn_id] => Resource id #38
    [result_id] => Resource id #83
    [result_array] => Array
        (
        )

    [result_object] => Array
        (
        )

    [current_row] => 0
    [num_rows] => 37
    [row_data] => 
)

I have another query running the page that returns data from exp_members just fine.

       
Greg Aker's avatar
Greg Aker
6,022 posts
15 years ago
Greg Aker's avatar Greg Aker

What’s your code to generate that?

Look at the CI docs on the database class: http://ellislab.com/codeigniter/user-guide/database/active_record.html

you need something like:

$query = $this->EE->db->select('foo')->get('bar');

if ($query->num_rows() !== 0) 
{
   foreach ($query->result() as $row) 
   {
      var_dump($row);

      // or
      echo $row->foo; // for the var you're trying to get
   }
}

I hope that makes sense. The CI docs should help.

-greg

       
rhino18's avatar
rhino18
7 posts
15 years ago
rhino18's avatar rhino18

Thank you!

       
rhino18's avatar
rhino18
7 posts
15 years ago
rhino18's avatar rhino18

Same result with a chained method as outlined in the CI documentation.

I printed out the CI_DB_mysql_driver Object and the query was valid. However, the result array returned empty–but the number of records was correct.

       
rhino18's avatar
rhino18
7 posts
15 years ago
rhino18's avatar rhino18

Here is one thing I’ve tried when retrieving number of new PM’s:

<?php 
echo "<pre>";
$this->EE->db->select('private_messages')->where('member_id', $this->EE->session->userdata['member_id']);
$new_messages = $this->EE->db->get('exp_members');
print_r($new_messages);
echo "</pre>

<p>”;
?>
</pre>

Which gives

CI_DB_mysql_result Object
(
    [conn_id] => Resource id #38
    [result_id] => Resource id #66
    [result_array] => Array
        (
        )

    [result_object] => Array
        (
        )

    [current_row] => 0
    [num_rows] => 1
    [row_data] => 
)
       
rhino18's avatar
rhino18
7 posts
15 years ago
rhino18's avatar rhino18

But, now this works:

$mquery = $this->EE->db->query("SELECT private_messages FROM exp_members WHERE member_id = '".$this->EE->session->userdata['member_id']."'"); 
echo "<pre>";
print_r($mquery);
echo "</pre>

<p>”;
</pre>

However, any query run after that in the query() method returns proper number of rows, but does not return result array or object.

       
rhino18's avatar
rhino18
7 posts
15 years ago
rhino18's avatar rhino18

Actually, it appears that given the way shown in my last post, most other queries fail to return a result array.

       
rhino18's avatar
rhino18
7 posts
15 years ago
rhino18's avatar rhino18

Anyone?

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.