7.25 用数据库管理网站内容
程序员文章站
2022-03-27 09:51:50
...
7月25日作业:
改写23日案例,用数据库管理网站内容:
案例:企业站调用数据库数据
备注:遇到了一个问题,快奔溃的时候才解决!
在调用数据的过程中,使用_GET变量获取传递过来的ID数值,但是在用if语句判断时, 传递过来的变量id已经被我转化成数字,而判断的id并没有转变类型 , 在===全等判断时,调不出数据. 解决方法: 用==(等于),或者先判断类型,再用===(全等).
首页面--实例(index-yan.php)
<?php //网站数据来源于数据库 $headline ='最权威展示平台'; //1.连接数据库 require __DIR__ . '/inc/connect-yan.php'; //获取数据库影视信息movies $sql = 'SELECT * FROM `movies` LIMIT 9'; $stmt = $pdo->prepare($sql); $stmt ->execute(); $movies=$stmt->fetchAll(PDO::FETCH_ASSOC); //echo '<pre>'.print_r($movies); //获取数据库栏目信息category $sql = 'SELECT * FROM `category` LIMIT 3'; $stmt = $pdo->prepare($sql); $stmt ->execute(); $cates=$stmt->fetchAll(PDO::FETCH_ASSOC); //echo '<pre>'.print_r($cates); //获取数据库栏目信息category $sql = 'SELECT * FROM `system` LIMIT 1'; $stmt = $pdo->prepare($sql); $stmt ->execute(); $system=$stmt->fetch(PDO::FETCH_ASSOC); //echo '<pre>'.print_r($system); ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title><?php echo $headline;?></title> </head> <style> ul, li { padding: 0; margin: 0; list-style: none; } .main-nav{ width: 600px; height: 50px; background-color: #1E9FFF; } .main-nav li{ width: 150px; height: 50px; line-height: 50px; text-align: center; float: left; } .main-nav li a{ color: #fff; font-size: 16px; } a{ text-decoration: none; } .banner{ margin: 10px auto; } .main { width: 600px; min-width: 1000px; height: auto; } .footer{ width: 600px; height: 50px; line-height: 50px; text-align: center; background-color: #1E9FFF; color: white; } </style> <body> <!--顶部--> <div class="header"> <ul class="main-nav"> <li><a href="index-yan.php">首页</a> </li> <?php foreach ($cates as $cate):?> <li><a href="<?php echo 'list-yan.php?cate_id='.$cate['cate_id'];?>"><?php echo $cate['alias'];?></a> </li> <?php endforeach;?> </ul> </div> <div class="banner"> <img src="https://www.php.cn/static/images/index_banner7.jpg" alt="" width="600" height="200"></div> <!--内容展示部分--> <div class="main"> <!-- 内容展示--> <?php foreach ($cates as $cate){ echo '<a><a href="list-yan.php?cate_id='.$cate['cate_id'].'" style="font-size:30px;">'.$cate['alias'].'</a></h1>'; echo '<ol>'; //遍历数组具体内容 foreach ($movies as $movie){ if($movie['cate_id']===$cate['cate_id']){ echo '<li><a href="detail-yan.php?mov_id='.$movie['mov_id'].'">'.$movie['name'].'</a> </li>'; } }; echo '</ol>'; } ?> </div> <!--底部--> <div class="footer">版权所有@ php.cn</div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
列表页--实例(list-yan.php)
<?php //网站数据来源于数据库 $headline ='最权威展示平台'; //1.连接数据库 require __DIR__ . '/inc/connect-yan.php'; //获取数据库影视信息movies $sql = 'SELECT * FROM `movies` LIMIT 9'; $stmt = $pdo->prepare($sql); $stmt ->execute(); $movies=$stmt->fetchAll(PDO::FETCH_ASSOC); //echo '<pre>'.print_r($movies); //获取数据库栏目信息category $sql = 'SELECT * FROM `category` LIMIT 3'; $stmt = $pdo->prepare($sql); $stmt ->execute(); $cates=$stmt->fetchAll(PDO::FETCH_ASSOC); //print_r($cates[0]); //var_dump($cates); //获取数据库栏目信息category $sql = 'SELECT * FROM `system` LIMIT 1'; $stmt = $pdo->prepare($sql); $stmt ->execute(); $system=$stmt->fetch(PDO::FETCH_ASSOC); //echo '<pre>'.print_r($system); ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title><?php echo $headline;?></title> </head> <style> ul, li { padding: 0; margin: 0; list-style: none; } .main-nav{ width: 600px; height: 50px; background-color: #1E9FFF; } .main-nav li{ width: 150px; height: 50px; line-height: 50px; text-align: center; float: left; } .main-nav li a{ color: #fff; font-size: 16px; } a{ text-decoration: none; } .banner{ margin: 10px auto; } .main { width: 600px; min-width: 1000px; height: auto; } .footer{ width: 600px; height: 50px; line-height: 50px; text-align: center; background-color: #1E9FFF; color: white; } </style> <body> <?php $cate_id = $_GET['cate_id']; //$cate_id = intval($cate_id); //print_r($cate_id); ?> <!--顶部--> <div class="header"> <ul class="main-nav"> <li><a href="index-yan.php">首页</a> </li> <?php foreach ($cates as $value):?> <li><a href="<?php echo 'list-yan.php?cate_id='.$value['cate_id'];?>"><?php echo $value['alias'];?></a> </li> <?php endforeach;?> </ul> </div> <div class="banner"> <img src="https://www.php.cn/static/images/index_banner7.jpg" alt="" width="600" height="200"></div> <!--内容展示部分--> <div class="main"> <!-- 内容展示--> <!-- 内容展示--> <?php foreach ($cates as $cate){ // print_r($cate['cate_id']); // var_dump($cate['cate_id']); if ($cate['cate_id'] === $cate_id){ echo '<a><a href="list-yan.php?cate_id='.$cate['cate_id'].'" style="font-size:30px;">'.$cate['alias'].'</a></h1>'; echo '<ol>'; //遍历数组具体内容 foreach ($movies as $movie){ if($movie['cate_id']===$cate['cate_id']){ echo '<li><a href="detail-yan.php?mov_id='.$movie['mov_id'].'">'.$movie['name'].'</a> </li>'; } }; echo '</ol>'; } } ?> </div> <!--底部--> <div class="footer">版权所有@ php.cn</div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
详情页--实例(detail-yan.php)
<?php //网站数据来源于数据库 $headline ='最权威展示平台'; //1.连接数据库 require __DIR__ . '/inc/connect-yan.php'; //获取数据库影视信息movies $sql = 'SELECT * FROM `movies` LIMIT 9'; $stmt = $pdo->prepare($sql); $stmt ->execute(); $movies=$stmt->fetchAll(PDO::FETCH_ASSOC); //echo '<pre>'.print_r($movies); //获取数据库栏目信息category $sql = 'SELECT * FROM `category` LIMIT 3'; $stmt = $pdo->prepare($sql); $stmt ->execute(); $cates=$stmt->fetchAll(PDO::FETCH_ASSOC); //echo '<pre>'.print_r($cates); //获取数据库栏目信息category $sql = 'SELECT * FROM `system` LIMIT 1'; $stmt = $pdo->prepare($sql); $stmt ->execute(); $system=$stmt->fetch(PDO::FETCH_ASSOC); //echo '<pre>'.print_r($system); ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title><?php echo $headline;?></title> </head> <style> ul, li { padding: 0; margin: 0; list-style: none; } .main-nav{ width: 600px; height: 50px; background-color: #1E9FFF; } .main-nav li{ width: 150px; height: 50px; line-height: 50px; text-align: center; float: left; } .main-nav li a{ color: #fff; font-size: 16px; } a{ text-decoration: none; } .banner{ margin: 10px auto; } .main { width: 600px; height: 400px; } .main p{ width: 600px; } .footer{ width: 600px; height: 50px; line-height: 50px; text-align: center; background-color: #1E9FFF; color: white; } </style> <body> <?php $mov_id = $_GET['mov_id']; //$mov_id = intval($mov_id); //print_r($cate_id); ?> <!--顶部--> <div class="header"> <ul class="main-nav"> <li><a href="index-yan.php">首页</a> </li> <?php foreach ($cates as $value):?> <li><a href="<?php echo 'list-yan.php?cate_id='.$value['cate_id'];?>"><?php echo $value['alias'];?></a> </li> <?php endforeach;?> </ul> </div> <div class="banner"> <img src="https://www.php.cn/static/images/index_banner7.jpg" alt="" width="600" height="200"></div> <!--内容展示部分--> <div class="main"> <!-- 内容展示--> <?php foreach ($movies as $movie) { if ($movie['mov_id'] === $mov_id) { echo '<p>'.$movie['detail'].'</p>'; } } ?> </div> <!--底部--> <div class="footer">版权所有@ php.cn</div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
上一篇: PHP 爬虫抓取歌词
下一篇: php联接访Oracle是用过oci函数