Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

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

<!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
Avatar of Gary
Gary
Flag of Ireland image

It's just looping through each row of the recordset while there are records
SOLUTION
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of rgb192

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

Ray's wording is best

This variable is either an array or a false value.

Thanks.