Jump to content

Recommended Posts

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;

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>";

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.