PHP 正在搞乱 HTML 设计 &在 HTML 之外显示结果 [英] PHP is messing with HTML Design & Displaying results outside of the HTML

查看:19
本文介绍了PHP 正在搞乱 HTML 设计 &在 HTML 之外显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定为什么会发生这种情况,我在 HTML 或任何东西中都不是那么棒",我主要认为我的问题是我如何显示结果......(任何提示/帮助/或建议非常感谢)..

脚本基本上是一个简单的FoodManagement"类,它管理食物......哈?

这是代码,(index.php,显示所有食物)

/** 导入类文件,所以这个文件看起来不会很乱.*/require("class.foodmanagement.php");$db["信息"] = 数组(主机"=>"本地主机",名称" =>食物",用户"=>根",通过" =>"");$db["tables"] = 数组(食物" =>食物");$db["columns"]["food"] = array(标题" =>标题",说明"=>描述",图片"=>图片",所有者ID" =>电子邮件");//基本上是你正在使用的餐馆的电子邮件/身份证明"...$poc = new FoodManagement($db["info"]["host"], $db["info"]["name"], $db["info"]["user"], $db["info"]["pass"], $db["tables"]["food"], $db["columns"]["food"]["title"], $db["columns"]["food"]["描述"], $db["columns"]["food"]["picture"], $db["columns"]["food"]["ownerid"]);//echo $poc->DeleteItem(13);//您可以简单地根据ID删除一个项目...//echo $poc->DeleteItem("johnanagram@example.com", "owner")//或者根据 OwnerID 字段删除它(默认情况下用于电子邮件...)//echo $poc->addItem("google", "这是 google.com!想买吗?", "https://www.google.com/images/srpr/logo11w.png", "johnanagram@example.com");//(标题",描述",图片",所有者")//$poc->displayFood();//显示每一种食物...//$poc->displayFood(26, "id");//根据ID显示单个食物项//$poc->displayFood("johnanagram@example.com");//显示属于'user'/'resteraunt'的所有食物.回声'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><头><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/><title>食品管理!</title><link rel="stylesheet" type="text/css" href="style.css"/><身体><中心><div id="容器"><h1>欢迎来到食品管理!</h1><div id="面包屑"><p><strong>看看我们所有的美味佳肴!</strong></center></p>

<div id="容器"><div id="面包屑">' .$poc->displayFood() .'

<div id="页脚">&copy 2014 - 食品管理

';

class.management.php

