I'm sorry for my ignorance, but I'm fairly new to PHP, so let me explain what I'm trying to do.

I'm using a CSV file as my data source, which isn't a problem, I can read/write fine.

I'm using this code to create a table from the CSV file:

<?php
$filename = "feedback.csv";
$id = fopen($filename, "r");
while ($data = fgetcsv($id, filesize($filename),";"))
$table[] = $data;
fclose($id);
echo "<table width=\"100%\" border=\"5\">\n";
foreach($table as $row)
{
echo "<tr>";
foreach($row as $data)
echo "<td>$data</td>";
echo "</tr>\n";
}
echo "</table>\n";
?>

The first field in the CSV file is a unique ID number. What I want to do is make that column in the table links.

For example, lets say an ID number is 12345. I would want that ID to link to something like contactdetail.php?ID=12345.

How would I do that? Also, for the contactdetail.php page, how would I retrieve that ID number and pull that one row of data from the CSV file into editable text boxes?

I know how to do this in ASP.NET, but this requires PHP since its on a Linux server.

Thanks for any help you can provide.

Here is an example.

<?php
$handle = fopen('somefile.csv', 'r');
if ($handle)
{
  
    //the top line is the field names
    $fields = fgetcsv($handle, 4096, ',');
   
    //loop through one row at a time
    while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
    {
        $name=$data[0];
        $address=$data[1];
        print "somesite.com?id=".$name.$address;
    }

    fclose($handle);
}
?>

If it isn't clear enough, just ask :)

Here is an example.

<?php
$handle = fopen('somefile.csv', 'r');
if ($handle)
{
  
    //the top line is the field names
    $fields = fgetcsv($handle, 4096, ',');
   
    //loop through one row at a time
    while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
    {
        $name=$data[0];
        $address=$data[1];
        print "somesite.com?id=".$name.$address;
    }

    fclose($handle);
}
?>

If it isn't clear enough, just ask :)

Thanks for that, it helps a little. Is there a way I can alter my existing code to be able to do this for just the ID (the first column in my table)?

Also this line

print "somesite.com?id=".$name.$address;

Doesn't create a URL, just text, can I alter that to create a URL out of it?

Is there a way I can alter my existing code to be able to do this for just the ID (the first column in my table)?

Use print_r to display the array contents and use the required field. ie., print_r($data);

Doesn't create a URL, just text, can I alter that to create a URL out of it?

print "<a href='http://www.somesite.com?id=$id'> Click here </a>"; ?

Thanks again for all your help. I have that page setup how I need it now thanks to your help. I'm able to pass the ID through the URL to a new page without a problem.

I know how to do this part in ASP.NET, but not PHP, so I need help with one more thing.

What I need is a statement that will pull the data for a specific ID.

For example, if my CSV has:
ID;First;Last
123;Robert;Russell
124;John;Smith

I would need a statement that will pull the data for the passed ID (like 123). I'm guessing the best way to do this would be to create an array with the data that matches that ID.

Then I would need some kind of statement that would update fields for that ID rather than adding a new record. It could re-write that whole record if that's easier.

Again, any help is appreciated.

As you have said it already, You can get the contents of the csv file to an array (keeping ID as key), modify/update the required records, write the data back to csv again using fputcsv .

As you have said it already, You can get the contents of the csv file to an array (keeping ID as key), modify/update the required records, write the data back to csv again using fputcsv .

Could you please give me an example of how I could create an array with just the data that matches a specific ID?

Sorry for my ignorance in this whole thing.

*Bump*

Can anyone help me with this last part? I need to pull a single record from a CSV file that matches the ID, then be able to edit that data via text boxes, then submit it and have it update that record in the CSV file.

I could easily do this with SQL statements, but seeing as I have to use a CSV file instead, I'm not sure how to even start.

Thanks in advance.

<?php
$handle = fopen("test.csv", "r"); //open the csv file
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { //get the contents
   $id = $data[0]; //first field is the id
   $string = implode(";",$data); //create a string from the array
   $dataholder[$id] = $string; //assign it to another array keeping id as index
   //echo $string."<br>";
}
fclose($handle); //close the file


$id = 2; //id specified by the user
if(array_key_exists($id,$dataholder)) {  //if that id is in the array that we created
	$value1 = "newvalues34"; //write the new values for that id
	$value2 = "newvalues34";
	$dataholder[$id] = $id.";".$value1.";".$value2; //update the values for that id
	
}

