I had a different account on here but forgot my info. Sorry about that. Anyway. I'm a programming student with a teacher that doesn't really teach. She just reads pre-made powerpoint slides and often seems surprised to learn about things that she was unaware you could do prior to reading the slides. Basically, I think the school needed a PHP teacher and just kinda said "Eh. close enough. You'll do." She's not really friendly and asking for help is usually met with a rude reply, a condescending tone and an answer that isn't really an answer. I've asked classmates for help to no avail - they're just as confused. There are literally no PHP tutors as it's just a community college. I had the same lady for SQL and knew what to expect, but she's the only professor for these classes. She's an ex-marine, she's probably in her 60s or so and is close to retiring so I kinda get the feeling that she doesn't really care at this point, but it does a huge disservice to us - her students. I've never actually seen her code a single thing in class and she often gets confused and has to stop to thumb through her book or power points in order for her to figure out how to even import a database into xampp just to show us what it looks like. So this is my last hope. My chosen major is programming, like OOP with VB and Java and such, but we're still required to take PHP and SQL. I'm working on talking to the dean about the issues with the prof, but until something if anything can be done I have to deal with what's at hand.
With that said, here my assignment specifications just so you all have an idea what I'm actually trying to do. Below I'll post code showing what I've done so far. I'm very confused, very frustrated and struggling so much with this thing. Please understand, I'm not asking anyone to do my homework for me. I need help. I have some random code snippets garnered from my book and the crap power-point, but I don't understand how they work or where to put them or anything.
==============================================
INSTRUCTIONS
===========================================
MY HTML PAGE
<!--Indicates page is HTML5 compliant-->
<!DOCTYPE html>
<html>
<head>
<title>Find Zipcode Data</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<main>
<h1>Zip code data by city and state</h1>
<form action="Display_data.php" method="post">
<!-- User input labels -->
<div id="data">
<label>City:</label>
<input type="text" name="city"><br>
<label>State (two letter abbreviation):</label>
<input type="text" name="state"><br>
</div>
<!-- Submit and Reset buttons -->
<div id="buttons">
<label> </label>
<input type="submit" value="Display Zipcodes"><br>
<input type="reset" value="Clear Form"><br>
</div>
</form>
</main>
</body>
</html>
==============================================
MY CSS PAGE
/*CSS for general page look*/
main {
width: 450px;
height: 190px;
margin: 0 auto;
padding: 1em;
background: white;
border: 2px solid navy;
}
body {
background-color: #fff;
font-family: Arial, Helvetica, sans-serif;
text-align: center
}
/* Header */
h1 {
margin-top: 0;
color: navy;
}
/*CSS for label positioning*/
label {
width: 12em;
float: left;
padding-bottom: .5em;
}
/*corresponds to div tag on index.html to adjust spacing around textboxes*/
#data input {
float: left;
width: 11em;
margin-bottom: .5em;
}
/*corresponds to div tag on index.html to adjust spacing below buttons*/
#buttons input {
margin-bottom: .5em;
left:50%;
}
===========================================
MY DISPLAY_DATA.PHP PAGE
<?php
$city = $_POST['city'];
$state = $_POST['state'];
$dsn = 'mysql:host=localhost;dbname=zipcodedata';
$username = 'mgs_user';
$password = 'pa55word';
try {
$db = new PDO($dsn, $username, $password);
echo '<p>You are connected to the database!</p>';
} catch (PDOException $e) {
$error_message = $e->getMessage();
echo "<p>An error occurred while connecting to
the database: $error_message </p>";
exit();
}
// creates PDO object
$db = new PDO($dsn, $username, $password);
?>
<!--Indicates page is HTML5 compliant-->
<!DOCTYPE html>
<html>
<head>
<!--Titles the page at the top such as on the browser tab (w/ Chrome)-->
<title>Display zip code data</title>
<!--Pulls the CSS styling from the main.css page-->
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<main>
</main>
</body>
</html>
======================================
MY DATABASE_ERROR.PHP PAGE
<?php
?>
<!--Indicates page is HTML5 compliant-->
<!DOCTYPE html>
<html>
<head>
<!--Titles the page at the top such as on the browser tab (w/ Chrome)-->
<title>Error</title>
<!--Pulls the CSS styling from the main.css page-->
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<main>
</main>
</body>
</html>
=================================================
RANDOM CODE SNIPPETS
I don't know how to use this. I see that there's things like $query which I'm guessing is a variable because it has a $ in front of it, but does it need to be declared somewhere? How would I go about doing that? What kind of stuff needs to be attached to it?
$query = 'SELECT * FROM products
WHERE categoryID = :category_id';
$statement = $db->prepare($query);
$statement->bindValue(':category_id', $category_id);
$statement->execute();
In the instructions it says that I need to Preparing, binding values and executing the query, but I have no clue what that means. Also we were told that part also needs to be in a try catch statement. As far as I can tell the one try catch thing I have in my code so far at least makes the "Database is Connected" string appear when I run the HTML code and click the submit button.
This is another code snipped I got from the powerpoint. Again I have no idea how most of it works. I know that foreach is a loop, but I don't know what the stuff in the parenthesis is. I guess it's aruments, but beyond that I don't understand what ________ as _______ is supposed to actually do. In Java I absolutely HATE for loops. I prefer to use do until or do while. For loops just don't seem to click very well for me. And somehow I'm supposed to use POST to get the data. I vaguely understand POST. In my understanding, it takes the name associated with an input box on the html form and whatever stuff is typed into it and stuffs it into a variable on the php page by putting the same name inside single quotes and setting it equal to a variable using POST. Ok, but beyond that I don't know what do do with that to make whatever the user types into the textbox actually work to retrieve data from a database and display it in a table.
<?php foreach ($products as $product) : ?>
<tr>
<td><?php echo $product['productCode']; ?></td>
<td><?php echo $product['productName']; ?></td>
<td><?php echo $product['listPrice']; ?></td>
</tr>
<?php endforeach; ?>
Please go easy on me. It's my 4th week in this hellish class. So far the little bit of code I've managed to gather up has taken me about 14 hours thus far and I don't even know if any of it is right.
=============================================
Rather than actually show us how to code, the prof simply pulled up MS Paint and created pictures to show us what she wants this thing to look like. If I could post photos on here I would at the very least share them so people can have an idea of the goal.
The "Good Data" Picture she drew has a table kinds like this:
Zipcode data for Memphis, TN
=================================================
| ZIPCODE | LATITUDE | LONGITUDE | EST. POPULATION|
| 37501 | 35.12 | -89.97 | 15,517 |
| 38104 | 35.12 | -89.97 | 17,217 |
| 38016 | 35.12 | -89.97 | 10,799 |
| 38109 | 35.12 | -89.97 | 12,867 |
The error picture she drew looks kinda like this:
DATABASE ERROR
System message: SQLSTATE[HY000] [1045] Access Denied for user 'mgs_user@localhost' (using password: YES)
There was an error connecting to the Database.
Please try back later.
The input page:
And the HTML + CSS I posted shows how the input page should look. But I have no clue how to make stuff that a person types in to a textbox actually get data from a database and make it populate a table with the results. That's all we got, was premade instructions, premade powerpoints and MS Paint pictures. I've been trying to make sense of this with my textbook, but most of it seems to be written from the perspective that the reader already has a base understanding of what this stuff actually is. PDO is PHP Data Object, but beyond a textbook definition I don't know what that actually means. So many issues coming into play that's making this miserably difficult. I didn't even want to take a PHP course much less with this lady, but I had no choice.