Jump to content
Larry Ullman's Book Forums

alex_r

Members
  • Content Count

    12
  • Joined

  • Last visited

Community Reputation

0 Neutral

About alex_r

  • Rank
    Newbie
  1. I've got it working now . (After a few changes) However I've some other question now: 1. Is doing something like this OK/good practice? Specially the elseif block (for SessionID) which does nothing if validated? if (isset($_GET['id'])) { if (filter_var($_GET['id'], FILTER_VALIDATE_INT, array('min_range' => 1))) { $_SESSION['id'] = $_GET['id']; } } elseif ( isset($_SESSION['id']) || filter_var($_SESSION['id'], FILTER_VALIDATE_INT, array('min_range' => 1)) ) { ; } else { throw new Exception("An Invalid Page Id was provided"); } 2. Will it be Ok/good practice to store $page object is session (through page.php) so that the script can call the User::canEditPage() method which requires an argument of type Page (and since the method needs to call the creatorId [$page->getCreatorId()] to do something equivalent to the scrippet below from add_page.php? // Redirect if the user doesn't have permission: if (!$user->canCreatePage()) { header ("Location:index.php"); exit; }
  2. I get this error message: "e1 An invalid page Id was provided to this page." which is in this code (above) throw new Exception('e1 An invalid page Id was provided to this page.'); under Validate page Id block.
  3. Here is my code which I've written to make the 'edit_page', but am unable to update the database. Please, anyone, take a look and help me out by pointing out my mistakes and how to rectify them. Also some pointers to improve code/coding skills would be really appreciated. Thanks in advance. <?php # Script 9.17 - edit_page.php // This page both displays and handles the "Edit" page. require ('includes/utilities.inc.php'); try { // Validate page Id if ( !isset($_GET['id']) || !filter_var($_GET['id'], FILTER_VALIDATE_INT, array('min_range' => 1)) ) { throw new Exception('e1 An invalid page Id was provided to this page.'); } $_SESSION['id'] = $_GET['id']; // Fetch content from the database: $q = 'SELECT id, title, content, creatorId FROM pages WHERE id=:pageId LIMIT 1'; $stmt = $pdo->prepare($q); $r = $stmt->execute(array(':pageId' => $_GET['id'])); if ($r) { $stmt->setFetchMode(PDO::FETCH_CLASS, 'Page'); $page = $stmt->fetch(); } if (!$page) { throw new Exception ('Failed to retrieve data.'); } } // Catch generic Exceptions: catch (Exception $e) { $pageTitle = 'Error'; include ('includes/header.inc.php'); include ('views/error.html'); include ('includes/footer.inc.php'); } // Create the form: require ('HTML/QuickForm2.php'); $form = new HTML_QuickForm2('editPageForm'); // Add the title field: $title = $form->addElement('text', 'title'); $title->setLabel('Page Title'); $title->addFilter('strip_tags'); $title->addRule('required', 'Please enter page title'); $title->setValue($page->getTitle()); $title->getValue(); // Add the content field: $content = $form->addElement('textarea', 'content'); $content->setLabel('Page Content'); $content->addFilter('trim'); $content->addRule('required', 'Please enter the page cotent'); $content->setValue($page->getContent()); $content->getValue(); // Add the submit button: $submit = $form->addElement('submit', 'submit', array('value' => 'Update This Page')); // Check for a form submission: // Handle the form submission: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Validate the form data: if ($form->validate()) { // Update the database: $q = 'UPDATE pages SET title=:title, content=:content dateUpdated=:date WHERE id=:pageId AND creatorId=:creatorId LIMIT 1'; $stmt = $pdo->prepare($q); $r = $stmt->execute ( array ( ':title' => $title->getValue(), ':content' => $content->getValue(), ':date' => NOW(), ':pageId' => $_GET['id'], ':creatorId' => $user->getId() ) ); // Freeze the form upon success: if ($r) { $form->toggleFrozen(true); $form->removeChild($submit); } } } $pageTitle = 'Edit Page'; include ('includes/header.inc.php'); include ('views/edit_page.html'); include ('includes/footer.inc.php'); ?> This is from post http://larryullman.com/forums/index.php?/topic/1758-chapter-9-exercises/?hl=%2Bedit+%2Bpage. Is this how I should do it? $_SESSION['id'] = $_GET['id']; I get only error when I click the submit button.
  4. Am not sure if my understanding of the concept is correct, so I'd appreciate any input to help me with it. set_include_path(get_include_path() . PATH_SEPARATOR . $path); This line tells PHP to first search for the included file (in this Script "require('HTML/QuickForm2.php');") in the path returned by "get_included_path()". If the file is not found there, then PHP will look for the file in "$path". Have I got it right?
  5. $path = '/usr/local/pear/share/pear/'; set_include_path(get_include_path() . PATH_SEPARATOR . $path); What does this line do? PHP documentation explains it as extending the include path. echo outputs : .:/opt/lampp/bin/php:/usr/local/pear/share/pear/ Can someone please break down the concept for me? Also login seems to work when even if I do this: (path where QuickForm2 is installed for my setup: Ubuntu 16.10 xampp-5.6.30-0) $path = '/opt/lampp/lib/php/HTML/';
  6. Am not sure what happened too. I tested using "include", which worked and later i used "require" and now seems to work fine no problem. I don't know what the problem is. // Load the class definitions /* include('Shape.php'); include('Triangle.php'); */ require('Shape.php'); require('Triangle.php');
  7. http://stackoverflow.com/questions/9882145/the-best-way-to-include-a-file-across-multiple-files-in-php-for-inheritance -- The selected answer to this question (answered by deceze) answers this, I think?
  8. abstract class Shape { // No attributes to declare // No constructor or destructor defined here // Method to calculate and return the area. abstract protected function getArea(); // Method to calculate and return the perimeter. abstract protected function getPerimeter(); } // End of Shape class. Next I declared "Triangle" class <?php class Triangle extends Shape { // Declare the attributes: private $_sides = array(); ... Then created the "abstract.php" <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Triangle</title> <link href="css/style.css" rel="stylesheet"> </head> <body> <?php # Script 6.3 - abstract.php // This page uses the Triangle class (Script 6.2), which is derived // from Shape (Script 6.1). // Load the class definitions require('Shape.php'); require('Triangle.php'); // Set the triangle's sides: $side1 = 5; ... When I run abstract.php, it generates the following error Fatal error: Class 'Shape' not found in C:~\Triangle.php on line 15 I don't understand why it's showing this error. Please help! My PHP version is 5.6.12 using XAMPP Version 5.6.12
  9. Thanks Larry. That point is clear. However this is not clear: Using the same array, but passing comp2() function as the second argument in uasort the result is inverted. The difference between comp1() and comp2() function is the difference in the position of arguments. The two function does the same comparison operation i.e $x < $y Maybe am missing something about using comparison operaiton inside 'return' or the way variables behave inside a function with respect to the arguments listed while defining function?
  10. I've just started this book and am really confused with an expression. Can somebody please explain the mechanism behind the expression, given below, used in Script 1.1 - sort.php return ($x < $y) // Creating an array $a = [1, 5]; // creating comparison function using the above expression function comp1($x, $y) { return ($x < $y); } // using uasort() uasort($a, 'comp1'); // output the result echo '<pre>' . print_r($a, 1) . '</pre>'; // creating the same function as above but interchanging the arguments position function comp2($y, $x) { return ($x < $y); } // using uasort() uasort($a, 'comp2'); // output the result echo '<pre>' . print_r($a, 1) . '</pre>'; This outputs Array ( [1] => 5 [0] => 1 ) Array ( [0] => 1 [1] => 5 ) Observation: reversing the position of arguments in function definition reverses the sort output. Can somebody please explain the 'mechanism' about using the expression return ($x < $y) - How and which values are returned.
  11. Hi, phpadawan Just checking in to see if you've solved the issue. The above post from admin (#4) is actually from me. (Sorry I had to do that, had issues with replying). Anyway I solved after about 3-4 weeks when I revisited my code. I had this habit of starting the php opening tag after a tab (4 spaces on my system) in the text editor. But now I've learned (the hard way, despite the many warnings in the book) that, that was the cause of the underlying issue. Removed the indentation, and problem solved. You might not have made the same error as mine but it's worth a shot. Thanks alot Larry Ullman for your great book.
×
×
  • Create New...