Page 1 of 1

Array loop

Posted: Mon Aug 18, 2003 5:46 pm
by jkmcgrath
I am trying to iterate thru this array with out having to type each array out like this $names[0] $names[1] ect. I want that to happen automaticaly untill they are all gone lol
Any pointers?

Code: Select all

<?php
($fp = fopen("proroster.csv", 'r')) or die ("Couldn't open the data file");     
// Iterate through each line of the file 
while ( ! feof($fp) ) &#123; 
   $line = fgets($fp); 
   // remove any extra white space or carriage returns 
   $line = chop($line); 
   
        
   // spilt each line into individual fields 
   $names = explode(",", $line); 

   // print the first name 
  print $names&#1111;count]; 

   // print the last name 
   //print $names&#1111;1]; 
   
  // print "<br>";
    
&#125; 
f
?>

Posted: Mon Aug 18, 2003 5:58 pm
by evilmonkey
Use the for loop. Here it is (briefly):

Code: Select all

<?php
$fp = fopen("proroster.csv", 'r');
for (i=0; i=feof($fp); i++) {
   $line = fgets($fp); 
   // remove any extra white space or carriage returns 
   $line = chop($line); 
    
        
   // spilt each line into individual fields 
   $names = explode(",", $line); 

   // print the first name 
  print $names[i]; 

   // print the last name 
   //print $names[i]; 
    
  // print "<br>"; 
    
}
That's the general idea, check the syntax though.

cheers!

Posted: Mon Aug 18, 2003 6:21 pm
by jkmcgrath
Nope I couldnt get this to work either :(

But thanks for the ideas

Posted: Mon Aug 18, 2003 6:23 pm
by evilmonkey
Well, the idea is there. I just don't know how to work with files :(. Don't just copy and paste my code because there is a 99.9% chance that it's wrong. But what I do know is that arrays are cycled through 'for' and 'foreach'. Research the foreach fuction as well. Also, look at feof() (end of file) to get your last value. Do you know how the for structure works? The first value is the original value (0), the second is the stop value (feof() because you want it to go to the end of the file), and the third is the increments (i++ is the same as i=i+1, so next round you start off at 1 and not 0).

Good luck.

Posted: Tue Aug 19, 2003 4:23 am
by twigletmac
Try file() and foreach:

Code: Select all

<?php

// instead of fopen() try file():
$lines = file('proroster.csv');

foreach ($lines as $line_no => $line) {
	// trim the lines
	$line = trim($line);
	$names = explode(',', $line);
	
	echo '<h1>Names on line no. '.$line_no.'</h1>';
	// display each name
	foreach ($names as $name) {
		echo $name;
		echo '<br />';
	}
}

?>
Mac

Posted: Tue Aug 19, 2003 12:51 pm
by jkmcgrath
I understand evil monkey and I am greatful for the help. I tried it several ways. I am not a great PHP programmer but I love to do it lol.
So I am sure the problem is on my end, my apologies if that came across wrong.

Thanks twigletmac. I will give that a shot when I get home. I eventualy want all this to go in a DB. I can do it with brute force and a sledge hammer now but I want to make my code more efficent.

I will post what I have when I get home so it explains it better.

Posted: Tue Aug 19, 2003 1:37 pm
by evilmonkey
Wait, wait. A database? Why don't you start with a database? It's a lot more efficient and fasted that files. For example (let's use MySQL):

Code: Select all

//connect like so...
$db=mysql_connect("server","username","password") or die ("Failed to connect because of ".mysql_error());
//selcet your database
mysql_select_db("your_database", $db);
//pull out your information
$query="SELECT firstname, lastname FROM table";
$result=mysql_query($query, $db);
//cycle through the results
while ($row=mysql_fetch_assoc($result)) {
//display the results (you can put a table here if you want, or any other HTML code)
echo $row['firstname']." ".$row['lastname'];
//end cycling and displaying
}
mysql_close($db);
The reason for using databases is that they are a lot faster, more secure, and more functional.

Good luck.

Posted: Tue Aug 19, 2003 5:34 pm
by jkmcgrath
The files I am recieving are from a stat service. I dont have the option of starting with the DB I have to parse the flat file into a db.

Here is a snippet of the file I am parsing.

Code: Select all

; 1999 Week 17 99WK17QO.NFL  QSS Offensive Player Stats
;                                                  |--------  Passing  --------|-- Rushing --|- Receiving -|-   Kicking      FG Distances -|
;                                                                    2-                    2-            2-                  01  30  40
;Name                     ID #  Tm/ID  Pos I S P T Cmp Att Yds In TD Pt Sck/Yds Car Yds TD Pt Rec Yds TD Pt EPM EPA FGM FGA -29 -39 -49 50+ FL TDs by distance
Aguiar, Louie             3024 GB  9021 P    0 1 0   0   0   0  0  0  0   0   0   0   0  0  0   0   0  0  0   0   0   0   0   0   0   0   0  0 
Aikman, Troy                 2 DAL 9014 QB   1 1 0  23  32 288  0  2  0   0   0   3  -2  0  0   0   0  0  0   0   0   0   0   0   0   0   0  0 T004T090
Akers, David              1594 PHI 9016 PK   0 1 0   0   0   0  0  0  0   0   0   0   0  0  0   0   0  0  0   0   0   1   1   0   0   1   0  0 G046
Alexander, Derrick        1020 KC  9010 WR   1 1 0   0   0   0  0  0  0   0   0   0   0  0  0   1  18  0  0   0   0   0   0   0   0   0   0  0
And the code I am using to parse it into the DB

Code: Select all

<?PHP 
//read.php
// Database settings
$dbhost     = "localhost";	// Database host
$dbname     = "nfl";	// Database name
$dbusername = "un";		// Database user name
$dbuserpw   = "pw";			// Database password
$dbtable	= "off_players";     // table of servers
$link_id = mysql_connect($dbhost,$dbusername,$dbuserpw)
			or die("Could not connect to MySQL.");
//echo("Connection Successfull.<br>");
$selected = mysql_select_db($dbname,$link_id)
			or die("Could not select database");
			
$offPlayers = "./nfl.txt" ;
if (!($fp = fopen($offPlayers, "r"))) die ("Cannot open file");
$player = fgets($fp);
$player = fgets($fp);
$player = fgets($fp);
$player = fgets($fp);
do
&#123;
$fullname = fread($fp,25);
$fullname = trim($fullname);
// spilt each line into individual fields 
   $names = explode(",", $fullname); 
$offset = fgetc($fp);
$id = fread($fp, 4);
$id = trim($id);
$offset = fgetc($fp);
$team = fread($fp, 3);
$team = trim($team);
$offset = fgetc($fp);
$teamid = fread($fp, 4);
$teamid = trim($teamid);
$offset = fgetc($fp);
$pos = fread($fp, 2);
$pos = trim($pos);
$offset = fgetc($fp);
$inj_stat = fread($fp, 1);
$inj_stat = trim($inj_stat);
$offset = fgetc($fp);
$start = fread($fp, 1);
$start = trim($start);
$offset = fgetc($fp);
$played = fread($fp, 1);
$played = trim($played);
$offset = fgetc($fp);
$inact = fread($fp, 1);
$inact = trim($inact);
$offset = fgetc($fp);
$pcmp = fread($fp, 3);
$pcmp = trim($pcmp);
$offset = fgetc($fp);
$patt = fread($fp, 3);
$patt = trim($patt);
$offset = fgetc($fp);
$pyds = fread($fp, 3);
$pyds = trim($pyds);
$offset = fgetc($fp);
$pint = fread($fp, 2);
$pint = trim($pint);
$offset = fgetc($fp);
$ptds = fread($fp, 2);
$ptds = trim($ptds);
$offset = fgetc($fp);
$p2pt = fread($fp, 2);
$p2pt = trim($p2pt);
$offset = fgetc($fp);
$psck = fread($fp, 3);
$psck = trim($psck);
$offset = fgetc($fp);
$psckyds = fread($fp, 3);
$psckyds = trim($psckyds);
$offset = fgetc($fp);
$car = fread($fp, 3);
$car = trim($car);
$offset = fgetc($fp);
$ryds = fread($fp, 3);
$ryds = trim($ryds);
$offset = fgetc($fp);
$rtds = fread($fp, 2);
$rtds = trim($rtds);
$offset = fgetc($fp);
$r2pt = fread($fp, 2);
$r2pt = trim($r2pt);
$offset = fgetc($fp);
$rec = fread($fp, 3);
$rec = trim($rec);
$offset = fgetc($fp);
$recyds = fread($fp, 3);
$recyds = trim($recyds);
$offset = fgetc($fp);
$rectds = fread($fp, 2);
$rectds = trim($rectds);
$offset = fgetc($fp);
$p2pts = fread($fp, 2);
$p2pts = trim($p2pts);
$offset = fgetc($fp);
$xpm = fread($fp, 3);
$xpm = trim($xpm);
$offset = fgetc($fp);
$xpa = fread($fp, 3);
$xpa = trim($xpa);
$offset = fgetc($fp);
$fgm = fread($fp, 3);
$fgm = trim($fgm);
$offset = fgetc($fp);
$fga = fread($fp, 3);
$fga = trim($fga);
$offset = fgetc($fp);
$fgm1 = fread($fp, 3);
$fgm1 = trim($fgm1);
$offset = fgetc($fp);
$fgm3 = fread($fp, 3);
$fgm3 = trim($fgm3);
$offset = fgetc($fp);
$fgm4 = fread($fp, 3);
$fgm4 = trim($fgm4);
$offset = fgetc($fp);
$fgm5 = fread($fp, 3);
$fgm5 = trim($fgm5);
$offset = fgetc($fp);
$fum = fread($fp, 2);
$fum = trim($fum);
$offset = fgetc($fp);
$td_dist = fgets($fp);
$td_dist = trim($td_dist);
echo $td_dist,"<BR>";
$query = "INSERT INTO $dbtable VALUES ('$names&#1111;0]','$names&#1111;1]','$id','$team','$teamid','$pos','$inj_stat','$start','$played','$inact','$pcmp','$patt','$pyds','$pint','$ptds','$p2pt','$psck','$psckyds','$car','$ryds','$rtds','$r2pt','$rec','$recyds','$rectds','$p2pts','$xpm','$xpa' ,'$fgm','$fga','$fgm1','$fgm3','$fgm4','$fgm5','$fum','$td_dist')";
$result = mysql_query($query);
$count = $count + "1";
&#125;
while(!Feof($fp));
fclose($fp);
echo "You added ",$count," records.";
?>
Now I am trying to improve that code to parse the file by using an array and also when I pull the data back in an array, I dont want to have to so much typing to acheive the results.

Does this make any sense?