Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

php - Undefined argument and undefined variable in foreach loop

I am doing a database project for recipes and have encountered this error:

Notice: Undefined variable: recipes in C:xampphtdocsCOMP1321 ecipes ecipes.html.php on line 16
Warning: Invalid argument supplied for foreach() in C:xampphtdocsCOMP1321 ecipes ecipes.html.php on line 16

Otherwise, it displays the results of the array just fine, it just shows this error.

The code looks like this in line 16 recipes.html.php

<?php foreach ($recipes as $recipe): ?>

and the $recipes array itself is in index.php

foreach ($result as $row) {
    $recipes[] = array(
        'id' => $row['id'],
        'recipename' => $row['recipename'],
        'time' => $row['time'],
        'ingredients' => $row['ingredients'],
        'recipetext' => $row['recipetext'],
        'servings' => $row['servings'],
        'nutritionfacts' => $row['nutritionfacts'],
        'recipedate' => $row['recipedate'],
        'image' => $row['image'],
    );
}

Full code of recipes.html.php

<?php include_once 'admin/includes/helpers.inc.php';?>
<?php include_once 'index.php';?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>List of Recipes</title>
    </head>
    <body>
        <p><a href="?addrecipe">Add your own recipe</a></p>
        <p>Here are all the recipes in the database</p>
        <!--into a table-->
        <table border="1">
            
            <?php foreach ($recipes as $recipe): ?>
                <form action="?deleterecipe" method="post">
                <tr>
                    <td><?php echo htmlspecialchars($recipe['recipename'], ENT_QUOTES, 'UTF-8');?></td>
                    <td><?php echo htmlspecialchars($recipe['time'], ENT_QUOTES, 'UTF-8');?></td>
                    <td><?php echo htmlspecialchars($recipe['ingredients'], ENT_QUOTES, 'UTF-8');?></td>
                    <td><?php echo htmlspecialchars($recipe['recipetext'], ENT_QUOTES, 'UTF-8');?></td>
                    <td><?php echo htmlspecialchars($recipe['servings'], ENT_QUOTES, 'UTF-8');?></td>
                    <td><?php echo htmlspecialchars($recipe['nutritionfacts'], ENT_QUOTES, 'UTF-8');?></td>
                    <?php $display_date = date("D d M Y", strtotime($recipe['recipedate']));?>
                    <td><?php echo htmlspecialchars($display_date, ENT_QUOTES, 'UTF-8'); ?></td>
                    <td><img height="100px" src="images/<?php echo htmlspecialchars($recipe['image'], ENT_QUOTES, 'UTF-8'); ?>" /></td>
                
                    <td><input type="hidden" name="id" value="<?php echo $recipe['id']; ?>">
                    <input type="submit" value="Delete"></td>    
                </tr>
                </form>
            <?php endforeach; ?>
        </table>
            <?php include 'admin/includes/footer.inc.html.php';?>
    </body>
</html>

Full code of index.php

<?php
include 'recipes.html.php';
include 'admin/includes/db.inc.php';
if(isset($_GET['addrecipe'])) {
    include 'form.html.php';
        
    exit();
}
//insert block
if (isset($_POST['recipename'])) {
    include 'admin/includes/db.inc.php';
    try
    {
        //prepared statement
        $sql = 'INSERT INTO recipe SET
            recipename = :recipename,
            recipedate = CURDATE()';
        $s = $pdo->prepare($sql);
        $s->bindValue(':recipename', $_POST['recipename']);
        $s->execute();
    } catch (PDOException $e) {
        $error = 'Error adding submitted recipe' . $e->getMessage();
        include 'error.html.php';
        exit();
    }
    header('Location: .');
    exit();
}
    
//delete block
if(isset($_GET['deleterecipe']))
{
    include 'admin/includes/db.inc.php';
    try
    {
        $sql = 'DELETE FROM recipe WHERE id = :id';
        $s = $pdo->prepare($sql);
        $s->bindValue(':id', $_POST['id']);
        $s->execute();
    }
    catch (PDOException $e)
    {
        $error = 'Error deleting recipe' . $e->getMessage();
        include 'error.html.php';
        exit();
    }
    header('Location: .');
    exit();
}
    
try // selection block
{
    $sql = 'SELECT * FROM recipe';
    $result = $pdo->query($sql);
} 
catch (PDOException $e) {
    $error = 'Error fetching recipes' . $e->getMessage();
    include 'error.html.php';
    exit();
}
    
foreach ($result as $row) {
    $recipes[] = array(
        'id' => $row['id'],
        'recipename' => $row['recipename'],
        'time' => $row['time'],
        'ingredients' => $row['ingredients'],
        'recipetext' => $row['recipetext'],
        'servings' => $row['servings'],
        'nutritionfacts' => $row['nutritionfacts'],
        'recipedate' => $row['recipedate'],
        'image' => $row['image'],
        );
}
include 'recipes.html.php'; 

Any help with this would be appreciated! Thanks in advance!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...