Jump to content

echo help


redhairgal

Recommended Posts

I'm a newbie (and this is my first post) and I just can not see what I'm doing wrong with this echo line

 

here's the code:

echo "<select name=\"records\" style=\"width:155px;\"/>";

 

The problem is everything after the semi-colon  to the next line semi-colon gets printed. Can someone who is not dog tired tell me what I did wrong?

Thank you,

red

Link to comment
Share on other sites

Thank you AbraCadaver (LOL) here's the whole snippet:

 

<?php
    include "./recordfx.inc";
    $bd = recordlist();
    echo "<select name=\"records\" style=\"width:155px;\"/>";
    while ($row = mysqli_fetch_array($bd)){ 
        echo ("<option value=\"" . $row[records] . "\">" . $row[records] . "</option>"); 
    }
    echo ("</select>");
?>

Link to comment
Share on other sites

Let's simplify the echos by using single quotes instead of double quotes so there's less escaping:

<?php
    include "./recordfx.inc";
    $bd = recordlist();
    echo '<select name="records" style="width:155px;"/>';
    while ($row = mysqli_fetch_array($bd)){ 
        echo '<option value="' . $row['records'] . '">' . $row['records'] . "</option>"; 
    }
    echo "</select>";
?>

 

Does this still present the problem?

 

Ken

 

Link to comment
Share on other sites

Hi all,

 

    while ($row = mysqli_fetch_array($bd)){

        echo '<option value="' . $row['records'] . '">' . $row['records'] . "</option>";

    }

 

could be replaced with:

 

    while ($row = mysqli_fetch_array($bd)){

        ?>

<option value="<?php echo $row['records'];?>"><?php echo $row['records']; ?></option>

<?php

    }

 

dipping in and out of php is less over head for the parser cpu to deal with, and it makes editing the html easier too, no need to escape the " quotes at all.

 

Rw

Link to comment
Share on other sites

@rwwd that makes it harder to read the structures though. Execution time is cheap, man hours are expensive. Not to mention this is one of those microoptimisations which is almost negligible.

 

@redhairgal just wondering why do you have <select ... /> on the first line? It should just be <select ... >

Not sure if that will help...

Link to comment
Share on other sites

Thank you for all the help yesterday. I got so frustrated it reached the point I was hurting the code more than helping so I walked away. (Of course after trying all the suggested changes). Oh and the

<select />

was a typo thank you for finding it but it didn't fix it

 

I did figure out why the code wasn't working for some reason the mysql data isn't being put into place so the php/html shows because the expected data isn't substituted.

 

For the life of me I can't figure out why the mysql data isn't be substituted. Could it be a state or scope issue? Dropping into and out of php how does that affect the scope of variables? The data is correctly pulled from the db but not inserted. I had to change from mysql to mysqli php connector but my mysqli calls seem to work in every other place but in this one?

 

So I'm clearer on the problem but no closer to a solution. I wanted to thank you all for your generous help!

Thanks again,

red

Link to comment
Share on other sites

You guys are NOT going to believe this. But I finally got the code to work. Actually there was nothing wrong with the code the entire problem was a state issue. I kept thinking and finally REALIZED that loading a drop down list with items then allowing a selection was a 2 state action!

 

I was trying to do this from an html file...... I changed my file extension to php and voila it all works.... 2 days wasted. Anyway you were all kind and generous about helping me and I learned from each and every response and cleaned up my code so it is much cleaner, thanks to all your input. But I just had to let you know what fixed it.

LOL

Thank you again,

red

Link to comment
Share on other sites

@ travo1992,

 

"Execution time is cheap, man hours are expensive"

 

CPU load on the parser is also an issue when you have high bandwidth issues, but I understand what you say, but having the html separated from the php like that means that during that time there would be a lot less load on the parser/CPU - more so if you have HUGE chunks of code being echo'd to screen this would be the better alternative IMO.

 

@redhairgal - we all do that from time to time, cool as its sorted out now :)

 

Rw

 

Link to comment
Share on other sites

@ rwwd

I understand your point, however this is more relevant with larger, or even medium sized chunks of text.

For smaller chunks of code, such as this, it is usually quicker (just) to just echo the code, rather than having php switch in and out

I just tested to make sure I wasnt going to make a fool of myself, over 100000 reps of echoing 3 variables rather than html with inline php, echoing the lot is approx 40ms quicker.

Inline PHP actually peaked higher cpu usage, showing that you need to take the context into account before you apply optimisations.

 

Saying that, for this it probably doesnt really matter too much anyway :P

 

@redhairgal that'll do it to you hehe, glad you worked it out.

Link to comment
Share on other sites

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.