$fp = fopen("test.csv","w"); //open the file for writing.. using w as mode will place the file pointer at the beginning (so everything you had in your file is gone!)
foreach ($dataholder as $line) { //for each element of dataholder array
	$line = $line."\n"; 
   fwrite($fp,$line); //write the values to the file
}
fclose($fp); //close the file

?>

I have written comments wherever necessary. First of all, you get all the contents of the csv file to an array keeping the id as the index.
Then for a specific id, change/update the data and put it back to the array.
Again, open the file in write mode and write the contents of the array to the file.
I agree, this isn't a beautiful solution (Infact, if your system crashes after opening the file in write mode, the original data is lost!). But yep, this is all I could think of.

Thanks again for your help! I know I'm getting close.

I put the write section in a function with a submit button, so that won't get executed until I click on submit.

So the $dataholder array would just have the data for the matched ID? What if I want to pull the individual fields from that array? I was trying to alter the code and couldn't get it to work.

I would want this because that way when I go to update the record I can rewrite the entire record with the original values except for the new/altered values.

I know normally I could do $firstName = dataholder[2]; etc. but that doesn't seem to work.

So the $dataholder array would just have the data for the matched ID?

No. In the above example, $dataholder will have all the records of the csv file.

What if I want to pull the individual fields from that array?

You can do that by specifying the id. If you want to pull the record for id 4, $dataholder[4] will pull the relevant record.

How would I pull just one field from $dataholder?

So $dataholder[4] would pull that entire record, which might look like id;date;fname;lname. What if I wanted to pull and display fname?

Use explode to explode the string to an array and use the 2nd index. ie., $array[2].

Almost there! I'm getting stuck at writing the updated array back to the file. It ends up erasing everything in the CSV file. I'm sure its something simple.

Here's my code for the entire page if you can help me (I took out the URL in the header redirects, but I didn't change anything else)!

<?php
$UID = $_GET['uid'];  //get UID passed from previous page

//call functions from form buttons
if ($_POST['send']) {
	writeFile();
} elseif ($_POST['cancel']) {
	redirect();
}

$handle = fopen("feedback.csv", "r"); //open the csv file as read only
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { //get the contents
    $id = $data[0]; //first field is the ID
	
	$string = implode(";",$data); //create a string from the array
   $dataholder[$id] = $string; //assign it to another array keeping id as index
	
   //$dataholder[$id] = $data[0] . ';' . $data[1] . ';' . $data[2] . ';' . $data[3] . ';' . $data[4] . ';' . $data[5] . ';' . $data[6] . ';' . $data[7] . ';' . $data[8] . ';' . $data[9] . ';' . $data[10] . ';' . $data[11] . ';' . $data[12] . ';' . $data[13] . ';' . $data[14];
   
   $updatedRecord = explode(';', $dataholder[$UID]); //create a new array with the record matching the UID
}
fclose($handle); //close the file


$id = $UID; //id specified by the user
if(array_key_exists($id,$dataholder)) {  //if that id is in the array that we created
	$value1 = $updatedRecord[1]; //write the new values for that id
	$value2 = $updatedRecord[2];
	$value3 = $updatedRecord[3];
	$value4 = $updatedRecord[4];
	$value5 = $updatedRecord[5];
	$value6 = $updatedRecord[6];
	$value7 = $updatedRecord[7];
	$value8 = $updatedRecord[8];
	$value9 = $updatedRecord[9];
	$value10 = $updatedRecord[10];
	$value11 = $updatedRecord[11];
	$value12 = $updatedRecord[12];
	$tempValue13 = $updatedRecord[13]; //pull old value from array
	$tempValue14 = $updatedRecord[14]; //pull old value from array
	$value13 = $_POST['concerning']; //assign new value
	$value14 = $_POST['employeeComments']; //assign new value
	$dataholder[$id] = $id.";".$value1.";".$value2.";".$value3.";".$value4.";".$value5.";".$value6.";".$value7.";".$value8.";".$value9.";".$value10.";".$value11.";".$value12.";".$value13.";".$value14; //update the values for that id
	
}

//writeFile function to update the record
function writeFile()
{
$fp = fopen("feedback.csv","w"); //open the file for writing.. using w as mode will place the file pointer at the beginning (so everything you had in your file is gone!)
foreach ($dataholder as $line) { //for each element of dataholder array
	$line = $line."\n"; 
   fwrite($fp,$line); //write the values to the file
}
fclose($fp); //close the file

header("Location: URL");
}

