Jump to content
Larry Ullman's Book Forums


  • Posts

  • Joined

  • Last visited

  • Days Won


Everything posted by phpRob

  1. Hi, I'm working through Chapter 7 'Arrays'. Having read about multidimensional arays on w3schools they suggest writing them like so: $books = array( "PHP for the web" => array(1 => "Numbers", "Variables", "Arrays"), "PHP and MySQL" => array(1 => "Databases", "Intro to SQL"), "PHP Advanced" => array(1 => "Introduction to OOP", "Advanced OOP", "Frameworks") ); Larry, do you still object to writing multidimensional arrays this way (nested)? What do you mean when you say it's too easy to make syntactical errors as the statement becomes more and more nested? Thanks
  2. I tried giving the default options of the drop-downs a value of 'Day' and 'Month' and calling the following validation via php: if ($day == 'Day') { print "<p>Please enter a value for day</p>"; $flag = FALSE; } if ($day == 'Month') { print "<p>Please enter a value for day</p>"; $flag = FALSE; } This works, but are there any other validation on 'select HTML elements' I need to be aware of?
  3. Just tried amending the script to use the declared variables $day and $month: //Validate Day if (!isset($day)) { print "<p>Please enter a value for day</p>"; $flag = FALSE; } //Validate Month if (!isset($month)) { print "<p>Please enter a value for month</p>"; $flag = FALSE; } It now reads no input from the html form as TRUE?? My Form script looks like this: <form action="handle_post.php" method="post"> <p>Email address: <input type="text" name="email" size="30" /></p> <p>Passsword: <input type="password" name="password" /></p> <p>Confirm password: <input type="password" name="confirm" /></p> <p>D.O.B: <select name="day"> <option value="">Day</option> <?php for ($i = 1; $i <= 31; $i++) { print "<option value=\"$i\">$i</option>\n\t\t"; } ?> </select> <select name="month"> <option value="">Month</option> <option value="january">January</option> <option value="february">February</option> <option value="march">March</option> <option value="april">April</option> <option value="may">May</option> <option value="june">June</option> <option value="july">July</option> <option value="august">August</option> <option value="september">September</option> <option value="october">October</option> <option value="november">November</option> <option value="december">December</option> </select> <input type="text" name="year" value="YYYY" size="4" /> </p> <p>Favourite colour: <select name="colour"> <option value="">----</option> <option value="blue">Blue</option> <option value="yellow">Yellow</option> <option value="green">Green</option> <option value="red">Red</option> </select></p> <p><input type="checkbox" name="terms" value="Yes" /> I accept the terms</p> <p><input type="submit" name="submit" value="Submit" /></p> </form> Any suggestions on how I can get this to work, noticed the validation dosn't work? Is it bcause the variable has a value of "" to begin with?
  4. Thanks for the replies, wasn;t sure i'd get any for a minute Maybe someone else can share some light on how to tackle the third task better? Well spotted on the fourth task and th calling of values via £_POST, yes I should have used the variables I delared at start of script. I did think about inserting the '/' between $day $month $year though I didn't think it was too improtant so left it out. I concerned with functionability more than anything at this stage.
  5. Hey ho, Just want to dedicate this post to a few questions regarding chpter 6 and it's pursue section. sorry in advance if these questions are 'dumb' 'm still getting my head around the basics! The first task was to use a variable for the current year, I used the date('Y') function like this $current_year = date('Y'); Then used $current_year instead of the hard coded value. Would this be correct? Second task was to display all inputted variables from the Registration Form, this is what did; //Declare variables $email = $_POST['email']; $password = trim($_POST['password']); $confirm = trim($_POST['confirm']); $year = $_POST['year']; $colour = $_POST['colour']; $terms = $_POST['terms']; //Print variable values $results = $email.'<br /> '.$password.'<br /> '.$confirm.'<br /> '.$year.'<br /> '.$colour.'<br /> '.$terms; print $results; Is there a better simpler way of doing this? Third task, displaying the colour chosen in the chosen colour, did this: <style type="text/css"> .blue { color: blue; } .yellow { color: yellow; } .red { color: red; } .green { color: green; } </style> switch ($colour) { case 'blue': $colour_type = 'primary'; print "<p class=\"blue\">The colour you selected was $colour of the $colour_type type</p>"; break; case 'red': $colour_type = 'primary'; print "<p class=\"red\">The colour you selected was $colour of the $colour_type type</p>"; break; case 'yellow': $colour_type = 'secondary'; print "<p class=\"yellow\">The colour you selected was $colour of the $colour_type type</p>"; break; case 'green': $colour_type = 'primary'; print "<p class=\"green\">The colour you selected was $colour of the $colour_type type</p>"; break; default: print "<p>Please select a colour from the list</p>"; $flag = FALSE; break; } Again is there a better way bearing in mind 've not used concatination? Forth task of validating day and month as well as seting avariable to show day/month/year I did this: //Declare Variables $day = $_POST['day']; $month = ucfirst($_POST['month']); $year = $_POST['year']; //Validate Day if (!isset($_POST['day'])) { print "<p>Please enter a value for day</p>"; $flag = FALSE; } //Validate Month if (!isset($_POST['month'])) { print "<p>Please enter a value for month</p>"; $flag = FALSE; } //Validate year is numeric and calculate age if (is_numeric($year) && (strlen($year) == 4)) { if ($year < $current_year) { $age = $current_year - $year; } else { print "<p>Please enter a valid year</p>"; $flag = FALSE; } } else { print "<p>Please re-enter the year you was born</p>"; $flag = FALSE; } then at end of script i did this: //Print registration results if ($flag) { $dob = $day.' '.$month.' '.$year; print "<p>Thank you. You have been registered</p>"; print "<p>Your D.O.B is: $dob</p>"; } Again in the context of what I've learnt so far is there a better way of doing this? Jst to add all of these tasked worked. Sorry for the long post by the way!
  6. Internet Explorer 9 and Firefox 7.0.1 both return correct values, not tried it on older browsers.
  7. I get the message 'The name you selected was Paul 'John' Thomas' as expected even though it includes spaces and apostrophes. $name = $_GET['name']; print = "The name you selected was $name";
  8. Hi, Firstly I'd like to say 'm really enjoying the book, it expalins things a little slower than the php & mysql book which suits me better My question is regarding the chapter 3 pursue and the $_GET variable. "If you're the inquisiive type, and don't mind waiting for answers, try passing more complicated values to a page through the URL. Try using spaces and punctuation to see what happens." Earlier in the chapter you said "They won't be passed to the php script properly...", yet when I tried this it returned the value no problem? <div> <p>Click a name</p> <ul> <li><a href="hello.php?name=Paul 'John' Thomas">Paul 'John' Thomas</a></li> <li><a href="hello.php?name=Lee">Lee</a></li> <li><a href="hello.php?name=John">John</a></li> </ul> </div> Am I misunderstanding something here? I'm running PHP version 5.2.14. Thanks, Rob
  • Create New...