Server changes and object iteration issues.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pthurmond
Forum Newbie
Posts: 2
Joined: Fri Aug 21, 2009 10:05 am

Server changes and object iteration issues.

Post by pthurmond »

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...

Code: Select all

 
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;
 
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):

Code: Select all

 
<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.


Thanks in advance for time and help.

-Patrick
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Server changes and object iteration issues.

Post by Darhazer »

Are you sure that you don't get in the loops? Or maybe the problem is in the output only?
I noticed that you are using <?= for echo, maybe on the production server short tags are disabled?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Server changes and object iteration issues.

Post by Benjamin »

More than likely, short tags is the issue.

:arrow: Moved to PHP - Code
pthurmond
Forum Newbie
Posts: 2
Joined: Fri Aug 21, 2009 10:05 am

Re: Server changes and object iteration issues.

Post by pthurmond »

I wish it were that simple, but I used the same short tags elsewhere in the same script to make form inputs and it outputs those just fine. Plus I have tried an echo at the beginning of the loop and that never executes either. If it were just the short tags then it should at least output the HTML that is within the loop.
Post Reply