我从列中获取数据并希望将其存储在数组中,然后我想从另一个数组中减去该值,但它只是减去数组的第一个值。
这是我的代码:
<?php
include 'include/header.php';
include 'include/connect.php';
// fetch student which are already in database of event
$district = ($_GET['dis']);
@$admdistrict = $_SESSION['district'];
//echo $district;
$ret_ = array();
$query="SELECT * FROM student_info where district = '".$admdistrict."' AND user_status = 'approved' ;
$resultt = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
// output data of each row
while($row = mysql_fetch_array($result)) {
echo $row['id'];
$ret_[] = implode(',',$row['id']);
}
}
print_r($ret_);
只有这个变量$ ret_才能正确地给出第一个值,所有变量和函数都在正常工作之下。我的student_info表就像
id ! name ! District
4 ! Rahul ! Karnal
5 ! Sonu ! Panipat
6 ! Rohit ! Jind
这个$ ret变量只给出4作为OUTPUT。但我想(4,5,6)必须使用逗号,以便我可以从std_fulllist_array变量中减去它
$query="SELECT * FROM events where id='$district'";
$showdata=mysql_query($query) or die(mysql_error());
$user=mysql_fetch_array($showdata);
if(isset($_POST['submit']))
//print_r ($_POST);
{
@$std_list=implode(',',$_POST['std_list']);
$std_fulllist_array = explode(',', $user['std_list']);
$std_new_array = explode(',', @$std_list);
$std_delete_array = explode(',', $delete['id']);
$values = array_diff($std_fulllist_array, $ret_);
$values = array_merge($values, $std_new_array);
// Only keep unique values
$values = array_unique($values);
// Sort (maybe?)
sort($values);
// Convert to string
$values = implode(',', $values);
echo $values;
// string(15) "1,2,3,4,5,7,8,9"
var_dump($values);
if(empty($std_list))
{
$error = 1;
$get_value = "Please select you event students.";
}
else
{
//$query = mysql_query("INSERT INTO events (std_list) VALUES('".$std_list."')") or die(mysql_error());
$query = mysql_query("UPDATE events SET std_list= '$values' WHERE id='".$district."' ") or die(mysql_error());
//echo "$msg";
echo "Students list submitted successfully";
}
}
?>
答案 0 :(得分:0)
我怀疑你想要group_concat()
。您的代码有点难以理解,但我的猜测是:
SELECT GROUP_CONCAT(id)
FROM student_info
WHERE district = '".$admdistrict."' AND user_status = 'approved' ;