Home > Software design >  How to save a value ($title) from Foreach loop (SQL,PHP) to another page?
How to save a value ($title) from Foreach loop (SQL,PHP) to another page?

Time:01-02

Page1: View all movies as linked items.

<?php

    session_start();
    require_once 'db_connectie.php';

    function alleFilms() {
        $db = maakVerbinding();
        $data = $db->query('select movie_id, title from Movie Order by title');
        $film= '<ol>';
        foreach ($data as $rij) {
            $movie_id           = $rij['movie_id'];
            $title              = $rij['title'];
            $film               .= '<li>';
            $film               .= "<a href=\"moviePagina.php?movieId=". $rij['movie_id'] ."\">";
            $film               .= $title;
            $film               .= '</a>';
            $film               .= '</li>';
            $film               .= '<br>';   
        }   
        $film   .= '</ol>';
        return $film;
    }
     
?>

<!DOCTYPE html>
<html lang="nl">
    <head>     
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Movies</title>
    </head>
    <body>
        <?php require_once 'header.php'; ?>
        <main>
            <?= alleFilms(); ?>
        </main>
        <?php require_once 'footer.php'; ?>
    </body>
</html>

Page2: View the details from the selected item.

<?php

    session_start();
    
    if(empty($_SESSION['customer_mail_address'])){
        echo "eerst inloggen";
        //naarInloggenPagina
        exit();
    }

    function movie(){
        $movie_id = $_GET['movie_id']; 
        $db     = maakVerbinding();
        $sql    = "select movie_id, title, duration from Movie where movie_id = '";
        $sql    .= $movie_id;
        $sql    .= "' Order by title";
        $data   = $db->query($sql);        
        $movie  = "<table>";
        foreach ($data as $rij) {
        $movie_id           = $rij['movie_id'];
        $title              = $rij['title'];
        $duration           = $rij['duration'];
        $movie .= "<tr>
                        <th>movie_id </th>
                        <th>title </th>
                        <th>duration </th>
                    </tr>";
        $movie .= "<tr>
                        <td>$movie_id </td>
                        <td>$title </td>
                        <td>$duration </td>
                    </tr>";
        }
        $movie .= "</table>";
        return $movie;
    }

?>

<!DOCTYPE html>
<html lang="nl">
    <head>     
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title><?= $_SESSION['title']; ?> movie</title>
    </head>
    <body>
        <?php require_once 'header.php'; ?>
        
        <main>
        <?= movie(); ?>
        </main>
        <?php require_once 'footer.php'; ?>
    </body>
</html>

I tried to send the $movie_id from the first page to another, but it did not work. I get this error "Undefined variable $movie_id". I want to use the $movie_id in the SQL query in the second page to view the selected movie.

CodePudding user response:

As ADyson mentioned, you need to pass the title to the url in the form of a url parameter as shown in the code.

            $film               .= '<li>';
            $film               .= "<a href=\"moviePagina.php?title=$rij['title']\">";
            $film               .= $rij['title'];
            $film               .= '</a>';

If you are going to do a lookup of the movie on the second page using the movie title, you should consider using a unique ID for each movie and passing that ID to the second page and using it to do the lookup.

  • Related