View Full Version : Need some help with a simple script.

08-30-2006, 03:44 PM
Hi there, i am new to php and havent spent 2 long learning it. I know how to read and write files using perl/cgi but arent accustomed to the language of php. I was wondering if anyone had a easy way to open a file, search for values and either update them or print them to screen?

ie i want to make a .txt file with a list of pictures . and comments and descriptions located adjacent. ie

photo 1 - comment='How stupid do i look', description='on holidays'\n
photo 2 - comment='blah' ... etc etc
photo 3

so when i enter a new description or comment it will find the corresponding picture and update the fields....

and ideas. Just a push in the right direction would be stella.

08-30-2006, 05:55 PM
I would recommend you use a database such as MySQL rather than a file. If you don't have access to a database or just don't want to use one, then I would recommend you use a comma-delimited file to store the information.

You can use the fputcsv() function (http://us2.php.net/manual/en/function.fputcsv.php) to write a record to the file, and the delimiters will be handled automatically. Use fgetcsv() (http://us2.php.net/manual/en/function.fgetcsv.php) to read the file.

08-31-2006, 03:58 AM
ok ive used fputcsv but am having trouble getting the entire line to be replaced with the value. It keeps replacing the line below it?
THe code is really messy. any ideas?

$mytival = "";

if($action == "attachme") {
$mytival = "$comdesc";
$stsearch = "$imname";
$list = array ("$stsearch,$mytival");

$row = 1;
$done = false;
$fp = fopen("commentstore.csv", "r+");
while (($data = fgetcsv($fp, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$settext = $data[$c];

if ($settext == $stsearch){ // it seems to be stuffing up in this if statement?

foreach ($list as $line){ fputcsv($fp, split(",", $line));
$done = true;


if ($done == false){
$list = array ("$stsearch,$mytival");
$fp = fopen("commentstore.csv", "a+");
foreach ($list as $line) {
fputcsv($fp, split(",", $line));