Avatar of rgb192
rgb192
Flag 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
PHP

Avatar of undefined
Last Comment
rgb192

8/22/2022 - Mon
Gary

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

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Cornelia Yoder

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.
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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
rgb192

ASKER
Ray's wording is best

This variable is either an array or a false value.

Thanks.