Jump to content


Photo

Need help with an array (SQL)


  • Please log in to reply
2 replies to this topic

#1 sbourdon

sbourdon
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 05 June 2006 - 02:07 PM

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:

# 
#----[ 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);
            }

            }

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

#2 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 05 June 2006 - 05:23 PM

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.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#3 sbourdon

sbourdon
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 08 June 2006 - 12:51 PM

[!--quoteo(post=380269:date=Jun 5 2006, 12:23 PM:name=Buyocat)--][div class=\'quotetop\']QUOTE(Buyocat @ Jun 5 2006, 12:23 PM) View Post[/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:

# 
#----[ 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);
            }

            }

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\" /]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users