0

使用此代码.. 我需要将 $code 变量添加到 sql 查询的 where 子句中。

if ( isset( $_POST['code_search_submitted'])) {

        if ( isset( $_POST['searchCode']) && !empty( $_POST['searchText'])) {           


    $searchCode = $_POST['searchCode']; 
        $searchText = $_POST['searchText'];     

        echo $searchCode;
        echo '<br />';
        echo $searchText;
        //if (is_numeric( $searchText ) && (strlen($searchText) == 4)) {    

        $numbers = explode(",", $searchText);
        foreach($numbers as $number) {
            $numbers =  trim($number);


            if (ctype_digit($numbers) && (strlen($numbers) == 4)) { 

                $code = $numbers ;

            } else {

                echo 'Can not cantain string and more than 4 digists number';
            }

        }
}
}

这是我的查询...

SELECT 
    tcs.tutor_id AS tid, 
    t.qualification,
    GROUP_CONCAT( DISTINCT o.option_name SEPARATOR ', ') AS tutor_option, 
    timg.image_name AS img, 
    city_name AS city,
    d.district_name AS district
FROM tutor_category_subject as tcs
    INNER JOIN subject AS s ON tcs.subject_id = s.subject_id
    INNER JOIN tutors AS t ON tcs.tutor_id = t.tutor_id
    INNER JOIN address ON address.address_id = t.address_id
    INNER JOIN city ON city.city_id = address.city_id
    INNER JOIN district AS d ON d.district_id = city.district_id
    LEFT JOIN tutor_images AS timg ON timg.tutor_id = tcs.tutor_id AND timg.image_type = 'profile'
WHERE t.tutor_code = ------------
GROUP BY tcs.tutor_id;

$code 变量有更多的成员代码,例如 2434,2345,5345,2354 等...谁能告诉我如何在 where 子句中使用它?

谢谢...

4

3 回答 3

0
Updated code for query:

$query="SELECT 
    tcs.tutor_id AS tid, 
    t.qualification,
    GROUP_CONCAT( DISTINCT o.option_name SEPARATOR ', ') AS tutor_option, 
    timg.image_name AS img, 
    city_name AS city,
    d.district_name AS district
FROM tutor_category_subject as tcs
    INNER JOIN subject AS s ON tcs.subject_id = s.subject_id
    INNER JOIN tutors AS t ON tcs.tutor_id = t.tutor_id
    INNER JOIN address ON address.address_id = t.address_id
    INNER JOIN city ON city.city_id = address.city_id
    INNER JOIN district AS d ON d.district_id = city.district_id
    LEFT JOIN tutor_images AS timg ON timg.tutor_id = tcs.tutor_id AND timg.image_type = 'profile'
WHERE 1=1";
if($code!="")
{
$query.=" && ( $code ) ";
}
$query.="GROUP BY tcs.tutor_id";
于 2012-11-02T17:33:53.310 回答
0

您可以使用array_map来转换/处理每个值,然后将它们组合回来并使用in子句:

$numbers = explode(",", $searchText);
$parsed = array_map(function ($a) {
   if (ctype_digit($a) && (strlen($a) == 4)) { 
        return "'$a'" ;
    } else {
        echo 'Can not cantain string and more than 4 digists number';
    }
},
$numbers);

$numbers = '(' . preg_replace('/\,+/', ',',implode(',', $parsed)) . ')';

接着

SELECT 
    tcs.tutor_id AS tid, 
    t.qualification,
    GROUP_CONCAT( DISTINCT o.option_name SEPARATOR ', ') AS tutor_option, 
    timg.image_name AS img, 
    city_name AS city,
    d.district_name AS district
FROM tutor_category_subject as tcs
    INNER JOIN subject AS s ON tcs.subject_id = s.subject_id
    INNER JOIN tutors AS t ON tcs.tutor_id = t.tutor_id
    INNER JOIN address ON address.address_id = t.address_id
    INNER JOIN city ON city.city_id = address.city_id
    INNER JOIN district AS d ON d.district_id = city.district_id
    LEFT JOIN tutor_images AS timg ON timg.tutor_id = tcs.tutor_id AND timg.image_type = 'profile'
WHERE t.tutor_code IN $numbers
GROUP BY tcs.tutor_id;
于 2012-11-02T17:39:19.210 回答
0
if ( isset( $_POST['code_search_submitted'])) {

        if ( isset( $_POST['searchCode']) && !empty( $_POST['searchText'])) {           


    $searchCode = $_POST['searchCode']; 
        $searchText = $_POST['searchText'];     

        echo $searchCode;
        echo '<br />';
        echo $searchText;
        //if (is_numeric( $searchText ) && (strlen($searchText) == 4)) {    

        $numbers = explode(",", $searchText);

        $countmax=count($numbers)-1;

        foreach($numbers as $key=>$number) {
            $numbers =  trim($number);


            if (ctype_digit($numbers) && (strlen($numbers) == 4)) { 
                if($key==$countmax)
                {
                $code .= "t.tutor_code='".$number."'" ;
                }
                else
                {
                $code .= "t.tutor_code='".$number."' || " ;
                }

            } else {

                echo 'Can not cantain string and more than 4 digists number';
            }

        }
}
}


/////////////////////////////////////
//Query Updated
/////////////////////////////////


SELECT 
    tcs.tutor_id AS tid, 
    t.qualification,
    GROUP_CONCAT( DISTINCT o.option_name SEPARATOR ', ') AS tutor_option, 
    timg.image_name AS img, 
    city_name AS city,
    d.district_name AS district
FROM tutor_category_subject as tcs
    INNER JOIN subject AS s ON tcs.subject_id = s.subject_id
    INNER JOIN tutors AS t ON tcs.tutor_id = t.tutor_id
    INNER JOIN address ON address.address_id = t.address_id
    INNER JOIN city ON city.city_id = address.city_id
    INNER JOIN district AS d ON d.district_id = city.district_id
    LEFT JOIN tutor_images AS timg ON timg.tutor_id = tcs.tutor_id AND timg.image_type = 'profile'
WHERE 1=1 && ( $code ) 
GROUP BY tcs.tutor_id;
于 2012-11-02T17:08:38.070 回答