PHP Echo Outputs Wrong Result?

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,
APD TorontoSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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
0
Julian HansenCommented:
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.
0
APD TorontoSoftware DeveloperAuthor 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.
0
Determine the Perfect Price for Your IT Services

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

APD TorontoSoftware DeveloperAuthor Commented:
I had a previos JS Function overwriting it... OOOPS ::)
0
APD TorontoSoftware DeveloperAuthor Commented:
See previous comment
0
Chris StanyonWebDevCommented:
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 !!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.