class FoodManagement {私人 $dbh;私人 $db_user;私人 $db_pass;私人 $db_name;私人 $db_host;私人 $tbl_food;私人 $col_food_title_pr;私人 $col_food_description_pr;私人 $col_food_picture_pr;私人 $col_food_owner_pr;公共函数__construct($dbhost, $dbname, $dbuser, $dbpass, $tblfood, $col_food_title, $col_food_description, $col_food_picture, $col_food_owner) {$this->db_host = $dbhost;$this->db_name = $dbname;$this->db_user = $dbuser;$this->db_pass = $dbpass;$this->tbl_food = $tblfood;$this->col_food_title_pr = $col_food_title;$this->col_food_description_pr = $col_food_description;$this->col_food_picture_pr = $col_food_picture;$this->col_food_owner_pr = $col_food_owner;$this->dbh = $this->dbhcon();}公共函数 dbhcon() {尝试 {return $this->dbh = new PDO("mysql:host=" . $this->db_host . ";dbname=" . $this->db_name, $this->db_user, $this->db_pass);}捕获(PDOException $e){//发生的任何错误,例如无效的用户名、密码或错误的主机.等等..echo $e->getMessage();}}公共函数 AddItem($title, $description, $picture, $owner) {尝试 {$sql = "插入`".$this->tbl_food ."` ($this->col_food_title_pr, $this->col_food_description_pr, $this->col_food_picture_pr, $this->col_food_owner_pr) VALUES (:title, :description, :picture, :owner)";$q = $this->dbh->prepare($sql);$q-> 执行(数组(':title' =>$title,':描述' =>$描述,':图片' =>$图片,':所有者' =>$所有者));返回成功!";}捕获(PDOException $e){返回 $e->getMessage();}}公共函数 DeleteItem($id, $base = "id") {尝试 {开关($base){案例所有者":$sql = "DELETE FROM `" .$this->tbl_food .`哪里".$this->col_food_owner_pr .=".$id ."'";$q = $this->dbh->prepare($sql);$q->execute();返回成功!";休息;案例ID":$sql = "DELETE FROM `" .$this->tbl_food .`哪里id =".$id ."'";$q = $this->dbh->prepare($sql);$q->execute();返回成功!";休息;默认:$sql = "DELETE FROM `" .$this->tbl_food .`哪里id =".$id ."'";$q = $this->dbh->prepare($sql);$q->execute();返回成功!";}}捕获(PDOException $e){返回 $e->getMessage();}}public function displayFood($id = null, $base = "owner") {//搜索$base字段,寻找$id...尝试 {if(isset($id)) {开关($base){案例ID":/** 抓取具有特定 ID 的项目 */$sql = $this->dbh->query("SELECT * FROM `" . $this->tbl_food . "` WHERE id='" . $id . "'");$sql->setFetchMode(PDO::FETCH_ASSOC);while($row = $sql->fetch()) {回声ID:".$row["id"] ."<br/>";回声 "" .$row[$this->col_food_title_pr] ."<br/>";回声 $row[$this->col_food_description_pr] ."<br/>";echo '<img src="' . $row[$this->col_food_picture_pr] . '"/><br/><br/><br/>';}休息;案例所有者":/** 抓取具有特定 ID 的项目 */$sql = $this->dbh->query("SELECT * FROM `" . $this->tbl_food . "` WHERE " . $this->col_food_owner_pr . "='" . $id . "'");$sql->setFetchMode(PDO::FETCH_ASSOC);while($row = $sql->fetch()) {回声ID:".$row["id"] ."<br/>";回声 "" .$row[$this->col_food_title_pr] ."<br/>";回声 $row[$this->col_food_description_pr] ."<br/>";echo '<img src="' . $row[$this->col_food_picture_pr] . '"></img><br/><br/><br/>';}休息;/** 抓取具有特定 ID 的项目 */$sql = $this->dbh->query("SELECT * FROM `" . $this->tbl_food . "` WHERE " . $this->col_food_owner_pr . "='" . $id . "'");$sql->setFetchMode(PDO::FETCH_ASSOC);while($row = $sql->fetch()) {回声ID:".$row["id"] ."<br/>";回声 "" .$row[$this->col_food_title_pr] ."<br/>";回声 $row[$this->col_food_description_pr] ."<br/>";echo '<img src="' . $row[$this->col_food_picture_pr] . '"></img>/><br/><br/><br/>';}默认:}} 别的 {/** 显示每一种食物 */$sql = $this->dbh->query("SELECT * FROM `" . $this->tbl_food . "`");# 设置获取模式$sql->setFetchMode(PDO::FETCH_ASSOC);while($row = $sql->fetch()) {回声ID:".$row["id"] ."<br/>";回声 "" .$row[$this->col_food_title_pr] ."<br/>";回声 $row[$this->col_food_description_pr] ."<br/>";echo '<img src="' . $row[$this->col_food_picture_pr] . '"></img><br/><br/><br/>';}}}捕获(PDOException $e){echo $e->getMessage();}}}

style.css

body {背景颜色:#E6E6E6;}h1{颜色:#6699FF;字体系列:Arial、Helvetica、sans-serif;字体粗细:粗体;字体大小:中等;边距:0 0 10px 8px;填充:0;}图像{边框:0;}形式 {颜色:#6699FF;边距:0;填充:0;}#容器 {宽度:600px;左边距:自动;边距右:自动;边框:1px 实心#CCCCCC;填充:9px;背景色:#FFFFFF;}#面包屑 {颜色:#6699FF;文本对齐:居中;字体系列:Arial、Helvetica、sans-serif;字体大小:小;边距:0 0 8px 8px;}#头文件{文本对齐:左;向左飘浮;宽度:320px;}#标题大小{文本对齐:右;宽度:75px;向左飘浮;}#页脚{底部:0px;位置:绝对;宽度:100%;文本对齐:居中;左边距:自动;边距右:自动;边框:1px 实心#CCCCCC;填充:9px;颜色:#6699FF;背景色:#FFFFFF;}

<小时>

注意我的设计不是很好,所以我只是使用了我找到的这个基本模板.

解决方案

index.php 期望 displayFood() 方法返回一个字符串,它连接到HTML,然后打印.但是 displayFood() 正在回显其结果,而不是将它们作为字符串返回.

所以你要么改变 displayFood() 返回一个字符串,要么改变 index.php 打印开始的 HTML,调用 displayFood(),然后打印结束的 HTML,例如

echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">...<div id="面包屑">';$poc->displayFood();回声'

<div id="页脚">&copy 2014 - 食品管理

';

I'm not to sure why this happening, I'm not "that great" in HTML or anything and I mainly assume my issue is how I'm displaying the results...(any tips / help / or suggestions is greatly appreciated)..

The script is basically a simple 'FoodManagement' class that, well, manages food... hah?

here is the code, (index.php , displays all the food)

/** Imports the class file, so this file doesn't look all messy. */
require("class.foodmanagement.php");

$db["info"] = array(
    "host" => "localhost",
    "name" => "food",
    "user" => "root",
    "pass" => "");

$db["tables"] = array(
    "food" => "food");

$db["columns"]["food"] = array(
    "title" => "title",
    "description" => "description",
    "picture" => "picture",
    "ownerid" => "email"); //Basically the resteraunt's Email / "Identification" you're using...


$poc = new FoodManagement($db["info"]["host"], $db["info"]["name"], $db["info"]["user"], $db["info"]["pass"], $db["tables"]["food"], $db["columns"]["food"]["title"], $db["columns"]["food"]["description"], $db["columns"]["food"]["picture"], $db["columns"]["food"]["ownerid"]);

//echo $poc->DeleteItem(13); //You can delete an Item based simpily on the ID...
//echo $poc->DeleteItem("johnanagram@example.com", "owner") //Or delete it based upon the OwnerID Field (default case is meant for Email...)
//echo $poc->addItem("google", "this is google.com! wanna buy it?", "https://www.google.com/images/srpr/logo11w.png", "johnanagram@example.com"); //("title", "description", "picture", "owner")
//$poc->displayFood(); //Displasy EVERY food item...
//$poc->displayFood(26, "id"); //Display a single food item based on the ID
//$poc->displayFood("johnanagram@example.com"); //Display every food item that belongs to a 'user'/'resteraunt'.

echo '

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>FoodManagement!</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<center>
<div id="container">
 <h1>Welcome to, FoodManagement!</h1>
 <div id="breadcrumbs">
   <p><strong>Look at all our wonderful foods!
   </strong></center>
</p>
 </div>

   </div>  


   <div id="container">
<div id="breadcrumbs">
' . $poc->displayFood() . '

</div>
</div>





<div id="footer">
&copy 2014 - FoodManagement
</div>


</body>
</html>';

class.management.php

class FoodManagement {

    private $dbh;
    private $db_user;
    private $db_pass;
    private $db_name;
    private $db_host;

    private $tbl_food;

    private $col_food_title_pr;
    private $col_food_description_pr;
    private $col_food_picture_pr;
    private $col_food_owner_pr;

    public function __construct($dbhost, $dbname, $dbuser, $dbpass, $tblfood, $col_food_title, $col_food_description, $col_food_picture, $col_food_owner) {

        $this->db_host = $dbhost;
        $this->db_name = $dbname;
        $this->db_user = $dbuser;
        $this->db_pass = $dbpass;

        $this->tbl_food = $tblfood;

        $this->col_food_title_pr = $col_food_title;
        $this->col_food_description_pr = $col_food_description;
        $this->col_food_picture_pr = $col_food_picture;
        $this->col_food_owner_pr = $col_food_owner;

        $this->dbh = $this->dbhcon();

    }

    public function dbhcon() {

      try {

            return $this->dbh = new PDO("mysql:host=" . $this->db_host . ";dbname=" . $this->
                db_name, $this->db_user, $this->db_pass);

        }
        catch (PDOException $e) {
            //any errors that occur, such as an invalid username, password, or the wrong host. etc..
            echo $e->getMessage();
        }

    }

    public function AddItem($title, $description, $picture, $owner) {

     try {

        $sql = "INSERT INTO `" . $this->tbl_food . "` ($this->col_food_title_pr, $this->col_food_description_pr, $this->col_food_picture_pr, $this->col_food_owner_pr) VALUES (:title, :description, :picture, :owner)";

        $q = $this->dbh->prepare($sql);
    $q->execute(array(
        ':title' => $title,
        ':description' => $description,
        ':picture' => $picture,
        ':owner' => $owner));

    return "success!";

     }

     catch (PDOException $e) {

     return $e->getMessage();

     }

    }

    public function DeleteItem($id, $base = "id") {

        try {
           switch($base) {

            Case "owner":

             $sql = "DELETE FROM `" . $this->tbl_food . "` WHERE " . $this->col_food_owner_pr . "='". $id . "'";
            $q = $this->dbh->prepare($sql);
            $q->execute();

            return "success!";

            break;

            Case "id":

             $sql = "DELETE FROM `" . $this->tbl_food . "` WHERE id='". $id . "'";
            $q = $this->dbh->prepare($sql);
            $q->execute();

            return "success!";

            break;

            default:
             $sql = "DELETE FROM `" . $this->tbl_food . "` WHERE id=". $id . "'";
            $q = $this->dbh->prepare($sql);
            $q->execute();

            return "success!";
           }

        } 
        catch (PDOException $e) {

            return $e->getMessage();
        }

    }

    public function displayFood($id = null, $base = "owner") { //Search $base field, for $id...
 try {


           if(isset($id)) {

         switch($base) {

            Case "id":

               /** Grab item with the specific ID */

            $sql = $this->dbh->query("SELECT * FROM `" . $this->tbl_food . "` WHERE id='" . $id . "'");
            $sql->setFetchMode(PDO::FETCH_ASSOC);

while($row = $sql->fetch()) {
    echo "ID: " . $row["id"] . "<br />";
    echo "" . $row[$this->col_food_title_pr] . "<br />";
    echo $row[$this->col_food_description_pr] . "<br />";
    echo '<img src="' . $row[$this->col_food_picture_pr] . '"/><br /><br /><br />';

}

            break;

            Case "owner":
               /** Grab item with the specific ID */

            $sql = $this->dbh->query("SELECT * FROM `" . $this->tbl_food . "` WHERE " . $this->col_food_owner_pr . "='" . $id . "'");
            $sql->setFetchMode(PDO::FETCH_ASSOC);

while($row = $sql->fetch()) {
    echo "ID: " . $row["id"] . "<br />";
    echo "" . $row[$this->col_food_title_pr] . "<br />";
    echo $row[$this->col_food_description_pr] . "<br />";
    echo '<img src="' . $row[$this->col_food_picture_pr] . '"></img><br /><br /><br />';

}

            break;

               /** Grab item with the specific ID */

            $sql = $this->dbh->query("SELECT * FROM `" . $this->tbl_food . "` WHERE " . $this->col_food_owner_pr . "='" . $id . "'");
            $sql->setFetchMode(PDO::FETCH_ASSOC);

while($row = $sql->fetch()) {
    echo "ID: " . $row["id"] . "<br />";
    echo "" . $row[$this->col_food_title_pr] . "<br />";
    echo $row[$this->col_food_description_pr] . "<br />";
    echo '<img src="' . $row[$this->col_food_picture_pr] . '"></img>/><br /><br /><br />';

}

            default:

         }

           } else {
            /** Display every food item */
           $sql = $this->dbh->query("SELECT * FROM `" . $this->tbl_food . "`");

# setting the fetch mode
$sql->setFetchMode(PDO::FETCH_ASSOC);

while($row = $sql->fetch()) {
    echo "ID: " . $row["id"] . "<br />";
    echo "" . $row[$this->col_food_title_pr] . "<br />";
    echo $row[$this->col_food_description_pr] . "<br />";
    echo '<img src="' . $row[$this->col_food_picture_pr] . '"></img><br /><br /><br />';

}

            }




 }
    catch(PDOException $e) {
        echo $e->getMessage();
    }

}
}

style.css

body {
        background-color: #E6E6E6;
}

h1 {
        color: #6699FF;
        font-family: Arial, Helvetica, sans-serif;
        font-weight:bold;
        font-size: medium;
        margin: 0 0 10px 8px;
        padding:0;
}

img {
        border: 0;
}

form {
    color: #6699FF;
        margin: 0;
        padding: 0;
}

#container {
        width: 600px;
        margin-left:auto;
        margin-right:auto;
        border: 1px solid #CCCCCC;
        padding: 9px;
        background-color:#FFFFFF;

}

#breadcrumbs {
        color: #6699FF;
    text-align: center;
        font-family: Arial, Helvetica, sans-serif;
        font-size:small;
        margin: 0 0 8px 8px;
}

#headerfile {
        text-align:left;
        float: left;
        width: 320px;
}

#headersize {
        text-align:right;
        width: 75px;
        float: left;
}  

 #footer {
    bottom: 0px;
    position: absolute;
    width: 100%;
    text-align: center;
        margin-left:auto;
        margin-right:auto;
        border: 1px solid #CCCCCC;
        padding: 9px;
    color: #6699FF;
        background-color:#FFFFFF;

}


Note I'm not that great in design so I simpily used this basic template I found.

解决方案

index.php expects the displayFood() method to return a string, which it concatenates to the HTML and then prints. But displayFood() is echoing its results instead of returning them as a string.

So you have to either change displayFood() to return a string, or change index.php to print the beginning HTML, call displayFood(), and then print the ending HTML, e.g.

echo '

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
...
<div id="breadcrumbs">
';

$poc->displayFood();

echo '

</div>
</div>





<div id="footer">
&copy 2014 - FoodManagement
</div>


</body>
</html>';

这篇关于PHP 正在搞乱 HTML 设计 &amp;在 HTML 之外显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