SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Non-Member
    Join Date
    May 2012
    Location
    CA, Irvine
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to store count of all the answers in an array.

    I am working on a PHP,duing survey,where i have some questions and their options to be choosen.I want to 'count' the number of answers in each question,and store them in an array,so that i can compare the count of answers of each question,to find the minimum count .

  2. #2
    SitePoint Enthusiast OMGCarlos's Avatar
    Join Date
    Apr 2012
    Location
    Boston, MA
    Posts
    91
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You'll want to POST/GET the answers as an array. To do that, you need to name your input fields in this way:
    HTML Code:
    <input name="ans[]" type="number">
    <input name="ans[]" type="number">
    <input name="ans[]" type="number">
    <input name="ans[]" type="number">
    <input name="ans[]" type="number">
    The "[]" defines an array in HTML.

    Then on the PHP side, to get the number of 'ans' checked off, do this:
    PHP Code:
    if(isset($_POST['ans'])){
      
    $count count($_POST['ans']);

    To count all the answers:
    PHP Code:
    if(isset($_POST['ans'])){
      
    $count count($_POST['ans']);
      
    $total 0;
      foreach(
    $_POST['ans'] as $val){
        
    $total += $val;
      }



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •