andy's php projects
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Script and site usage:
All of the scripts on this site are provided free for non-commerical, personal or non-profit use. If you find these scripts useful to you, appreciate the free support, or are just an all round nice person, then why not donate a little dosh? If you wish to use any of these in a commercial system, please refer to the payment plan.


SQL table?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    php.amnuts.com Forum Index -> Battleships By Email (BBE)
View previous topic :: View next topic  
Author Message
Mike250
Newbie


Joined: 23 Sep 2002
Posts: 20
Location: Colorado, USA

PostPosted: Mon Sep 23, 2002 4:13 pm    Post subject: SQL table? Reply with quote

Do I have to create the SQL tables?
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Mon Sep 23, 2002 9:09 pm    Post subject: Re: SQL table? Reply with quote

Mike250 wrote:
Do I have to create the SQL tables?


If you dont want to store the results, then just remove the call to
record_stats() and you wont need it. Else create a SQL table with the
following:

Code:
CREATE TABLE results (
  id int(11) NOT NULL auto_increment,
  email varchar(255) NOT NULL default '',
  won mediumint(9) NOT NULL default '0',
  lost mediumint(9) NOT NULL default '0',
  PRIMARY KEY  (id),
  KEY email (email)
) TYPE=MyISAM;


Cheers,

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Mike250
Newbie


Joined: 23 Sep 2002
Posts: 20
Location: Colorado, USA

PostPosted: Tue Sep 24, 2002 10:16 am    Post subject: Reply with quote

WHen does the results actually save to the database? After each turn or at the completion of the game? I have a game in progress right now, if I add this table now will the results save to it?
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Tue Sep 24, 2002 11:14 am    Post subject: Reply with quote

It only saves at the end of the game, when it knows what email address the winner is from, and where the loser is from.

I'm thinking I might update the code so it uses MySQL throughout - even to store the game data. This way all I'll have to do is send along a unique id (like an MD5 string) and not the whole board info.... But I guess that kinda defeats the purpose of what I was trying to accomplish. Wink Let me know what you think, though!

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Mike250
Newbie


Joined: 23 Sep 2002
Posts: 20
Location: Colorado, USA

PostPosted: Tue Sep 24, 2002 10:09 pm    Post subject: Reply with quote

I agree, you wanted to do it via URL all along. Keep it that way I guess.
Hey what do I need to do after I setup the table? Anything at all to be able to view the results?
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Wed Sep 25, 2002 7:15 am    Post subject: Reply with quote

Yeah, as of yet there is nothing in the code that allows you to see the results. Something will have to be put in there to do that. You could do something like:

Code:

function show_stats_winner($limit = 0)
{
   global $dbVars;
   $sql = "SELECT email,won FROM {$dbVars['tbl_results']} ORDER BY won";
   if ($limit) $sql .= " LIMIT $limit";
   echo "<p>The ", ($limit ? "top $limit " : ""), "winners are:</p>\n";
   _real_show_stats($sql);
}

function show_stats_loser($limit = 0)
{
   global $dbVars;
   $sql = "SELECT email,lost FROM {$dbVars['tbl_results']} ORDER BY lost";
   if ($limit) $sql .= " LIMIT $limit";
   echo "<p>The ", ($limit ? "top $limit " : ""), "losers are:</p>\n";
   _real_show_stats($sql);
}

function show_stats_email($limit = 0)
{
   global $dbVars;
   $sql = "SELECT * FROM {$dbVars['tbl_results']} ORDER BY lost";
   if ($limit) $sql .= " LIMIT $limit";
   echo "<p>The ", ($limit ? "top $limit " : ""), "players are:</p>\n";
   _real_show_stats($sql);
}

function _real_show_stats($sql="")
{
   global $dbVars,$bs_struct,$whoishit,$whojustwent;
   
   if ($sql == "")
   {
      echo "<p>Could not determine the results.</p>\n";
      return;
   }
   if (($id = @mysql_connect($dbVars['host'],$dbVars['user'],$dbVars['pass'])))
   {
      if (@mysql_select_db($dbVars['name'],$id))
      {
         $result = @mysql_query($sql,$id);
         if (!@mysql_num_rows($result))
         {
            echo "<p>No results could be found.</p>\n";
            return;
         }
         while (($row = @mysql_fetch_array($result,MYSQL_ASSOC)))
         {
            $info = array();
            if ($row['email']) $info[] = $row['email'];
            if ($row['won']) $info[] = "a total of {$row['won']} win" . ($row['win']==1 ? "" : "s");
            if ($row['lost']) $info[] = "a total of {$row['lost']} los" . ($row['lost']==1 ? "s" : "ses");
            echo @join(" :: ",$info), "<br>\n";
         }
      }
      else
      {
         echo "<p>Could not determine the results.</p>\n";
         return;
      }
   }
   else
   {
      echo "<p>Could not determine the results.</p>\n";
      return;
   }
}


And then just call then function you need to display whatever info you want to show. One of show_stats_winner, show_stats_loser or show_stats_email. Obviously you can put whatever HTML treatment you want into those functions, depending on how you want to dislay the info.

NOTE: I just typed this up off the top of my head and haven't tested it. Let me know how it works out for ya.

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Mike250
Newbie


Joined: 23 Sep 2002
Posts: 20
Location: Colorado, USA

PostPosted: Thu Sep 26, 2002 10:48 am    Post subject: Reply with quote

What's this doing?

if (@mysql_select_db($dbVars['name'],$id))

the '[name']
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Thu Sep 26, 2002 10:55 am    Post subject: Reply with quote

Mike250 wrote:
What's this doing?

if (@mysql_select_db($dbVars['name'],$id))

the '[name']


You're selecting the database that has the same name as assigned in the $dbVars array. If you check out setup.php you'll see:

Code:

   $dbVars = array(
        "user" => "username",
        "pass" => "password",
        "host" => "localhost",
        "name" => "battleships",
        "tbl_results" => "results"
    );


so you'll be trying to select the database named 'battleships' (unless you defined a different name for your MySQL db, in which case change the dbVars to match up.)

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    php.amnuts.com Forum Index -> Battleships By Email (BBE) All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group