pthurmond Posted August 20, 2009 Share Posted August 20, 2009 Hey guys, I thought I would bounce something off you to see what you thought. I have a project that I have worked for months on. I finally got it done and it works perfectly. To go live with it the IT dept had to copy the site to a production server... and that is when I noticed a problem. I use a lot of objects in my code, including database objects with data returned as objects. Simple matter, keeps code a little bit cleaner than arrays. But I noticed that foreach loops that are iterating through objects (at least in the places I have looked so far) are not running. The server seems to just skip over the code. If I do a print_r() on the variable I see the object in all its glory looking great. I did that right before the loop. So then I moved into the loop itself and tried to echo something at the very beginning, nothing. Not good. Now I want to remind you that this is on the production server, if I check out the same code and output on the dev server it goes into the loop and works fine. So my next thought was that maybe there is a corrupt file on the server. So I tried copying over just that file. That didn't work. Scratching my head I decided to check out trusty phpinfo(). It told me that the dev server is running 5.2.6 and that the production server is running 5.1.6. So my thoughts at this point are maybe there is a PHP version problem. Unfortunately getting IT to upgrade to 5.2.6 on the production server will be difficult since there are a lot of websites running from that server. Are there any known issues with foreach and objects in 5.1? Are there any changes you can suggest aside from switching all my code to use arrays instead of objects to hold data? The only other thing I can add is I did all this in CodeIgniter v1.7.1. Here is a sample of the code... foreach($questions_answers AS $qa): ?> <div class="question_block"> <div class="question"> <?=form_hidden('questions[]', $qa->question_id)?> <div class="question_num"><?=form_label('Question #'. $i, $qa->question_id)?></div> <div class="question_text"><?=$qa->question?></div> </div> <div class="question_answers"> <?php $validate_tag = FALSE; foreach ($qa->answers AS $a): if ($a->answer_id == set_value('qag_'. $qa->question_id)) $checked = TRUE; else $checked = FALSE; $input_params = array( 'name' => 'qag_'. $qa->question_id, 'value' => $a->answer_id, 'checked' => $checked ); if ($validate_tag != TRUE) { $input_params['class'] = 'required'; $validate_tag = TRUE; } ?> <div class="answer"> <?=form_radio($input_params)?> <?=form_label($a->answer)?> </div> <?php endforeach; ?> </div> </div> <?php $i++; endforeach; Quote Link to comment https://forums.phpfreaks.com/topic/171199-server-changes-and-object-iteration-issues/ Share on other sites More sharing options...
RichardRotterdam Posted August 20, 2009 Share Posted August 20, 2009 I notice 2 foreach loops <?php foreach($questions_answers AS $qa): foreach ($qa->answers AS $a): endforeach; endforeach; Now which one wont run? And what is the output of(if its simple enough to show as code and not a load of 2 pages): echo "<pre>",print_r($questions_answers),"</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/171199-server-changes-and-object-iteration-issues/#findComment-902893 Share on other sites More sharing options...
pthurmond Posted August 21, 2009 Author Share Posted August 21, 2009 The outer loop does not execute, so neither does the inner one. As for the output of the loop, each iteration should output something like this (sample taken from the working dev server): <div class="question_block"> <div class="question"> <input type="hidden" name="questions[]" value="50" /> <div class="question_num"><label for="50">Question #2</label></div> <div class="question_text">I'd prefer a job that requires less than 2 years training.</div> </div> <div class="question_answers"> <div class="answer"> <input type="radio" name="qag_50" value="28" class="required" /> <label>Strongly Agree</label> </div> <div class="answer"> <input type="radio" name="qag_50" value="27" /> <label>Agree</label> </div> <div class="answer"> <input type="radio" name="qag_50" value="26" /> <label>Neutral</label> </div> <div class="answer"> <input type="radio" name="qag_50" value="25" /> <label>Disagree</label> </div> <div class="answer"> <input type="radio" name="qag_50" value="24" /> <label>Strongly Disagree</label> </div> </div> </div> I have tried it with braces instead of the method you see in my first sample, but that didn't seem to change anything. Quote Link to comment https://forums.phpfreaks.com/topic/171199-server-changes-and-object-iteration-issues/#findComment-903369 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.