Page 1 of 1

Counter on table columns

Posted: Fri Jul 20, 2007 11:44 am
by UrButtFullOfArr0ws
I have a table which i use to both enter data and display it. I use <input type="text"> in the <td> elements.
I got 10 rows, 14 columns but they are not required, so a user can enter just a column in the middle of the table on the 5th row.
Also i use UPDATE statement for the mysql queries.

I want to ask if there is a way to count the amount of rows the user enters. I've had a few ideas like the in_array() function but that didnt turn out well, or using a for statement, and an if statement... something like:

Code: Select all

for($a = 0; $a <= 9; $a++){
$rowa = $row1 . $a;           //$row1 is the name of the column and im using $a to give it an unique name so i can call it easier
$rowb = $row2 . $a;
...
...
...
If($_POST["$rowa"] == "^" OR $_POST["$rowb"] == "^" OR ...... ){  //  "^" is a wildcard i've used in mysql so i can update even if i'm supposed to use INSERT. (i've got my reasons to use UPDATE  )
$counter++;
}
}
Well something of the kind... :D
Anyone got any ideas?
And of course suggestions to my original code is greatly appreciated. :)
Thank you.

Posted: Fri Jul 20, 2007 1:03 pm
by Zoxive
UrButtFullOfArr0ws wrote:And of course suggestions to my original code is greatly appreciated.
Can you show use the original code?

So right now all your rows are named row1, row2, row3 etc to row9?
Whats rowb?

Code: Select all

$count = 0;
for($i=1;$i<=9;$i++){
  if(!empty($_POST['row' . $i])){
    $count++;
  }
}
echo $count;

// If you can change the names of the rows, i would rather use arrays because then the number of rows can change above and below 9
$total = (!empty($_POST['row']))? count($_POST['row'])-1 : 0; // - 1 because arrays start at 0
for($i=0;$i<=$total;$i++){
  if(!empty($_POST['row'][$i])){
    $count++;
  }
}
If you used arrays your inputs would look like so

Code: Select all

<input type="text" name="row[]">

Posted: Sat Jul 21, 2007 9:41 am
by UrButtFullOfArr0ws
So right now all your rows are named row1, row2, row3 etc to row9?
No. That was just an example. They each got their unique names so i can identify them fast. The 1, 2, 3 etc are only added when i enter and extract the data the user entered/requested from the database. My bad, i wanted to say the rows are named row1 + $a which would be the unique identifier.
I've also thought of an array but im in a bit of a hurry so a complete remake of the original code is post-poned
Whats rowb?

...
...
Ehm that's a bit obvious from the code :?
Ill explain anyway:
$rowa and $rowb and so on are the names i use to extract from the $_POST array having sent it in the same manner beforehand.
So i'm extracting $_POST["$rowa"] 10 times, but each time $rowa is different.
If you can change the names of the rows, i would rather use arrays because then the number of rows can change above and below 9
Can you explain a bit? I don't get it...
Actually there's one thing i haven't mentioned:
The counter is supposed to be used to limit the number of times $a is incremented so i could only send that number of rows... sorry :)
<input type="text" name="row[]">
:? :?:
if(!empty($_POST['row' . $i]))
Yes, but well, i need it for all 14 columns, and i've also thought of something like that :)
Thanks for your answer.

P.S.
Can you show use the original code?
Not really... i was refering to my idea to use "for" and "if" in my code...
I don't want to show it becouse it's back from the age when i was a real newbie - not that i'm much different now :D - but it's kind of embarrasing to show it :) It's very messy... I barely get around it myself 8)

Edit to superdezign below: Ok. But do you mind explaining how it works before i jump to :google: :D

Posted: Sat Jul 21, 2007 10:16 am
by superdezign
Using HTML arrays (<input name="name[]" />) allows you to place multiple inputs into one posted array.