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.
Page 1 of 4 123 ... LastLast
Results 1 to 15 of 52

Thread: foreach/loop

  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts

    foreach/loop

    hi guys, Im confused as the best way to go about this, but i want to create check boxes on the inboxs page of my website, for each checkbox that is selected i want to delete it from the inbox table and insert it into the deletedinbox table, below is how my code works for displaying inboxes, i wont post all of it as it is really long, just the main parts, i just need help with the looping through to select the ones that have been deleted and how to insert them all into the deleted inbox table:

    PHP Code:
    $mysql1 mysql_query("SELECT * FROM `inbox` WHERE `to`='".dbSafe($fetch->username)."' ORDER by id DESC");

    while(
    $get mysql_fetch_object($mysql1)){

    $t++;

    $n++;



    echo 
    "

                    <table border=1 cellpadding=2 cellspacing=0 width=70%>
                    <tbody><tr>
                    <td class=$classheader><b>From:</b> <a href='profile.php?viewuser=$get->from'>$get->from</a> <b>On:</b> $get->date</td>
                    </tr>
                    <tr>
                    <td class=$classmsg bgcolor=#$bgcol>"
    .replace($get->message)."
                    <br>
                    <br>
                    <br>
    <a href=inbox.php?del=$get->id>Delete</a> - <a href='?save=$get->id'>Save</a> - <a href='send.php?fromper=$get->from&id=$get->id'>Reply</a>
                    </td></tr>
                    </tbody></table>
                    <br>
                    
                    
    "
    ;


    I know how to put a check box into it, and how to link the checkbox to an id, however what kind of code would i use to loop through all of the selected boxes and insert them into the new table/copy them into the new table, and then delete them from the inbox table.

    Cheers.

    Dan

    [/php]
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,546
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    So you're going to have to start out with a <form action="process.php" method="post">
    And your loop will display the messages with a checkbox array. At the bottom
    somewhere is your "delete all checked items" box.

    PHP Code:
    while($get mysql_fetch_object($mysql1)){

    $t++;

    $n++;



    echo 
    "

                    <table border=1 cellpadding=2 cellspacing=0 width=70%>
                    <tbody><tr>
                    <td class=$classheader><b>From:</b> <a href='profile.php?viewuser=$get->from'>$get->from</a> <b>On:</b> $get->date</td>
                    </tr>
                    <tr>
                    <td class=$classmsg bgcolor=#$bgcol>"
    .replace($get->message)."
                    <br>
                    <br>
                    <br>
                    <input type='checkbox' name='del[]' value='$get->id'> Delete &nbsp;|&nbsp; 
                    <a href='send.php?fromper=$get->from&id=$get->id'>Reply</a>
                    </td></tr>
                    </tbody></table>
                    <br>
                    
                    
    "
    ;



    In the script that processes it "process.php", you'll loop through the checkbox
    array and update or move each one ...

    use this to test it out ...

    foreach($_post['del'] as $item){
    echo "delete: $item <br />";
    // within this loop you would do the switch from one table to the other
    }


    .

  • #3
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    i get the error:

    Code:
    Warning: Invalid argument supplied for foreach() in /home/tacticsc/domains/360-tactics.co.uk/public_html/crimewave/inbox.php on line 87
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by Dan13071992 View Post
    i get the error:

    Code:
    Warning: Invalid argument supplied for foreach() in /home/tacticsc/domains/360-tactics.co.uk/public_html/crimewave/inbox.php on line 87
    Lets see your updated code.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #5
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    PHP Code:

    if ($_POST['deleteselected']){
    foreach(
    $_POST['deletesel'] as $item){
    $message"delete: $item <br />";
    // within this loop you would do the switch from one table to the other
    }
    }

    $mysql1 mysql_query("SELECT * FROM `inbox` WHERE `to`='".dbSafe($fetch->username)."' ORDER by id DESC");


    while(
    $get mysql_fetch_object($mysql1)){

    $t++;

    $n++;

    echo 
    "

                    <table border=1 cellpadding=2 cellspacing=0 width=70%>
                    <tbody><tr>
                    <td class=$classheader><b>From:</b> <a href='profile.php?viewuser=$get->from'>$get->from</a> <b>On:</b> $get->date</td>
                    </tr>
                    <tr>
                    <td class=$classmsg bgcolor=#$bgcol>"
    .replace($get->message)."
                    <br>
                    <br>
                    <br>
    <input type='checkbox' name='deletesel[]' value='$get->id'> - <a href=inbox.php?del=$get->id>Delete</a> - <a href='?save=$get->id'>Save</a> - <a href='send.php?fromper=$get->from&id=$get->id'>Reply</a>
                    </td></tr>
                    </tbody></table>
                    <br>
                    
                    
    "
    ;

    }} 
    there is an option to delete singly, plus the check box, plus there is a button to press to delete selected which is below:

    Code:
    <input type="submit" style="border: 1px none; width: 115px;" class="sub2" value="Delete selected" name="deleteselected"/>
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,546
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    We have no clue what the script names are of the posts you are showing.
    Your error is in "input.php" ... how do we know which script it is? Post that one,
    and indicate what it's called right above it.

  • #7
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    its actually on inbox.php line 110:

    PHP Code:
    if (isset($_POST['deleteselected'])){
    foreach(
    $_POST['deletesel'] as $item){
    $message"delete: $item <br />";
    // within this loop you would do the switch from one table to the other
    }

    line 110:

    PHP Code:
    foreach($_POST['deletesel'] as $item){ 
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,546
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    hmmm ...
    I can't see what I did wrong.

    Try the changes made below. If you get an error,
    let me know. If nothing displays, that means you
    didn't check any boxes, or for some reason they
    are not there?

    PHP Code:
    if (isset($_POST['deleteselected'])){
       if(
    $_POST['deletesel']){
       
    $deletesel=$_POST['deletesel'];
          foreach(
    $deletesel as $item){
          
    $message"delete: $item <br />";
          
    // within this loop you would do the switch from one table to the other
          
    }
       }  


  • #9
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    ok using what you just game me, selecting no check boxes (leaving them all empty) gives no message, but selecting 1 gives that error again with line number 112:

    PHP Code:
    foreach($deletesel as $item){ 
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #10
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,546
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    doh!

    Later, I'll make a test script and try to see what it doesn't work.
    I can't see any problem with it.

    Can you post the entire thing, so I can see it all between <form ...> and </form>

    EDIT: I made a test script, it works just as I expected it to ...

    Here's my HTML test form:
    PHP Code:
    <html>
    <
    body>

    <
    form action="test.php" method="post">
    <
    input type='checkbox' name='deletesel[]' value='1'<br />
    <
    input type='checkbox' name='deletesel[]' value='2'<br />
    <
    input type='checkbox' name='deletesel[]' value='3'<br />
    <
    input type='checkbox' name='deletesel[]' value='4'<br />
    <
    input type="submit" style="border: 1px none; width: 115px;" class="sub2" value="Delete selected" name="deleteselected"/>
    </
    form>

    </
    body>
    </
    html

    Here is my "test.php" script:
    PHP Code:
    <?php

    if (isset($_POST['deleteselected'])){
       if(
    $_POST['deletesel']){
       
    $deletesel=$_POST['deletesel'];
          foreach(
    $deletesel as $item){
          echo 
    "delete: $item <br />";
          
    // within this loop you would do the switch from one table to the other
          
    }
       }  
    }  

    ?>

    I need to see your entire script ...
    Your problem being caused by something else.

    .
    Last edited by mlseim; 01-18-2012 at 01:48 AM.

  • #11
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    form to form:

    PHP Code:
    <form method=POST action=inbox.php>
    <?php if ($_GET){ ?> <?php echo "<font color=white><b><center>$message</center><b><br>"?> <? ?>
    <?php 
    if ($_POST){ ?> <?php echo "<font color=white><b><center>$message</center><b><br>"?> <? ?>



        <input type="submit" style="border: 1px none; width: 115px;" class="sub2" value="Delete selected" name="deleteselected"/> 
        <input type="submit" onclick="if(confirm('Are you sure you want to delete ALL of your messages?')) return true; return false;" style="border: 1px none; width: 115px;" class="sub2" value=" Delete all " name="deleteall"/>
        <input type="button" onclick="location.href='?filter=yes'" style="border: 1px none; width: 115px;" class="sub2" value="  Filter  " name="filter"><br/>
    <br>
    <br>
    <?php




    echo "<center>";

    $mysql1 mysql_query("SELECT * FROM `inbox` WHERE `to`='".dbSafe($fetch->username)."' ORDER by id DESC");

    $num_rows mysql_num_rows($mysql1);



    if (
    $num_rows == "0"){

    echo 
    "<center>You have no mail.</center>";

    }else{

    while(
    $get mysql_fetch_object($mysql1)){

    $t++;

    $n++;

    $sender $get->from;
    $senderQuery mysql_query("SELECT * FROM users WHERE username='".dbSafe($sender)."'");
    $senderFetch mysql_fetch_object($senderQuery);
    $senderUserLevel $senderFetch->accesslevel;

    if (
    $get->read == 0){
    $bgcol 999999;
    }elseif (
    $get->read == 1){
    $bgcol 333333;
    }

    if(
    $senderUserLevel == || $senderUserLevel == 5) {
    $classheader 'headerRed';
    $classmsg 'msgRed';
    }
    elseif(
    $senderUserLevel == || $senderUserLevel == 3) {
    $classheader 'headerGreen';
    $classmsg 'msgGreen';
    }
    elseif(
    $senderUserLevel == || $senderUserLevel == 1) {
    $classheader 'header';
    $classmsg 'msgNormal';
    }





    echo 
    "

                    <table border=1 cellpadding=2 cellspacing=0 width=70%>
                    <tbody><tr>
                    <td class=$classheader><b>From:</b> <a href='profile.php?viewuser=$get->from'>$get->from</a> <b>On:</b> $get->date</td>
                    </tr>
                    <tr>
                    <td class=$classmsg bgcolor=#$bgcol>"
    .replace($get->message)."
                    <br>
                    <br>
                    <br>
    <input type='checkbox' name='deletesel[]' value='$get->id'> - <a href=inbox.php?del=$get->id>Delete</a> - <a href='?save=$get->id'>Save</a> - <a href='send.php?fromper=$get->from&id=$get->id'>Reply</a>
                    </td></tr>
                    </tbody></table>
                    <br>
                    
                    
    "
    ;

    }}



    mysql_query("UPDATE `inbox` SET `read`='1' WHERE `to`='".dbSafe($fetch->username)."'");

    ?>
    </form>
    code above it just for the loop is:

    PHP Code:
    if (isset($_POST['deleteselected'])){
       if(
    $_POST['deletesel']){
       
    $deletesel=$_POST['deletesel'];
          foreach(
    $deletesel as $item){
          echo 
    "delete: $item <br />";
          
    // within this loop you would do the switch from one table to the other
          
    }
       }  

    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #12
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,546
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    I can go no further.
    I have no way to install and test what you have, so you're on your own I guess.
    Maybe you can get someone else to actually go into your files? Or maybe
    someone else can see where it's failing. I can't see it.

  • #13
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    thanks for your help.

    i asked my host if they had reported questions about this before, and they suggested doing the foreach loop like this:

    foreach ((array)$deletesel as $item){

    well this actualkly posted, and what it posted was:

    Code:
    delete: Array
    if that helps at all :s
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #14
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by Dan13071992 View Post
    thanks for your help.

    i asked my host if they had reported questions about this before, and they suggested doing the foreach loop like this:

    foreach ((array)$deletesel as $item){

    well this actualkly posted, and what it posted was:

    Code:
    delete: Array
    if that helps at all :s
    You only have one checkbox using deletesel[], so having an array seems kind of redundant.

    Regardless, $deletesel should still be an array IF the checkbox is checked. I would surround your foreach in if(isset($_POST['deletesel'])) to verify you have at least 1 checkbox checked
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #15
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    the check boxes are given from inboxes messages, i can give you the whole page script and a dump of the mysql table if that helps?
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags


  •  
    Page 1 of 4 123 ... LastLast

    Posting Permissions

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