Welcome Guest, Not a member yet? Register   Sign In
Foreach problem with 5.3.15
#1

[eluser]j0se[/eluser]
Hi Folks.

I have a problem with a foreach at php 5.3.15. It returns a error:
Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined variable: d
Filename: core/Loader.php(829) : eval()'d code
Line Number: 20

But if I use a php 5.2 (its works)

Can anybody help me?

In view:
Code:
<?foreach ($dat as $d):?>
   <tr>
    <td>&lt;?=$d['one']?&gt;</td>
    <td>&lt;?=$d['two']?&gt;</td>
    <td>&lt;?=$d['three']?&gt;</td>
    <td>&lt;?=$d['four']?&gt;</td>
    
   </tr>
   &lt;?endforeach?&gt;

In model:
Code:
function return_record() {
  $this->db->select('one,two,three,four');
  $q=$this->db->get('table');
  return $q->result_array();
  
}

In controller:

Code:
$dat_v=array ('dat' => $this->main_model->return_record());
$this->load->view('view_1',$dat_v);


Thx in advance.
#2

[eluser]MonkeyZeus[/eluser]
It sounds like $dat is empty.

Code:
&lt;?php

//try using this in your view
echo '<pre>'.print_r($dat, true).'</pre>';

if(count($dat) > 0){
   foreach($dat as $d){
?&gt;
   <tr>
      <td>&lt;?=$d['one']?&gt;</td>
      <td>&lt;?=$d['two']?&gt;</td>
      <td>&lt;?=$d['three']?&gt;</td>
      <td>&lt;?=$d['four']?&gt;</td>
   </tr>
&lt;?php
   }
}

?&gt;
#3

[eluser]j0se[/eluser]
$dat has values an array, but with print_r($dat)

I dont understand
#4

[eluser]MonkeyZeus[/eluser]
Please post the output of:

Code:
echo '<pre>'.print_r($dat, true).'</pre>';



Or you can try this and see if $d is what you expect it to be:

Code:
&lt;?php

if(count($dat) > 0){
   foreach($dat as $d){
      echo '<pre>'.print_r($d, true).'</pre>';
   }
}

?&gt;
#5

[eluser]CroNiX[/eluser]
try using a full php open tag here:
Code:
&lt;?foreach ($dat as $d):?&gt;

Code:
&lt;?php foreach ($dat as $d): ?&gt;
#6

[eluser]j0se[/eluser]
Its works. Thx.

I see that in 5.3 short_tags are off by default.





Theme © iAndrew 2016 - Forum software by © MyBB