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
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 に答える
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 に答える