Webmaster Forum

Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
Thread Tools Display Modes
Share |
  #1  
Old 09-14-2004, 07:53 AM
spl1nter spl1nter is offline
Junior Member
 
Join Date: 03-09-04
Posts: 19
iTrader: 0 / 0%
Adding 1 to array?

In one of my scripts I have this code:

PHP Code:
$set_num 0;
while ( 
$answers[$set_num] != "" )
{
        if ( 
$answers[$set_num] == $actual_answer )
        {
             
$results[$set_num]++;
        }
        
$set_num++;
}
unset(
$set_num); 
Currently $answers[] contains 3 values but this may change depending on the results it pulls from the database earlier in the script.
As an example (because I'm not very good at explaining ):

$answers[0] = Yes;
$answers[1] = No;
$answers[2] = Don't know;

$results[0] = 2;
$results[1] = 7;
$results[2] = 3;

$actual_answer contains the data submitted by the user. Basically I want 1 to be added to one of the $results[] when the corresponding $answer[] is submitted. This script works for $result[0] and $result[1] but when the user selects the answer corresponding to $answers[2], $results[2] does not get 1 added to it's value.

Why does this happen and how can I fix it?
 
Reply With Quote

Advertisement

Advertisement

  #2  
Old 09-14-2004, 04:15 PM
LazyJim's Avatar
LazyJim LazyJim is offline
v7n Mentor
 
Join Date: 10-13-03
Location: UK
Posts: 2,469
iTrader: 0 / 0%
does result[2] definately contain a number?

Last edited by LazyJim; 09-14-2004 at 04:20 PM.
 
Reply With Quote
  #3  
Old 09-14-2004, 06:09 PM
imaginemn's Avatar
imaginemn imaginemn is offline
v7n Mentor
 
Join Date: 02-18-04
Location: Brownsville, Texas
Posts: 1,354
iTrader: 0 / 0%
Try:

Code:
$set_num = 0; while ( $answers[$set_num] != "" ) { if ( $set_num == $actual_answer ) { $new_value = $results[$set_num]+1; } $set_num++; } unset($set_num);
imaginemn
 
Reply With Quote
  #4  
Old 09-14-2004, 07:02 PM
littleFella's Avatar
littleFella littleFella is offline
Senior Member
 
Join Date: 06-20-04
Location: Ontario
Posts: 1,756
iTrader: 0 / 0%
yeah, that seems strange, although it happens in more than one language that an increment function won't kick in for some reason. I admit I''ve been looking at this and couldn't find a problem in code.

It almost looks as if the WHILE condition is not always seeing the right values so the loop breaks prematurely. If so then another approach could be the use of a FOR loop instead. At least you force the code to iterate a predefined number of times. The trick is that you may need to dynamically establish a number of iterations before jumping into the loop.
 
Reply With Quote
  #5  
Old 09-14-2004, 07:22 PM
imaginemn's Avatar
imaginemn imaginemn is offline
v7n Mentor
 
Join Date: 02-18-04
Location: Brownsville, Texas
Posts: 1,354
iTrader: 0 / 0%
This one is a little tough because I do not know if $actual_answer is a number or a string. If it's a string I would probably use the strcmp(str1, str2) command instead.

In reality there is nothing wrong with the code that was submitted. I guess we would need to see the rest of the code because the function does work. I added a print command to see what was being displayed and in this example the first output is 3 and the second is 4.

Code:
$actual_answer = "Don't know"; $answers[0] = "Yes"; $answers[1] = "No"; $answers[2] = "Don't know"; $results[0] = "2"; $results[1] = "7"; $results[2] = "3"; $set_num = 0; while ( $answers[$set_num] != "" ) { if ( $answers[$set_num] == $actual_answer ) { print $results[$set_num]; print "<BR>"; $results[$set_num]++; print $results[$set_num]; print "<BR>"; } $set_num++; } unset($set_num);
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
associative array vs object I like pie Coding Forum 6 01-31-2007 07:56 PM
array weirdness in php I like pie Coding Forum 7 01-27-2007 12:36 AM
imploding array keys I like pie Coding Forum 7 12-28-2006 05:55 PM
quick array question I like pie Coding Forum 1 12-02-2006 10:05 PM
SELECT * FROM table WHERE x='$array' hatchet Coding Forum 10 09-19-2004 08:02 AM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 09:11 PM.
Powered by vBulletin
Copyright 2000-2014 Jelsoft Enterprises Limited.
Copyright © 2003 - 2018 VIX-WomensForum LLC