Counting field and update database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpnewbie112
    New Member
    • Nov 2007
    • 1

    Counting field and update database

    Hi everyone, I am new to php and I was trying hard to alter an existing script ut I didn't succeed to...

    I have 2 databases:
    - Categories (catid, shortname, description)
    - Transactions (date, amount, balance, user)

    in the categories I have 9 categories.

    the script displays the categories as checkboxes to choose from. I want to add the following functionality:

    If I select for example 3 categories, I would like to know how can I count the selected checkboxes and based on the count update the transactions database:

    in another way:
    if count=3 => add amount x 3 to the transactions database so if a user select 4 categories I update the transaction database with $5 x 4 categories and then we will get a balance of $20 for the choosen categories.

    I have tried to use:

    $checked = count($_POST['category']);

    but with this one, if I already have 2 selected then choose another two then the database will update 4 cats rather then the new added 2.

    thanks in advance
  • assimlation
    New Member
    • Nov 2007
    • 9

    #2
    you can get a list/count of selected checkboxes like this:

    [code=php]
    <?php

    $categories = $_POST['categories'];

    if (isset($categor ies))
    {
    foreach ($categories as $value)
    {
    echo "$value<br> ";
    }

    echo "<Br><BR>Items: ";
    echo sizeof($categor ies);
    }
    else
    {
    echo ("<form method=post>
    <input type='checkbox' name='categorie s[]' value='Comedy'> Comedy<BR>
    <input type='checkbox' name='categorie s[]' value='Horror'> Horror<BR>
    <input type='checkbox' name='categorie s[]' value='Action'> Action<BR>
    <input type='checkbox' name='categorie s[]' value='Musical' >Musical<BR>
    <input type='Submit' value='Select'> ");
    }

    ?>
    [/code]

    Comment

    Working...