SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Approving posts

  1. #1
    Accredited Master scotty2260's Avatar
    Join Date
    Apr 2004
    Location
    California
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Approving posts

    I made a shoutbox with an admin pnel where you have to approve the post before it will show on the public site. I did this by creating a field called 'approve', and setting it to 'n'(not approved) or 'y' (yes approved).

    I am trying to display all the posts from the table where the field = n, and dispaly checkboxes next to each post, so that when the admin checks the posts that he wants to approve and clicks submit, it will appove those (set those to 'y'), and delete the ones that were left unchecked (the ones left 'n').

    Basically, I don't know how to see if the checkboxes are checked.

    Here is what i have so far:
    This is the form to display the posts where the field='n', not working?
    PHP Code:
    <?php session_start();
        if (@
    $_SESSION['auth'] != "yes")
        {
            
    header("Location: http://www.southhillsfootball.com/shfootball/");
            exit();
            }
    ?>
    <html><head><title></title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
    <body bgcolor="#00CC00">
    <form name="form2" method="post" action="scripts/shoutbox/approve_post.php">
    <table width="100%"  border="0">
        <tr>
          <th width="100%" scope="row"><div align="center">
    <?php
        
    include ("../../template.php");
            
    $connection mysql_connect($host,$user,$password)
                    or die (
    "Couldnt connect to server");
            
    $db mysql_select_db($database$connection)
                    or die (
    "Couldnt select database");
            
    $sql "SELECT * FROM shoutbox WHERE `approve` ='n'";
            
    $result mysql_query($sql)
                or die(
    "Couldn't execute query");
            echo
    "<table width=\"100%\" border=\"0\"><tr>";
            while (
    $row mysql_fetch_array($result)){
            
    $comment $row['comment'];
            
    $id $row['id'];
            echo 
    "<td><input type=\"checkbox\" name=\"$id\" value=\"checked\"></td>";
            echo 
    "<td>$comment</td>";
            }
            echo 
    "</tr></table>";
            
    ?>
    </div></th>
        </tr>
        <tr>
          <th scope="row"><input type="submit" name="Submit" value="Approve Selected"></th>
        </tr>
      </table>
    </form>
    </body>
    </html>
    And here is the script it will run through to change the database.
    PHP Code:
    <?php session_start();
        include (
    "../../../template.php");
            
            
    $connection mysql_connect($host,$user,$password)
                    or die (
    "Couldnt connect to server");
            
    $db mysql_select_db($database$connection)
                    or die (
    "Couldnt select database");
            
    $sql "SELECT * FROM shoutbox WHERE id='$deleteid'";
            
    $result mysql_query($sql)
                    or die (
    "Couldnt execute query.");
            foreach (
    $_POST as $field $value){
                if (
    $checked){
                    
    $sql "UPDATE shoutbox SET approve='y'";
                    
    mysql_query($sql)
                        or die(
    "error");
                        exit();
                } 
    //ends if statement
                
    else if (!$checked){
                    
    $sql "DELETE * FROM shoutbox WHERE id='$id'";
                    
    mysql_query($sql
                        or die (
    "error 2");
                        exit();
                } 
    //ends else
            
    //ends for each            
    ?>
    Thanks for any help.
    Scott

  2. #2
    SitePoint Evangelist chiphunt1's Avatar
    Join Date
    Oct 2003
    Location
    louisville, ky
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Build an array of all the records where approval == 'n'. Then make the input for the checkboxes an array. Then after the post, loop through original array and check to see if those values are in the input array. If not delete them, if y then set their value to y. In your loop:
    foreach ($_POST as $field > $value){
    Your trying to see if the field is checked.
    There won't be a field posted if it's wasn't checked, so you need to check the original array against the new array. In fact you can just do an array_diff, to get what values aren't in the new array. Hope this helps.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Ontario, Canada
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <input type="checkbox" name="approve[]" value="329" />
    <input type="checkbox" name="approve[]" value="330" />
    <input type="checkbox" name="approve[]" value="331" />

    Assuming the latter two are checked, upon submitting the form your script will be fed an array (via $_GET or $_POST, depending on the <form> method) containing an array that looks like: Array ([0] => 330, [1] => 331). So, anything checked will be added to the array. The value="" attribute would contain the ID of the given post. I don't suspect you'll be showing approved and pending-approval posts on the same page, but even that's easy, as you'll have access to the "approved" field of each post while you're looping through the result and spitting out checkboxes. No need to query for all existing "approved" fields beforehand.

  4. #4
    Accredited Master scotty2260's Avatar
    Join Date
    Apr 2004
    Location
    California
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chiphunt1
    Build an array of all the records where approval == 'n'. Then make the input for the checkboxes an array. Then after the post, loop through original array and check to see if those values are in the input array. If not delete them, if y then set their value to y. In your loop:
    foreach ($_POST as $field > $value){
    Your trying to see if the field is checked.
    There won't be a field posted if it's wasn't checked, so you need to check the original array against the new array. In fact you can just do an array_diff, to get what values aren't in the new array. Hope this helps.
    Sounds really good, but I have no idea how to do it? could you show me an example code please? Thanks very much!!!
    Scott

  5. #5
    Accredited Master scotty2260's Avatar
    Join Date
    Apr 2004
    Location
    California
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry to bump, but I relly need help with this.
    Here is the form.
    PHP Code:
    <?php
        
    include ("../template.php");
            
    $connection mysql_connect($host$user$password)
                        or die (
    "Couldn't connect to server.");
            
    $db mysql_select_db($database$connection)
                        or die (
    "Couldn't select database.");
            
    $sql "SELECT `name`,`content`,`id`,`date`, `approve` FROM `shoutbox` WHERE `approve` ='n' ORDER BY `date` DESC";
            
    $result mysql_query($sql)
                        or die (
    "Couldnt execute query");
            echo 
    "<table width=\"100%\" border=\"0\" class=\"style4\"><tr>";
            while (
    $row mysql_fetch_array($result)) {
            
    $name=$row['name'];
            
    $id=$row['id'];
            
    $content=$row['content'];
            echo 
    "<td width=\"85%\">$name</td>";
            echo 
    "<td width=\"15%\">$id <input type=\"checkbox\" name=\"checkbox\" value=\"true\"></td></tr><tr>";
            echo 
    "<td colspan=\"2\">$content<br><br></td></tr>";
            }
            echo
    "</table></p>";
      
    ?>
    And here is the place its running through. It doesn't give any erors, but nothing happens?
    PHP Code:
    <?php session_start();
        
    /*  Program: approve_post.php
            Descrip: This allows admin to approve posts before they go public.
        */
        
    include ("../../../template.php");
            
            
    $connection mysql_connect($host,$user,$password)
                    or die (
    "Couldnt connect to server");
            
    $db mysql_select_db($database$connection)
                    or die (
    "Couldnt select database");
            foreach(
    $_POST as $key => $value){
                if (
    $id){
                    
    $sql "UPDATE shoutbox SET approve='y' WHERE id='$id'";
                    
    mysql_query($sql)
                        or die(
    "error");
                        exit();
                } 
    //ends if statement
                
    else if ($id== "false"){
                    
    $sql "DELETE * FROM shoutbox WHERE id='$id'";
                    
    mysql_query($sql
                        or die (
    "error 2");
                        exit();
                } 
    //ends else
            
    //ends for each            
    ?>
    Scott

  6. #6
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    offtone gave a good clue @ post# 3.

    Try changing this...
    PHP Code:
    echo "<td width=\"15%\">$id <input type=\"checkbox\" name=\"checkbox\" value=\"true\"></td></tr><tr>"
    to this...
    PHP Code:
    echo "<td width=\"15%\">$id <input type=\"checkbox\" name=\"checkbox[]\" value=\"$id\"></td></tr><tr>"
    Lats...

  7. #7
    Accredited Master scotty2260's Avatar
    Join Date
    Apr 2004
    Location
    California
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    k thanks! but still nothing
    Here is the form now:
    PHP Code:
    <?php
        
    include ("../template.php");
            
    $connection mysql_connect($host$user$password)
                        or die (
    "Couldn't connect to server.");
            
    $db mysql_select_db($database$connection)
                        or die (
    "Couldn't select database.");
            
    $sql "SELECT `name`,`content`,`id`,`date`, `approve` FROM `shoutbox` WHERE `approve` ='n' ORDER BY `date` DESC";
            
    $result mysql_query($sql)
                        or die (
    "Couldnt execute query");
            echo 
    "<table width=\"100%\" border=\"0\" class=\"style4\"><tr>";
            while (
    $row mysql_fetch_array($result)) {
            
    $name=$row['name'];
            
    $id=$row['id'];
            
    $content=$row['content'];
            echo 
    "<td width=\"85%\">$name</td>";
            echo 
    "<td width=\"15%\">$id <input type=\"checkbox\" name=\"checkbox[]\" value=\"$id\"></td></tr><tr>";
            echo 
    "<td colspan=\"2\">$content<br><br></td></tr>";
            }
            echo
    "</table></p>";
      
    ?>
    And here is the code its running through:
    PHP Code:
    <?php session_start();
        
    /*  Program: delete_post.php
            Descrip: This deletes post from the DB.
        */
        
    include ("../../../template.php");
            
            
    $connection mysql_connect($host,$user,$password)
                    or die (
    "Couldnt connect to server");
            
    $db mysql_select_db($database$connection)
                    or die (
    "Couldnt select database");
            
    $id $_POST['$id'];
            foreach(
    $_POST as $key => $value){
                if (
    $id){
                    
    $sql "UPDATE shoutbox SET approve='y' WHERE id='$id'";
                    
    mysql_query($sql)
                        or die(
    "error");
                        exit();
                } 
    //ends if statement
                
    else if ($id== "false"){
                    
    $sql "DELETE * FROM shoutbox WHERE id='$id'";
                    
    mysql_query($sql
                        or die (
    "error 2");
                        exit();
                } 
    //ends else
            
    //ends for each            
    ?>
    Again, no errors, it's just not doing anything.
    Scott

  8. #8
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You seem to be missing a <form> tag in the first part. You'll be needing something like this within the table tags...
    PHP Code:
    <form name='something' method='post' action='next_part.php'>
    ...
    </
    form
    Lats...

  9. #9
    Accredited Master scotty2260's Avatar
    Join Date
    Apr 2004
    Location
    California
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i put the form tags outside of the php code. sorry about that, I only posted the actual php parts... thanks though
    Scott


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
  •