Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi all,

I bought this book 2 weeks ago and have starting reading it a few days back.

I am having a problem. I'm doing the review of the 2nd chapter but I modified the tasks a bit and I am in a quagmire with a function inside a function.

 

The problem is that I want the "checked" to be contained inside the <input checked="checked"> but instead it keeps reverting back to before the entire html output.

 

 

Relevant PHP

 

// function to control which radio button will be checked depending on the value entered by user
function ifstatement1 ($value,$id) {
    if
    (
     ($value < 5.7) && ($id==1) // checked first radio button
     OR
     (($value>5.7) && ($value <6.4)) && ($id==2) // checked second radio button
     OR
     ($value > 6.4 ) && ($id==3) // checked third radio button
     )
    {
        echo "checked";
    }
}
// Create radio button function
function create_radio ($id, $name, $value, $description){
    echo '<p><input type="radio" id="'.$id.'" name="'.$name.'" value="'.$value.'" checked="'. ifstatement1 ($value,$id).'" > '. $description .'</p>';
}

 

Any help will be appreciated,

 

Thanks

Link to comment
Share on other sites

You can't put an if statement in the middle of a PHP statement.

I think you're crossing up the syntax used when PHP is mixed with HTML and just PHP.

 

To get what you want, change your code to the following:

 

function create_radio ($id, $name, $value, $description){
    echo '<p><input type="radio" id="'.$id.'" name="'.$name.'" value="'.$value.'" checked="';
    
    if
    (
     (($value < 5.7) && ($id==1)) // checked first radio button
     ||
     ((($value>5.7) && ($value <6.4)) && ($id==2)) // checked second radio button
     ||
     (($value > 6.4 ) && ($id==3)) // checked third radio button
     )
    {
        echo "checked";
    }
    
    echo '" > '. $description .'</p>';
}
Link to comment
Share on other sites

Thanks for this solution, it works just fine.

I didn't know a function couldn't be inside an echo statement.

 

I know this is unrelated but eventhough the correct <input> has checked="checked" --> this doesn't display on the screen as such.

Do you know why this is, do i need to use javascript or something?

 

& just way off the point, if this question doesn't bother you, do you know how long it would take to get good at PHP,MySQL & Javascript?

[like good enough to do it as a proper paid job]

I work part-time in another field but I can spend around 3 full days per week learning this web development & just started recently enough.

Link to comment
Share on other sites

It depends on the input type you're using (e.g., text, radio, checkbox).

For example, you can't used checked="checked" with text input (because it just doesn't make any sense to do so and no such attribute is defined for text input in HTML anyway).

As such, make sure you're using either radio or checkbox input before you attempt checked="checked".

 

-----

 

As for how long it takes to get a paid proper job, well, that depends on a lot.

In my case, I started PHP about 5 years ago and JS seriously about 3 years ago, and I'm just now reaching the point where I feel confident and competent enough to get paid to make sites for clients, etc.

And even with that said, I'm still constantly learning new things all the time.

 

I guess if I had to give one piece of advice (although, perhaps I shouldn't give any advice at all), you should ask yourself if you really, really enjoy web development and want to do it for a living (and not just for the money). In other words, do you enjoy sitting home alone on Saturday nights, coding HTML sticky forms and JS animations?

If the answer is yes, then start devouring any and all information you can get your hands on ASAP.

 

And more importantly, start making sites like a mad dog. I can't count how many mock sites and pages I've made in the past five years in an effort to learn anything and everything I could.

The more you read about the web industry and the technologies at play, and more importantly, the more you actually sit down and code, the faster you will learn.

 

I like your motivation and moxie though, and I think that if you have a genuine interest and motivation, you can do it. Get your skills up to where you feel more or less comfortable making standard run-of-the-mill sites, and then try to drum up some business from friends and business associates. Often, if you offer to do some work for free (for the sake of building a portfolio, etc.), you'll get more business than you can handle (or, at least, that's my experience).

 

Feel free to ask any other questions you might have (although it might be best to move them over to another topic if they're unrelated to this one).

Link to comment
Share on other sites

Edit: I probably should have read your previous post more closely. Likely, my proposed solution will not help you at all.

If you are using a radio input and typing in checked="checked" in the HTML, then the radio button should be selected by default.

 

The only reason I can imagine it not being selected is because of a possible syntax error in the HTML. After generating the page with your PHP, view the HTML source code for the page, and see if there are any syntax errors in your HTML radio input tag, and then please let us know what you find.

 

Thank you.

Link to comment
Share on other sites

Hi Stanley,

 

Thanks for your answers. Actually, it is a radio and selected input but the checked button doesn't change dynamically when view on the screen but the HTML itself does and the HTML looks right.

 

When I enter 4, I expect the first radio to be checked. When I enter 6, I expect the second radio to be checked but this does not happen.

 

About learning:

Do I enjoy it; YES. [but there are moments after a few hours looking at some silly error, I question that!]

I really appreciate your insights because that kind of information is tough to come across.

I started learning HTML & CSS @ end of November. Then I moved to Javascript in January. Now that's when I realized that this stuff is going to take a long time to get right. I'm hoping that in 4 or 5 months I will have covered "PHP and MySQL for Dynamic Web Sites:" and then maybe "Modern Javascript" in a further 4 months.

Then I suppose I could practice making some fully fledged sites. I have an idea to make my own and one for a family member.

If you don't mind my asking, were you working full-time on learning web development during those 5 years?

 

Thanks a lot!,

Stephen

Link to comment
Share on other sites

Just for the record, my name is not Stanley.

 

Anyway, thanks for the link to your site. I figured out what the problem is, and I guess I should have figured it out sooner.

Because of the way browsers parse (invalid) HTML (affectionately known as tag soup (wiki: http://en.wikipedia.org/wiki/Tag_soup)), checked="" is being interpreted as the same thing as checked="checked". And if you have more than one "checked" radio button for a given group, only the last of those checked radio buttons is actually checked.

 

To better illustrate what I mean, let's take a look at the following snippet from your code:

 

<p><input type="radio" id="1" name="d_status" value="4" checked="checked" > Normal (less than 5.7%)</p>
<p><input type="radio" id="2" name="d_status" value="4" checked="" > Pre-d (between 5.7% and 6.4%)</p>
<p><input type="radio" id="3" name="d_status" value="4" checked="" > d ( greater than 6.4%)</p>

 

If you'll notice, the top option is correctly set to checked="checked", but the problem is that the next two are set as checked="", which is being interpreted by the browser as meaning checked="checked" for both.

It's also worth noting that while checked="checked" is the correct syntax, just checked by itself will achieve the same effect and is considered okay in HTML5.

 

The solution, of course, is to include the checked="" part as well in the echo statements within the if/if-else statements so that nothing is echoed unless the corresponding radio button is selected.

This should solve your problem.

 

As a random off-note, you may want to replace the p tags in the code above with label tags. They're more semantically sound and add a little extra functionality and ease-of-use to the form.

 

-----

 

I think you can probably get a lot of the basics of HTML, CSS, JS, PHP and MySQL down within a year by sticking with Larry's books and online resources; it's learning how to properly apply all that information where things get difficult and start to become time-consuming (not that I'm trying to scare you or anything). It's kinda like that old adage about the more you learn, the more you realize you don't know.

 

Just to give you a little background about myself, I've lived in Japan for the past 7+ years, and I learned everything I know about PHP, etc. while holding a full-time job, as well as being with my wife and two daughters. As such, things are hectic for me, and I can't always find the time I want to devote to learning, which is why you can likely learn faster than me, if you're dedicated.

 

Anyway, if you have any other questions, just ask.

I also participate a lot on the JS board, so any questions you have there, I will likely answer as well.

 

In the meantime, if you have an idea for a website, run with it! You'll learn a lot from the experience.

Link to comment
Share on other sites

 Share

×
×
  • Create New...