#1  
Old 12th July 2007, 22:06
robj robj is offline
Junior Member
 
Join Date: Jul 2007
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default PHP Wishlist

Hi Everyone,

I am looking to write a basic php wishlist but am unsure where is best to start. It only needs to keep track of one list of items, and I want to store all the data in a text file. (or CSV etc)

I have looked at some wish lists and they require databases or are too advanced with 3rd party site integration. I want to find or write a very basic list that can be viewed and items can be removed and added via the php interface.

No authentication is needed. Any ideas would be very must appreciated.

Thanks from a beginner, really trying to learn PHP.
Reply With Quote
Sponsored Links
  #2  
Old 13th July 2007, 09:04
Ben Ben is offline
Moderator
 
Join Date: Jul 2006
Posts: 1,029
Thanks: 7
Thanked 62 Times in 56 Posts
Default

Ok, if you want to learn then you should start by doing this your own.
What you need is

-a form to create / edit existing entries (pure HTML, maybe +JS; depends on your needs)
- an output (ideally generated by php) page
- the script itself, that handles input / ouput / storage

at first you can decide, if you have one script handling everything or splitting in to simple scripts.
Next thing is on thinking about how to read data from a form, and how to handle files. But be aware that it is much easier to store data into a database (also the syntax / code is not really complicated) if you expect multiple parallel accesses to the data. Cause to change the data, you have to read the whole file, change the selected dataset and store everything (!) back.

To read data from a form / a http request, you take the superglobals $_GET / $_POST / $_REQUEST (combines POST + GET). If you do not know anything about arrays have a look here : (http://www.php.net/manual/en/ref.array.php)

For filehandling you should take a look at www.php.net for the functions
fopen, fgets, fputs, fclose.
Also take a look at is_resource to handle errors (or the "or die ..." - construct)

If you store the data you should think about marking each field with a sign, e.g. a colon etc.. so you need to split the read string as well as convert the data to such string.
Herefore the functions split, explode will be helpfull.
Reply With Quote
The Following User Says Thank You to Ben For This Useful Post:
robj (17th July 2007)
  #3  
Old 17th July 2007, 17:43
robj robj is offline
Junior Member
 
Join Date: Jul 2007
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Ben,

Thank you so much for your help, I have had a go and have managed to make some progress. I have a text file on the server that stores the data, a form to add new items, a page for viewing the wishlist and a edit page.

At the moment the edit page is just a text area showing the whole text file. When I access the page I get an error, having google'd it, most replies say to turn off error reporting. This seems like its just hiding the problem though. I have my error reporting high so I can make sure my code is ok. Adding
Code:
error_reporting(E_ALL ^ E_NOTICE);
does not seem like a fix.

Also what would be the best way of editing the items? I was thinking of getting the user to click an edit item link, and then brake the items data fields out into text fields.

What do you think?

Thank you so much for your help so far, this is my first PHP experience.

Code:
Notice: Undefined index: save_file in /var/www/html/wishlist/edit.php on line 13
Notice: Undefined index: savecontent in /var/www/html/wishlist/edit.php on line 14
Code:
<?php

require ('config.php');

$save_file = $_POST['save_file'];
$savecontent = $_POST['savecontent'];

$loadcontent = "$wishlistdata";
    if($save_file) {
        $savecontent = stripslashes($savecontent);
        $fp = @fopen($loadcontent, "w");
        if ($fp) {
            fwrite($fp, $savecontent);
            fclose($fp);
                header( "Location: $viewurl" ); // Take the user to the view page
        }
    }
    $fp = @fopen($loadcontent, "r");
        $loadcontent = fread($fp, filesize($loadcontent));
        $loadcontent = htmlspecialchars($loadcontent);
        fclose($fp);

echo "<html>\n";
echo "<head><title>$wishlistname</title></head>\n";
echo "<center>\n";
echo "<h1>$wishlistname</h1>\n";
echo "<a href=\"$viewurl\">View Wishlist</a> | <a href=\"$addurl\">Add Item</a> | Edit Wishlist<br><br>\n";
?>

<form method=post action="<?=$_SERVER['PHP_SELF']?>">
<textarea name="savecontent" cols="70" rows="25"><?=$loadcontent?></textarea>
<br>
<input type="submit" name="save_file" value="Save">
</form>

<?php
echo "</center>\n";
echo "</html>";
?>
Reply With Quote
  #4  
Old 17th July 2007, 18:42
Ben Ben is offline
Moderator
 
Join Date: Jul 2006
Posts: 1,029
Thanks: 7
Thanked 62 Times in 56 Posts
 
Default

Ok regarding the notices, I would disable the notices via the php.ini.
These notices appear, if you try to access non existing parts in an array.

Beside that I do not see a sense in copying vars in that short code.
I would leave out the 2nd and 3rd line and access them directly. And I would leave out some other parts:

PHP Code:
<?php

require ('config.php');


    if( isset(
$_POST['save_file']) && isset($_POST['savecontent']) ) {
        
$_POST['savecontent'] = stripslashes($_POST['savecontent']);
        
$fp = @fopen($wishlistdata"w");
        if(
$fp) {
            
fwrite($fp$_POST['savecontent']);
            
fclose($fp);
                
header"Location: $viewurl" ); // Take the user to the view page
                
exit;
        }
    }
    
    
//file_get_contents results in the same then the code below
      
$loadcontent = (is_file($wishlistdata)) ? htmlspeciachars(file_get_contents($wishlistdata)) : '';
    
/*
    $fp = @fopen($wishlistdata, "r");
        $loadcontent = fread($fp, filesize($loadcontent));
        $loadcontent = htmlspecialchars($loadcontent);
        fclose($fp);
    */
    
echo "<html>\n";
echo 
"<head><title>$wishlistname</title></head>\n";
echo 
"<center>\n";
echo 
"<h1>$wishlistname</h1>\n";
echo 
"<a href=\"$viewurl\">View Wishlist</a> | <a href=\"$addurl\">Add Item</a> | Edit Wishlist<br><br>\n";
?>

<form method=post action="<?=$_SERVER['PHP_SELF']?>">
<textarea name="savecontent" cols="70" rows="25"><?= $loadcontent ?></textarea>
<br>
<input type="submit" name="save_file" value="Save">
</form>

<?php
echo "</center>\n";
echo 
"</html>";
?>
But overall it looks great.
Next what you could think about is checking the input better, or splitting the code in central functions like wishlist_read(), wishlist_write() etc.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Freebsd 6.1 support misterm Installation/Configuration 10 9th April 2009 09:29
php eating cpu shajazzi Server Operation 15 15th August 2007 01:19
Slightly Confused (DNS & Server Help) JohnnyBGoode Installation/Configuration 26 14th August 2007 09:54
Apache2 Freezes celtic Server Operation 31 28th May 2007 17:18


All times are GMT +2. The time now is 15:24.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.