Jump to content

[SOLVED] Array_walk or foreach??


crimsonjade

Recommended Posts

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 ^^"

Link to comment
https://forums.phpfreaks.com/topic/52142-solved-array_walk-or-foreach/
Share on other sites

<?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?

<?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";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.