I’m learning…I’ve read through the tutorials on http://www.w3schools.com, I’ve looked through the manual on http://www.php.net and have a pretty good understanding of how everything works. But what I’m confused about how to structure my pages. What things should be put together on the same page and what should have it’s own page and just be included/required?
One of the things I still need to read up on is parameterizing, so don’t beat me up on that…yet.
Here is what I have:
a.php This is the page the user sees when they type in http://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 This page is included/required in 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
}
?>
Here are my questions Thank you in advance:
1) How would using functions benefit me in building these pages or would they not benefit me?
2) Couldn’t I leave out the echo on b.php and just call the rows in a.php like below and put in a foreach statement? I actually tested this and I kept getting an unexpected $end error so I’m not sure if this is not a good way or if I did something wrong?
<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) Let’s say I add in some if/else statements like below, should I put these in b.php or put them in their own file c.php and then put an include in 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) In my $query I have SELECT * FROM contacts WHERE contacttype IN ('Buyer','Seller','Buyer / Seller','Investor') rather than typing that out in every if/else statement or lets say I needed to add another field like, ‘Renter’, could I do something like:
$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";
}
I would recommend you start using a micro-framework to help you organize your code. One that I prefer is Silex because it promotes/encourages good programming habits/practices and as your projects grow you can seamlessly evolve into the more full-stack world of Symfony.
You don’t need a framework, but I believe they can help you learn best-practices for organizing any codebase even if you decide not to use one for some projects.