Jump to content

Zugzwangle

Members
  • Posts

    56
  • Joined

  • Last visited

Everything posted by Zugzwangle

  1. Hi. I have a simple question about using foreach statements. I have data posted from a form, which is in the following format: [Event "Rated game, 90m + 5s"] [site "Room 1"] [Date "2010.07.05"] [Round "?"] [White "Jugarul"] [black "Zeroonze"] [Result "0-1"] [ECO "C00"] [WhiteElo "1953"] [blackElo "2052"] [PlyCount "84"] [EventDate "2010.07.05"] [TimeControl "5400+5"] 1. e4 {4} e6 {1} 2. d3 {7} d5 {15} 3. Nd2 {2} Nf6 {3} 4. g3 {15} Nc6 {217} 5. Bg2 {11} dxe4 {11} 6. dxe4 {15} e5 {11} 7. Ngf3 {50} Bc5 {28} 8. O-O {42} O-O { 3} 9. h3 {295 Jugarul offers a draw} Qe7 {211} 10. c3 {7} a5 {147} 11. a4 {5} b6 {343} 12. Re1 {185} Ba6 {20} 13. Qb3 {1022} Bd3 {710} 14. Nh4 {64} g6 {211} 15. Nf1 {220} Rfe8 {940} 16. Be3 {101} Kg7 {91} 17. Rad1 {324} Rad8 {96} 18. Rxd3 {202} Rxd3 {19} 19. Qb5 {6} Rd6 {259} 20. Bxc5 {68} bxc5 {3} 21. Ne3 {96} Qe6 {4} 22. Qxc5 {598} Rb8 {272} 23. Nd5 {296} Rxb2 {41} 24. Nxc7 {45} Qd7 {2} 25. Nd5 {38} Ra2 {24} 26. Ne3 {399} Rad2 {345} 27. Nc4 {92} Rd1 {3} 28. Rxd1 { 126} Rxd1+ {3} 29. Kh2 {0} Qe6 {308} 30. Nxa5 {313} Nxa5 {3} 31. Qxa5 {3} Ra1 { 55} 32. Qb4 {531} Ra2 {196} 33. Kg1 {103} Qd7 {71} 34. f4 {169} Qd1+ {67} 35. Kh2 {4} exf4 {5} 36. gxf4 {11} Qd2 {65} 37. e5 {45} Nh5 {6} 38. f5 {64} Qe3 { 141 '} 39. f6+ {1} Kh6 {5} 40. Qf8+ {11} Kg5 {22} 41. Qb4 {1} Qg3+ {19} 42. Kg1 {2} Ra1+ {2 Jugarul resigns (Lag: Av=1.67s, max=3.1s)} 0-1 I have created some code to sort this data, by the constants from the data recieved - for example "[site " will always -> Room 1. Here is my code: <?php { function main_main() { echo ""; global $dbhost, $dbname, $dbuser, $dbpasswd; $link = mysql_connect($dbhost, $dbuser, $dbpasswd) or die('Could not connect: ' . mysql_error()); mysql_select_db($dbname) or die('Could not select database'); $textData = $_POST["myTextArea"]; //$textData = $pgn; $partsTop = str_replace('[', "", $textData); $partsTop2 = str_replace('\"', "", $partsTop); $parts = explode("]", $partsTop2); $numberOf = count($parts); foreach($parts as $val) { $whiteNo = strstr($val, 'White '); $blackNo = strstr($val, 'Black '); $controlNo = strstr($val, 'Event '); $locationNo = strstr($val, 'Site '); $dateNo = strstr($val, 'EventDate '); $resultNo = strstr($val, 'Result '); $notation = strstr($val, '1. '); $white = str_replace('White ', "", $whiteNo); $black = str_replace('Black ', "", $blackNo); $control = str_replace('Event ', "", $controlNo); $location = str_replace('Site ', "", $locationNo); $date = str_replace('EventDate ', "", $dateNo); $result = str_replace('Result ', "", $resultNo); echo $white; } echo $white; } } ?> The above, within the foreach statement, the variables I desire, for example $white = Jugarul, $black = Zeroonze, etc, to later be manipulated. However, the "echo $white;" within the 'foreach' statement works fine on all of the variables I need, but the "echo $white;" statement outside the 'foreach' returns with nothing. What am I misunderstanding about foreach statements?
  2. I have the below array, $withoutBreaks: Array ( [0] => [Event 90m + 5s, rated [1] => [site Room 1 [2] => [Date 2010.06.18 [3] => [Round ? [4] => [White Zugzwang1 [5] => [black Jeff Morris [6] => [Result 1-0 [7] => [WhiteElo 1543 [8] => [blackElo 1537 [9] => [PlyCount 5 [10] => [EventDate 2010.06.18 [11] => 1. d4 {3} c6 {2} 2. Nc3 {2} d5 {1} 3. Nf3 {4 Jeff Morris resigns (Lag: Av=0. 22s, max=0.3s)} 1-0 ) This array is made by a form. The elements may differ in both number and in order, for example [blackElo may be element [3] rather than [6]. One constant, is the '[' followed by the immediate word i.e. [Event. What I would like to happen, is to use array_search() to find which element the data is held in. My code: $datePart = array_search("[Date ", $withoutBreaks); echo ($datePart); Output: None!!! What am I doing wrong?
  3. Thanks for that... 'trim' worked.. !! Thanks very much, that cost me a few hours!!
  4. The output for the following: <?php echo '<pre>' . print_r($parts,true) . '</pre>'; ?> is Array ( [0] => [Event \"90m + 5s, rated\" [1] => [site \"Room 1\" [2] => [Date \"2010.06.18\" [3] => [Round \"?\" [4] => [White \"Zugzwang1\" [5] => [black \"Jeff Morris\" [6] => [Result \"1-0\" [7] => [WhiteElo \"1543\" [8] => [blackElo \"1537\" [9] => [PlyCount \"5\" [10] => [EventDate \"2010.06.18\" [11] =>
  5. $parts[] is an array of the following: [Event \"90m + 5s, rated\" [site \"Main Playing Hall\" [Date \"2010.06.17\" [Round \"?\" [White \"Zugzwang1\" [black \"Jeff Morris\" [Result \"0-1\" [WhiteElo \"1189\" [blackElo \"1233\" [PlyCount \"30\" EventDate \"2010.06.17\" [EventType \"blitz\" [TimeControl \"60\" The first for statement below works.. the 2nd doesn't, yet $partsLocation and $correctLocation are the same value, as tested with an echo statement (echo ($partsLocation).'<br>'; echo ($correctLocation). <?php $correctControl = '[Event \"90m + 5s, rated\"'; $partsControl = $parts[0]; //echo ($partsControl.'<br>'); //echo ($correctControl.'<br>'); if ($partsControl == $correctControl) { echo ("Correct time control!!<br>"); } else { echo ("Time Control is wrong!!!"); } $correctLocation = '[site \"Room 1\"'; $partsLocation = $parts[1]; //echo ($partsLocation).'<br>'; //echo ($correctLocation); if ($partsLocation == $correctLocation) { echo "Correct Location"; } else { echo ("Location is wrong!!!<br>"); } ?> OUTPUTS: Correct time control!! Location is wrong!!! The first for statement works, and the 2nd doesn't.. I can't see why. Can you?
×
×
  • 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.