【问题标题】:Wanting index.php to load when loaded想要 index.php 在加载时加载
【发布时间】:2021-03-25 13:20:33
【问题描述】:

我几乎已经使用 PHP OOP 创建了一个完整的动态页面。我已经成功创建了一个工作菜单——即当单击菜单项时,页面上会显示相关文本。但是,我想要的是默认显示主页文本,而不是单独单击主页链接。

这是我正在使用的代码:

    <?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>

到目前为止我已经尝试过:$id = 1;并使用重定向到 index.php。两者都不起作用,后者是由于“重定向很多”。

我想保留我目前使用的代码,而不是再次重做,所以如果你能提供帮助,请给我关于这段代码的建议。

谢谢

【问题讨论】:

  • 为什么此时需要重定向?如果未设置$_GET['id'],则设置$id = 1;,然后按照相同的方式进行操作,如果该值首先是从外部传入的……
  • $data=$newArticle-&gt;fetch_data($id); 应该在 if 之外。在任何情况下,您都希望获取数据,而不是如果设置了$_GET['id']

标签: php html mysqli


【解决方案1】:

如果 url 中没有传递任何 id,您需要做的就是将 id 设置为 1(或者您的主页是什么)。不需要 if 语句。

下面是使用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>

如果$newArticle-&gt;fetch_data($id) 没有返回任何内容,我还添加了如何显示“找不到页面”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    相关资源
    最近更新 更多