Welcome Guest, Not a member yet? Register   Sign In
View variable dying in foreach

This is a weird one so please bare with me, I am new to Code Igniter, but I have spent a day fighting this and can't find a good answer.

So a variable is exposed to the view and I have a loop like the following that can use it correctly to fill the options in a select input:

<select id="region_id" name="region_id" onchange="setOptions();">
<option value="">Select Area</option>
<?php $selected = set_value('region_id', (empty($region_id) ? '' : $region_id)); ?>
<?php foreach ($regions as $region) :
if (!empty($region->published)) : ?>
<option value="<?=$region->region_id?>" <?=(set_value('region_id', $selected)==$region->region_id?' selected="selected"':'')?>><?=htmlentities($region->name)?>
<?php endif;
endforeach; ?>

Now elsewhere in the same view, if I go to use this $regions variable again (before or after this instance that is working), it causes the page to throw a connection time out. I have my PHP.ini timeouts set to over 10 seconds and it times out in only 3-4 seconds and watching memory allocation (which I also bumped up) there is no spike. I can echo out a count of the items in the $regions array, but even something as simple as this causes the error:

<?php foreach ($regions as $region) :

endforeach; ?>

I am completely baffled on what would cause this but figured that there must be an obvious explanation. Any thoughts?

I'm not sure this is the solution, but I would recommend using set_select() to build out the options and remove the $selected variable. For example:

PHP Code:
<select id="region_id" name="region_id" onchange="setOptions();">
   <option value="">Select Area</option>
($regions as $region) :
       if (! empty($region->published)) : 
    <option value="<?=$region->region_id?><?=set_select('region_id'$region->region_id, isset($region_id) && $region_id == $region->region_id)?>><?=htmlentities($region->name)?></option>

You may also want to set the options for htmlentities(), since the default character set can change quite a bit based on the version of PHP in use, and the default options are not the best for some applications...

Finally, if the amount of data in $regions is big enough, you might want to consider whether it may be worthwhile to get whatever you need the first time you loop through the data, rather than looping through it again later.

are you sure this code in view is causing the problem and not the code in controller?

(07-08-2015, 07:43 AM)gadelat Wrote: are you sure this code in view is causing the problem and not the code in controller?

This $regions variable works elsewhere in the page and a count and var_dump on it both suggest that it is being sent correctly from the controller to the view.

As a follow up, I was running this locally on WAMP (Windows 8) when I got the problem. Last night out of desperation I pushed it up to a test server............... and there's no error. There must be some environmental issue causing the connection reset, I am baffled as to what at this point. If I do figure it out I will post my findings. Thanks for the replies, I look forward to being involved in this community!

Theme © iAndrew 2016 - Forum software by © MyBB