Re: 2d array or?
| From: | Daniel Convissor | Date: | Wed, 18 Oct 2000 01:52:22 +0000 |
| Subject: | Re: 2d array or? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-20767@lists.php.net to get a copy of this message | ||
Hey Ajdin:
> Here is my problem. When a user goes to "check-out" I list all items
> they have selected (from the shopping bascet) and puts them in a form.
> Now part of the form is quantity field which can be amended by user on
> the form. How do I assign quantity entered to a particular item.
> I have seen here on the list something like
> echo"<td bgcolor=CCFFFF><input type=text name=qty[] value=1 size=2
> maxlenght=2></td>"; but "name=qty[]"; doesn't work and I am not
> sure of
> the exact sintax. Would a solution to this be to create 2d array??
> ie. key,value1,value2 where key is the position within the array, value1
> is product_id an value2 would be qty. Is this a 2d array????
You're on the right track, but missing the correct station. Your present
code only uses an enumerated array. How will that relate back to a partuclar
item? As you've found out, it doesn't. Thus, you can't know how many items
to ship to a person. But, going to a two dimensional array using Product ID
as the second key still leaves the first key as an enumerated array. The
answer is to make qty[] an associative array using the product ID as the
index, which I've done in my example below.
Before I get into that, please note, I modifid your code in the following
ways:
1) Turned do while into a straight while. Perhaps it's a taste thing, but I
find them easier to read.
2) Ripped out the background color for the cells. Setting font and table
element colors creates problems for people trying to read your web pages.
Check out http://www.analysisandsolutions.com/code/weberror.htm
for more info
on this matter.
3) Dropped the array to variable reasignment
($albumname=$myrow["albumname"];). I prefer to refer directly to the array.
Saves a step and memory.
4) Added quote marks around the form element descriptor values.
5) Added MYSQL_ASSOC to the fetch_array().
6) Corrected spelling of maxlenght to maxlength
So, here we go.....
> .... snip ...
>
> # $basket is a session and array/user shopping basket
>
> while (list($key,$value) = each($basket)) {
> $result = mysql_query("SELECT * FROM albums,band WHERE
> albums.id=$value",$db) or die("Bad query on select BAND/ALBUMS:
> ".mysql_error());
> if ($myrow = @mysql_fetch_array($result))
> {
while ( $myrow = mysql_fetch_array($result, MYSQL_ASSOC) ) {
echo "<tr><td>" . $myrow["bandname"] . "</td>";
echo "<td>" . $myrow["albumname"] . "</td>";
echo "<td><input type=\"text\" size=\"2\"
maxlength=\"2\"";
echo " name=\"qty['" . $myrow["albumid"] . "']
value=\"1\" /></td>";
echo "<td>$price</td></tr>";
}
> }
> ... snip ...
By the way, my SQL Solution class has a function named RecordSetAsInput()
which does all of that automatically:
http://www.analysisandsolutions.com/code/sql/sql-man.htm#RecordSetAsInput
Check it out.
Enjoy,
--Dan
--
PHP scripts that make your job easier
http://www.analysisandsolutions.com/code/
SQL Solution | Layout Solution | Form Solution
T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y
4015 7 Ave, Brooklyn NY 11232 v: 718-854-0335 f: 718-854-0409