This is a discussion on Multi dimensional array within the PHP Language forums, part of the PHP Programming Forums category; Hi, I want to go create a table with a textfield where the user can enter an amount for a ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi, I want to go create a table with a textfield where the user can
enter an amount for a certain dynamically created article coming from the database. Later, I want to calculate the whole price and so on. It works nicely to create my multi dimensional array, however, it does not work to save the values coming from the form to calculate the price. What's wrong??? <form name="shop" method="post" action="index.php"> $articleTable = array(); while ($row = mysql_fetch_array($result)) { $articleNr= $row["articleNr"]; $name= $row["name"]; $picName= $row["picName"]; $price = $row["price"]; $amount=""; $articleRow = array( "articleNr" => $articleNr, "name" => $name, "picName" => $bildName, "price " => $price , "amount" => $amount ); array_push($articleTable , $articleRow ); } foreach($articleTable as $article) { ?> <input type="text" name="article[amount]" size="2" maxlength="2" value="<? $article[amount] ?>"></td> } <? } ?> </form> |
|
|||
|
On Tue, 15 Feb 2005 04:19:45 -0800, Stefan Richter wrote:
> What's wrong??? Well, for one, you are writing way too much code for this. simplify! Like, why are you taking all the data from the database, putting it in a new array, and after that, looping through the new array to print out form fields? You can do it all in one step! (Actually, I found the real problem -- you are not printing anything out in the value="" part of the text field. You need either "echo ..." or <?= ... ?> But here is my *untested* simplification of your code: <form name="shop" method="post" action="index.php"> while ($row = mysql_fetch_array($result)) { echo "<input type='text' name='article[amount]' size='2' maxlength='2' value='${row['amount']}'></td>\n"; } </form> Plus, there are other problems with your original code. What is "$article" getting set to?? $article = "" means it is getting set to nothing. How are you going to have a value print out that is nothing? later... -- JDS | jeffrey@example.invalid | http://www.newtnotes.com DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/ |
|
|||
|
> Well, for one, you are writing way too much code for this.
> > simplify! > > Like, why are you taking all the data from the database, putting it in a > new array, and after that, looping through the new array to print out form > fields? You can do it all in one step! I guessed that, but so far I didnt know how, so I thought SOME solution is better then none. Your example definitely helps, but there is still the main point - Your code example will result in at least 2 different arrays - One for the regular values, one for the amount. I set the amount to "" because it is not coming from the database. The database just sends the details of the articles. Then the user types in the amount he wants to order from that article. I got working code where I have the data from the database in one array, and the amount in another, but then I have to loop through them and have to assume that index 0 in the one array fits to index 0 in the other array - if I had all in one array, I wouldn't have that problem.. Do you get what I mean? Thanks, Stefan |
|
|||
|
On Tue, 15 Feb 2005 12:07:30 -0800, Stefan Richter wrote:
> I guessed that, but so far I didnt know how, so I thought SOME > solution is better then none. > > Your example definitely helps, but there is still the main point - > > Your code example will result in at least 2 different arrays - > One for the regular values, one for the amount. > > I set the amount to "" because it is not coming from the database. > The database just sends the details of the articles. > > Then the user types in the amount he wants to order from that article. > > I got working code where I have the data from the database in one > array, > and the amount in another, but then I have to loop through them and > have to assume that index 0 in the one array fits to index 0 in the > other array - > if I had all in one array, I wouldn't have that problem.. > > Do you get what I mean? > > Thanks, No, I don't get what you mean. If you mean to set the value inside the <input> field to blank (""), then why did you put anything inside the value? See your original post: foreach($articleTable as $article) { ?> <input type="text" name="article[amount]" size="2" maxlength="2" value="<? $article[amount] ?>"></td> } What are you trying to do here? I only say the value is getting set to "" because the PHP statment <? $article[amount] ?> is not doing anything!! I still really don't get what you are trying to do. My code example does not result in two different arrays! My example does not result in any arrays -- it just prints out stuff. Unless you mean the naming of the <input> tag which will result in an array being created within whatever PHP script receives the data. Ultimately I think you have to rethink your problem a little, and learn how to create and use multi-dimensional arrays. Hint: Use two for() or foreach() loops -- one inside the other! I am trying to be helpful, really, and not trying to be a prick. But I simply do not see what you are trying to do. later... -- JDS | jeffrey@example.invalid | http://www.newtnotes.com DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/ |