Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Aug 2009
    Posts
    215
    Thanks
    74
    Thanked 0 Times in 0 Posts

    ShoutBox , Need Someone to finish

    I have this Very Simple ShoutBox , and looking for someone Finish it by adding Statment to be able delete Message's ..

    Mysql Tabel
    PHP Code:
    CREATE TABLE `shoutbox` (
    `
    idint(11NOT NULL auto_increment,
    `
    nametext NOT NULL,
    `
    messagelongtext NOT NULL,
    `
    timetext NOT NULL,
    PRIMARY KEY  (`id`)
    TYPE=MyISAM
    Shoutbox.php
    PHP Code:
    <?
    //the host, name, and password for your mysql
    mysql_connect("localhost","user","password");

    //select the database
    mysql_select_db("spoono_sections");

    if(
    $submit)
    {
       
    //use the PHP date function for the time
       
    $time=date("h:ia d/j/y");
       
       
    // inserting it into the shoutbox table which we made in the mysql statements before
       
    $result=MYSQL_QUERY("INSERT INTO shoutbox (id,name,message,time)".
          
    "VALUES ('NULL','$name', '$message','$time')");
    }
    ?>
    <?
    //returning the last 5 messages
    $result mysql_query("select * from shoutbox order by id desc limit 5");

    //the while loop
    while($r=mysql_fetch_array($result))
    {        
       
    //getting each variable from the table
       
    $time=$r["time"]; 
       
    $id=$r["id"];
       
    $message=$r["message"];
       
    $name=$r["name"];
    ?>
       <? echo $time ?><br>
       <? echo $name ?><br>
       <? echo $message ?><br>
    <? ?>
    <form action="<? echo $php_self ?>" method="post">
    <INPUT TYPE='TEXT' value='name' NAME='name' SIZE=30 maxlength='100'><br>
    <INPUT TYPE='TEXT' value='message' NAME='message' SIZE=30 maxlength='100'>
    <input type="submit" name="submit" value="submit">
    </form>

  • #2
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    Use $_GET in a link like

    PHP Code:
    echo '<a href="shoutbox.php?delete=' $id '">[X]</a>'
    Then MySql

    PHP Code:
    <?php
    if (isset($_GET['delete']))
    {
       
    mysql_query('DELETE FROM `shoutbox` WHERE `id` = \' ' . (int) $_GET['delete'] . ' \' ') or die(mysql_error());
    }
    Last edited by Zangeel; 09-03-2009 at 04:49 AM.
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • Users who have thanked Zangeel for this post:

    ajloun (09-03-2009)

  • #3
    Regular Coder
    Join Date
    Aug 2009
    Posts
    215
    Thanks
    74
    Thanked 0 Times in 0 Posts
    Thank you Man ,

    Just One thing , Why it takes tow Clicks to X to delete the Message?
    Last edited by ajloun; 09-03-2009 at 06:53 AM.

  • #4
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    It takes two clicks to delete it?
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • Users who have thanked Zangeel for this post:

    ajloun (09-04-2009)

  • #5
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    add this to code

    PHP Code:
    <?php
    if (isset($_GET['delete']))
    {
       
    mysql_query('DELETE FROM `shoutbox` WHERE `id` = \' ' . (int) $_GET['delete'] . ' \' ') or die(mysql_error());
    header("Location: ");
    }

  • Users who have thanked funnymoney for this post:

    ajloun (09-04-2009)

  • #6
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    I think if he adds my code above the code to output shoutbox messages that wouldn't happen.

    Ultimately I would use JS, and AJAX on this though. Ajax to delete it, JS to remove it cosmetically without needing to refresh, like adding an ID to each message then onclick, send an xmlhttp request then making the id of the message display = 'none' woul make it much smoother.
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • Users who have thanked Zangeel for this post:

    ajloun (09-04-2009)

  • #7
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    think something like this?

    shoutbox.php
    PHP Code:
    <?php
    session_start
    ();

    if (!empty(
    $_SESSION['name']) && isset($_POST['shout'])) {
        
    $handle fopen("shouts.txt""a+");
        
    fwrite($handletime()." <strong>Name</strong>: ".$_SESSION['name']. " <strong>shout:</strong> ".htmlspecialchars($_POST['shout'])."\n");
        
    fclose($handle);
        
    header("Location: shoutbox.php");
    }

    if (empty(
    $_SESSION['name']) && !isset($_POST['name'])) {
    $html =
    <<<EOF
    <form action="" method="post">
    <p>Your name <input type="text" name="name" value="" /></p>
    <input type="submit" value="Enter name" />
    </form>
    EOF;
    }
    elseif (empty(
    $_SESSION['name']) && isset($_POST['name'])) {
        
    $_SESSION['name'] = htmlspecialchars($_POST['name']);
        
    header("Location: shoutbox.php");
    }
    else {
    $html =
    <<<EOF
    <form action="" method="post">
    <p>
    {$_SESSION['name']} <input type="hidden" name="name" value="{$_SESSION['name']}" /> <input type="text" name="shout" value="Shout here..." /></p>
    <input type="submit" value="SHOUT!" />
    </form>
    <p><a href="?logout=1">Logout</a></p>

    EOF;
    }

    if (isset(
    $_GET['logout'])) {
        unset(
    $_SESSION);
        
    session_destroy();
        
    header("Location: shoutbox.php");
    }

    if (isset(
    $_GET['ajax'])) {

    $handle     fopen("shouts.txt""r");
    $contents     stream_get_contents($handle);


    $shouts file("shouts.txt");
    $size =  count($shouts);

    $all "";
    $i 0;

    while (
    $size >= 0) {
        
    $all .= "<p>".$shouts[$size]."</p>";
        
    $size--;
        
    $i++;
        if (
    $i 10) {
        exit;
        }
    }
    print 
    $all;

    }

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
     
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="texthtml;charset=utf-8" />
    <script type="text/javascript">
    function ajaxFunction()
    {
    var xmlhttp;
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else if (window.ActiveXObject)
      {
      // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    else
      {
      alert("Your browser does not support XMLHTTP!");
      }
    xmlhttp.onreadystatechange=function()
    {
    if(xmlhttp.readyState==4)
      {

      document.getElementById("shouts").innerHTML = xmlhttp.responseText;
      }
    }
        xmlhttp.open("POST","shoutbox.php?ajax=1",true);
        xmlhttp.send(null);
    }

    var c=0
    var t

    function timedCount()
    {
    ajaxFunction();
    t=setTimeout("timedCount()",4000);
    }

    </script>
    </script>
    <style type="text/css">

    </style>
    </head>
    <body onload="timedCount()">

    <div id="shouts">

    </div>
    <?php

    if (!isset($_GET['ajax'])) {
        print 
    $html;
    }

    ?>
    </body>
    </html>
    feel free to improve it with jquery

  • Users who have thanked funnymoney for this post:

    ajloun (09-04-2009)

  • #8
    Regular Coder
    Join Date
    Aug 2009
    Posts
    215
    Thanks
    74
    Thanked 0 Times in 0 Posts
    @funnymoney than u Man for the solution you posted above ..

  • #9
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    no problem, although it's not a solution, more like an idea. you can mess around with code to get more neat solution.

    i noticed that this part of code needs a bit more work but i belive you can handle it yourself

    PHP Code:
    if (isset($_GET['ajax'])) {

    $handle     fopen("shouts.txt""r");
    $contents     stream_get_contents($handle);


    $shouts file("shouts.txt");
    $size =  count($shouts);

    $all "";
    $i 0;

    while (
    $size >= 0) {
        
    $all .= "<p>".$shouts[$size]."</p>";
        
    $size--;
        
    $i++;
        if (
    $i 10) {
        exit;
        }
    }
    print 
    $all;



  • Users who have thanked funnymoney for this post:

    ajloun (09-05-2009)

  • #10
    Regular Coder
    Join Date
    Aug 2009
    Posts
    215
    Thanks
    74
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by funnymoney View Post
    i noticed that this part of code needs a bit more work but i belive you can handle it yourself


  •  

    Posting Permissions

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