Jump to content
Larry Ullman's Book Forums

Add Specific Coffees Form


Recommended Posts

At the bottom of the code shown below (from page 328 of the book), which creates the form for adding a specific coffee, Larry uses $row[0] to create an option value and then $row[1].

 

I'm assumign that $row[0] = id from the query, and $row[1] equals category from the query defined a few lines above.

 

If that's correct, are these values visible somewhere in the form? what do they do? I've attached an image below of the General Coffee Table form but don't see where the $row[0] and $row[1] would be represented...

 

sform.jpg

 

 

<h3>Add Specific Coffees</h3>

<form action="add_specific_coffees.php" method="post" accept-charset="utf-8">

<fieldset><legend>Fill out the form to add specific coffee products to the site.</legend>

	<div class="field"><label for="category"><strong>General Coffee Type</strong></label><br />
	<select name="category"><option>Select One</option>
	<?php // Retrieve all the categories and add to the pull-down menu:
	$q = 'SELECT id, category FROM general_coffees ORDER BY category ASC';		
	$r = mysqli_query ($dbc, $q);
		while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) {
			echo "<option value=\"$row[0]\">$row[1]</option>\n";
		}
	?>

Link to comment
Share on other sites

$row is an array.

 

Looking at the code you copied in it looks like an array is being created with two elements or place holders that contain the row number and contents of that table column in your image. Then it is echo'd out in the form in a loop to bring out all the rows for that form.

 

First loop it brings out "Dark Roast" and I assume '1' for the item number then second loop it brings out "Kona" and I assume '2' for the item number... I am just going off of what you posted, I haven't looked at the book before posting.

 

 

- T

  • Upvote 1
Link to comment
Share on other sites

You didn't really answer the question I asked, as far as I can tell.

 

Assuming that $row[0] is the id from the query and $row[1] equals the category, then what do those array elements do in relation to the form. Why do they need to get repeated every loop? What do they do?

 

If Terry was trying to suggest that the $row[0] was Dark Roast and $row[1] was Kona, then I don't think you are correct.

Link to comment
Share on other sites


while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) {
        echo "<option value=\"$row[0]\">$row[1]</option>\n";
     }

 

The above is the loop that creates the drop down menu for the category.

 

$row[0] is category id

$row[1] is category title

 

What it is doing is grabbing each row for the category and echoing the individual row info on each loop through.

 

So like I said above on the first loop through $row[0] would be 1 then next would be 2 then three OR whatever the actual data is in the database table columns. It could be 54, 55, 56 or whatever, doesn't matter, whatever the next value is it is displayed.

 

$row[1] on first loop is Dark Roast, then second it is Kona then third it is Original Blend.

 

This line

 


echo "<option value=\"$row[0]\">$row[1]</option>\n";

 

Is the options for the drop down menu and it is being dynamically populated with the data from the database. The data is inserted from an array that is populated by the WHILE statement. Each loop through $row[0] and $row[1] have different data in it.

 

 

 

You didn't really answer the question I asked, as far as I can tell.

 

Assuming that $row[0] is the id from the query and $row[1] equals the category, then what do those array elements do in relation to the form. Why do they need to get repeated every loop? What do they do?

 

If Terry was trying to suggest that the $row[0] was Dark Roast and $row[1] was Kona, then I don't think you are correct.

  • Upvote 1
Link to comment
Share on other sites

LOOP 1

 

$row[0] == 1

$row[1] == Dark Roast

 

<option value="1">Dark Roast</option>

 

 

Loop 2

 

$row[0] == 2

$row[1] == Kona

 

<option value="2">Kona</option>

 

 

Loop 3

 

$row[0] == 3

$row[1] == Original Blend

 

<option value="3">Original Blend</option>

 

 

When it is finished you have a drop down menu with THREE options

 

Dark Roast

Kona

Original Blend

 

 

Does this make sense now?

  • Upvote 1
Link to comment
Share on other sites

Now if you wanted to test and see if I am correct You could do something like this:

 

 


$i = 0;
while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) {
        $i++;
        echo "Loop: ".$i."<br />row[0] = ".$row[0]."<br />row[1] = ".$row[1]."<br />----<br /><br />"; 
        echo "<option value=\"$row[0]\">$row[1]</option>\n";
     }


 

 

This way you can watch as the information is taken out of the database and inserted into the array and see what is there.

 

EDIT: actually the database has already been queried at this point and the WHILE is taking the array $r and extracting individual rows and inserting into a NEW array called $row which consists of 2 elements id and category which are placed individually into $row's FIRST element which is $row[0] and SECOND element which is $row[1].

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...