Hi, I'm new to php and need help. Please find the code below. It is supposed to retrieve data from my csv file and sort it by last name; placing it in a sorted table. I'm getting errors and don't know how to fix it. I'm also running out of time to get it done. Please help! :eek:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Telephone Directory Sorted Array</title>
</head>
<body>
<?php
// Opens and Gets the data found within the CSV file called data_file.csv
$datafile = "data_file.csv";
if (is_readable($datafile)){
 $data = file_get_contents("data_file.csv");
 explode(", ", $data);
 }
 // Associative Array
  $data["lastname"];
  $data["firstname"];
  $data["areacode"];
  $data["phonenum"];

  // Table header
  echo "<table border='1'>";
  echo "<tr>";
  echo "<th>Name</th>";
  echo "<th>Phone Number</th>";
  echo "</tr>";

  // Sort the associate array by key
  ksort($lastname);

  //Print out the sorted array
  foreach($data as $lastname => $phonenum) {
    echo "<tr>";
   echo "<td>$lastname, $firstname</td>";
 echo "<td>$areacode-$phonenum</td><br>";
 echo "</tr>";
  }
  echo "</table>";
?> 
</body>
</html>
    cbyrns1125;10936768 wrote:

    Hi, I'm new to php and need help. Please find the code below. It is supposed to retrieve data from my csv file and sort it by last name; placing it in a sorted table. I'm getting errors and don't know how to fix it. I'm also running out of time to get it done. Please help! :eek:

    and the errors are ?

      fgetcsv, would be a better place to start than file_get_contents

        Warning: ksort() expects parameter 1 to be array, null given in C:\wamp\www\ByrnsAssignment4\telephone_directory_array.php on line 29

        Warning: Invalid argument supplied for foreach() in C:\wamp\www\ByrnsAssignment4\telephone_directory_array.php on line 32

          well as the error says, ksort expects an array, your passing it nothing as $lastname is not defined.

          your explode is wrong, but that can be fixed when you replace file_get_contents with fgetcsv

          the lines under "Associative Array" are meaningless.

            Do you have other suggestions to try?

            I changed the $data = fgetcsv("data_file.csv"); but am now getting undefined variable. Where do I define the variable?

            How do I change the Associative Array for it not to be seen as null?

              read the manual page on fgetcsv(), you can't expect to replace one function with another and magicly have it working.

                // Associative Array
                $lastname[0] = "LastName";
                $firstname[1] = "FirstName";
                $areacode[2] = "AreaCode";
                $phonenum[3] = "PhoneNum";

                  cbyrns1125;10936793 wrote:

                  // Associative Array
                  $lastname[0] = "LastName";
                  $firstname[1] = "FirstName";
                  $areacode[2] = "AreaCode";
                  $phonenum[3] = "PhoneNum";

                  that's not going to help you, you have simply created 4 arrays with one key value pair in each

                    I can't 'pick' a value in the csv file, since there are so many different entries. How would you pick one then?

                      basically in here:

                          while(!feof($fp)) {
                             $line = fgets($fp);
                             echo "$line <br>";
                          }
                      

                      you will have to break each line into the components you are looking for (first,last,etc) and then add them to into either many arrays (more over head) or one comprehensive array that you can iterate over to make your table

                      what we really need to see is an example of one line of data so we can help you write the code that will break it into the components you need

                        cbyrns1125;10936797 wrote:

                        I can't 'pick' a value in the csv file, since there are so many different entries. How would you pick one then?

                        extract them in to an array, if your going to do any serious data manipulation, read the csv in to a db

                          Thread's merged; please don't re-post the same topic in new threads.

                            When creating algorithms where you don't instinctively know what to do, put your problem ito words, and start breaking it down, still using words.
                            I want an associative array of people from this file, doesn't help much, but you know some things about the data (that byt the way the rest of us don't, so I'm purely guessing):
                            Each line in the file contains a person.
                            Each person is described by CSV, where the 1st is fname, 2nd is lname etc.

                            So:

                            1. Get data for all people
                            2. Build an array from 1.
                            3. Sort array
                            

                            Might change into...

                            1. Build an array of people by...
                            	1. get data for one person
                            	2. add to result array
                            	3. repeat 1.1 until no more people
                            2. Sort array
                            

                            And then you break down some things into smaller parts

                            1.1 Get data for one person
                            	1. read line from file
                            	2. parse the different parts of the line to get: fname, lname, acode, phone
                            	3. store 1.2 as an array
                            		1. [this part is not included in my example code skeleton below]
                            		    make this array associative with keys 'lastName', 'firstName' etc
                            

                            You wanted to sort by last name, but you may realize that you also need to sort by first name to avoid things like:
                            Davis John
                            Davis Joe
                            So you split the sorting into
                            2.1 Sort by lname
                            2.2 sort by fname

                            But later down the line when you start replacing parts with actual code, you can even reduce several steps into one step sometimes.

                            2. Sort array
                            	1. Sort by lname and fname => array_multisort
                            

                            I think what you want is

                            fopen( , 'r');
                            while(fgets()) {
                            	str_getcsv();
                            	# associative array representing a person should be dealt with here
                            }
                            fclose();
                            array_multisort();
                            

                            Complete the blanks to have a working script. Then you can add code to change 0, 1, 2, 3 into 'firstName', 'lastName', 'areaCode' and 'phone' by adding more code where the comment is.

                              Write a Reply...