Jump to content

PHP and CSV help please...


Kristoff1875

Recommended Posts

Hi, I need to make a gift list, and found one online that uses a CSV file to display the gifts, then when people choose a gift, it deletes the chosen one from the CSV. This is the code:

 

$fp = fopen('wishlist2.csv', 'w');

  	if (($handle = fopen("wishlist.csv", "r")) !== FALSE) {
  		$row = 1;
  	    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  	    	if ($row!=$aGift[0]) {
				fputcsv($fp,$data);
  	    	$row++;
		}
  	    	else {
  	    		$present = $data[0];
  	    		$row++;
  	    	}
        }
    fclose($handle);
    fclose($fp);
    unlink('wishlist.csv');
    rename('wishlist2.csv','wishlist.csv');
}
echo "<h1>Thanks for getting $present for me!</h1>";
}

 

I have added a data field which says "Available" And what I actually want is for this text to change to "Reserved" rather than the row to be deleted.

 

I changed it to this after some research:

 

$fname = "wishlist.csv";

$row = 1;
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));

$content = str_replace("Available", "Reserved", $content);

$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
fclose($fhandle);

 

Which does what I want, only it changes every "Available" in to "Reserved"

 

How do I change it only on the selected line?

 

There will no doubt be more questions after this one, but thanks in advanced for this! Total newbie to CSV having not used them before.

Link to comment
Share on other sites

Hi, I need to make a gift list, and found one online that uses a CSV file to display the gifts

 

it seems you're only using a .csv because that was what you found online. Why not convert it to a database? Otherwise, every time you need to change something, you'll have to read the entire file into arrays, replace what you want, then convert the arrays back to a flat format and write the whole file back...

Link to comment
Share on other sites

Thanks for the suggestion, I had considered a database... However i'd prefer not to use a database for 2 reasons. Firstly, it's a very small part of the site and secondly there could be potentially a fair few of these lists needed meaning having multiple databases could get confusing. Especially as there's only actually 3 data fields and it's not really gathering any information.

 

As for which row gets changed. Here is the script that i'm modifying:

 

http://coderoman.com/2011/01/php-wishlist-scriptapp/

 

giftlist page:

<form action="list/wishlist-form.php" method="post">    
<?php

$row = 1;
if (($handle = fopen("list/wishlist.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
	$num = count($data);

	switch ($num) {
	case 0:
		break;
	case 2:
		 echo "<input type=\"radio\" name=\"gift[]\" value=\"$row\" />
			$data[0] - $data[2]<br />";
		break;
	case 3:
		echo "<input type=\"radio\" name=\"gift[]\" value=\"$row\" />
			$data[0] - $data[1] - $data[2]<br />";
		break;
	case 4:
		echo "<input type=\"radio\" name=\"gift[]\" value=\"$row\" />
			<a href=\"$data[3]\" target=\"_blank\">$data[0]</a> - $data[1] - $data[2]<br />";
		break;
}

        $row++;
    }
    fclose($handle);
}
?>
<br />
<input type="submit" name="formSubmit" value="Click Here" />
</form>

 

CSV:

"Morphy Richards Red Accents slow cooker","£34.99","Available"
"Morphy Richards Red Accents slow cooker","£34.99","Available","http://www.houseoffraser.co.uk/Morphy+Richards+Red+Accents+slow+cooker+48728/146808112,default,pd.html"
"Morphy Richards Red Accents slow cooker","£34.99","Available","http://www.houseoffraser.co.uk/Morphy+Richards+Red+Accents+slow+cooker+48728/146808112,default,pd.html"
"Morphy Richards Red Accents slow cooker","£34.99","Available","http://www.houseoffraser.co.uk/Morphy+Richards+Red+Accents+slow+cooker+48728/146808112,default,pd.html"
"Morphy Richards Red Accents slow cooker","£34.99","Available","http://www.houseoffraser.co.uk/Morphy+Richards+Red+Accents+slow+cooker+48728/146808112,default,pd.html"

 

 

wishlist-form.php:

<?php
$aGift = $_POST['gift'];
if(empty($aGift)) 
echo "<h1>You didn't select any presents =(.</h1>";

else {

$fp = fopen('wishlist2.csv', 'w');
  	if (($handle = fopen("wishlist.csv", "r")) !== FALSE) {
  		$row = 1;
  	    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  	    	if ($row!=$aGift[0]) {
				fputcsv($fp,$data);
  	    	$row++;
		}
  	    	else {
  	    		$present = $data[0];
  	    		$row++;
  	    	}
        }
    fclose($handle);
    fclose($fp);
    unlink('wishlist.csv');
    rename('wishlist2.csv','wishlist.csv');
}
echo "<h1>Thanks for getting $present for me!</h1>";
}
?>

<p>You are the pride of [subject town].</p>

Link to comment
Share on other sites

Just a simple schema to get you started:

 

Table GiftList

ID_GiftList (Primary Key, Auto Increment, Not Null...)

Owner (Integer that holds ID_Users from User table)

ExpireDate

 

Table GiftListItems

ID_GiftListItem (Primary Key, Auto Increment, Not Null...)

GiftList (Stores the integer of the GiftList ID_GiftList... Identifies which GiftList it belongs)

Description

 

Table Users

ID_Users

Name

Username

Password

UserLevel

 

 

Link to comment
Share on other sites

Just a simple schema to get you started:

 

Table GiftList

ID_GiftList (Primary Key, Auto Increment, Not Null...)

Owner (Integer that holds ID_Users from User table)

ExpireDate

 

Table GiftListItems

ID_GiftListItem (Primary Key, Auto Increment, Not Null...)

GiftList (Stores the integer of the GiftList ID_GiftList... Identifies which GiftList it belongs)

Description

 

Table Users

ID_Users

Name

Username

Password

UserLevel

 

I'm quite new to this. I've set up the 3 tables, but how do I go about getting a GiftList from in the first table to contain the data from GiftListItems and Users tables?

 

Thanks in advanced

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.