crimsonjade Posted May 19, 2007 Share Posted May 19, 2007 I'm working on a school project that requires me to echo out information in a table. How it's supposed to work is this: The first php script asks the user to input the proper username and password, and select the course file they want to work with from a selection box (drop down). The second php script receives the information and checks to see if the username and password are correct and if they are, it will proceed to echo out the information contained in the selected file in a table. The format of information within a file is like this: studentnumber,lastname,firstname,coursename,email So they're delimited by the comma. Each file contains a varying amount of lines. I have to split the information into a table with each piece of information in their own cell in the appropriate row. It's required that I use file();, trim();, and split(); however, when I try to use trim or split, I get this notice of an array to string conversion and I don't know how to go around it. $resultfile = $_GET["filename"]; if (!($_GET["filename"])) { die ("Please select a course file to work with."); } else { $file = file("./courses/".$resultfile); foreach ($file as $newPiece) { $newStr = trim($newPiece); $newLine = split(",",$newStr); } } I read that array_walk loops through an array and can perform functions on each element of the array, but I'm not quite sure how to go about it. So...how would you go about it for either foreach or array_walk? Any help would be much appreciated! Thanks in advance ^^" Quote Link to comment Share on other sites More sharing options...
thetxt Posted May 20, 2007 Share Posted May 20, 2007 maybe try using something like this in your foreach: foreach ($file as $key => $value) { // $key is your array index and $value is the value. i.e., $file[ $key ] = $value } Quote Link to comment Share on other sites More sharing options...
chigley Posted May 20, 2007 Share Posted May 20, 2007 <?php $resultfile = $_GET["filename"]; if (!($_GET["filename"])) { die ("Please select a course file to work with."); } else { $file = file("./courses/".$resultfile); foreach ($file as $newPiece) { $newStr = trim($newPiece); $newLine = explode(",",$newStr); // You now have an array like this: array(0 => studentnumber 1 => lastname 2 => firstname 3 => coursename 4 => email) which you can use however you want } } ?> Is that what you're after? Quote Link to comment Share on other sites More sharing options...
crimsonjade Posted May 21, 2007 Author Share Posted May 21, 2007 !!! I never thought of exploding it! Hmm, I'll try that out, thanks! Edit: I tried it...and all that comes out is "Array"...not my information at all :'( P.S. I also have to use split() as it's mandatory in the assignment *sigh* Quote Link to comment Share on other sites More sharing options...
crimsonjade Posted May 21, 2007 Author Share Posted May 21, 2007 <?php $resultfile = $_GET["filename"]; if (!($_GET["filename"])) { die ("Please select a course file to work with."); } else { $file = file("./courses/".$resultfile); foreach ($file as $newPiece) { $newStr = trim($newPiece); $newLine = explode(",",$newStr); // You now have an array like this: array(0 => studentnumber 1 => lastname 2 => firstname 3 => coursename 4 => email) which you can use however you want } } ?> Is that what you're after? Actually, ended up doing something like this: $resultfile = $_GET["filename"]; if (!($_GET["filename"])) { die ("Please select a course file to work with."); } else { $file = file("./courses/".$resultfile); foreach ($file as $newPiece) { $newStr = trim($newPiece); $tempArr = split(",",$newStr); foreach ($tempArr as $tempStr) { echo "\t\t<td>".$tempStr."</td>\n"; } echo "\t</tr>\n"; } echo "</table>\n"; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.