Thread: PHP count all radio buttons' values?

Page 2 of 5 FirstFirst 1234 ... LastLast
Results 11 to 20 of 42
  1. #11  
    Registered Member
    Anthony`'s Avatar
    Join Date
    Sep 2008
    Age
    29
    Posts
    763
    Thanks given
    75
    Thanks received
    164
    Rep Power
    204
    Quote Originally Posted by Miko View Post
    No they aren't. There is one question with 'yes' or 'no' answer choice. Multiply that with 30.

    But hmm.. I just can't picture it for some reason. Instead of printing the results, how could you add the points? Every 'yes' answer is +1 point. So how can you calculate the points with that method of yours?
    Yes, which is what I had except there was two of them... not thirty. You would have to get the results of each question array. I think instead of something like q1[] and q2[] you can probably do something like q[1][], q[2][], etc... So that you have a 2-dimensional array containing every choice for every question. This way you can easily iterate over each question, adding up the points if they selected 'yes'.
    Reply With Quote  
     

  2. #12  
    Donator

    ProFiles's Avatar
    Join Date
    May 2011
    Posts
    1,673
    Thanks given
    4
    Thanks received
    240
    Rep Power
    118
    Quote Originally Posted by Anthony` View Post
    Yes, which is what I had except there was two of them... not thirty. You would have to get the results of each question array. I think instead of something like q1[] and q2[] you can probably do something like q[1][], q[2][], etc... So that you have a 2-dimensional array containing every choice for every question. This way you can easily iterate over each question, adding up the points if they selected 'yes'.
    Oh, I see. I haven't done much with arrays so I don't know how to make all that happen.
    Reply With Quote  
     

  3. #13  
    Registered Member
    Join Date
    May 2013
    Posts
    276
    Thanks given
    31
    Thanks received
    28
    Rep Power
    11
    So i wrote a quick script, this script will work if you name each radio button a name of a question, as stated above eg radio name="q1[yes]" value="1", if you do not decide to do this the code will need to be modified.

    Code:
    	$y = 0; // variable containing yes to questions
    	$n = 0; // variable containing no to questions
    	foreach($_POST as $u => $i)
    	{
    		if(strpos($u, 'q') === 0) // checks all inputs sent through post in which their name starts with q
    		{
    			$y+= $i; // Adds 1 to yes variable if the radio is equal to yes
    			$n+= $i == 0 ? 1 : 0; // Adds 1 to no variable if the radio is equal to no
    		}
    	}
    Also note the code is not perfect i literally wrote it in under 5 minutes, so there are obviously better methods to do this, and if you were more clear on what you are in need of i possibly could have made it more effective.
    Reply With Quote  
     

  4. #14  
    Donator

    ProFiles's Avatar
    Join Date
    May 2011
    Posts
    1,673
    Thanks given
    4
    Thanks received
    240
    Rep Power
    118
    Quote Originally Posted by EMPIRIOUS View Post
    So i wrote a quick script, this script will work if you name each radio button a name of a question, as stated above eg radio name="q1[yes]" value="1", if you do not decide to do this the code will need to be modified.

    Code:
    	$y = 0; // variable containing yes to questions
    	$n = 0; // variable containing no to questions
    	foreach($_POST as $u => $i)
    	{
    		if(strpos($u, 'q') === 0) // checks all inputs sent through post in which their name starts with q
    		{
    			$y+= $i; // Adds 1 to yes variable if the radio is equal to yes
    			$n+= $i == 0 ? 1 : 0; // Adds 1 to no variable if the radio is equal to no
    		}
    	}
    Also note the code is not perfect i literally wrote it in under 5 minutes, so there are obviously better methods to do this, and if you were more clear on what you are in need of i possibly could have made it more effective.
    Unsupported operand types - error.. I'm terrible at errors so I have no idea how to fix this.

    Code:
    	if (isset($_POST['submitti'])) {
    	
    	$y = 0; // variable containing yes to questions
    	$n = 0; // variable containing no to questions
    	foreach($_POST as $u => $i)
    	{
    		if(strpos($u, 'q') === 0) 
    		{
    			$y+= $i; // it's pointing at this line.
    			$n+= $i == 0 ? 1 : 0; 
    		}
    	}
    Reply With Quote  
     

  5. #15  
    Registered Member
    Join Date
    May 2013
    Posts
    276
    Thanks given
    31
    Thanks received
    28
    Rep Power
    11
    Which line was the error on?

    try replacing the corresponding line of code with this

    Code:
          $n+= $i == 0 ? 1 : 0; // Adds 1 to no variable if the radio is equal to no
    Code:
          if($i == 0) $n += 1; // Adds 1 to no variable if the radio is equal to no
    Reply With Quote  
     

  6. #16  
    Donator

    ProFiles's Avatar
    Join Date
    May 2011
    Posts
    1,673
    Thanks given
    4
    Thanks received
    240
    Rep Power
    118
    Quote Originally Posted by EMPIRIOUS View Post
    Which line was the error on?
    the one I commented "it's pointing at this line".
    Reply With Quote  
     

  7. #17  
    Registered Member
    Join Date
    May 2013
    Posts
    276
    Thanks given
    31
    Thanks received
    28
    Rep Power
    11
    Try adding a space after it so it would be like $y += $i; or try this alternate code which should definitely work

    Code:
    $y == $y + $i; // Adds 1 to yes variable if the radio is equal to yes
    $n == $n + $i == 0 ? 1 : 0; // Adds 1 to no variable if the radio is equal to no
    Reply With Quote  
     

  8. #18  
    Donator

    ProFiles's Avatar
    Join Date
    May 2011
    Posts
    1,673
    Thanks given
    4
    Thanks received
    240
    Rep Power
    118
    Quote Originally Posted by EMPIRIOUS View Post
    Try adding a space after it so it would be like $y += $i; or try this alternate code which should definitely work

    Code:
    $y == $y + $i; // Adds 1 to yes variable if the radio is equal to yes
    $n == $n + $i == 0 ? 1 : 0; // Adds 1 to no variable if the radio is equal to no
    syntax error, unexpected '==' (T_IS_EQUAL)


    And for the previous one where I add a space:

    Unsupported operand types on line:

    Code:
    $y += $i;
    Reply With Quote  
     

  9. #19  
    Registered Member
    Join Date
    May 2013
    Posts
    276
    Thanks given
    31
    Thanks received
    28
    Rep Power
    11
    Haha, can't believe i made that mistake, sorry, the 2 equals should only be one, so just backspace one on each line herp.
    Reply With Quote  
     

  10. #20  
    Donator

    ProFiles's Avatar
    Join Date
    May 2011
    Posts
    1,673
    Thanks given
    4
    Thanks received
    240
    Rep Power
    118
    Quote Originally Posted by EMPIRIOUS View Post
    Haha, can't believe i made that mistake, sorry, the 2 equals should only be one, so just backspace one on each line herp.
    Lol. Well it still gives the same error D:
    Reply With Quote  
     

Page 2 of 5 FirstFirst 1234 ... LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Change content of dropdown list depending on radio buttons
    By Aekramer in forum Application Development
    Replies: 3
    Last Post: 01-22-2013, 02:46 AM
  2. [569]all logout button and interfaces.
    By Discardedx2 in forum Snippets
    Replies: 5
    Last Post: 11-22-2009, 02:11 AM
  3. [HTML+PHP]NEARLY all of http://www.runescape.com + Updated
    By Jammy780 in forum Application Development
    Replies: 45
    Last Post: 05-07-2009, 08:25 AM
  4. Replies: 8
    Last Post: 02-14-2008, 11:07 AM
  5. Resetting all buttons [ANTI LEECH]
    By Diablo1123 in forum Tutorials
    Replies: 15
    Last Post: 05-25-2007, 06:45 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •