我正在学习...我已经阅读了http://www.w3schools.com上的教程,浏览了 www.php.net 上的手册,并且对一切工作原理有了很好的理解。但是我对如何构建我的页面感到困惑。哪些内容应该放在同一个页面上,哪些内容应该有自己的页面并且只包含/需要?
我仍然需要阅读的一件事是参数化,所以不要在这方面打败我......但是。
这是我所拥有的:
a.php 这是用户在输入 www.mydomain.com/a.php 时看到的页面
<html>
<body>
<?php require("b.php"); ?>
// this requires that the page gets b.php and puts it here
<p>Name: <?php echo $row['firstname']; ?></p>
<p>Last Name: <?php echo $row['lastname']; ?></p>
</body>
</html>
b.php 此页面包含/需要在 a.php 中
<?php
require("../dbunamepword.php");
// this connects to my database
$query = ("SELECT * FROM contacts WHERE contacttype IN ('Buyer','Seller','Buyer / Seller','Investor') AND leadstatus = 'New' ORDER BY date DESC");
$result = $mysqli->query($query) or die ("Error: ".mysqli_error($mysqli));
while ($row = $result->fetch_array()) {
// this fetches the data I want from the table in my db while all cases match above
$firstname = $row ['firstname'];
$lastname = $row['lastname'];
// these are the rows in the table I want to fetch
echo "First Name: " . $firstname . " " .$lastname ."<br />";
// this is how I want the data presented
}
?>
这是我的问题提前谢谢你:
1) 在构建这些页面时,使用函数对我有什么好处,或者它们对我没有好处?
2) 我不能省略echo
b.php 上的行,然后像下面那样调用 a.php 中的行并输入一个foreach
语句吗?我实际上对此进行了测试,并且一直收到意外的 $end 错误,所以我不确定这是否不是一个好方法或者我做错了什么?
<html>
<body>
<?php require("b.php"); ?>
// this requires that the page gets b.php and puts it here
<p>Name: <?php echo $firstname; ?></p>
<p>Last Name: <?php echo $lastname; ?></p>
</body>
</html>
3) 假设我添加了一些 if/else 语句,如下所示,我应该将它们放在 b.php 中还是将它们放在它们自己的文件 c.php 中,然后在 b.php 中添加一个包含?
if(isset($_GET['leadstatus']) && $_GET['leadstatus'] == "All")
{
$query = "SELECT * FROM contacts WHERE contacttype IN ('Buyer','Seller','Buyer / Seller','Investor') ORDER BY contacts.date DESC";
}
elseif (in_array($_GET['leadstatus'], array('New', 'Hot', 'Warm', 'Cold', 'Rejected', 'Closed')))
{
$status = $_GET['leadstatus'];
$query = "SELECT * FROM contacts WHERE leadstatus = '".$status."' AND contacttype IN ('Buyer','Seller','Buyer / Seller','Investor') ORDER BY contacts.date DESC";
}
4)在我$query
有SELECT * FROM contacts WHERE contacttype IN ('Buyer','Seller','Buyer / Seller','Investor')
而不是在每个 if/else 语句中输入它,或者说我需要添加另一个字段,例如“Renter”,我可以做类似的事情:
$type = ('Buyer','Seller','Buyer / Seller','Investor')
$query = ("SELECT * FROM contacts WHERE contacttype = $type AND leadstatus = 'New' ORDER BY date DESC");
if (($_GET['date'] == 'today'))
{
$$query = "SELECT * FROM contacts WHERE contacttype = $type AND date = DATE(NOW()) ORDER BY date DESC";
}
if (($_GET['date'] == 'thisweek'))
{
$thisweek = date('Y-m-d', strtotime('last sunday'));
$query = "SELECT * FROM contacts WHERE contacttype = $type AND date BETWEEN '$thisweek' AND '$thisweek' + INTERVAL 7 DAY ORDER BY date DESC";
}