• Welcome to Journal web site.

我是 PHP 程序员

- 开发无止境 -

Next
Prev

实战:列表页和详情页

Data: 2021-04-02 14:58:05Form: JournalClick: 7

# 实战:列表页和详情页


# 一、首页:文本传值

<header class="entry-header">
    <h1 class="entry-title">
        <a href="/details.php?title='<?php echo $article_v['title']; ?>'"><?php echo $article_v['title']; ?></a>
    </h1>
</header>

# 二、详情页:接收值

<?php
    $pdo = new PDO('mysql:host=localhost;dbname=boke', 'root' , 'root');
    $stmt = $pdo->prepare('SELECT * FROM article WHERE `title`="'.$_GET['title'].'"');
    $stmt->execute();
    $arr = $stmt->fetchAll();
    $find = $arr[0];
    print_r($find);
?>
<article>
    <header class="entry-header">
        <h1 class="entry-title">
        <?php echo $find['title']; ?>
        </h1>
    </header>
    <img style="width:100%;" src="<?php echo $find['img']; ?>" alt="" />
    <div class="entry-content ql-editor" id="md-editor" style="padding: 0;">
        <?php echo $find['content']; ?>
    </div>

    <footer class="entry-meta">
        发布于
        <a href="/index.html?time=2020-10-02" title="2020-10-02" rel="bookmark">
        <time class="entry-date" datetime="2020-10-02"><?php echo $find['date']; ?></time> </a>。 属于
        <a href="/index.html?cate=4" title="查看 Linux中的全部文章" rel="category"><?php echo $find['class'] ?></a>
        分类
    </footer>
</article>

# 三、使用 ID

  • 首页
<header class="entry-header">
    <h1 class="entry-title">
        <a href="/details.php?id='<?php echo $article_v['id']; ?>'">
            <?php echo $article_v['title']; ?>
        </a>
    </h1>
</header>

-详情页

<?php
    $pdo = new PDO('mysql:host=localhost;dbname=boke2', 'root' , 'root' , array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8';"));
    $stmt = $pdo->prepare('select * from `article` WHERE `id`="'.$_GET['id'].'"');
    $stmt->execute();
    $arr = $stmt->fetchAll();
    $find = $arr[0];
?>

# 四、分类

<?php
    $pdo = new PDO('mysql:host=localhost;dbname=boke2', 'root' , 'root' , array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8';"));
    $stmt = $pdo->prepare('SELECT * FROM class ORDER BY sort DESC,id DESC');
    $stmt->execute();
    $menu = $stmt->fetchAll();
?>
<nav id="site-navigation" class="main-navigation" role="navigation">
    <ul class="nav-menu">
        <?php
            foreach($menu as $menu_v){
                echo '<li>';
                echo '	<a href="/index.php?id='.$menu_v['id'].'">'.$menu_v['name'].'</a>';
                echo '</li>';
            }
        ?>
    </ul>
</nav>

# 五、文章列表

<?php
    if(!empty($_GET['id'])){
        $sql = 'SELECT * FROM article WHERE `class`='.$_GET['id'].' ORDER BY date DESC';
    }else{
        $sql = 'SELECT * FROM article ORDER BY date DESC';
    }
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    $article = $stmt->fetchAll();

    $stmt = $pdo->prepare('SELECT * FROM class');
    $stmt->execute();
    $menu = $stmt->fetchAll();
    $class = [];
    if(!empty($menu)){
        foreach($menu as $menu_v){
            $class[$menu_v['id']] = $menu_v;
        }
    }
?>
<div id="content" role="main">
    <?php
        foreach($article as $article_v){
    ?>
    <article>
        <header class="entry-header">
            <h1 class="entry-title">
                <a href="/details.php?id=<?php echo $article_v['id']; ?>"
                ><?php echo $article_v['title']; ?></a
                >
            </h1>
        </header>
        <?php
            if($article_v['img']){
        ?>
        <img style="width:200px;height:100px;" src="<?php echo $article_v['img']; ?>" alt=""/>
        <?php
            }
        ?>

        <?php
            if($article_v['content']){
        ?>
        <div class="entry-content"><?php echo $article_v['content']; ?></div>
        <?php
            }
        ?>
        <footer class="entry-meta">
            发布于
            <a href="/index.html?time=2021-02-11">
                <time class="entry-date"><?php echo $article_v['date']; ?></time>
            </a>。 属于
            <?php
                if($article_v['class']){
            ?>
            <a href="/index.php?id=<?php echo $article_v['class']; ?>"><?php echo $article_v['class']; ?></a>
            分类
            <?php
                }
            ?>
        </footer>
    </article>
    <?php
        }
    ?>
</div>

# 六、分类参照

$stmt = $pdo->prepare('SELECT * FROM class');
$stmt->execute();
$menu = $stmt->fetchAll();
$class = [];
if(!empty($menu)){
    foreach($menu as $menu_v){
        $class[$menu_v['id']] = $menu_v;
    }
}
<a href="/index.php?id=<?php echo $article_v['class']; ?>"><?php echo $class[$article_v['class']]['name']; ?></a>分类

# 七、搜索功能

<script type="text/javascript">
    $(function () {
        $("#searchsubmit").click(function () {
            if ($("#s").val() != "") {
                location.href = "/index.php?s=" + $("#s").val();
            }
            return false;
        });
    });
</script>
<?php
    if(!empty($_GET['id'])){
        $sql = 'SELECT * FROM article WHERE `class`='.$_GET['id'].' ORDER BY date DESC';
    }else if(!empty($_GET['s'])){
        $sql = 'SELECT * FROM article WHERE `title`="'.$_GET['s'].'" ORDER BY date DESC';
    }else{
        $sql = 'SELECT * FROM article ORDER BY date DESC';
    }
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    $article = $stmt->fetchAll();

    $stmt = $pdo->prepare('SELECT * FROM class');
    $stmt->execute();
    $menu = $stmt->fetchAll();
    $class = [];
    if(!empty($menu)){
        foreach($menu as $menu_v){
            $class[$menu_v['id']] = $menu_v;
        }
    }
?>
Name:
<提交>