Jump to content
Larry Ullman's Book Forums

Recommended Posts

This is just a note to say that I haven't officially created any "Review and Pursue" threads in this forum yet as I didn't know what, exactly, readers would want. So feel free to post your questions as you have them and I'll answer them as they come. In time I may then shuffle things around to create an organized "Review and Pursue" system. Thanks for your interest in the book!

Link to comment
Share on other sites

  • 4 weeks later...

Hi Larry. I was wondering if you could help me with a persue point on the first chapter. I am having a bit of trouble on the 7th persue point from going to showing all the tasks by removing the WHERE clause to trying to indicate and specifically format uncompleted and completed tasks differently in the same function.

 

Any help on this would be greatly appreciated, though I'm finding the new review and persue section very useful for reinforcing the chapter.

Link to comment
Share on other sites

Sorry for the delay, Luke. As for your question, you've already removed the WHERE clause, which is good. Then you need to also select a value so you know whether or not the task has been completed. You could select the date_completed value, or select true/false based upon whether date_completed is null (that's a bit more complex to do). Then, in the PHP code, change the formatting based upon this value. You would not show the checkbox for completed items, of course. You could also use CSS classes here.

 

Hope that helps!

  • Upvote 1
Link to comment
Share on other sites

  • 2 weeks later...

Yeah thanks. Don't know if I did it the most efficient way but I used an array to indicate completion with the task ID and checked for it using in_array(). Hopefully I won't run into too many more problems, but its good to know this resource is available if needs be.

Link to comment
Share on other sites

  • 1 month later...

Hi again. I just have a question on the second last pursue exercises for chapter 3 (page 118) about the maximum range for distance. I've created the form to optionally search by range with no problem but my SQL is coming up with errors.

 

I added the WHERE distance<=X to the query as noted in page 107, however adding this comes up with SQL syntax errors. I think this SQL could be causing errors due to it using a WHERE on an alias, but I could be wrong. Any help on this would, as always, be greatly appreciated.

 

Example SQL:

SELECT name, CONCAT_WS('', address1, address2), city, state, stores.zip_code, phone, ROUND(return_distance(39.09, -94.58, latitude, longitude)) AS distance FROM stores WHERE distance<=1000 LEFT JOIN zips USING (zip_code) ORDER BY distance ASC LIMIT 3

Link to comment
Share on other sites

  • 1 year later...

Hi,

 

I am tempted to try the 'pursue' suggestion on page 282, specifically setting up a 'task' class.  My background experience is almost all procedural but I'm trying to expand my knowledge of OO.

 

My specific question is: in setting up a 'task' class, would, say, the 'add' method include the MySQL required to add a task to the database?

 

As an aside, I have found the tasks application a terrific one to 'pursue' with, and have just finished converting my (mainly procedural) already-pursued version to incorporate PDO.

 

Cheers from Oz, Necuima.

Link to comment
Share on other sites

From Stack Overflow I see that one can indeed include the MYSQL statements required to interact with the database as functions within the class.

 

I will give it a go and if anyone is interested, will post the class definition if/when I get it to work!

 

Cheers.

Link to comment
Share on other sites

OK, am making good progress, but I have a question.

 

In the 'mark as completed' processing, my database functionality does not act on one specific task object, as more than one task can be marked for completion at a time. I am using the "where task_id IN" approach as per Larry's example.

 

So I create a 'dummy' task object and then use that to invoke the 'mark as completed' database functionality method and pass the other needed data to the method by reference.

 

An alternative is to mark the selected tasks as completed one by one but that would require a database activity for each task - would that be 'better' though?

 

Any thoughts and advice will be most appreciated.

 

Cheers from Oz.

Link to comment
Share on other sites

For anyone tempted to fetch the existing tasks from the database into Task objects, after a lot of trial and error the following works for me (from 'pursue', suggestion 6, page 282):

 

 

function list_live_tasks(&$pdo)
 {
  // Run the query to select all non-completed tasks
     $q = 'SELECT task_id, parent_id, task AS task_name FROM tasks WHERE date_completed IS NULL ORDER BY date_added ASC';
  $r = $pdo->query($q);
     if ($r->rowCount() <= 0)
   {
    echo "<p><font color='red'>There are no 'live' tasks in the database.</font></p>";
   }
  else
   { 
       $tasks = array();  // Initialize the storage array
    $obj = new Task(0, 0, NULL); // required by "FETCH_INTO"
    $r->setFetchMode(PDO::FETCH_INTO, $obj);
    // Add the results to the array
    while ($obj = $r->fetch())
     { // the fetch populates the $obj instance of a Task object with attribute values from the database query 
      $tasks[$obj->parent_id][$obj->task_id] = $obj->task_name;
     }
       unset($obj);
    echo "<h4>    Live Tasks</h4>";
    list_tasks($tasks[0], $tasks); // recursive function to list out the tasks
   }
 }

 

If there is a better way, please share your thoughts.

 

Thanks, Necuima.

  • Upvote 1
Link to comment
Share on other sites

  • 5 weeks later...
  • 5 weeks later...

OK, am making good progress, but I have a question.

 

In the 'mark as completed' processing, my database functionality does not act on one specific task object, as more than one task can be marked for completion at a time. I am using the "where task_id IN" approach as per Larry's example.

 

So I create a 'dummy' task object and then use that to invoke the 'mark as completed' database functionality method and pass the other needed data to the method by reference.

 

An alternative is to mark the selected tasks as completed one by one but that would require a database activity for each task - would that be 'better' though?

 

Any thoughts and advice will be most appreciated.

 

Cheers from Oz.

 

Okay, so...this is the catch when mapping objects to database records. If you stick with the object, it's inefficient to make changes on multiple rows at once (because it would require multiple UPDATE calls, one for each object). My inclination would be to use an IN and one UPDATE call, even if that means you're not using the object right there. 

Link to comment
Share on other sites

Hi Larry,

 

Thanks for the feedback. I took the "IN" approach but the code was an object method which required me to create a 'dummy' instance, but it works just fine. As mentioned, I provided the list of database records that needed to be affected via a reference to the array which contained the database record keys.

 

Again, I found the 'tasks' application a terrific one for learning and extension, first via procedural code and subsequently creating an OO version.

 

Cheers from Oz.

Link to comment
Share on other sites

  • 7 months later...

Hi again. I just have a question on the second last pursue exercises for chapter 3 (page 118) about the maximum range for distance. I've created the form to optionally search by range with no problem but my SQL is coming up with errors.

 

I added the WHERE distance<=X to the query as noted in page 107, however adding this comes up with SQL syntax errors. I think this SQL could be causing errors due to it using a WHERE on an alias, but I could be wrong. Any help on this would, as always, be greatly appreciated.

 

Example SQL:

SELECT name, CONCAT_WS('', address1, address2), city, state, stores.zip_code, phone, ROUND(return_distance(39.09, -94.58, latitude, longitude)) AS distance FROM stores WHERE distance<=1000 LEFT JOIN zips USING (zip_code) ORDER BY distance ASC LIMIT 3

Did you ever get this figured out?? i am pulling my hair out trying to figure out where to put this WHERE distance<=x in the sql............I keep getting syntax errors.

Link to comment
Share on other sites

You can only use aliases in GROUP BY, ORDER BY and HAVING clauses. One workaround would be to create a view and then select from that view using the WHERE distance <= 1000 clause.

do you have an example of how i handle this?

Link to comment
Share on other sites

  • 2 years later...

My problem was in chapter 2 project which is:  in main.in.php 
 

//Redirect if this page was successed directly:
// if (!defined("BASE_URL")) {
 
// // Need the BASE_URL, defined in the config file:
// require('./includes/config.inc.php');
 
// // Redirect to the index page:
// $url = BASE_URL . 'index.php';
// header ("Location: $url");
// exit;
 
// } // End of defined() IF

I had to comment these lines off cause the error was this: Fatal error: Cannot redeclare my_error_handler() (previously declared 
So i fixed it thanks a lot its amazing book im learning. :)
 
Link to comment
Share on other sites

Thanks for the nice words! I really appreciate it. I think the reason you had that problem was because you weren't defining BASE_URL in your config file (probably due to a typo). So, because that constant isn't defined, it re-includes the config file, which creates that error. 

Link to comment
Share on other sites

 Share

×
×
  • Create New...