I'm trying to learn PHP and have created exercises for myself based on the information in the book I'm reading.
So I begin to create a simple form: You select from a drop down list a year and - after I've created the "results" page - a results page loads and has retrieved and outputted a bunch of info on the Heisman winner from that particular year from a MySQL db. After I wrote the code, the form page worked perfectly. Here's the code:
<?php
$user = "SKIP";
$host = "SKIP";
$password = "SKIP";
$database = "Heisman";
$connection =mysqli_connect($host, $user, $password) or die ("Cannot connect to MySQL");
$db = mysqli_select_db($connection, $database) or die ("Cannot connect to that particular database. ");
$query = "SELECT year FROM Heismantable";
$result = mysqli_query($connection, $query) or die ("Couldn't execute query.");
$nrows = mysqli_num_rows($result);
echo "<form action='heismanoutput.php' method='POST'>\n
<select name='HeismanYear'> \n";
for ( $i=0; $i<$nrows; $i++ )
{
$row = mysqli_fetch_array($result);
extract ($row);
echo "<option value=$year>$year";
}
echo "</select> <input type='submit' align='center' value='Select Heisman Year'>
</form>";
?>[/B]
The problem is that I don't understand how the FOR LOOP works. How does it know to move on to the next row? In my drop down box are the years 1935 thru 2005. So somehow each row was selected and outputted. The $i++ variable is not included or recognized (as far as I know) by any of the statements within the loop. I've done something right, but at the same time I don't know what. Can someone explain? Thanks. 🙂