Kristoff1875 Posted August 11, 2011 Share Posted August 11, 2011 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. Quote Link to comment Share on other sites More sharing options...
guyfromfl Posted August 11, 2011 Share Posted August 11, 2011 It looks like you're reading the whole file into an array, then changing everything to Reserved. What is uniquely identifying which row gets changed to Reserved? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 11, 2011 Share Posted August 11, 2011 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... Quote Link to comment Share on other sites More sharing options...
Kristoff1875 Posted August 11, 2011 Author Share Posted August 11, 2011 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> Quote Link to comment Share on other sites More sharing options...
guyfromfl Posted August 11, 2011 Share Posted August 11, 2011 there could be potentially a fair few of these lists needed meaning having multiple databases could get confusing. You would store all the data in 1 table, with a ListID index, then SELECT * WHERE ListID='$desiredList'.. Quote Link to comment Share on other sites More sharing options...
Kristoff1875 Posted August 11, 2011 Author Share Posted August 11, 2011 How would I edit the data? Would I be able to make a page that can be managed by different people? So if there are 3 lists belonging to 3 people, they could all login to an admin page and update their list? Quote Link to comment Share on other sites More sharing options...
guyfromfl Posted August 11, 2011 Share Posted August 11, 2011 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 Quote Link to comment Share on other sites More sharing options...
Kristoff1875 Posted August 11, 2011 Author Share Posted August 11, 2011 Can I put all of those in to a table that contains all of them? So that I could then use another table for something else not related to the list? Quote Link to comment Share on other sites More sharing options...
guyfromfl Posted August 11, 2011 Share Posted August 11, 2011 No, if you did you would disrupt the data integrity. There would be no way to uniquely identify them. You should create a Database GuestList, then put those 3 tables in it. Quote Link to comment Share on other sites More sharing options...
Kristoff1875 Posted August 11, 2011 Author Share Posted August 11, 2011 Ok thanks, will give it a go now. Quote Link to comment Share on other sites More sharing options...
guyfromfl Posted August 11, 2011 Share Posted August 11, 2011 Cool, Try downloading MySQL Workbench, it will help you out alot.. Its a GUI tool from MySQL Let us know if you have any questions. Quote Link to comment Share on other sites More sharing options...
Kristoff1875 Posted August 11, 2011 Author Share Posted August 11, 2011 Have created a SQL file exported from my database. Would you be able to check it over? I've sent you a private message. Thanks Quote Link to comment Share on other sites More sharing options...
guyfromfl Posted August 11, 2011 Share Posted August 11, 2011 sure Quote Link to comment Share on other sites More sharing options...
Kristoff1875 Posted August 12, 2011 Author Share Posted August 12, 2011 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 Quote Link to comment Share on other sites More sharing options...
gizmola Posted August 12, 2011 Share Posted August 12, 2011 Please make a new topic for this, as you have moved onto an entirely different problem. Quote Link to comment Share on other sites More sharing options...
Kristoff1875 Posted August 12, 2011 Author Share Posted August 12, 2011 Ok, didn't know whether to make a new topic or not, sorry. Quote Link to comment Share on other sites More sharing options...
gizmola Posted August 12, 2011 Share Posted August 12, 2011 Ok, didn't know whether to make a new topic or not, sorry. No problem. Topics are not a scarce resource here Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.