Jump to content

Recommended Posts

Ok, I'm creating a trade system for a website I work for. The problem is, when I'm selecting the items and then splitting them, I can easily echo all one-five number(s) with | between each one.

 

So it would look like: 1|2|53|5345

 

The problem is, I'm submitting the data into the table inside that foreach, but it's creating a separate query for each number.

 

How can I get all the numbers to stay one [1|2|53|5345] instead of having four queries for 1, 2, 53 and 5345?

 

if($act == go){
$items = $_POST[items];

if($items){
print_r($items);
$x=0;
foreach($items as $sluts){
$x++;
}
if($x > 5){
echo "5 items only!";
}else {
	$y=0;
	foreach($items as $itemids){
		if(!is_numeric($itemids)){
		echo "Item #$y [$itemids] is not numeric!<br>\n";
		}else {
			$sql = "SELECT * FROM `user_items` WHERE `id` =$itemids AND `uid` =$_COOKIE[uid]";
			$res = mysql_query($sql) or die(mysql_error());
				if(mysql_num_rows($res) == 0){
				echo "One of those items does not belong to you!<br>\n";
				}else {
					$row = mysql_fetch_assoc($res);
					$sql2 = "SELECT * FROM `items` WHERE `itemid` =$row[itemid]";
					$res2 = mysql_query($sql2) or die(mysql_error());
					if(mysql_num_rows($res2) == 0){
					echo "One of those items is not a real item!<br>\n";
					}else {
					$wishlist = protect($_POST['wishlist']);
						if($wishlist){
							if(strlen($wishlist > 250)){
							echo "Your wishlist is too much!";
							die();
							}
						}
						if($y == 0){                   
						$set = "$itemids";
						}else{
						$set = "|$itemids";
						}
							$time = time();
							//$sql = "INSERT INTO `trades` (`uid`,`items`,`wishlist`,`time`) VALUES('$_COOKIE[uid]','$set','$wishlist','$time');";
							//$res = mysql_query($sql) or die(mysql_error());
							//$limit = $y-1;
							//$sql2 = "DELETE FROM `trades` WHERE `uid` =$_COOKIE[uid] AND `time` =$time AND `items` ='$set' LIMIT $limit";
							//$res2 = mysql_query($sql2) or die(mysql_error());
					}
				}
		}

	$y++;
	}

}
echo "You trade has been created successfully!\n";
}else {
echo "YOU NEED TO SELECT ATLEAST ONE ITEM";
}

}

Link to comment
https://forums.phpfreaks.com/topic/53951-foreach-help/
Share on other sites

The foreach the queries are in it defines the set.

 

If y = 0 then it means it's only one number,but if it isn't it creating |$itemids

 

So If I had 1 number it would create a number as: 55342

 

But if I had the max of 5 numbers it would create 55342|55343|55344|55345|55346

 

In this case it's creating 5 queries for one set.

 

So it's creating an individual query for just 55342, 55343, 55344, 55345 and 55346, instead of one whole query with the |'s in side it

 

I'll post the whole cost how it's created:

 

<?php
$global_check=3;
$trade_check=3;
include('/home/MYSITE/public_html/global.php');
login($_COOKIE[uid]);

$act = $_GET[act];

if(!$act){
$sql = "SELECT * FROM `user_items` WHERE `place` ='Inventory' AND `uid` =$_COOKIE[uid] ORDER BY RAND()";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) > 0){
echo "<form action=\"?act=go\" method=post>\n";
echo "<table border=0 cellspacing=3 cellpadding=3 name=check width=600>\n";
echo "<tr><td colspan=5 align=center class=bar>Clicking the checkbox below the item name will make the item up for trades when you hit the submit button. Once the checkbox is enabled the image will be given a 3 point green border, showing you the item is now being traded once you hit the submit button. To remove the green border along with the check in the checkbox, just click the image and it will be no longer up for trades!</td></tr>\n";
?>
<script language="Javascript">
function counterUpdate(opt_countedTextBox, opt_countBody, opt_maxSize) {
        var countedTextBox = opt_countedTextBox ? opt_countedTextBox : "counttxt";
        var countBody = opt_countBody ? opt_countBody : "countBody";
        var maxSize = opt_maxSize ? opt_maxSize : 1024;

        var field = document.getElementById(countedTextBox);

        if (field && field.value.length >= maxSize) {
                field.value = field.value.substring(0, maxSize);
        }
        var txtField = document.getElementById(countBody);
                if (txtField) { 
                txtField.innerHTML = field.value.length;
        }
}
</script>
<?php
echo "<tr>\n";
$x=1;

//for($i=1;$i<=50;$i++){
while($row = mysql_fetch_assoc($res)){
//$d = $i;
$id = $row[id];
$itemid = $row[itemid];
$res2 = mysql_query("SELECT * FROM `items` WHERE `itemid` =$itemid") or die(mysql_error());
$row2 = mysql_fetch_assoc($res2);
$src = $row2[image];
$name = $row2[name];


echo "<td align=center><img onClick=\"document.getElementById('cb$id').checked=false;document.getElementById('img$id').style.border='0px';\" src=\"/images/items/$src\" style=\"border:0px;\" id=\"img$id\"><br>$name<br><input id=\"cb$id\" type=checkbox name=\"items[]\" value=\"$id\" onClick=\"document.getElementById('img$id').style.border='3px solid green'\"></td>\n";
if($x == 5){
echo "</tr><tr>\n";
$x=0;
}
$x++;

}

echo "<tr><td colspan=5 align=center>\n";
echo "<textarea id=wishlist name=wishlist cols=30 rows=6 onkeyup=\"counterUpdate('wishlist', 'wltxt','250');\"></textarea>\n";
echo "<br>You typed <B><span id=\"wltxt\">0</span></b> characters<br>Max. Length : 250 Chars\n";
echo "</td></tr>\n";
echo "<tr><td colspan=5 align=center>\n";
echo "<input type=submit value=\"Start Trading\"></td></tr>";
echo "</table></form>\n";

}else {
echo "You do not have any items to trade!";
}

}

if($act == go){
$items = $_POST[items];

if($items){
print_r($items);
$x=0;
foreach($items as $sluts){
$x++;
}
if($x > 5){
echo "5 items only!";
}else {
	$y=0;
	foreach($items as $whores){
		if(!is_numeric($whores)){
		echo "Item #$y [$whores] is not numeric!<br>\n";
		}else {
			$sql = "SELECT * FROM `user_items` WHERE `id` =$whores AND `uid` =$_COOKIE[uid]";
			$res = mysql_query($sql) or die(mysql_error());
				if(mysql_num_rows($res) == 0){
				echo "One of those items does not belong to you!<br>\n";
				}else {
					$row = mysql_fetch_assoc($res);
					$sql2 = "SELECT * FROM `items` WHERE `itemid` =$row[itemid]";
					$res2 = mysql_query($sql2) or die(mysql_error());
					if(mysql_num_rows($res2) == 0){
					echo "One of those items is not a real item!<br>\n";
					}else {
					$wishlist = protect($_POST['wishlist']);
						if($wishlist){
							if(strlen($wishlist > 250)){
							echo "Your wishlist is too much!";
							die();
							}
						}
						if($y == 0){                   
						$set = "$whores";
						}else{
						$set = "|$whores";
						}
							$time = time();
							//$sql = "INSERT INTO `trades` (`uid`,`items`,`wishlist`,`time`) VALUES('$_COOKIE[uid]','$set','$wishlist','$time');";
							//$res = mysql_query($sql) or die(mysql_error());
							//$limit = $y-1;
							//$sql2 = "DELETE FROM `trades` WHERE `uid` =$_COOKIE[uid] AND `time` =$time AND `items` ='$set' LIMIT $limit";
							//$res2 = mysql_query($sql2) or die(mysql_error());
					}
				}
		}

	$y++;
	}

}
echo "You trade has been created successfully!\n";
}else {
echo "YOU NEED TO SELECT ATLEAST ONE ITEM";
}

}

include('/home/ataria/public_html/bottom.php');
?>

Link to comment
https://forums.phpfreaks.com/topic/53951-foreach-help/#findComment-267023
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.