简体   繁体   中英

Wanting index.php to load when loaded

I have almost created a fully working dynamic page using PHP OOP. I have successfully created a working menu - that is when a menu item is clicked, relevant text is displayed on the page. However, what I want is to have the home page text displayed by default and not solely clicking the Home link.

Here is the code I am using:

    <?php

include_once('includes/connection.php');
include_once('includes/Article.php');

//instanstiating the article class and ssigning it to a variable

$article = new Article;
//assigning the contents of the method called fetch_all to  the variable $articles
//this fetch_all method is inside the Articles class which is assigned to the above variable $article
$articles = $article->fetch_all();

?>

<!DOCTYPE html>
<html>
<head>
    <title>Simple PDO CMS</title>
        <link rel="stylesheet" type="text/css" href="assets/styles.css">
</head>
<body>
       
    <div class="container">
        
        <table class="topMenu">
            
            <tr>
                <td>
                    <h1 class="siteName">site name</h1>
                </td>
                
                <!--Displaying the articles using a foreach loop-->
                <?php foreach ($articles as $article){ ?>
                
                <td class="navItem">
                    
                    <a href="index.php?id=<?php echo $article['id']; ?>"><?php echo $article['menuheader'];  ?></a>
                    
                </td>
                
                <?php } ;?>
                
            </tr>
            
        </table>
           
    </div>
    
    <p> this is where the time line will go</p>
    
    <?php 
    
        //instanstiating the article class and ssigning it to a variable
        $newArticle = new Article;

        //checking if the user clicked the menu link 
        if (isset($_GET['id'])) {
                //the display the article content
                $id=$_GET['id'];

                $data=$newArticle->fetch_data($id);

            ?>
            <p><?php echo $data['bodytext']; ?></p>

            <?php

        } else {
           
        }
        ?>
    
    <p>This is where the footer will go</p>
    
</body>
</html>

So far I have tried: $id = 1; and using a redirect to index.php. Neither worked, the later due to 'to many redirects'.

I would like to keep the code I have used so far rather than redoing it again, so if you can help, please give me advice on this code.

Thanks

All you need to do is to set the id to 1 (or what ever your home page is) if there aren't any id passed in the url. No need for the if-statement.

Here's how it can be done using the null coalescing operator :

$newArticle = new Article;

// Sets the id to what $_GET['id'] is set to if it exists, otherwise, set it to 1
$id   = $_GET['id'] ?? 1;

$data = $newArticle->fetch_data($id);
?>

<p><?php echo $data['bodytext'] ?? 'Page not found'; ?></p>

I also added in how you can show "Page not found" if $newArticle->fetch_data($id) didn't return anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM