Jump to content


Photo

array problem


  • Please log in to reply
3 replies to this topic

#1 desithugg

desithugg
  • Members
  • PipPipPip
  • Advanced Member
  • 281 posts
  • LocationScarborough

Posted 20 August 2006 - 06:01 PM

umm im having trouble checking if all the items selected in a form are all owned by the selecter.
$my_items = $_POST['my_items'];
$other = $_POST['other'];
$other_items = $_POST['other_items'];
$link = mysql_connect('localhost', $my_sql_password, $my_sql_username);
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('osiris_thugg');
if (!$db_selected) {
   die('Could not select database: ' . mysql_error());
}
$query = "SELECT item_id FROM items where owner = '$user_currently_loged'"; 
	 $result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$my_check1[] = $row['item_id'];
}
$query = "SELECT item_id FROM items where owner = '$other'"; 
	 $result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$other_check1[] = $row['item_id'];
}
if(count(array_intersect($my_items, $my_check1)) != count($my_items)) {
echo"One or more of the items selected never did or does not belong to you.";
Echo"selected<br>";
print_r(array_values($my_items));
echo"Owned<br>";
print_r(array_values($my_check1));
exit;
}
Okay the items i selected were all owned by the two people
"$other","$user_currently_loged" but it still gives me that error to make sure i selected the right items i added
Echo"selected<br>";
print_r(array_values($my_items));
echo"Owned<br>";
print_r(array_values($my_check1));
and it showed me that i had selected the right items.it showed me
selected
Array ( [0] => 107 [1] => 1322 ) 
Owned
Array ( [0] => 10 [1] => 11 [2] => 12 [3] => 16 [4] => 23 [5] => 24 [6] => 30 [7] => 107 [8] => 1322 )
so you can see i selected the ids 107 and 1332 and they are both owned by that selecter yet it keeps giving me that error.can any1 see an error??

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 August 2006 - 06:15 PM

Maybe this will help:

$my_items = $_POST['my_items'];
$other = $_POST['other'];
$other_items = $_POST['other_items'];

$link = mysql_connect('localhost', $my_sql_password, $my_sql_username) or die(mysql_error());
$db_selected = mysql_select_db('osiris_thugg') or die(mysql_error());

$query = "SELECT COUNT(item_id) FROM items WHERE owner = '$user_currently_loged' AND item_id IN (" . implode(", ", $my_items) . ")";
$result = mysql_query($query) or die(mysql_error());

$count = mysql_result($result, 0);

if ($count != count($my_items)) {
	echo"One or more of the items selected never did or does not belong to you.";
	Echo "selected<br>";
	print_r(array_values($my_items));
	echo"Owned<br>";
	echo $count;
	exit;
}


#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 20 August 2006 - 06:22 PM

Odd ???

If I run this

<?php
$my_items = array (107,1322)  ;
$my_check1 = array (10 , 11 , 12 , 16 , 23 , 24 , 30 , 107 ,1322)  ;

if(count(array_intersect($my_items, $my_check1)) != count($my_items)) {
echo "One or more of the items selected never did or does not belong to you.";
}else echo "OK" ;
?>

It says "OK"
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 desithugg

desithugg
  • Members
  • PipPipPip
  • Advanced Member
  • 281 posts
  • LocationScarborough

Posted 20 August 2006 - 06:46 PM

yes i know but it is but i tried hitman6003's idea and it works perfect :) thnx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users