dongxieyi9115 2014-02-01 01:00
浏览 18
已采纳

PHP正在搞乱HTML设计并在HTML之外显示结果

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.

  • 写回答

1条回答 默认 最新

  • duangu6588 2014-02-01 01:07
    关注

    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>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。