Jump to content

Recommended Posts

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/207030-foreach-usage/
Share on other sites

each iteration of the foreach loop overwrites the previous value of $white (and the other variables) with a new value... if you are wanting to save the data from each iteration, store it in an array:

 

$white[] = str_replace('White ', "", $whiteNo);

 

then for instance you can access the value of the first iteration with $white[0], 2nd iteration with $white[1], etc...

Link to comment
https://forums.phpfreaks.com/topic/207030-foreach-usage/#findComment-1082637
Share on other sites

You can reduce the following code

		$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;
	}

down to just

preg_match_all('/\[(.*)\]/', $textData, $matches);

$vars = array();
foreach($matches[1] as $value)
{
   list($key, $data) = explode(' "', $value);
   
   $var[$key] = trim($data, '"');
}

echo $var['White'];

Link to comment
https://forums.phpfreaks.com/topic/207030-foreach-usage/#findComment-1082646
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.