Welcome Guest, Not a member yet? Register   Sign In
variables in if statement
#1

[eluser]brian88[/eluser]
I am using a variable in a if statement and it is not recognizing it as a normal variable. Just gives me an error saying unknown variable $i. any ideas?

Code:
&lt;?php for ($i=1; $i<=$num_of_images ; $i++): ?&gt;
  // this $i does not work, unknown var
   &lt;?php if( $img->image.$i != 'nopic.jpg' ): ?&gt;

heres the full code

controller
Code:
$data['images'] = $this->main_mod->getImages();
$data['num_of_images'] = 8;

// views
$data['content'] = 'main/post';
$this->load->view('templates/main', $data);

view
Code:
&lt;?php foreach($images as $img): ?&gt;
   <p>&lt;?php echo $img->name; ?&gt;</p>

  &lt;?php for ($i=1; $i<=$num_of_images ; $i++): ?&gt;
  // this $i does not work, unknown var
   &lt;?php if( $img->image.$i != 'nopic.jpg' ): ?&gt; // if an image was uploaded by user

   <img src="&lt;?php echo base_url('assets/images/').'/'.$img-&gt;image . $i; ?&gt;" width="30" alt=""><br />

   &lt;?php endif; ?&gt;
  &lt;?php endfor; ?&gt;
&lt;?php endforeach; // end ?&gt;
#2

[eluser]PhilTem[/eluser]
I'm not sure if this solves your problem, but it will at least narrow down where to look for:
Code:
&lt;?php if( $img->{image.$i} != 'nopic.jpg' ): ?&gt; // if an image was uploaded by user

If you want to address all attributes image1, image2, image3, ... and so on and so forth for $img, it should be the solution. Otherwise you would have to clarify what exactly you want to get Wink
#3

[eluser]brian88[/eluser]
thanks. but no luck. the error i get is...

Use of undefined constant image - assumed 'image'

Code:
&lt;?php  if( $img->{image.$i} != 'nopic.jpg' ): ?&gt;
#4

[eluser]PhilTem[/eluser]
Ah man, as I was just writing another possible solution, I most likely found your problem

Try:
Code:
&lt;?php  if( $img->{'image' . $i} != 'nopic.jpg' ): ?&gt;

Gonna give you a beer if it's still not the solution Wink




Theme © iAndrew 2016 - Forum software by © MyBB