Webmaster Forum


Go Back   Webmaster Forum > Web Development > Web Design Lobby > Coding Forum
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Coding Forum Problems with your code? Let's hear about it.

   

Reply
 
LinkBack Thread Tools Display Modes
Old 09-14-2004, 08:53 AM   #1 (permalink)
Inactive
 
Join Date: 03-09-04
Posts: 20
iTrader: 0 / 0%
Latest Blog:
None

spl1nter is liked by many
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?
spl1nter is offline  
Add Post to del.icio.us
Reply With Quote
Old 09-14-2004, 05:15 PM   #2 (permalink)
Moderator
 
LazyJim's Avatar
 
Join Date: 10-13-03
Location: UK
Posts: 2,821
iTrader: 0 / 0%
Latest Blog:
None

LazyJim is a name known to allLazyJim is a name known to allLazyJim is a name known to allLazyJim is a name known to allLazyJim is a name known to allLazyJim is a name known to allLazyJim is a name known to allLazyJim is a name known to allLazyJim is a name known to allLazyJim is a name known to allLazyJim is a name known to all
Send a message via MSN to LazyJim
does result[2] definately contain a number?
__________________

-LJ-

My advice is to look at each case individually, with an informed mind and an appropriately balanced and objective viewpoint.

Web Design and Development, Ipswich, UK.
My deviantArt

Last edited by LazyJim : 09-14-2004 at 05:20 PM.
LazyJim is offline  
Add Post to del.icio.us
Reply With Quote
Old 09-14-2004, 07:09 PM   #3 (permalink)
v7n Mentor
 
imaginemn's Avatar
 
Join Date: 02-18-04
Location: Minneapolis, Minnesota
Posts: 1,946
iTrader: 0 / 0%
Latest Blog:
None

imaginemn is a name known to allimaginemn is a name known to allimaginemn is a name known to allimaginemn is a name known to allimaginemn is a name known to allimaginemn is a name known to allimaginemn is a name known to allimaginemn is a name known to allimaginemn is a name known to allimaginemn is a name known to allimaginemn is a name known to all
Send a message via MSN to imaginemn Send a message via Yahoo to imaginemn Send a message via Skype™ to imaginemn
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
__________________
Need a project done? - Set Your Own Price!
Imagine Creative Services
- Design : Marketing : Multimedia : More
imaginemn is offline  
Add Post to del.icio.us
Reply With Quote
Old 09-14-2004, 08:02 PM   #4 (permalink)
Inactive
 
littleFella's Avatar
 
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
iTrader: 0 / 0%
Latest Blog:
None

littleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to behold
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.
littleFella is offline  
Add Post to del.icio.us
Reply With Quote
Old 09-14-2004, 08:22 PM   #5 (permalink)
v7n Mentor
 
imaginemn's Avatar
 
Join Date: 02-18-04
Location: Minneapolis, Minnesota
Posts: 1,946
iTrader: 0 / 0%
Latest Blog:
None

imaginemn is a name known to allimaginemn is a name known to allimaginemn is a name known to allimaginemn is a name known to allimaginemn is a name known to allimaginemn is a name known to allimaginemn is a name known to allimaginemn is a name known to allimaginemn is a name known to allimaginemn is a name known to allimaginemn is a name known to all
Send a message via MSN to imaginemn Send a message via Yahoo to imaginemn Send a message via Skype™ to imaginemn
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);
__________________
Need a project done? - Set Your Own Price!
Imagine Creative Services
- Design : Marketing : Multimedia : More
imaginemn is offline  
Add Post to del.icio.us
Reply With Quote
Go Back   Webmaster Forum > Web Development > Web Design Lobby > 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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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 09:02 AM


Sponsor Links
Get exposure! Get exposure! Find Scripts Web Hosting Directory Get exposure! SEO Blog


All times are GMT -7. The time now is 02:39 AM.
© Copyright 2008 V7 Inc