Jump to content
Larry Ullman's Book Forums

johnyjharris

Members
  • Posts

    3
  • Joined

  • Last visited

Posts posted by johnyjharris

  1. I think I had moment of clarity this morning! After re-reading the paragraph about elseif I presume elseif isn't classed as a nested conditional - rather adding a conditional to an existing one.

     

    Anyway I managed it with the following code.

    if (isset($_POST['gender']) && ($_POST['gender'] == 'M')) {
    		$greeting = '<p><b>Good day, Sir!<b></p>';
    	} elseif ((isset($_POST['gender']) && ($_POST['gender'] == 'F')) {
    		$greeting = '<p><b>Good day, Madam!<b></p>';
    	} else {
    		$gender = NULL;
    		echo '<p class="error">Gender should be either "M" or "F"!</p>';
    }	
    
  2. Many thanks Larry,

     

    I'm afraid to say, I'm still stumped with this, at least that is if i've understood it correctly- I'm reading it as using only one conditional and no nested conditionals. 

     

    With one conditional I can only work out how to account for 2 eventualities - either gender being M or F.

    if (isset($_REQUEST['gender']) && ($_REQUEST['gender'] == 'M')) {
    	$greeting = '<p><b>Good day, Sir!<b></p>';
    } else {
    	$greeting = '<p><b>Good day, Madam!<b></p>';
    	}
    

    I understand this no good as it does not account for checking isset and setting gender = NULL if the user hasn't selected M or F

  3. Hi folks

     

    And thanks Larry for a great book, its quite inspiring. I'm new to programming, I know my HTML and CSS but that's it. I've been following your book and I'm up to Chapter 2 -  Review and Pursue and stuck on the third question, sorry if this has been covered already.

     

    Rewrite the gender conditional in handle_form.php (Script2.4) as one conditional instad of two nested ones, Hint: you'll need to use the AND operator.

     

     

     

    The code in question is

    if (isset($_REQUEST['gender'])) {
     $gender = $_REQUEST['gender'];
     if ($gender == 'M') {
      $greeting = '<p><b>Good day, Sir!<b></p>';
     } elseif ($gender == 'F') {
      $greeting = '<p><b>Good day, Madam!<b></p>';
     } else {
      $gender = NULL;
      echo '<p class="error">Gender should be either "M" or "F"!</p>';
    } 

    I've been stuck all day, I have gone away and come back again and I'm still drawing a blank.

     

    Any help would be really appreciated, thanks.

×
×
  • Create New...