counter / adder function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dyluck
Forum Commoner
Posts: 54
Joined: Thu Jun 26, 2008 1:44 pm

counter / adder function

Post by dyluck »

I need some help to build this function and then call it.
Seems easy, but for me not :-P
I have input checkboxes that contain the word "item" in the id= tag.
As the database starts to populate it's array to the table / form using a while loop,
I would like this function to count the existing "item"s and then place the number after "item" that it corrisponds to the number of "item"+1 to on the fly.
(it's for a checkbox limiter that requires sequencial numbers)

Code: Select all

function itemcount($fieldData) {
    global $itemCount;
    $itemCount++;
 
    if ($fieldData['id'] = 'item') {
        $fieldData['id'].=$itemCount;
    }
 
    //rest of script to display the field (this is where I need more help)
}
The last part is, how to i call the function?

ie. <input type="checkbox" id="item'.function(itemcount).'" name = "stream[]" onclick="setItems(this.form);">

Another option would be if someone knows how to index the number of rows pulled in a query and then echo the generated indexed row number corrisponding with the particular row being displayed in the loop.
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: counter / adder function

Post by Chalks »

you're going to want something like this (I think).

Code: Select all

<form>
<?php
  for($i=0; $i < $itemListArray.length; $i++)
    echo "<input type=\"checkbox\" id=\"$itemListArray[$i]($i)\" name=\"stream[]\" onclick=\"dostuff(this.form)\" />";
?>
</form>
This is very rough (and untested). It should give you the basic idea though. If you want to use a mysql query instead, do something like this:

Code: Select all

<?php
$i = 0;
  while($row = mysql_fetch_assoc($mysqlResource))
  {
    echo "row number is: $i";
    echo "data is: " . $row['item'];
    $i++;
  }
?>
Again, rough and untested. :)
User avatar
dyluck
Forum Commoner
Posts: 54
Joined: Thu Jun 26, 2008 1:44 pm

Re: counter / adder function

Post by dyluck »

Hi Chalks
I chose to use the later, so easy and it works perfectly!
Thanks for your help!

PS: I am praying for US and Canada too! (we are in the same boat)
http://www.godtube.com/view_video.php?v ... fc7b2c449f

Cheers brother!
Post Reply