Jump to content

newbie questions


sws

Recommended Posts

I have a mysql table that contains the entire nhl schedule in it. Fields in the table are as follows:

ID
Home Team
Visiting Team
Date

I'm trying to create a form that lists every game being played on that particular date (always today) with radio buttons beside each team name.

Basically what I'm trying to accomplish is to display a form that shows every game for that day and to let the user pick a winner for each game.  How to I populate the form with JUST the games for the present day ?

Also,  How do I write the picks to the database.
Link to comment
Share on other sites

First make sure the field isn't called "date" this confuses PHP. change it to "game_date" or something. I'll use that in this little walkthrough
populate it:
[code]
<?php

// How you pull the date depends on how you store the date.
// Look into the "date" function for better understanding

$query = mysql_query("SELECT * FROM table WHERE game_date='".NOW()."'") or die(mysql_error());
?>
<form name="nhl" action="" method="POST">
Home : Away
<?php
while($row = mysql_fetch_array($query)){
echo "<input type='radio' name='bet[".$row['id']."]' value='home_team'> : <input type='radio' name='bet[".$row['id']."]' value='visiting_team'> - ".$row['home_team']." VS. ".$row['visiting_team']."<br>";
}
?>
</form>
[/code]

and then fix the code to place the bets (assuming thats what your doing here).

Fields in this case are "id, home_team, visiting_team, game_date"
Link to comment
Share on other sites

Thanks Jocka.

I'll implement this. Once I have this working, how would I then enter the picks into the database ?  I'll need to createa table containing the user's id and all of the pick values each day.

Any advice on how I should set that up ?
Link to comment
Share on other sites

This will work as long as the Date field is actually a date field and not a char field
[code]<?php
if(!isset($_POST['submit'])){
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=POST>";
echo "<table width=800 align=center>
<tr>
<td width=200>Home Team</td>
<td width=200>Away Team</td>
<td width=100>Select</td>
</tr>";
$today = date("Y-m-d");
$sql = "SELECT * FROM nhltable WHERE Date = $date ORDER BY Date ASC";
  $res = mysql_query($sql) or die (mysql_error());
    while($r = mysql_fetch_assoc($res)){
      echo "<tr>
              <td>".$r['Home Team']."</td>
              <td>".$r['Away Team']."</td>
              <td><input type=radio name=gameid value=".$r['ID']."></td>
              </tr>";
}
echo "<tr>
        <td colspan=3 align=center><input type=submit name=submit value=Submit></td>
        </tr>
        </table>";
} else {
// put your code for after the submit


}
?>[/code]

Ray
Link to comment
Share on other sites

just a table called "game_bets" with fields like "bet_date, user_id, game_id, user_bet"  -- date, user id, the "game" id and their bet of course.
I forgot a submit button by the way so throw that in the form before "</form>"
[code]
<?php

// first we find our results
if(isset($_POST['bet'])){ // check to see if bet was sent to the page

// DO YOUR CHECK TO SEE IF THEY ARE LOGGED IN
// WE'LL CALL $user_id FOR THE ID YOU HAVE FOR THE LOGGED IN USER

foreach($_POST['bet'] as $id => $bet){ // id is game id and bet is their choice
      $query = mysql_query("INSERT INTO `game_bets` VALUES (NOW(), '".$user_id."','".$id."','".$bet."')") or die(mysql_error()); // dies on error
}

// ALL IN DATABASE
?>

[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.