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

Link to comment
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?

 

 

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

Link to comment
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.