473,320 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Multi dimensional array

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>
Jul 17 '05 #1
3 6477
JDS
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 | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 17 '05 #2
> 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
Jul 17 '05 #3
JDS
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 | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
5
by: Cant Think Today | last post by:
I have multi-dimesional arrays that can be specifed by the user, e.g 1,2,3,4,5 1,2,3,4,5,6,7,8,9,10 1,2,3,4,5,6 I think a bit of code that will iterate over these arrays to print out the...
1
by: Matthew Jakeman | last post by:
How can i create a multi dimensional array of "strings"? I've tried char * ; no joy Cheers for any help Matt
4
by: Robert P. | last post by:
I can easily store a one-dimensional array in viewstate ( see Test1 ) If I try storing a multi-dimensional array in the viewstate it's crapping out on me when it goes to serialize the array (not...
3
by: SQLScott | last post by:
I have looked all over and I cannot find an example or information on passing a multi-dimensional array. Well, that is not true. I found a close example in C++ but it didn't work when I...
4
by: entitledX | last post by:
Hi, I'm trying to use the HDF library to read a few HDF files that I need to process. The data in each file varies in rows, but the columns remain constant. Because of that, I had dynamically...
4
by: Balaskas Evaggelos | last post by:
Hi, does anyone know how i can sort a multi-dimensional array by a specific field ? for example i want to sort arr where n=2, but i need the data of every array to follow that order. ...
11
by: gianluca | last post by:
Hy list, I've to declare a 3D matrix in C . I tried with that code: double ***mat; mat=(double***)G_malloc(ndimension*(sizeof(double)); but the program send me a run time error.
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
4
by: =?Utf-8?B?SGVucmlrIFNjaG1pZA==?= | last post by:
Hi, consider the attached code. Serializing the multi-dimensional array takes about 36s vs. 0.36s for the single-dimensional array. Initializing the multi-dimensional array takes about 4s...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.