Re: Getting a (non-radio) button's index number from array



shimmyshack wrote:
On Apr 19, 5:28 am, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:
sgotte...@xxxxxxxxx wrote:
Hello,
If you could assist me with the following situation, I would be very
grateful.
I have a table of data retrieved from database displayed on screen.
To each row of data, I have added action buttons, such as "Edit",
"Add", and "Comment". Since I do not know how many rows of data will
be retrieved - and therefore how many buttons I need - I am using
button arrays for each button, like so:
echo "<input type=\"submit\" value=\"Comment\" name=\"Comment[]\" />";
In the php file that processes the input from this form, I have the
following code, which I was under the impression would give me the
index in the Comment array of the button that was fired.
if (isset($_POST['CommentMedicalHistory']))
{
$indexOfComment = each($_POST['CommentMedicalHistory']);
echo "index = {$indexOfComment['key']}";
}
Unfortunately, it is returning 0 as the index all the time, even when
I do not click on the Comment button in the first row.
Do you know by any chance how I could get the correct index of the
button that was pressed from the array?
Thank you, once again, for any assistance that you can provide,
Simon Gottesman
Simon,

Since you can only press one button to submit your form, you will only
get one button back - and it's index will always be zero. In the case
of a submit button, the brackets in 'name="Comment[]"' are superfluous -
you can't get back more than one button.

What you want is to get the id of the row (you do have a unique ID for
each row, right?) and use it in your name, i.e.

" ... name=\"comment[$id]\" ... "

Now you can get the $_POST['comment'] array and check your key to get
the id of the row.

No javascript or DOM needed.

Of course, there are other ways, but I like this one.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@xxxxxxxxxxxxx
==================

i suppose it could be added that the [] technique /can/ be very useful
if you are making many comments at once, which is what I assumed.
another eg. When you are /submitting/ a lot of inputs each with unique
values which taken together mean something, for instance a series of
ordered triples, like 3D coordinates.
As Jerry points out, you don't need js if you print out the full DOM
in markup, you are advised to use paging with a LIMIT BY clause in
your sql, to ensure things don't grow unmanageably as the data in the
table grows.


He doesn't need the full DOM at all. Just use the row id as the index to the array.

There needs to be some unique way to identify the row in the database. Otherwise you can get the wrong row, i.e. if the database has changed as you noted.

Whether it's an autoincrement or other type of column, that identifier can be used as the index to the array. Then it's easy to retrieve the correct row all the time.

And I agree - he needs to limit the amount of data returned now, not later.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.



Relevant Pages

  • Re: Getting a (non-radio) buttons index number from array
    ... index in the Comment array of the button that was fired. ... you don't need js if you print out the full DOM ... There needs to be some unique way to identify the row in the database. ... Then it's easy to retrieve the ...
    (comp.lang.php)
  • Re: Getting a (non-radio) buttons index number from array
    ... index in the Comment array of the button that was fired. ... Jerry Stuckle ... There needs to be some unique way to identify the row in the database. ... Then it's easy to retrieve the ...
    (comp.lang.php)
  • Re: Std Class Object to Array
    ... I'm moving database data from one server to another, in the process I'm changing field names and some other bits. ... So, I thought I would json_encode the data set, retrieve it, then json_decode it and iterate through the data one row at a time and insert. ... I thought I was pushing on an array, but apparently that is an object, which mystifies me because at that point I can retrieve fields like an array. ... I'm using MySQL, but the utility is for a MySQL to Access connector. ...
    (comp.lang.php)
  • Re: Simple multiuser java database
    ... > store and retrieve data. ... I am currently using an array for ... > configuring and managing a SQL database but need to distribute this ... > classs to multiple computers. ...
    (comp.lang.java.programmer)
  • Re: Advice about fetching user information
    ... Jerry Stuckle wrote: ... Looks like you need as SQL database. ... But there comes a time where that is no longer feasible, and it's time to start using a database of some sort - that's what they were made for. ... The same is pretty much true for searching an array in PHP - small arrays are faster, but as the array grows, the difference becomes less. ...
    (comp.lang.php)