簡體   English   中英

PHP搞砸了HTML設計並在HTML之外顯示結果

[英]PHP is messing with HTML Design & Displaying results outside of the HTML

我不確定為什么會這樣,我在HTML或其他方面都不“那么好”,我主要認為我的問題是我如何顯示結果...(任何提示/幫助/或建議都將不勝感激)..

該腳本基本上是一個簡單的“ FoodManagement”類,可以管理食物……哈?

這是代碼(index.php,顯示所有食物)

/** 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;

}

注意我的設計不是很好,所以我簡單地使用了我發現的這個基本模板。

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="breadcrumbs">
';

$poc->displayFood();

echo '

</div>
</div>





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


</body>
</html>';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM