Page 1 of 1

Looping with Different Data Each Time

Posted: Mon Jun 28, 2010 7:32 pm
by azylka
Hi there, I'm not new to PHP, but until now I've never needed to use for() or while() (or actually any type of looping). SO, my question is: Is it possible to loop or call a function with different data each time? My code is below, but I'd really like to shorten it somehow.

Example input:
33.69*50 Free*33.49
29.99*50 Free*29.59
28.59*50 Free*26.69
27.99*50 Free*25.19

PHP Script:

Code: Select all

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Season: <input name="season" type="radio" value="SCY" checked>SCY | <input name="season" type="radio" value="LCM" />LCM | <input name="season" type="radio" value="SCM" />SCM<br />
Data Block: <br /><textarea rows="4" cols="25" name="data"></textarea><br />
<input type="submit" value="Add Event" />

<?php
if(isset($_POST['data']) && isset($_POST['season'])) {
$data = $_POST['data'];
$season = $_POST['season'];

//split rows into strings
$data = explode("\n", $data);
list($data1, $data2, $data3, $data4) = $data;

//row 1 - separate strings into: girls times/event/boys times
$data1 = explode("*", $data1);
list($girls1, $event, $boys1) = $data1;

//row 2 - separate strings into: girls times/event/boys times
$data2 = explode("*", $data2);
list($girls2, $event, $boys2) = $data2;

//and it keeps going up until row 4

$event_stripped = str_replace (" ", "", $event); //strip whitespace + make lowercase for ease of links
$event_stripped_lower = strtolower($event_stripped);
$season_lower = strtolower($season);

echo "<br><br><br><textarea style=\"width:50%;height:40%;\">
<div id=\"$event_stripped_lower-$season_lower\" title=\"$event ($season)\" class=\"panel\">
	<table class=\"itable\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">
		<tr class=\"header\">
			<th>Age</th>
			<th>Girls</th>
			<th class=\"last\">Boys</th>
		</tr>
		<tr class=\"reg\">
			<td class=\"first\">10&U</td>
			<td>$girls1</td>
			<td class=\"last\">$boys1</td>
		</tr>
		<tr class=\"alt\">
			<td class=\"first\">12&U</td>
			<td>$girls2</td>
			<td class=\"last\">$boys2</td>
		</tr>
		<tr class=\"reg\">
			<td class=\"first\">14&U</td>
			<td>$girls3</td>
			<td class=\"last\">$boys3</td>
		</tr>
		<tr class=\"alt\">
			<td class=\"first\">Open</td>
			<td>$girls4</td>
			<td class=\"last\">$boys4</td>
		</tr>
	</table>
</div>
</textarea>";
}
//hopefully I'll be able to input all of the blocks of data  I have (at once) and get an output of the div above for each
?>
Thanks,
Alex Zylka

Re: Looping with Different Data Each Time

Posted: Mon Jun 28, 2010 8:10 pm
by eruna
I don't get exactly what you are doing, but in general it works like this.

$myArray=( 10,20,30,40 );

foreach($myArray as $v){
echo "$v * 50=".($v*50)."\n";
}

This would return:

10 * 50=500
20 * 50 =1000
etc..

Re: Looping with Different Data Each Time

Posted: Mon Jun 28, 2010 9:45 pm
by azylka
Sorry, that's not quite what I'm looking for. The "*" is a separator for the explode() function. Actually, I kind of gave up on this thread and a more active one can be found here (http://forums.devshed.com/php-developme ... 20632.html) with the latest "updates."