Jump to content

Archived

This topic is now archived and is closed to further replies.

sbourdon

Need help with an array (SQL)

Recommended Posts

Hello,

I'm doing my best to learn php and SQL just by looking at the code and trying different things; so far, so good but I need help with an array... I've read this [a href=\"http://www.phpfreaks.com/quickcode/checkbox/337.php\" target=\"_blank\"]example [/a]on php freaks, but I'm not sure how to use it in my code.

Here's the thing...

I'm using the Fantasy Sports MOD and wanted to be able to reset each team record (number of wins/ties/losses) in the "Add games" menu, using checkboxes. The code I've added works great... if you only add one game! As soon as there are 2 or more games added, it does nothing! That's why I think it's because of an array problem, where the code should check every entry; unfortunately, I have no clue how to do that! :-?

Here's the php code I've added so far:

[code]#
#----[ FIND ]------------------------------------------- (Fantasy Sports: ajout des pointages de séries)
#
            // Bonus game?
            $set_bonus = (@in_array($i, (array)$bonus_game)) ? 1 : 0;

            $sql = "INSERT INTO " . GAME_TABLE . " (league_id,gametime,homeid,awayid,bonus_game".$line.") VALUES ('$league_id','$gametime','$hometeam[$i]','$awayteam[$i]','$set_bonus'".$line2.")";
            if(!$db->sql_query($sql))
            {
                message_die(GENERAL_ERROR,'Error adding game to the database','',__LINE__,__FILE__,$sql);
            }
            $gamesadded++;

#
#----[ AFTER, ADD ]------------------------------------------- (Fantasy Sports: ajout des pointages de séries)
#
            // New series?
            if( $HTTP_POST_VARS['new_series'] )
            {
        
            $sql = "UPDATE " . TEAM_TABLE . " SET wins_series=0, ties_series=0, losses_series=0 WHERE id=$hometeam[$i] OR id=$awayteam[$i]";
            if(!$db->sql_query($sql))
            {
                message_die(GENERAL_ERROR,'Error updating series in the database','',__LINE__,__FILE__,$sql);
            }

            }
[/code]

As I said, it does work, but only when one game is added at a time; how can I make it work with multiple entries?

Thank you for your help! :D

Share this post


Link to post
Share on other sites
Be a little more specific what exactly is your problem? From the looks of it the query string that Inserts and Updates the database is using a series of arrays, for example $hometeam[] and you're pulling the $i th value from that array and entering it into the database. However I'm not sure where you set $i so it might just be that it's being treated as zero (ie the first entry in the array) and I also don't see you incrementing it which means it will never look for data beyond the first entry. So, I guess what I'm trying to say is, if you have an array filled with data, say there are 2 entries in each array, and you want to enter it all in, then you need to try a for loop, for ($i = 0; $i < count($array); $i++), and run through each value of the array like that.

Share this post


Link to post
Share on other sites
[!--quoteo(post=380269:date=Jun 5 2006, 12:23 PM:name=Buyocat)--][div class=\'quotetop\']QUOTE(Buyocat @ Jun 5 2006, 12:23 PM) [snapback]380269[/snapback][/div][div class=\'quotemain\'][!--quotec--]
...I also don't see you incrementing it which means it will never look for data beyond the first entry...
[/quote]

Thank you very much!
I was able to fix that problem doing this:

[code]#
#----[ FIND ]------------------------------------------- (Fantasy Sports: ajout des pointages de séries)
#
            // Bonus game?
            $set_bonus = (@in_array($i, (array)$bonus_game)) ? 1 : 0;

            $sql = "INSERT INTO " . GAME_TABLE . " (league_id,gametime,homeid,awayid,bonus_game".$line.") VALUES ('$league_id','$gametime','$hometeam[$i]','$awayteam[$i]','$set_bonus'".$line2.")";
            if(!$db->sql_query($sql))
            {
                message_die(GENERAL_ERROR,'Error adding game to the database','',__LINE__,__FILE__,$sql);
            }
            $gamesadded++;

#
#----[ REPLACE WITH ]------------------------------------------- (Fantasy Sports: ajout des pointages de séries)
#
            // Bonus game?
            $set_bonus = (@in_array($i, (array)$bonus_game)) ? 1 : 0;
            $set_series = (@in_array($i, (array)$new_series)) ? 1 : 0;

            $sql = "INSERT INTO " . GAME_TABLE . " (league_id,gametime,homeid,awayid,bonus_game,new_series".$line.") VALUES ('$league_id','$gametime','$hometeam[$i]','$awayteam[$i]','$set_bonus','$set_series'".$line2.")";
            if(!$db->sql_query($sql))
            {
                message_die(GENERAL_ERROR,'Error adding game to the database','',__LINE__,__FILE__,$sql);
            }
            $gamesadded++;

            // New series?
            if( (@in_array($i, (array)$new_series)) )
            {
        
            $sql = "UPDATE " . TEAM_TABLE . " SET wins_series=0, ties_series=0, losses_series=0 WHERE id=$hometeam[$i] OR id=$awayteam[$i]";
            if(!$db->sql_query($sql))
            {
                message_die(GENERAL_ERROR,'Error updating series in the database','',__LINE__,__FILE__,$sql);
            }

            }
[/code]

Now that a 1 or 0 value is entered in the DB, I can make a loop through the array and not be limited to the first entry! By the way, is it possible to do that without enterrring a value in the DB? Is that what you were telling me to do? Finally, this code works, but does it make any sense?

Thank you for helping a beginner at this! [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.