//function to redirect if cancel button clicked
function redirect()
{

	header("Location: URL");
}

?>


<!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 name="Robots" content="all,index,follow" />
<meta http-equiv="Pragma" content="no-cache" />
<title>CASA - Contact Us Details</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['DOCUMENT_URL']?>">
<table border="5">
	<tr>
		<td>ID:</td>
		<td><?php echo $UID?></td>
	</tr>
	<tr>
		<td>Date:</td>
		<td><?php echo $value1?></td>
	</tr>
	<tr>
		<td>First Name:</td>
		<td><?php echo $value2?></td>
	</tr>
	<tr>
		<td>Last Name:</td>
		<td><?php echo $value3?></td>
	</tr>
	<tr>
		<td>Address:</td>
		<td><?php echo $value4?></td>
	</tr>
	<tr>
		<td>City:</td>
		<td><?php echo $value5?></td>
	</tr>
	<tr>
		<td>State:</td>
		<td><?php echo $value6?></td>
	</tr>
	<tr>
		<td>Zip:</td>
		<td><?php echo $value7?></td>
	</tr>
	<tr>
		<td>Email:</td>
		<td><?php echo $value8?></td>
	</tr>
	<tr>
		<td>Phone:</td>
		<td><?php echo $value9?></td>
	</tr>
	<tr>
		<td>Concerning:</td>
		<td><?php echo $value10?></td>
	</tr>
	<tr>
		<td>User Comments:</td>
		<td><?php echo $value11?></td>
	</tr>
	<tr>
		<td>Hear About Us:</td>
		<td><?php echo $value12?></td>
	</tr>
	<tr>
		<td>Have Contacted:</td>
		<td><select name="haveContacted" id="haveContacted">
                <option value="None"> --- </option>
                <option value="Emailed">Emailed</option>
                <option value="Phone Call">Phone Call</option>
                <option value="Mailed">Mailed</option>
                <option value="Other">Other</option>
              </select>
			&nbsp;&nbsp;Old Value:&nbsp;&nbsp;<?php echo $tempValue13?></td>
	</tr>
	<tr>
		<td>Employee Comments:</td>
		<td><textarea name="employeeComments" id="employeeComments" rows="5" cols="50"><?php echo $tempValue14?></textarea></td>
	</tr>
	<tr>
		<td colspan="2" align="center">
			<input type="submit" name="send" value="Submit" class="button"/>
            <input type="submit" name="cancel" value="Cancel" class="button"/>
		</td>
	</tr>
</table>			  
</form>
</body>

You need to change the mode to a or a+

function writeFile()
{
$fp = fopen("feedback.csv","a"); //open the file for writing.. using a as mode so that the pointer is at the end of the file
...
}

You need to change the mode to a or a+

function writeFile()
{
$fp = fopen("feedback.csv","a"); //open the file for writing.. using a as mode so that the pointer is at the end of the file
...
}

I think there's still an issue somewhere. I tried that and it didn't add/change anything in the CSV file.

Not sure if this will fix your problem, but this is definitely wrong:

function writeFile()
{
  $fp = fopen("feedback.csv","a"); 

  foreach ($dataholder as $line)
  {
      $line = $line."\n"; 
      fwrite($fp,$line); //write the values to the file
  }
  fclose($fp); //close the file
  header("Location: URL");
}

Where did $dataholder come from? If you want to use the var you created higher in your code, than you need to do one of two things. First, declare that you are using the global variable, $dataholder, in your function. Variables in function are local to that function by default in most any programming language.

So,

function writeFile()
{
  global $dataholder;
  $fp = fopen("feedback.csv","a"); 

  foreach ($dataholder as $line)
  {
      $line = $line."\n"; 
      fwrite($fp,$line); //write the values to the file
  }
  fclose($fp); //close the file
  header("Location: URL");
}

A better solution is to pass the variable to the function:

function writeFile($data)
{
  $fp = fopen("feedback.csv","a"); 

  foreach ($data as $line)
  {
      $line = $line."\n"; 
      fwrite($fp,$line); //write the values to the file
  }
  fclose($fp); //close the file
  header("Location: URL");
}


// $dataholder declared and set here
writeFile($dataholder); // call function passing our array

Why? Localizing the scope of a function makes them versatile and easily reused. The stand-alone, so to speak.

Oh, and you still want to use a mode, for append data to file.

mcd is right. You need to pass the array as a parameter if you want it to use inside a function.

Thanks! I had to change a few other things around but its finally working!!!

Thanks alot for your help everyone!