Вопрос

I'm trying to check first if the project exists, then if it does - I use a while loop to get the project information and output it on a smarty template.

This code redirects me to "projects.php" if the ID doesn't exists, but if it does exists then it just simply does nothing. What am I doing wrong? I would appreciate if you can guide me in the correct path.

Thanks!

if(isset($_GET['id'])) {

try {

    $stmt = $db->prepare('SELECT * FROM projects WHERE slug = :slug');
    $stmt->execute(array(':slug' => $_GET['id']));
    $id = $stmt->fetchColumn();

    if ($id) {
        while($row = $stmt->fetch()) {
            $projects[] = $row; 
            $smarty->assign('projects',$projects); 
        }
    } else {
        header("Location: /projects.php");
        exit();
    }

} catch(PDOException $e) {
    die ('ERROR: ' . $e->getMessage());
}

}
Это было полезно?

Решение 2

You are writing too much code. PHP just chokes with amount and can't process further

if(isset($_GET['id'])) {

    $stmt = $db->prepare('SELECT * FROM projects WHERE slug = ?');
    $stmt->execute(array($_GET['id']));
    $projects = $stmt->fetchAll();

    if ($projects) {
        $smarty->assign('projects',$projects); 
    } else {
        header("Location: /projects.php");
        exit();
    }
}

is all the code you need

Другие советы

You are passing variable to Smarty inside your while loop, it should be after while loop like:

$projects = array();
while($row = $stmt->fetch()) {
    $projects[] = $row;
}
$smarty->assign('projects',$projects); 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top