在不使用echo的情况下查询数据库,为设计

时间:2017-04-03 19:10:47

标签: php mysql sql mysqli

目前有一些令我有点头疼的事情。我在点击上一页的链接后查询数据库检索一些数据。

我很容易在“回声”中玩一些代码。问题出现在'echo'中,因为我需要将php包括在内,以及用于设计目的的一般html /设计代码。有没有一种方法或方法可以编写代码但稍后再次调用变量并且能够放置其他php代码,例如includes?基本上是让我必须拥有代码的东西,然后让我专注于我的页面的设计方面。

任何帮助都非常感激。

<?php
if(!empty($_GET['book_url']))
{
   $sql = "
SELECT articles.id, articles.order_ref, articles.art_title, articles.art_book, articles.art_url, book.id AS bookid, book.book_name AS book_name, book.book_url AS book_url
FROM articles
LEFT JOIN book ON articles.art_book = book.id
WHERE book_name = \"" .$_GET['book_url'] . "\"
ORDER BY id ASC
";
    // then do the query, etc....
} 

$results = $db->query($sql);

if($results->num_rows) {
    While($row = $results->fetch_object()) {
        echo "

        ////////Show Stuff

        ";
        ?>

1 个答案:

答案 0 :(得分:0)

您可以将代码放在<?php ?>中来分隔代码和HTML。然后,在HTML中,您可以使用简短的代码片段来打印您在代码部分中所做的工作。像这样(可能有一两个语法错误,我没有Apache测试):

<?php

$name = $_GET['name'];

function stuff() {
    // this would normally be long and complicated
    return "stuff";
}

?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>title</title>
    <link rel="stylesheet" href="stylesheet.css" type="text/css">
  </head>
  <body>
    <!-- try to keep these short  so you can focus on design-->
    <h1>The name is: <?php echo $name; ?></h1>
    <h1>The stuff is: <?php echo stuff(); ?></h1>
  </body>
</html>

或者,您可以使用Laravel之类的框架来提供模板系统。