私は学んでいます... http://www.w3schools.comのチュートリアルを読み、www.php.net のマニュアルを読み、すべてがどのように機能するかをよく理解しています。しかし、自分のページをどのように構成するかについて混乱しています。同じページにまとめる必要があるものと、独自のページを持ち、含める/必須にするものは何ですか?
私がまだ読む必要があることの 1 つはパラメーター化です。
ここに私が持っているものがあります:
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
on 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) 私の場合、すべての if/else ステートメントでそれを入力するので$query
はSELECT * FROM contacts WHERE contacttype IN ('Buyer','Seller','Buyer / Seller','Investor')
なく、「レンター」などの別のフィールドを追加する必要があるとしましょう。
$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";
}