We help IT Professionals succeed at work.

I do not understand a while that came from database table rows

rgb192
rgb192 asked
on
322 Views
Last Modified: 2014-05-05
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Add a Task</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<?php # Script 8.7 - add_task.php #3 (in this chapter)
//  This page adds tasks to the tasks table using PDO.

// Try to connect to the database:
try { 
    $postTask=$_POST['task'];
    // Create the object:
    $pdo = new PDO('mysql:dbname=test;host=localhost', 'username', 'password');

    // Check for a form submission:
    if (($_SERVER['REQUEST_METHOD'] == 'POST') && !empty($_POST['task'])) {
    
        // Minimal validation:
        if (isset($_POST['parent_id']) && 
        filter_var($_POST['parent_id'], FILTER_VALIDATE_INT, array('min_range' => 1)) ) {
           $parent_id = $_POST['parent_id'];
        } else {
           $parent_id = 0;
        }

        // Add the task to the database:
        $q = 'INSERT INTO tasks (parent_id, task) VALUES (:parent_id, :task)';
        $stmt = $pdo->prepare($q);

        // Confirm the results:
        if ($stmt->execute(array(':parent_id' => $parent_id, ':task' => $_POST['task']))) {
            echo '<p>The task has been added!</p>';
        } else {
            echo '<p>The task could not be added!</p>';
        }

    } // End of submission IF.

    // Start the form:
    echo '<form action="add_task.php" method="post">
    <fieldset>
        <legend>Add a Task</legend>
        <p>Task: <input name="task" type="text" size="60" maxlength="100"></p>
        <p>Parent Task: <select name="parent_id"><option value="0">None</option>';
        
    // Run the query:
    $q = 'SELECT task_id, task FROM tasks WHERE date_completed="0000-00-00 00:00:00" ORDER BY date_added ASC'; 
    $r = $pdo->query($q);
    
    // Set the fetch mode:
    $r->setFetchMode(PDO::FETCH_NUM);
    
    // Show the results:
    while ($row = $r->fetch()) {
       echo "<option value=\"$row[0]\">$row[1]</option>\n";
    }
    
    // Complete the form:
    echo '</select></p>
    <input name="submit" type="submit" value="Add This Task">
    </fieldset>
    </form>';

    // Unset the object:
    unset($pdo);

} catch (PDOException $e) { // Report the error!
    echo '<p class="error">An error occurred: ' . $e->getMessage() . '</p>';
}

?>
</body>
</html>

Open in new window


I still do not understand THIS while statement

I have investigated while on php.net and w3schools
but I do not understand a while that came from database table rows
Comment
Watch Question

CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014

Commented:
It's just looping through each row of the recordset while there are records
Dan CraciunIT Consultant
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
I'm sure the confusing thing is that it's not real apparent HOW this is working.

The while() makes the loop, but the

$row = $r ... etc... is actually setting $row to the values from a retrieved row.  Each time through the loop, the next row is taken from the $r resultset and assigned to $row.

Note that the single equal sign, =, is an assignment, not a comparison operator, so an assignment takes place at the same time as the while() loops.

When the resultset is exhausted (you have processed each row), the while() will terminate.

Author

Commented:
The while() makes the loop, but the

$row = $r ... etc... is actually setting $row to the values from a retrieved row.  Each time through the loop, the next row is taken from the $r resultset and assigned to $row.

because more details

Each time through the loop??
how is the loop advanced?


The fetch method will return the next record and advance the pointer, or NULL if there is no next record.
I do not understand: advance the pointer


It's just looping through each row of the recordset
how?



I am failing to understand a simple concept

I can see a while loop works using a debugger on a php ide
but I do not know why it works
CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Ray's wording is best

This variable is either an array or a false value.

Thanks.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.