3

PHP Undefined offset: 0

 3 years ago
source link: https://www.codesd.com/item/php-undefined-offset-0.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

PHP Undefined offset: 0

advertisements

I am having troubles with this part

    ";

    $primary_image = dbSelect($db, $query);
    $project->primary_image = $primary_image[0];

    }

of this code

<?php

require_once('includes/config.php');

    enter code here

require_once('includes/lib.php');

$db = dbConnect();

$query = "SELECT projects.* FROM projects ORDER BY rank ASC";
$projects = dbSelect($db, $query);

foreach($projects as $project) {
    $query = "
        SELECT media.* FROM media
        WHERE media.project_id = '$project->id'
        AND media.primary = '1'
    ";

    $primary_image = dbSelect($db, $query);
    $project->primary_image = $primary_image[0];

    }

dbClose($db);

?>

<!DOCTYPE html>
<html>
<head>
<title>Missy Skae's Portfolio</title>
<link rel="stylesheet" href="style.css">
</head>
<body>

<?

foreach($projects as $project) {
    ?>
    <div class="project">
        <h2><?=$project->date;?></h2>
        <h1><a href="project.php?id=<?=$project->id;?>"><?=$project->title;?></a></h1>
        <div class="primary-image">
            <a href="project.php?id=<?=$project->id;?>"><img src="media/<?=$project->primary_image->filename;?>"></a>
        </div>
        <h2><?=$project->tag1;?></h2>
        <h1><?=$project->description;?></h1>
    </div>
    <?
}

?>

</body>
</html>

the error being Notice: Undefined offset: 0

Anyone have any ideas? Thanks!


The notice should give you a line number, Can you provide it.

In your query, You should update this line:

    WHERE media.project_id = '$project->id'

to read

    WHERE media.project_id = '{$project->id}'

I beleive it would cause an error.

It's difficult to pinpoint the error without knowing what line this is happening on or whats in the other included files. It's more than likely on this line..

$project->primary_image = $primary_image[0];

You could test it by using a var_dump on $primary_image, like so

$primary_image = dbSelect($db, $query);
var_dump($primary_image);
$project->primary_image = $primary_image[0];




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK