PHP Wishlist

Discussion in 'Programming/Scripts' started by robj, Jul 12, 2007.

  1. robj

    robj New Member

    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.
     
  2. Ben

    Ben Active Member Moderator

    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.
     
  3. robj

    robj New Member

    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>";
    ?>
     
  4. Ben

    Ben Active Member Moderator

    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:
    <?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.
     

Share This Page