garrensilverwing
New Member
- Messages
- 148
- Reaction score
- 0
- Points
- 0
i wrote this code with some of your help (thanks very, very much!) and i need a bit of help with it, when i enter in any amount of PGN's it works perfectly with one exception, it duplicates the extra game at the end every time which gives me one extra MySQL query which can screw up the entire /games portion of my webiste (offsetting the links provided by the search, etc) so i was wondering if there is a way to either prevent the final array value from being processed or to just delete it entirely before the entire array is processed here is the code if you need it (ignore the notes i wrote them so i would be lessed confused when i looked at the code, i deleted most of them because of length issues though):
Code:
echo "<ol>";
$string = $_POST['pgn'];
$string = strtolower($string);
$string = stripslashes($string); //removes any slashes from $string to protect against malicious users
$string = str_replace("[","<br>[",$string); more easily read by humans
$string = str_replace("\"1-0\"","!",$string);
$string = str_replace("\"0-1\"","@",$string);
$string = str_replace("\"1/2-1/2\"","#",$string);
$string = str_replace("1-0","~",$string);
$string = str_replace("0-1","~",$string);
$string = str_replace("1/2-1/2","~",$string);
$string = str_replace("!","\"1-0\"",$string);
$string = str_replace("@","\"0-1\"",$string);
$string = str_replace("#","\"1/2-1/2\"",$string);
$pgn=explode("~",$string);
foreach($pgn as $number => $game)
{
preg_match_all('/\[(\w+) "([^"]+)"\]/', $game, $matches, PREG_SET_ORDER);
foreach($matches as $key => $value)
{
$tag=$value[1];
$data[$tag]=$value[2];
}
$wName=$data['white'];
$bName=$data['black'];
$wRating=$data['whiteelo'];
$bRating=$data['blackelo'];
$eco=$data['eco'];
$date=$data['date'];
$outcome=$data['result'];
$error=0;
if(preg_match('/,/', $wName)==1 OR preg_match('/ /', $wName)==1)
{
$wName=str_replace("'","",$wName);
if(preg_match('/,/',$wName)==1)
{
$wName=str_replace(" ","",$wName);
$white_name=explode(",",$wName);
if(count($white_name)< 3)
{
$wLast=$white_name[0];
$wFirst=$white_name[1];
}
else
{
$error=1;
echo "Error the format of black's name is incorrect. Please have black's name in either \"last, first\" or \"first last\" formats."; }
}
else
{
$white_name=explode(" ",$wName);
if(count($white_name) < 3)
{
$wFirst=$white_name[0];
$wLast=$white_name[1];
}
else
{
$error=1;
echo "Error the format of white's name is incorrect. Please have white's name in either \"last, first\" or \"first last\" formats.";
}
}
}
else
{
$wFirst=$wName;
$wLast=$wName;
}
if(preg_match('/,/', $bName)==1 OR preg_match('/ /', $bName)==1)
{
$bName=str_replace("'","",$bName);
if(preg_match('/,/',$bName)==1)
{
$bName=str_replace(" ","",$bName);
$black_name=explode(",",$bName);
if(count($black_name)< 3)
{
$bLast=$black_name[0];
$bFirst=$black_name[1];
}
else
{
$error=1;
echo "Error the format of black's name is incorrect. Please have black's name in either \"last, first\" or \"first last\" formats."; //warns the user that the name is incompatable
}
}
else //if it is not in "last, first" format it is assumed it is in "first last" format or another format with spaces
{
$black_name=explode(" ",$bName); //seperates black's first and last name
if(count($black_name) < 3) //checks if there is more than 2 names found
{
$bFirst=$black_name[0]; //seperates black's first and
$bLast=$black_name[1]; //last name from the array
}
else
{
$error = 1;
echo "Error the format of black's name is incorrect. Please have black's name in either \"last, first\" or \"first last\" formats."; //warns the user that the name is incompatable
}
}
}
else //if black's name is not in last, first or first last, that is there are no spaces or commas
{
$bFirst=$bName; //sets both white's first and
$bLast=$bName; //last name to the pgn's original value (usually an online handle)
}
if($date!=="????.??.??" AND $date!=="*" AND $date!=="" AND $date!=="??") //verifies that there is usable data in the pgn's date tag
{
$day=explode(".",$date); //seperates the date's year month and day in to an array
$year=$day['0']; //since the standard pgn date format is "yyyy.mm.dd" the first array value is used for year
if(strlen($year)!==4) //if the number of numbers in year is not four then the format of date must have been different
{
$year = $day['1']; //this sets the year to the second value of array $day, that is if the date is in "dd.yyyy.mm" or "mm.yyyy.dd" format
if(strlen($year)!==4) //if the number of numbers in $year is not for then the format must be "dd.mm.yyyy" or "mm.dd.yyyy"
{
$year = $day['2']; //sets the year to third value in array $day
}
}
}
else
{
$year=0000; //uses whatever the pgn's date tag says either
}
if($outcome !== "*" AND $outcome !== "??" AND $outcome!=="") //verifies there is usable data in the pgn's result tag
{
if($outcome == "0-1") //if black wins
{
$result = 0; //used to signify black wins
}
if($outcome == "1-0") //if white wins
{
$result = 1; //used to signify white wins
}
if($outcome == "1/2-1/2") //if the game is a draw
{
$result = 2; //used to signify a draw
}
}
else
{
$result = 3; //this will be used to signify that the result is still pending whether the game was adjourned or never completed
}
if($error!==1)
{
$sql = "INSERT into `games`(`wFirst`,`wLast`,`wRating`,`bFirst`,`bLast`,`bRating`,`eco`,`result`,`year`)
VALUES ('$wFirst','$wLast','$wRating','$bFirst','$bLast','$bRating','$eco','$result','$year');";
mysql_query($sql) or die(mysql_error());
echo "<li>$wFirst $wLast ($wRating) vs $bFirst $bLast ($bRating) $eco $result $year, has been entered into that database.</li>"; //places each game's info in an ordered list and states that it has been submitted properly
}
else
{
echo "your pgn ($game) was not submitted to the database, please check the formatting of the names";
}
}
echo "</ol>";
?>
Last edited: