...

View Full Version : foreach/loop



Dan13071992
01-17-2012, 09:44 PM
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:



$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]

mlseim
01-17-2012, 10:00 PM
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.



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
}


.

Dan13071992
01-17-2012, 10:21 PM
i get the error:



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

BluePanther
01-17-2012, 10:27 PM
i get the error:



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.

Dan13071992
01-17-2012, 10:31 PM
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:



<input type="submit" style="border: 1px none; width: 115px;" class="sub2" value="Delete selected" name="deleteselected"/>

mlseim
01-17-2012, 11:42 PM
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.

Dan13071992
01-17-2012, 11:43 PM
its actually on inbox.php line 110:



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:



foreach($_POST['deletesel'] as $item){

mlseim
01-18-2012, 12:07 AM
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?



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
}
}
}

Dan13071992
01-18-2012, 12:16 AM
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:



foreach($deletesel as $item){

mlseim
01-18-2012, 01:36 AM
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:


<html>
<body>

<form action="test.php" method="post">
<input type='checkbox' name='deletesel[]' value='1'> 1 <br />
<input type='checkbox' name='deletesel[]' value='2'> 2 <br />
<input type='checkbox' name='deletesel[]' value='3'> 3 <br />
<input type='checkbox' name='deletesel[]' value='4'> 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

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.

.

Dan13071992
01-18-2012, 02:45 AM
form to form:



<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 == 4 || $senderUserLevel == 5) {
$classheader = 'headerRed';
$classmsg = 'msgRed';
}
elseif($senderUserLevel == 2 || $senderUserLevel == 3) {
$classheader = 'headerGreen';
$classmsg = 'msgGreen';
}
elseif($senderUserLevel == 0 || $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:



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
}
}
}

mlseim
01-18-2012, 02:52 AM
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.

Dan13071992
01-18-2012, 03:01 AM
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:



delete: Array


if that helps at all :s

BluePanther
01-18-2012, 01:33 PM
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:



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

Dan13071992
01-18-2012, 02:28 PM
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?

BluePanther
01-18-2012, 03:13 PM
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?

You should be able to just follow my instructions in my last reply. Post your form and current updated code if my amendments don't work :)

Dan13071992
01-18-2012, 03:17 PM
its still not working, even with what you said, here is my entire page:



<?php
include('includes/db_connect.php');
include('includes/inc-logincheck.php');
include('includes/inc-functions.php');
include('includes/inc-userfunctions.php');
include('includes/inc-menu.php');
include('includes/smile.php');
$strTitle='Inbox';
$strMessage='';
$intUserID=$_SESSION['uID'];
$fetch=$user;
include('includes/inc-head.php');
?>
<?php
if (strip_tags($_GET['filter']) == "yes"){ ?>

<td width="99%" background="/game/site/site/innerbg.gif" bgcolor="#222222" valign="top">
<br />
<center>


<?php if ($_POST){ ?> <?php echo "<font color=white><b><center>$message</center><b><br>"; ?> <? } ?>

<br><br>


<span class="insideTables">
<form method="POST" action="?filter=yes">
<table border=1 cellspacing=0 cellpadding=2 bordercolor=black width=50% class=sub2>
<tr>
<td align=center colspan=2 class=header>Filter</td>
</tr>
<tr>
<td colspan=2 align=center>No blocked users</td>

</tr>
<tr>
<td align=center colspan=2 class=header>Add player</td>
</tr>
<tr>
<td>Username:</td>
<td><input type=text name=addusername></td>
</tr>

<tr>
<td align=right colspan=2><input type=submit name=add value="Block!">
</tr>
</table>
</center>
</form>

</span>


</td>

</td>

<?php
include("includes/inc-footer.php");
?>




<?php

} elseif (strip_tags(!$_GET['filter'])){

?>
<?php
if ($_GET['save']){

$save=mysql_real_escape_string($_GET['save']);

$checkersave = mysql_query("SELECT * FROM `inbox` WHERE `to` ='".dbSafe($fetch->username)."' AND `id` ='".dbSafe($save)."' ");

$rowssave=mysql_fetch_object($checkersave);

if ($rowsave > "0"){
$message= "You are not Authorised to <b>SAVE</b> this message";
}else{
mysql_query("INSERT INTO `saved` (`id`, `to`, `from`, `message`, `date`) VALUES ('', '".dbSafe($rowssave->to)."', '".dbSafe($rowssave->from)."', '".dbSafe($rowssave->message)."', '".dbSafe($rowssave->date)."');") or die (mysql_error());
$message= "<center>Message saved</center><br> ";
mysql_query("DELETE FROM inbox WHERE id='".dbSafe($save)."'");

}}



if ($_GET['del']){

$del=mysql_real_escape_string($_GET['del']);

$checker = mysql_query("SELECT * FROM `inbox` WHERE `to` ='".dbSafe($fetch->username)."' AND `id` ='".dbSafe($del)."' ");

$rows=mysql_fetch_object($checker);

if ($rows < "1"){
$message= "You are not Authorised to <b>DELETE</b> this message";
}else{
$message= "<center>Message deleted</center><br>";
mysql_query("INSERT INTO `deletedinbox` (`id`, `to`, `from`, `message`, `date`) VALUES ('', '".dbSafe($rows->to)."', '".dbSafe($rows->from)."', '".dbSafe($rows->message)."', '".dbSafe($rows->date)."');") or die (mysql_error());
mysql_query("DELETE FROM inbox WHERE id='".dbSafe($del)."'");

}}





?>
<td width="99%" background="/game/site/site/innerbg.gif" bgcolor="#222222" valign="top">
<br />
<center>
<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
if (isset($_POST['deleteselected'])){
if(isset($_POST['deletesel'])){
$deletesel=$_POST['deletesel'];
$message= "$deletesel<br>";
foreach($deletesel as $item){
$message= "delete: $item <br />";
// within this loop you would do the switch from one table to the other
}
}
}



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 == 4 || $senderUserLevel == 5) {
$classheader = 'headerRed';
$classmsg = 'msgRed';
}
elseif($senderUserLevel == 2 || $senderUserLevel == 3) {
$classheader = 'headerGreen';
$classmsg = 'msgGreen';
}
elseif($senderUserLevel == 0 || $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>


<?php
}
include("includes/inc-footer.php");


?>


error is on line 132, which is the foreach.

_Aerospace_Eng_
01-18-2012, 03:23 PM
What is the error now? Also you are supposed to be using echo $message or you won't see any thing. When you are testing this, are any of your checkboxes checked?

Dan13071992
01-18-2012, 03:26 PM
instead of echo i use <?php if ($_POST){ ?> <?php echo "<font color=white><b><center>$message</center><b><br>"; ?> <? } ?> as otherwise it comes up at the top of the page when it should be centered, also, yes i am checking multiple boxes and changing the ammount i check each time, between 1 and 5 boxes are always checked, error i get now is:



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


that line being:



foreach($deletesel as $item){

_Aerospace_Eng_
01-18-2012, 04:01 PM
Change this

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

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

Dan13071992
01-18-2012, 04:05 PM
Number checked boxes = 1

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

that being with me checking more than one checkbox, and when checking only 1 box, so regardless of howmany i check it still counts 1

_Aerospace_Eng_
01-18-2012, 04:17 PM
That should be working but it shouldn't be returning 1. Change this

<?php
include('includes/db_connect.php');
include('includes/inc-logincheck.php');
include('includes/inc-functions.php');
include('includes/inc-userfunctions.php');
include('includes/inc-menu.php');
include('includes/smile.php');
$strTitle='Inbox';
$strMessage='';
$intUserID=$_SESSION['uID'];
$fetch=$user;
include('includes/inc-head.php');
?>
<?php
if (strip_tags($_GET['filter']) == "yes"){ ?>

<td width="99%" background="/game/site/site/innerbg.gif" bgcolor="#222222" valign="top">
<br />
<center>


<?php if ($_POST){ ?> <?php echo "<font color=white><b><center>$message</center><b><br>"; ?> <? } ?>

<br /><br />


<span class="insideTables">
<form method="post" action="?filter=yes">
<table border="1" cellspacing="0" cellpadding="2" bordercolor="black" width="50%" class="sub2">
<tr>
<td align="center" colspan="2" class="header">Filter</td>
</tr>
<tr>
<td colspan="2" align="center">No blocked users</td>

</tr>
<tr>
<td align="center" colspan="2" class="header">Add player</td>
</tr>
<tr>
<td>Username:</td>
<td><input type="text" name="addusername" /></td>
</tr>

<tr>
<td align="right" colspan="2"><input type="submit" name="add" value="Block!" />
</td>
</tr>
</table>
</center>
</form>

</span>


</td>

</td>

<?php
include("includes/inc-footer.php");
?>




<?php

} elseif (strip_tags(!$_GET['filter'])){

?>
<?php
if ($_GET['save']){

$save=mysql_real_escape_string($_GET['save']);

$checkersave = mysql_query("SELECT * FROM `inbox` WHERE `to` ='".dbSafe($fetch->username)."' AND `id` ='".dbSafe($save)."' ");

$rowssave=mysql_fetch_object($checkersave);

if ($rowsave > "0"){
$message= "You are not Authorised to <b>SAVE</b> this message";
}else{
mysql_query("INSERT INTO `saved` (`id`, `to`, `from`, `message`, `date`) VALUES ('', '".dbSafe($rowssave->to)."', '".dbSafe($rowssave->from)."', '".dbSafe($rowssave->message)."', '".dbSafe($rowssave->date)."');") or die (mysql_error());
$message= "<center>Message saved</center><br> ";
mysql_query("DELETE FROM inbox WHERE id='".dbSafe($save)."'");

}}



if ($_GET['del']){

$del=mysql_real_escape_string($_GET['del']);

$checker = mysql_query("SELECT * FROM `inbox` WHERE `to` ='".dbSafe($fetch->username)."' AND `id` ='".dbSafe($del)."' ");

$rows=mysql_fetch_object($checker);

if ($rows < "1"){
$message= "You are not Authorised to <b>DELETE</b> this message";
}else{
$message= "<center>Message deleted</center><br>";
mysql_query("INSERT INTO `deletedinbox` (`id`, `to`, `from`, `message`, `date`) VALUES ('', '".dbSafe($rows->to)."', '".dbSafe($rows->from)."', '".dbSafe($rows->message)."', '".dbSafe($rows->date)."');") or die (mysql_error());
mysql_query("DELETE FROM inbox WHERE id='".dbSafe($del)."'");

}}





?>
<td width="99%" background="/game/site/site/innerbg.gif" bgcolor="#222222" valign="top">
<br />
<center>
<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
if (isset($_POST['deleteselected'])){
if(isset($_POST['deletesel'])){
$deletesel=(array)$_POST['deletesel'];
echo 'Boxes checked: '.count($deletesel);
echo '<pre>';
print_r($deletesel);
echo '</pre>';
$message= "$deletesel<br>";
foreach($deletesel as $item){
$message= "delete: $item <br />";
// within this loop you would do the switch from one table to the other
}
}
}



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 == 4 || $senderUserLevel == 5) {
$classheader = 'headerRed';
$classmsg = 'msgRed';
}
elseif($senderUserLevel == 2 || $senderUserLevel == 3) {
$classheader = 'headerGreen';
$classmsg = 'msgGreen';
}
elseif($senderUserLevel == 0 || $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>


<?php
}
include("includes/inc-footer.php");


?>
Post the results. FYI your html is pretty bad. You don't use quotes in a lot of places, you are using the center tag which has been deprecated for quite some time now. Your have some incorrect nesting going on. The only time you should use a table is if you are working with data which for the most part you are.

Do you even get any other $messages printed out? I'm guessing no.

Dan13071992
01-18-2012, 08:26 PM
results:



Boxes checked: 1

Array
(
[0] => Array
)



thats with 2 boxes checked.

by the way, i know my html and code is a bit funny, i just wanted to get it all working first then i was going to tidy it all up. and by nested what doi you mean, also could you point out where just for fuure refrence for me please.

_Aerospace_Eng_
01-19-2012, 04:13 AM
Change this

$deletesel=(array)$_POST['deletesel'];
echo 'Boxes checked: '.count($deletesel);
echo '<pre>';
print_r($deletesel);
echo '</pre>';
to this

$deletesel=$_POST['deletesel'];
echo 'Boxes checked: '.count($deletesel);
echo '<pre>';
print_r($deletesel);
echo '</pre>';
Post the results. As to your nesting, you have a <center> tag, then your table, form, center tag, then you close your table.

Also is it possible for you to give us a live link? If you are only getting one item back then it is possible that your names aren't what we think they are.

Dan13071992
01-19-2012, 05:14 PM
sorry for taking so long to reply, ive been at a funeral all day.

below is the error message using that latest code you gave me:



Boxes checked: 1

Array


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


if you want to go to the live page you would need to log in using these account settings:

username:

codingforums

password:

codingforums

link:

http://crimewave.360-tactics.co.uk/inbox.php

Dan13071992
01-21-2012, 11:36 AM
would it help if i gave you a link to my phpinfo, maybe there is something in there that you can see might be the problem :S

_Aerospace_Eng_
01-21-2012, 03:26 PM
It would help if that username and password you gave us worked, inbox.php redirects me to index.php?page=logout fyi.

Dan13071992
01-21-2012, 03:33 PM
it redirects because there is no session, sorry the username and password should work now, i forgot to actually activate the account, sorry

_Aerospace_Eng_
01-21-2012, 03:45 PM
Okay so I took your output, tried it on my server and it worked fine. So we will try one last thing, if it doesn't work then sure show us your phpinfo output.
Change this

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

if(isset($_POST['deleteselected']))
{
if(isset($_POST['deletesel']))
{
$deletebox = $_POST['deletesel'];
print_r($deletebox);
}
}
What is the output?

Dan13071992
01-21-2012, 03:48 PM
it just says:




Array


php info page is here:

http://crimewave.360-tactics.co.uk/phpinfo.php

_Aerospace_Eng_
01-21-2012, 04:12 PM
Hmm nothing really stands out in there. Try adding this attribute to your opening form tag

enctype="multipart/form-data"

Dan13071992
01-21-2012, 08:26 PM
that stills comes up with Array.

this is really confusing me now, i seriously dont know why it is doing this :s

_Aerospace_Eng_
01-21-2012, 08:33 PM
Same here. The only thing I can think of is all of the html errors in your page are causing this. You don't have a doctype, you seem to include an entire page inside of another page, you have no <head></head> tags, no <html> tag.

Do a view source of inbox.php and then go to validator.w3.org and paste in your code (direct input), validate it and see how many errors you have. 113 errors and 5 warnings btw.

Dan13071992
01-22-2012, 10:57 PM
i think i know the problem, as when i removed this from my functions it started working, so can anyone see anything wrong with this?



function undoMagicQuotes($v) {
return is_array($v) ? array_map('undoMagicQuotes',$v) : stripslashes($v);
}
$_GET=array_map('trim',$_GET);
$_POST=array_map('trim',$_POST);
$_COOKIE=array_map('trim',$_COOKIE);
if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
$_GET=array_map('undoMagicQuotes',$_GET);
$_POST=array_map('undoMagicQuotes',$_POST);
$_COOKIE=array_map('undoMagicQuotes',$_COOKIE);
}

Dan13071992
01-24-2012, 06:55 PM
_Aerospace_Eng_ do you have any idea? or does anyone else?

BluePanther
01-24-2012, 07:17 PM
Lifted from a comment on http://php.net/manual/en/function.trim.php


There is a trap when making "form prefilters". Something like
$_POST = array_map('trim', $_POST)
may be not what we wanted, because when there are arrays in form made by field[] this "prefilter" return not an array but, according to his behavior, string. So as a effect we see in var_dump not array but field = "Array" what can mess up our validation.

I believe he is describing exactly your problem, and the reasoning behind it - which is easy to miss! You'll need to create a recursive trim function to check for arrays, like your undoMagicQuotes function (which is another reason I can't believe I didn't spot this). Something like this:


function undoMagicQuotes($v) {
return is_array($v) ? array_map('undoMagicQuotes',$v) : stripslashes($v);
}
function trimArray($v){
return is_array($v) ? array_map('trimArray',$v) : trim($v);
}
$_GET=array_map('trimArray',$_GET);
$_POST=array_map('trimArray',$_POST);
$_COOKIE=array_map('trimArray',$_COOKIE);
if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
$_GET=array_map('undoMagicQuotes',$_GET);
$_POST=array_map('undoMagicQuotes',$_POST);
$_COOKIE=array_map('undoMagicQuotes',$_COOKIE);
}

Dan13071992
01-24-2012, 07:42 PM
would that just replace the old one and do everything the old one did?

BluePanther
01-24-2012, 08:26 PM
would that just replace the old one and do everything the old one did?

Replace the one in post #34, with my one. Any array_map'ing you're doing with a function that returns a string needs to be treated like this it seems. A demonstration:
http://codepad.org/WogPXObg

Dan13071992
01-24-2012, 08:40 PM
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
}
}
}





$array = array(
0 => 'Value',
1 => array(
0 => 'Another value'
)
);


so how would i put these two together?

BluePanther
01-24-2012, 08:45 PM
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
}
}
}





$array = array(
0 => 'Value',
1 => array(
0 => 'Another value'
)
);


so how would i put these two together?

No no no, you're missing the point :D. The problem is when you use array_map() with a function that returns a string, if trim is given an array (which your script is giving it) it will return a string 'Array'. Replace the snippet in post #34 with the snippet I gave you in #36.

Kind of leads me to think why you treated magic_quotes the correct way, but didn't apply that to trim?

Dan13071992
01-24-2012, 09:42 PM
a personal friend of mine actually helped me with some of my functions, that might/mightnot be one of them, it all works fine now, so just to get this foreach right would i do it like this as using that new bit of code you gave me, it works:



if (isset($_POST['deleteselected'])){
if($_POST['deletesel']){
$deletesel=$_POST['deletesel'];
foreach($deletesel as $item){
mysql_query("INSERT INTO `deletedinbox` (`id`, `to`, `from`, `message`, `date`) VALUES ('', '".dbSafe($item->to)."', '".dbSafe($item->from)."', '".dbSafe($item->message)."', '".dbSafe($item->date)."');") or die (mysql_error());
mysql_query("DELETE FROM inbox WHERE id='".dbSafe($item->id)."'");

$message= "delete: $item <br />";
// within this loop you would do the switch from one table to the other
}
}
}

would that be right?

BluePanther
01-24-2012, 09:50 PM
a personal friend of mine actually helped me with some of my functions, that might/mightnot be one of them, it all works fine now, so just to get this foreach right would i do it like this as using that new bit of code you gave me, it works:



if (isset($_POST['deleteselected'])){
if($_POST['deletesel']){
$deletesel=$_POST['deletesel'];
foreach($deletesel as $item){
mysql_query("INSERT INTO `deletedinbox` (`id`, `to`, `from`, `message`, `date`) VALUES ('', '".dbSafe($item->to)."', '".dbSafe($item->from)."', '".dbSafe($item->message)."', '".dbSafe($item->date)."');") or die (mysql_error());
mysql_query("DELETE FROM inbox WHERE id='".dbSafe($item->id)."'");

$message= "delete: $item <br />";
// within this loop you would do the switch from one table to the other
}
}
}

would that be right?

Close, you should always omit empty fields from insert queries. Your default value for the field in mysql (that you should have set up when creating the table) will be filled in by the database - this could be an auto_increment from an id primary key.


if (isset($_POST['deleteselected'])){
if($_POST['deletesel']){
$deletesel=$_POST['deletesel'];
foreach($deletesel as $item){
mysql_query("INSERT INTO `deletedinbox` (`to`, `from`, `message`, `date`) VALUES ('".dbSafe($item->to)."', '".dbSafe($item->from)."', '".dbSafe($item->message)."', '".dbSafe($item->date)."');") or die (mysql_error());
mysql_query("DELETE FROM inbox WHERE id='".dbSafe($item->id)."'");

$message= "delete: $item <br />";
// within this loop you would do the switch from one table to the other
}
}
}

Dan13071992
01-24-2012, 09:52 PM
ok, ill try that, it is an auto increment, its the id feild

Dan13071992
01-24-2012, 09:57 PM
using what you just gave me back, i dont even get the $message $_POST :s any ideas why?

BluePanther
01-24-2012, 10:53 PM
Just noticed this - $item->to. This will be the 5th time I've explained to you now, and you haven't taken notice - a wee bit annoying. -> refers to a method or property inside an OBJECT. There are no objects here. $item will hold the id of your selected message as a regular variable.

Dan13071992
01-24-2012, 11:04 PM
sorry i get confused alot with some things, I think ive finally got it to work, well it appears to be working now that is :) below is my code, however if you still think it is wrong, or you feel im using the wrong method of doing it, please just let me know by saying it should be an array or other and i will correct it.




if (isset($_POST['deleteselected'])){
if($_POST['deletesel']){
$deletesel=$_POST['deletesel'];
foreach($deletesel as $item){

$delquery = mysql_query("SELECT * FROM inbox WHERE id='$item'");
$delfetch = mysql_fetch_object($delquery);

mysql_query("INSERT INTO `deletedinbox` (`to`, `from`, `message`, `date`) VALUES ('".dbSafe($delfetch->to)."', '".dbSafe($delfetch->from)."', '".dbSafe($delfetch->message)."', '".dbSafe($delfetch->date)."');") or die (mysql_error());
mysql_query("DELETE FROM inbox WHERE id='".dbSafe($delfetch->id)."'");

$message= "delete: $item <br />";
// within this loop you would do the switch from one table to the other
}
}
}

BluePanther
01-24-2012, 11:11 PM
That should work fine, although you COULD gather all the id's into a string then querying outside the loop to make things a bit more efficient, but that's more of a luxury.

Dan13071992
01-24-2012, 11:17 PM
by string, do you mean an array? or am i getting the wrong definition of string?

BluePanther
01-24-2012, 11:46 PM
by string, do you mean an array? or am i getting the wrong definition of string?

http://php.net/manual/en/language.types.php - that'll give a better explanation :). What I was getting at, was you could have a string, say $ids, that holds all the id's separated by OR's. Like


$string = 'id=1 OR id=2 OR id=5';

// Then construct query like so:
$query = 'SELECT info FROM table WHERE '.$string;
// etc. etc.

Dan13071992
01-24-2012, 11:47 PM
thanks BP :)

BluePanther
01-24-2012, 11:56 PM
No worries :thumbsup:.

Now, go have a good look through the different PHP variable types :)

Dan13071992
01-25-2012, 02:42 PM
Can a moderator please mark this as Resolved please, as it will not let me do it myself.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum