Code so far
Posted: Wed Mar 02, 2011 10:36 am
So far, so good... I've got a few tweaks to make to the code, but I'm about to start moving it to a production server. It seems that Yahoo is a little behind what I installed for testing, as they are on MySQL v4.somethingorother, so I hope this translates. My PHP server info and my opening HTML entities are in one of my required files, as is my session starter...
First, the file to check your ID and password. The functions at the bottom were pulled from (I think) one of the books I was reading, but the second one didn't work, which is why I went with the $cxn -> real_escape_string. I tried another built-in function, but the function or syntax wasn't working...
Now my logout page. Much simpler...
Finally, my one large page... This is the page where we will be able to both mark a game as postponed, and to take games marked as postponed and schedule them for a new date. This is all one page, but I think I'll break it up into chunks for everyone's sanity... And the A / AA / AAA are just used to keep track of the brackets...
This first section is to take the games that had been postponed previously and schedule them for a new date / time / field. I set it up so that if any of the three values change, it reschedules the game. If none change, the game stays as "postponed."
Now the section to take the games with the checkmarks and mark them as postponed. The only thing I have to do here (and above) is to get rid of the "die" so that the program continues in case of a mistake. I know I saw that somewhere, so I can find it again.
Next, the section to list the games that have been marked as postponed and allow us to change the Date / Time / Field.
OK. Last part. Not to sound too jerkish, but I'm pretty happy that I've made it thus far and it works... Any optimizations you want to suggest, I'm happy to hear. While I may have made it this far, I have a LOOOOOOOOOOOOOOONG way to go....
Last part. This one........ I don't remember... Lemme look at it.... Oh yeah. It defaults to today, but offers a box to pick a date to mark games as postponed.
Wow..... That's it......
So I'm not asking for people to look over everything and optimize my code for me. But if anyone wants to give it a quick read and say "This section is a really bad idea because....." or "This section could be better done if you looked into this function...." or "For this long section, look at this short snippet of code and learn from it."
If you've made it this far, thanks for your time!!!!!!!!
Jef
First, the file to check your ID and password. The functions at the bottom were pulled from (I think) one of the books I was reading, but the second one didn't work, which is why I went with the $cxn -> real_escape_string. I tried another built-in function, but the function or syntax wasn't working...
Code: Select all
<?php
require_once 'login.php';
#connect to the databasae
$cxn=mysqli_connect($db_hostname, $db_username, $db_password, $db_database);
If (!$cxn) die ("Error, could not connect to the database<BR>");
$username = $cxn->real_escape_string($_POST['username']);
$userpass = $cxn->real_escape_string($_POST['userpw']);
$username = sanitizeString($username);
$userpass = sanitizeString($userpass);
$userpass5 = MD5($userpass);
#pull the user from MySQL
$userquery = "SELECT * FROM users WHERE Username = '$username' AND Password = '$userpass5'";
$result = mysqli_query($cxn, $userquery)
or die ("Couldn't execute query: " .mysql_error());
#check for one match. One of the examples I read said that if
#there was more than one match to the ID / pw, something wasn't right.
#It was like an easy security check.
$count = mysqli_num_rows($result);
If ($count == 1)
{ #This seems like I can remove the while loop if there's only one......
while($row = mysqli_fetch_assoc($result))
{
extract ($row);
}
#I wasn't going to, but looks like I'm going to have to jump straight into
#user rights so that not everyone who's logged in gets rights to everything,
#but I can continue to research that, because at least for now,
#there's no form to create a login ID.
$_SESSION['username'] = $username;
header("location:mptl.php"); #redirects back to the home page.
#}
ELSE
{
header("location:users.php"); #Try again, bad ID or pw.
}
#functions I don't remember if the second one didn't work or if I forgot to
#fix it up top. So that's something I have to play with.
#Suggestions welcome for better field checking.
function sanitizeString($var)
{
$var = stripslashes($var);
$var = htmlentities($var);
$var = strip_tags($var);
return $var;
}
function sanitizeMySQL($var)
{
$var = $cxn->real_escape_string($var);
$var = sanitizeString($var);
return $var;
}
?>
</body>
</html>
Code: Select all
<?php
session_start();
session_unset();
session_destroy();
header("location:mptl.php")
?>
</body>
</html>
This first section is to take the games that had been postponed previously and schedule them for a new date / time / field. I set it up so that if any of the three values change, it reschedules the game. If none change, the game stays as "postponed."
Code: Select all
POSTPONED.PHP
<?php
require_once 'login.php';
require_once 'auth.php';
#connect to the database
$cxn=mysqli_connect($db_hostname, $db_username, $db_password, $db_database);
If (!$cxn) die ("Error, could not connect to the database<BR>");
require_once 'header.php';
############################
# #
#List of games re-scheduled#
# #
############################
#pull the games with changes
if(isset($_POST['gameupd']))
{#A
foreach($_POST as $array)
{#AA
foreach($array as $game)
{#AAA check for changes to any of the 3 values
IF($game['Date']!=$game['newDate'] OR $game['Time']!=$game['newTime'] OR $game['Field']!=$game['newField'])
{#AAAA Format the data to the way I want it formatted.
#Yes, this could probably be done inline, but I wanted to see the results as I went.
$f_Date = date("l, F j", strtotime($game['Date']));
$f_Time = date("g:i A", strtotime($game['Time']));
$f_newDate = date("l, F j", strtotime($game['newDate']));
$f_newTime = date("g:i A", strtotime($game['newTime']));
$sql_Date = date("Y-m-d",strtotime($game['Date']));
$sql_Time = date("G:i:s", strtotime($game['Time']));
$sql_newDate = date("Y-m-d",strtotime($game['newDate']));
$sql_newTime = date("G:i:s", strtotime($game['newTime']));
$sqlupdate = "UPDATE schedule SET Rainout = '0', Date = '$sql_newDate', Time = '$sql_newTime', Field = '$game[newField]' WHERE Date = '$sql_Date' AND Time = '$sql_Time' AND Field = '$game[Field]'";
#update the database
IF($update = mysqli_query($cxn, $sqlupdate))
{
#update text statement. I left-aligned here for readability.
echo "<center>The " .$game['Division']. " division game between " .$game['AwayColor']. " (" .$game['AwayManager']. ") and ".$game['HomeColor']. " (" .$game['HomeManager']. ") has been rescheduled from ".$game['Field']. " on " .$f_Date. " at " .$game['Time']. " to " .$game['newField']. " on " .$f_newDate. " at " .$game['newTime']. ".<BR /><BR /></center>";
}
ELSE
{
#update failed.
die ("Couldn't update " .$game['Division']. " division game between " .$game['AwayColor']. " and " .$game['HomeColor']. ".<BR />"
."Please check to see that that field is available on that date and time.<BR />"
."If fields are available, please contact the WebMaster.<BR /><BR />");
}
}#AAAA close the IF
}#AAA close the FOR EACH
}#AA close the FOR EACH
}#A close the IF
echo "<hr />";
Code: Select all
#################################
# #
#List of games already postponed#
# #
#################################
#check to see if games were marked as postponed
if(isset($_POST['Game']))
{
#Update the schedule table
foreach($_POST as $array)
{
foreach($array as $game)
{
IF($game['rain']=='on')
{
$sqlupdate = "UPDATE schedule SET Rainout = '1' WHERE Date = '$game[Date]' AND Time = '$game[Time]' AND Field = '$game[Field]'";
IF(!$update = mysqli_query($cxn, $sqlupdate))
die ("Couldn't update record.");
}
}
}
}
Code: Select all
#setup the MySQLI queries
$schedulequery = "SELECT * FROM schedule WHERE RAINOUT = '1' ORDER BY time";
$fieldquery = "SELECT * FROM fields ORDER BY Fieldsel"; #I have a list of fields we can play on and times (see next line) to make selection easy.
$timequery = "SELECT * FROM times";
$result = mysqli_query($cxn,$schedulequery)
or die ("Couldn't execute query.");
$i = 0;
echo "<form action='postponed.php' method='POST'>\n";
echo "<center><H3>Postponed Games</H3></center>\n\n";
echo "<table border='1' cellpadding='5' align='center'>\n"
."<tr>"
."<th>Date</th>"
."<th>Time</th>"
."<th>Field</th>"
."<th>Division</th>"
."<th>Away Color</th>"
."<th>Away Manager</th>"
."<th>Home Color</th>"
."<th>Home Manager</th>"
."</tr>";
while($row = mysqli_fetch_assoc($result))
{
$fieldresult = mysqli_query($cxn,$fieldquery)
or die ("Couldn't execute field query.");
$timeresult = mysqli_query($cxn,$timequery)
or die ("Couldn't execute time query.");
extract ($row);
$f_Date = date("F j", strtotime($Date)); #format the date and time
$f_Time = date("g:i A", strtotime($Time));
#I set three hidden fields to date / time / field, then three text / select fields as date / time / field,
#so that if any of the three changed, I could update the record. If nothing changed, they would stay put.
#Not sure if that's the best way, but it works. Ideas welcome!
#These values will come out as $_POST[gameupd] and will trigger the 3rd code sample I posted.
echo "<tr>\n"
."<input type='hidden' name='gameupd[$i][Date]' value='$f_Date'>\n"
."<input type='hidden' name='gameupd[$i][Time]' value='$f_Time'>\n"
."<input type='hidden' name='gameupd[$i][Field]' value='$Field'>\n"
."<td><input type='text' name='gameupd[$i][newDate]' value='$f_Date' size='15'/></td>\n"
."<td><select name='gameupd[$i][newTime]'>\n";
#Create the select-box with the times.
while($rowtime = mysqli_fetch_assoc($timeresult))
{
extract ($rowtime);
echo " <option value='$Timesel'";
If ($f_Time == $Timesel)
{
echo " selected = 'selected'";
}
echo ">$Timesel</option>\n";
}
echo "</select></td>\n"
."<td><select name='gameupd[$i][newField]'>";
#Create the select box with the ballfield names
while($rowfield = mysqli_fetch_assoc($fieldresult))
{
extract ($rowfield);
echo " <option value='$Fieldsel'";
If ($Field == $Fieldsel)
{
echo " selected = 'selected'";
}
echo ">$Fieldsel</option>\n";
}
echo "</select></td>\n"
."<td><input type='hidden' name='gameupd[$i][Division]' value='$Division'>$Division</td>\n"
."<td><input type='hidden' name='gameupd[$i][AwayColor]' value='$AwayColor'>$AwayColor</td>\n"
."<td><input type='hidden' name='gameupd[$i][AwayManager]' value='$AwayManager'>$AwayManager</td>\n"
."<td><input type='hidden' name='gameupd[$i][HomeColor]' value='$HomeColor'>$HomeColor</td>\n"
."<td><input type='hidden' name='gameupd[$i][HomeManager]' value='$HomeManager'>$HomeManager</td>\n"
."</tr>\n";
++$i;
}
echo "</table><BR /><BR />\n"
."<center><input type='submit' value='Update games'>" #Submit button
."</form></center>\n\n";
echo "<hr />\n";
Last part. This one........ I don't remember... Lemme look at it.... Oh yeah. It defaults to today, but offers a box to pick a date to mark games as postponed.
Code: Select all
####################################
# #
#List of games to mark as postponed#
# #
####################################
#set the date from text box below (well, on the ELSE)
If (!isset($_POST['udate']))
{
$udate = date("Y-m-d");
}
Else
{
$udate = date("Y-m-d",strtotime($_POST['udate']));
}
require_once 'header.php';
#pull the schedule from MySQL
$schedulequery = "SELECT * FROM schedule WHERE Date = '$udate' AND RAINOUT = '0' ORDER BY time";
$result = mysqli_query($cxn,$schedulequery)
or die ("Couldn't execute query.");
$i=0;
ECHO "<center><h3>Games to be Postponed</h3></center>\n\n";
#Form to pick date
ECHO "<center><form action='postponed.php' method='POST'>"
."<input type = 'text' name = 'udate'></td>"
."<input type = 'submit' value = 'New Date'>"
."</form></center>\n\n";
#Table headers
echo "<form action='postponed.php' method='POST'>"
."<table border='1' cellpadding='5' align='center'>\n"
."<tr>\n"
."<th>Date</th>\n"
."<th>Time</th>\n"
."<th>Field</th>\n"
."<th>Division</th>\n"
."<th>Away Color</th>\n"
."<th>Away Manager</th>\n"
."<th>Home Color</th>\n"
."<th>Home Manager</th>\n"
."<th>Reschedule</th>\n"
."</tr>\n";
#create the table data
while($row = mysqli_fetch_assoc($result))
{
extract ($row);
#format Date and Time
$f_Date = date("F j", strtotime($Date));
$f_Time = date("g:i A", strtotime($Time));
echo "<tr>\n"
."<td align='center'><input type = 'hidden' name = 'Game[$i][Date]' value = '$Date' />$f_Date</td>\n"
."<td align='center'><input type = 'hidden' name = 'Game[$i][Time]' value = '$Time' />$f_Time</td>\n"
."<td align='center'><input type = 'hidden' name = 'Game[$i][Field]' value = '$Field' />$Field</td>\n"
."<td align='center'><input type = 'hidden' name = 'Game[$i][Division]' value = '$Division' />$Division</td>\n"
."<td align='center'><input type = 'hidden' name = 'Game[$i][AwayColor]' value = '$AwayColor' />$AwayColor</td>\n"
."<td align='center'><input type = 'hidden' name = 'Game[$i][AwayManager]' value = '$AwayManager' />$AwayManager</td>\n"
."<td align='center'><input type = 'hidden' name = 'Game[$i][HomeColor]' value = '$HomeColor' />$HomeColor</td>\n"
."<td align='center'><input type = 'hidden' name = 'Game[$i][HomeManager]' value = '$HomeManager' />$HomeManager</td>\n"
."<td align='center'><input type='checkbox' name='Game[$i][rain]' /></td>\n"
."</tr>\n";
$i++;
}
echo "<center></table><BR /><BR />\n"
."<input type='submit' value='Postpone Selected Games'>"
."</form></center>\n\n";
?>
</body>
</html>
So I'm not asking for people to look over everything and optimize my code for me. But if anyone wants to give it a quick read and say "This section is a really bad idea because....." or "This section could be better done if you looked into this function...." or "For this long section, look at this short snippet of code and learn from it."
If you've made it this far, thanks for your time!!!!!!!!
Jef