SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2001
    Location
    Australia
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Guestbook embedded in a page?

    I am trying to figure out how to add a comment page to one of my own pages as seen here http://www.robhoran.com/videos/Video...p?Video_ID=311 i have seen guestbook cgi scripts before but dont know how to do it like the one on this site, can someone please help me to figure it out? Is it a cgi script or something more complicated?

    Steve.

  2. #2
    SitePoint Wizard Rick's Avatar
    Join Date
    Oct 2002
    Location
    Lancashire, UK
    Posts
    3,847
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The page you linked just uses an ASP script and a database to take user comments, store them and list them on the page.

    This would be very simple to do (I could have prolly written the script in the time its taking me to type this and watch the video on the site).

    You could just use a form taking the name and comments of the user, add this to a database and then output all (or possibly just the latest 20 or so) comments onto the page.

    Kevin Yanks book, building a database driven site using PHP and MySQL explains how to do this (its pretty much his first example - he uses jokes though). The book is here: http://sitepoint.com/books/?bookid=kevinyank

    Rick
    Last edited by zoo; Nov 4, 2002 at 11:26.
    Rick

  3. #3
    SitePoint Wizard Rick's Avatar
    Join Date
    Oct 2002
    Location
    Lancashire, UK
    Posts
    3,847
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey sgwilks

    Becuase I'm such a nice guy I have written a script that will do your guestbook thingy. Well OK I didn't write it especially, but I did modify it for you

    I have uploaded an example here: http://www.hilariousminds.com/comments/index.php

    The code is below...

    But heres what you need to do:

    Using the following code create a MySQL table called comments...
    PHP Code:
    #
    # Table structure for table `comments`
    #

    CREATE TABLE comments (
      
    page_id smallint(6NOT NULL default '0',
      
    date text NOT NULL,
      
    user text NOT NULL,
      
    comment text NOT NULL
    TYPE=MyISAM;

    #
    # Dumping data for table `comments`
    #

    INSERT INTO comments VALUES (1'Monday 4th Nov 2002''Ricky''Very Nice'); 
    Then edit the script with your details - i.e. host address (In most cases localhost), username and password - get these off your host if you unsure.

    You can add the code between the first <?php tag and the last ?> tag, into your .php wherever you want your comments system.

    PHP Code:
    <html>
      <head>
        <title>Comments system</title>
      </head>

      <body>

        <P>This is our content for you to comment on!</P>

    <?php

    // the script...

    if (isset($addcomment)):
    ?>

    <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
    <P><B>Your Name:</B>&nbsp;<INPUT TYPE="TEXT" NAME="user" value="your name..."><BR>
    <B>Your Comment:</B>&nbsp;<INPUT TYPE="TEXT" NAME="comment" value="your comment..."></P>
    <P><input type="submit" name="submit" value="OK"></P>
    </FORM>

    <? else:

     
    // The page ID to get us going
        
    $page_id "1";

     
    // OK lets connect to MySQL 
        // Connect to database server...
        
    $dbcnx mysql_connect('localhost' 'username' 'password');

        if (!
    $dbcnx) {
            echo (
    '<P>Sorry, Unable to connect to the database server</P>');
        } else {

        
    // Select the database
        
    if (! @mysql_select_db(z001) ) {
            echo(
    '<P>Sorry, Unable to locate the ' $dbname ' database at this time</P>');
            exit();
        }
        }

     
    // If a user has submitted a comment, lets add it to the database...

        
    if ("OK" == $submit) {
        
    // Define the date
        
    $date date('l jS M Y');
          
    // Note: you could also replace $date with curdate() in the SQL command

        // The SQL Query
        
    $sql "INSERT INTO comments SET  page_id='$page_id', comment='$comment', date='$date', user='$user'";

        
    // Insert the comment into the database
        
    if(mysql_query($sql)) {
            echo (
    'Your comment has been added<BR>');
            
    // Send the user back to the page
        
    } else {
            echo (
    '<P>Sorry, error adding joke:<br>'
                  
    mysql_error() . '</P>');
            exit();
        }
        }

     
    // Now lets display all comments...

        
    echo ('<P>Here is what our users have said so far...</P>');

        
    $result mysql_query("SELECT comment, date, user FROM comments WHERE page_id='$page_id'");

            
    // If theres a problem....
                
    if (!$result) {
                    echo(
    '<P>Sorry - error getting comments</P>');
                } else {

                    
    // Display the text of each comment in a paragraph
                    
                    
    while ( $row mysql_fetch_array($result) ) {

                        echo(
    '<P>' $row['user'] . ' said ' $row['comment'] . ' on ' $row['date'] . '</P>');
                    }

                }

            
    // Display a link to add comment...

                
    echo ('<a href="' $PHP_SELF '?addcomment=true">Add Comment</a>');

        
    // Thats it for showing comments, and adding them for that matter!

    endif;

    ?>

    <P>Rent-a-footer</P>

      </body>
    </html>
    Anyone who thinks they can use this please do

    Good luck with your project

    Rick
    Rick


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •