Search code examples
phpmysqlurlget

How could I load a page with parameter?


I'm working on a blog project(learning course) which shows the bills of trains and comments associated. It's working with GET parameters.

I made every improvments ask by the professor except the one where I have to load the blog with parameters on url. There below you have a screenshot it's show undefined index and it's fine but I don't know how to load it with parameters :

Cpt1: Cpt1

I try with include but I don't have others file to load with I made all improvments in same page and with empty fonction() too but after I don't know how to make it ( I'm beginner).

I'm not asking the answer but If you have any hint to help to deal with or If it's too complex give me an explanation please ! ( Sorry for If my english is bad I'm not native one)

The code :

<?php 
include('connexion.php');
?>
<!DOCTYPE html>
<html lang="en">
<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">
    <link rel="stylesheet" href="css/style.css">
    <title>Blog</title>
</head>
<body>
    <h1>Mon super Blog !</h1>
    <p>Derniers billets du blog : </p>



    <!-- NEWS -->
    <div class="news">
        
        
        <?php
        
        $recuperation_titre_du_billets_et_contenu = $bdd->query("SELECT Id,Titre,Contenu,DATE_FORMAT(Date_creation,'%d/%m/%Y à %H:%i') AS date_affiché FROM billets LIMIT 0,4");
        while($données = $recuperation_titre_du_billets_et_contenu->fetch())
        {
            echo "<h3>".htmlspecialchars($données['Titre'])." le ".htmlspecialchars($données['date_affiché'])."</h3>";
            
            ?>
            <p>
                <?php echo htmlspecialchars($données['Contenu']);
            ?>
            
            <a href="commentaires.php?Id_billet=<? echo $données['Id']?>&amp;page_commentaire=1">Commentaires</a>
            
        </p>
        
        <?php 
        }
        $recuperation_titre_du_billets_et_contenu->closeCursor();
        ?>

</div>
<!-- FIN NEWS -->

<!-- NEWS2 -->

<div class="news2">
    
    <?php
                    
                    $billetsdelapage2 = $bdd->prepare("SELECT Id,Titre,Contenu,DATE_FORMAT(Date_creation,'%d/%m/%Y à %H:%i') AS date_affiché FROM billets LIMIT 4,4");
                    $billetsdelapage2->execute(array($_GET['page']));
                    
                    if(isset($_GET['page']) == 2)
                    {
                        while($data = $billetsdelapage2->fetch())
                        {
                            echo "<h3>".htmlspecialchars($data['Titre'])." le ".htmlspecialchars($data['date_affiché'])."</h3>";
                            ?>
                                      <p>
                                          <?php echo htmlspecialchars($data['Contenu']);
                                                ?>
                                                <a href="commentaires.php?Id_billet=<? echo $data['Id'];?>">Commentaires</a>
                                                <?php
                        }
                        ?>

<style>    
                                                    .news
                                                    {
                                                        display:none;
                                                    }
                                                    </style>


</p>
<?
                                    }
                                    $billetsdelapage2->closeCursor();       
                                    ?>
                                    
                                </div>
                                <!-- FIN NEWS2 -->  

                                <!-- NEWS3 -->
                                <div class="news3">
                                    
                                    <?php
                    
                    $billetsdelapage3 = $bdd->prepare("SELECT Id,Titre,Contenu,DATE_FORMAT(Date_creation,'%d/%m/%Y à %H:%i') AS date_affiché FROM billets LIMIT 8,12");
                    $billetsdelapage3->execute(array($_GET['page']));
                    
                    if(isset($_GET['page']) AND ($_GET['page'] != 2 ))
                    {
                        while($data = $billetsdelapage3->fetch())
                        {
                            echo "<h3>".htmlspecialchars($data['Titre'])." le ".htmlspecialchars($data['date_affiché'])."</h3>";
                            ?>
                                      <p>
                                          <?php echo htmlspecialchars($data['Contenu']);
                        ?>
                        <a href="commentaires.php?Id_billet=<? echo $data['Id']?>">Commentaires</a>
                        <?
                    }
                    ?>
                                                    <style>    
                                                    .news
                                                    {
                                                        display:none;
                                                    }
                                                    
                                                    .news2{
                                                        display:none;
                                                    }
                                                    </style>

</p>
<?
                                    }
                                    $billetsdelapage3->closeCursor();       
                                    ?>
                                    
                                </div>
                                
                                
                                <!-- FIN NEWS3 -->
                                
                                
                                
                                
                            </div>
                            
                            <div class="pagination">
                                <!-- Petite erreur qui sera rectifié plus tard vis à vis des numéros de page  ! -->
                                <a href="index.php">1</a>
                                <a href="index.php?page=2">2</a>
                                <a href="index.php?page=3">3</a>
    
</div>

</body>
</html>


Solution

  • If what you want is to get a parameter from the URL like:

    HTTP://myurl.com/index.php?myParameter=10

    Then using something like this should work.

    if (isset($_GET['page'])){ //check to see if 'page' is set.
    $pageNo= $_GET['page']; //then set a variable equal to the parameter.
    }