PHP Echo Outputs Wrong Result?

APD Toronto
APD Toronto used Ask the Experts™
on
Hi Experts,

I'm expiriencing one of the strangest issues.

I have the following PHP Code

function ins_checkboxes($name, &$chk_array){
    
    $code = '';
    $lbl = '';
    $type = '';
    $onclick = '';
    
    foreach ($chk_array as $chk):
    
        $code = $chk['code'];
        $lbl = $chk['lbl'];
        $type = $chk['type'];
        
        $onclick = '';
        $div_specify = '';
        echo "code = $code <br />";
        if ($type == 'chk+txt') {
            
            $div_specify = "divSpecify-" . $code;
            $onclick = 'onchange="show_hide(\'' . $div_specify . '\');"';
        }
        echo "code = $code <br />";
    ?>    
    <div class="divChecks">
        
        <input type="checkbox" name="<?php echo $name; ?>[]" 
               id="chk<?php echo $code; ?>" value="<?php echo $lbl; ?>" 
                   <?php echo $onclick; ?> >
        <label for="chk<?php echo $code; ?>"><?php echo $lbl; ?></label>
            
    </div>
    
    <?php
    echo "code = $code <br />";
    if ($type == 'chk+txt'){
    ?>
        <div id="<?php echo $div_specify; ?>" class="divNoDisplay">

            <label for="txt<?php echo $code; ?>">Please Specify</label>
            <input type="text" id="txt<?php echo $code; ?>" name="txt<?php echo $code; ?>">

        </div>
    <?php
    }

    endforeach;
}

Open in new window


Thee resulting HTML of the first 2 loops iss
        code = strQ5Cert_Drive <br>code = strQ5Cert_Drive <br>    
    <div class="divChecks">
        
        <input type="checkbox" name="strQ5Certs[]" id="Driver's Licence" value="Driver's Licence">
        <label for="Driver's Licence">Driver's Licence</label>
            
    </div>
    
    code = strQ5Cert_Drive <br>code = strQ5Cert_Health <br>code = strQ5Cert_Health <br>    
    <div class="divChecks">
        
        <input type="checkbox" name="strQ5Certs[]" id="Health &amp; Safety Representative" value="Health &amp; Safety Representative">
        <label for="Health &amp; Safety Representative">Health &amp; Safety Representative</label>
            
    </div>
    
    code = strQ5Cert_Health <br>

Open in new window


You will note that I'm echo-ing my $code variable twice before my <input> and <label> tags, and once after.

In the first instance, $code = strQ5Cert_Drive, which is correct. However, when I use $code for my <label for=> and  <input id=>, you will see thhat the output  is different ... its that of $lbl.

Its been a long day, maybe I'm tired... How can this be?

Any help will be appreciated,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
This may sound a little obvious, but are you absolutely sure you're calling the correct function. The reason I ask is because, regardless of the issue you're facing, the layout of the generated HTML doesn't match the layout of the HTML in your function. In your function, your <input> is spread over 2 lines, but not in the generated output.

Also, even if the variables were being set wrong, the ID of the input and the for in the label should both start with chk. Those prefixes have nothing to do with the PHP - it's just static HTML
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
I don't see a problem (other than the one's Chris pointed out.)

Let's look at it like this: You have 3 echo statements
Line: 16
Line: 22
Line: 34

These include <br> elements BUT whatever you used to view the code is not the screen so we are not seeing the line breaks (there are no \n's in the string so the output is on the same line.)

We see 6 results of the echo statement
Line 1: x2
Line 9: x3
Line 17: x1

This is correct as your output is 3 per item (2 on a line then 1).
But because there are no line breaks - the 2 from the output of the next item appear on the same line as the last 1 from the previous item.

Not sure if that explains the issue.

It is a bit odd though that your output posted does not match the code - so, as Chris said, difficult to give a definitive answer.
APD TorontoSoftware Developer

Author

Commented:
Chris,

Also, even if the variables were being set wrong, the ID of the input and the for in the label should both start with chk. Those prefixes have nothing to do with the PHP - it's just static HTML

Open in new window


Exactly, but if I change Lines 24-31 to
<!--div class="divChecks">
        
        <input type="checkbox" name="<?php echo $name; ?>[]" 
               id="chk<?php echo $code; ?>" value="<?php echo $lbl; ?>" 
                   <?php echo $onclick; ?> >
        <label for="chk<?php echo $code; ?>"><?php echo $lbl; ?></label>
            
    </div-->

Open in new window


I get...
<!--div class="divChecks">
        
        <input type="checkbox" name="strQ5Certs[]" 
               id="chkstrQ5Cert_Drive" value="Driver's Licence" 
                    >
        <label for="chkstrQ5Cert_Drive">Driver's Licence</label>
            
    </div-->

Open in new window


As you can see, when I add HTML comments the $code is picked up. I find that very wierd.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

APD TorontoSoftware Developer

Author

Commented:
I had a previos JS Function overwriting it... OOOPS ::)
APD TorontoSoftware Developer

Author

Commented:
See previous comment
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Haha - yeah - that'll do it. Lost count of the hours I've spent debugging code only to find it's something else causing the problem !!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial