상품 검색은 굉장히 중요하다. 어떤 기준에 따라서 상품을 분류해서 보고 싶어하는 경우가 많기때문이다. 지금 해볼것은 진짜 기본적인 것들이다. 이것을 바탕으로 다양한 요구들에 대한 검색을 처리할 수 있다.
일단 카테고리 분류부터 확인해보자.
/admin/product/product_list.php
<?php
include $_SERVER [ "DOCUMENT_ROOT" ] . "/inc/header.php" ;
if (! $_SESSION [ 'AUID' ]){
echo "<script>alert('권한이 없습니다.');history.back();</script>" ;
exit ;
}
$pageNumber = $_GET [ 'pageNumber' ]?? 1 ; //현재 페이지, 없으면 1
if ( $pageNumber < 1 ) $pageNumber = 1 ;
$pageCount = $_GET [ 'pageCount' ]?? 10 ; //페이지당 몇개씩 보여줄지, 없으면 10
$startLimit = ( $pageNumber - 1 )* $pageCount ; //쿼리의 limit 시작 부분
$firstPageNumber = $_GET [ 'firstPageNumber' ];
$cate1 = $_GET [ 'cate1' ];
$cate2 = $_GET [ 'cate2' ];
$cate3 = $_GET [ 'cate3' ];
$query = "select * from category where step=1" ;
$result = $mysqli -> query ( $query ) or die ( "query error => " . $mysqli -> error );
while ( $rs = $result -> fetch_object ()){
$cate1array []= $rs ;
}
if ( $cate1 ){
$query = "select * from category where step=2 and pcode='" . $cate1 . "'" ;
$result = $mysqli -> query ( $query ) or die ( "query error => " . $mysqli -> error );
while ( $rs = $result -> fetch_object ()){
$cate2array []= $rs ;
}
}
if ( $cate2 ){
$query = "select * from category where step=3 and pcode='" . $cate2 . "'" ;
$result = $mysqli -> query ( $query ) or die ( "query error => " . $mysqli -> error );
while ( $rs = $result -> fetch_object ()){
$cate3array []= $rs ;
}
}
$scate = $cate1 . $cate2 . $cate3 ; //카테고리를 모두 묶음
if ( $scate ){
$search_where = " and cate like '" . $scate . "%'" ; //like 검색으로 검색
}
$sql = "select * from products where 1=1" ;
$sql .= $search_where ;
$order = " order by pid desc" ; //마지막에 등록한걸 먼저 보여줌
$limit = " limit $startLimit , $pageCount " ;
$query = $sql . $order . $limit ;
echo "query=>" . $query . "<br>" ;
$result = $mysqli -> query ( $query ) or die ( "query error => " . $mysqli -> error );
while ( $rs = $result -> fetch_object ()){
$rsc []= $rs ;
}
//전체게시물 수 구하기
$sqlcnt = "select count(*) as cnt from products where 1=1" ;
$sqlcnt .= $search_where ;
$countresult = $mysqli -> query ( $sqlcnt ) or die ( "query error => " . $mysqli -> error );
$rscnt = $countresult -> fetch_object ();
$totalCount = $rscnt -> cnt ; //전체 갯수를 구한다.
$totalPage = ceil ( $totalCount / $pageCount ); //전체 페이지를 구한다.
if ( $firstPageNumber < 1 ) $firstPageNumber = 1 ;
$lastPageNumber = $firstPageNumber + $pageCount - 1 ; //페이징 나오는 부분에서 레인지를 정한다.
if ( $lastPageNumber > $totalPage ) $lastPageNumber = $totalPage ;
function isStatus ( $n ){
switch ( $n ) {
case - 1 : $rs = "판매중지" ;
break ;
case 0 : $rs = "대기" ;
break ;
case 1 : $rs = "판매중" ;
break ;
}
return $rs ;
}
? >
< div style = " text-align:center; " >< h3 > 제품 리스트 </ h3 ></ div >
< form method = "get" action = " <?php echo $_SERVER [ "PHP_SELF" ] ? > " >
< div class = "row g-3" style = " padding-bottom:10px; " >
< div class = "col-md-4" >
< select class = "form-select" name = "cate1" id = "cate1" aria-label = "Default select example" >
< option value = "" > 대분류 </ option >
<?php
foreach ( $cate1array as $c ){
? >
< option value = " <?php echo $c -> code ; ? > " <?php if ( $cate1 == $c -> code ){ echo "selected" ;} ? > > <?php echo $c -> name ; ? > </ option >
<?php } ? >
</ select >
</ div >
< div class = "col-md-4" >
< select class = "form-select" name = "cate2" id = "cate2" aria-label = "Default select example" >
< option value = "" > 중분류 </ option >
<?php
foreach ( $cate2array as $c ){
? >
< option value = " <?php echo $c -> code ; ? > " <?php if ( $cate2 == $c -> code ){ echo "selected" ;} ? > > <?php echo $c -> name ; ? > </ option >
<?php } ? >
</ select >
</ div >
< div class = "col-md-4" >
< select class = "form-select" name = "cate3" id = "cate3" aria-label = "Default select example" >
< option value = "" > 소분류 </ option >
<?php
foreach ( $cate3array as $c ){
? >
< option value = " <?php echo $c -> code ; ? > " <?php if ( $cate3 == $c -> code ){ echo "selected" ;} ? > > <?php echo $c -> name ; ? > </ option >
<?php } ? >
</ select >
</ div >
</ div >
< div class = "input-group mb-12" style = " width:100%;padding-bottom:10px; " >
< input class = "form-check-input" type = "checkbox" name = "ismain" id = "ismain" value = "1" > 메인
< input class = "form-check-input" type = "checkbox" name = "isnew" id = "isnew" value = "1" > 신제품
< input class = "form-check-input" type = "checkbox" name = "isbest" id = "isbest" value = "1" > 베스트
< input class = "form-check-input" type = "checkbox" name = "isrecom" id = "isrecom" value = "1" > 추천
판매종료일: < input type = "text" class = "form-control" style = " max-width:150px; " name = "sale_end_date" id = "sale_end_date" value = " <?php echo date ( "Y-m-d" ); ? > " >
< input type = "text" class = "form-control" name = "search_keyword" id = "search_keyword" placeholder = "제목과 내용에서 검색합니다." value = " <?php echo $search_keyword ; ? > " aria-label = "Recipient's username" aria-describedby = "button-addon2" >
< button class = "btn btn-outline-secondary" type = "submit" id = "search" > 검색 </ button >
</ div >
</ form >
< table class = "table table-sm table-bordered" >
< thead >
< tr style = " text-align:center; " >
< th scope = "col" > 사진 </ th >
< th scope = "col" > 제품명 </ th >
< th scope = "col" > 가격 </ th >
< th scope = "col" > 재고 </ th >
< th scope = "col" > 메인 </ th >
< th scope = "col" > 신제품 </ th >
< th scope = "col" > 베스트 </ th >
< th scope = "col" > 추천 </ th >
< th scope = "col" > 상태 </ th >
</ tr >
</ thead >
< tbody >
<?php
foreach ( $rsc as $r ){
? >
< tr >
< th scope = "row" style = " width:100px; " >< img src = " <?php echo $r -> thumbnail ; ? > " style = " max-width:100px; " ></ th >
< td > <?php echo $r -> name ; ? > </ td >
< td style = " text-align:right; " >< s > <?php echo number_format ( $r -> price ); ? > 원 </ s >
<?php if ( $r -> sale_price > 0 ){ ? >
< br >
<?php echo number_format ( $r -> sale_price ); ? > 원
<?php } ? >
</ td >
< td style = " text-align:right; " > <?php echo number_format ( $r -> cnt - $r -> sale_cnt ); ? > EA </ td >
< td style = " text-align:center; " >< input type = "checkbox" name = "ismain[ <?php echo $r -> pid ; ? > ]" id = "ismain_ <?php echo $r -> pid ; ? > " value = "1" <?php if ( $r -> ismain ){ echo "checked" ;} ? > ></ td >
< td style = " text-align:center; " >< input type = "checkbox" name = "isnew[ <?php echo $r -> pid ; ? > ]" id = "isnew_ <?php echo $r -> pid ; ? > " value = "1" <?php if ( $r -> isnew ){ echo "checked" ;} ? > ></ td >
< td style = " text-align:center; " >< input type = "checkbox" name = "isbest[ <?php echo $r -> pid ; ? > ]" id = "isbest_ <?php echo $r -> pid ; ? > " value = "1" <?php if ( $r -> isbest ){ echo "checked" ;} ? > ></ td >
< td style = " text-align:center; " >< input type = "checkbox" name = "isrecom[ <?php echo $r -> pid ; ? > ]" id = "isrecom_ <?php echo $r -> pid ; ? > " value = "1" <?php if ( $r -> isrecom ){ echo "checked" ;} ? > ></ td >
< td style = " text-align:center; " > <?php echo isStatus ( $r -> status ); ? > </ td >
</ tr >
<?php } ? >
</ tbody >
</ table >
< a href = "product_up.php" >
< button class = "btn btn-primary" type = "button" > 제품등록 </ button >
</ a >
< link rel = "stylesheet" href = "//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" >
< script >
$ ( function (){
$ ( "#sale_end_date" ). datepicker ({ dateFormat: 'yy-mm-dd' });
});
$ ( "#cate1" ). change ( function (){
var cate1 = $ ( "#cate1 option:selected" ). val ();
var data = {
cate1 : cate1
};
$ . ajax ({
async : false ,
type : 'post' ,
url : 'category2.php' ,
data : data ,
dataType : 'html' ,
error : function () {} ,
success : function ( return_data ) {
$ ( "#cate2" ). html ( return_data );
}
});
});
$ ( "#cate2" ). change ( function (){
var cate2 = $ ( "#cate2 option:selected" ). val ();
var data = {
cate2 : cate2
};
$ . ajax ({
async : false ,
type : 'post' ,
url : 'category3.php' ,
data : data ,
dataType : 'html' ,
error : function () {} ,
success : function ( return_data ) {
$ ( "#cate3" ). html ( return_data );
}
});
});
</ script >
<?php
include $_SERVER [ "DOCUMENT_ROOT" ] . "/inc/footer.php" ;
? >
이렇게 하고 저장한 후 대분류를 하나 선택하고 검색을 해보자. 그때 쿼리문이 어떻게 나오는지 확인하기 위해 쿼리를 찍어 보았다. 쿼리를 확인해보자.
select * from products where 1=1 and cate like 'A0001%' order by pid desc limit 0, 10
중분류를 선택하고 검색해보자.
select * from products where 1=1 and cate like 'A0001B0001%' order by pid desc limit 0, 10
소분류도 해보자.
select * from products where 1=1 and cate like 'A0001B0001C0001%' order by pid desc limit 0, 10
제품을 생성할때 각각의 카테고리 코드값을 모두 저장한 이유가 이것이다. 대분류나 중분류만 선택해도 제품을 검색할 수 있도록 하기 위함이다.
이번엔 메인등 체크박스로 돼 있는 부분에 대한 검색이다.
/admin/product/product_list.php
<?php
include $_SERVER [ "DOCUMENT_ROOT" ] . "/inc/header.php" ;
if (! $_SESSION [ 'AUID' ]){
echo "<script>alert('권한이 없습니다.');history.back();</script>" ;
exit ;
}
$pageNumber = $_GET [ 'pageNumber' ]?? 1 ; //현재 페이지, 없으면 1
if ( $pageNumber < 1 ) $pageNumber = 1 ;
$pageCount = $_GET [ 'pageCount' ]?? 10 ; //페이지당 몇개씩 보여줄지, 없으면 10
$startLimit = ( $pageNumber - 1 )* $pageCount ; //쿼리의 limit 시작 부분
$firstPageNumber = $_GET [ 'firstPageNumber' ];
$cate1 = $_GET [ 'cate1' ];
$cate2 = $_GET [ 'cate2' ];
$cate3 = $_GET [ 'cate3' ];
$ismain = $_GET [ "ismain" ];
$isnew = $_GET [ "isnew" ];
$isbest = $_GET [ "isbest" ];
$isrecom = $_GET [ "isrecom" ];
$query = "select * from category where step=1" ;
$result = $mysqli -> query ( $query ) or die ( "query error => " . $mysqli -> error );
while ( $rs = $result -> fetch_object ()){
$cate1array []= $rs ;
}
if ( $cate1 ){
$query = "select * from category where step=2 and pcode='" . $cate1 . "'" ;
$result = $mysqli -> query ( $query ) or die ( "query error => " . $mysqli -> error );
while ( $rs = $result -> fetch_object ()){
$cate2array []= $rs ;
}
}
if ( $cate2 ){
$query = "select * from category where step=3 and pcode='" . $cate2 . "'" ;
$result = $mysqli -> query ( $query ) or die ( "query error => " . $mysqli -> error );
while ( $rs = $result -> fetch_object ()){
$cate3array []= $rs ;
}
}
$scate = $cate1 . $cate2 . $cate3 ; //카테고리를 모두 묶음
if ( $scate ){
$search_where = " and cate like '" . $scate . "%'" ; //like 검색으로 검색
}
if ( $ismain ){ //값이 있는 경우에만 검색 조건에 추가한다.
$search_where .= " and ismain=1" ;
}
if ( $isnew ){
$search_where .= " and isnew=1" ;
}
if ( $isbest ){
$search_where .= " and isbest=1" ;
}
if ( $isrecom ){
$search_where .= " and isrecom=1" ;
}
$sql = "select * from products where 1=1" ;
$sql .= $search_where ;
$order = " order by pid desc" ; //마지막에 등록한걸 먼저 보여줌
$limit = " limit $startLimit , $pageCount " ;
$query = $sql . $order . $limit ;
echo "query=>" . $query . "<br>" ;
$result = $mysqli -> query ( $query ) or die ( "query error => " . $mysqli -> error );
while ( $rs = $result -> fetch_object ()){
$rsc []= $rs ;
}
//전체게시물 수 구하기
$sqlcnt = "select count(*) as cnt from products where 1=1" ;
$sqlcnt .= $search_where ;
$countresult = $mysqli -> query ( $sqlcnt ) or die ( "query error => " . $mysqli -> error );
$rscnt = $countresult -> fetch_object ();
$totalCount = $rscnt -> cnt ; //전체 갯수를 구한다.
$totalPage = ceil ( $totalCount / $pageCount ); //전체 페이지를 구한다.
if ( $firstPageNumber < 1 ) $firstPageNumber = 1 ;
$lastPageNumber = $firstPageNumber + $pageCount - 1 ; //페이징 나오는 부분에서 레인지를 정한다.
if ( $lastPageNumber > $totalPage ) $lastPageNumber = $totalPage ;
function isStatus ( $n ){
switch ( $n ) {
case - 1 : $rs = "판매중지" ;
break ;
case 0 : $rs = "대기" ;
break ;
case 1 : $rs = "판매중" ;
break ;
}
return $rs ;
}
? >
< div style = " text-align:center; " >< h3 > 제품 리스트 </ h3 ></ div >
< form method = "get" action = " <?php echo $_SERVER [ "PHP_SELF" ] ? > " >
< div class = "row g-3" style = " padding-bottom:10px; " >
< div class = "col-md-4" >
< select class = "form-select" name = "cate1" id = "cate1" aria-label = "Default select example" >
< option value = "" > 대분류 </ option >
<?php
foreach ( $cate1array as $c ){
? >
< option value = " <?php echo $c -> code ; ? > " <?php if ( $cate1 == $c -> code ){ echo "selected" ;} ? > > <?php echo $c -> name ; ? > </ option >
<?php } ? >
</ select >
</ div >
< div class = "col-md-4" >
< select class = "form-select" name = "cate2" id = "cate2" aria-label = "Default select example" >
< option value = "" > 중분류 </ option >
<?php
foreach ( $cate2array as $c ){
? >
< option value = " <?php echo $c -> code ; ? > " <?php if ( $cate2 == $c -> code ){ echo "selected" ;} ? > > <?php echo $c -> name ; ? > </ option >
<?php } ? >
</ select >
</ div >
< div class = "col-md-4" >
< select class = "form-select" name = "cate3" id = "cate3" aria-label = "Default select example" >
< option value = "" > 소분류 </ option >
<?php
foreach ( $cate3array as $c ){
? >
< option value = " <?php echo $c -> code ; ? > " <?php if ( $cate3 == $c -> code ){ echo "selected" ;} ? > > <?php echo $c -> name ; ? > </ option >
<?php } ? >
</ select >
</ div >
</ div >
< div class = "input-group mb-12" style = " width:100%;padding-bottom:10px; " >
< input class = "form-check-input" type = "checkbox" name = "ismain" id = "ismain" value = "1" <?php if ( $ismain ){ echo "checked" ;} ? > > 메인
< input class = "form-check-input" type = "checkbox" name = "isnew" id = "isnew" value = "1" <?php if ( $isnew ){ echo "checked" ;} ? > > 신제품
< input class = "form-check-input" type = "checkbox" name = "isbest" id = "isbest" value = "1" <?php if ( $isbest ){ echo "checked" ;} ? > > 베스트
< input class = "form-check-input" type = "checkbox" name = "isrecom" id = "isrecom" value = "1" <?php if ( $isrecom ){ echo "checked" ;} ? > > 추천
판매종료일: < input type = "text" class = "form-control" style = " max-width:150px; " name = "sale_end_date" id = "sale_end_date" value = " <?php echo date ( "Y-m-d" ); ? > " >
< input type = "text" class = "form-control" name = "search_keyword" id = "search_keyword" placeholder = "제목과 내용에서 검색합니다." value = " <?php echo $search_keyword ; ? > " aria-label = "Recipient's username" aria-describedby = "button-addon2" >
< button class = "btn btn-outline-secondary" type = "submit" id = "search" > 검색 </ button >
</ div >
</ form >
< table class = "table table-sm table-bordered" >
< thead >
< tr style = " text-align:center; " >
< th scope = "col" > 사진 </ th >
< th scope = "col" > 제품명 </ th >
< th scope = "col" > 가격 </ th >
< th scope = "col" > 재고 </ th >
< th scope = "col" > 메인 </ th >
< th scope = "col" > 신제품 </ th >
< th scope = "col" > 베스트 </ th >
< th scope = "col" > 추천 </ th >
< th scope = "col" > 상태 </ th >
</ tr >
</ thead >
< tbody >
<?php
foreach ( $rsc as $r ){
? >
< tr >
< th scope = "row" style = " width:100px; " >< img src = " <?php echo $r -> thumbnail ; ? > " style = " max-width:100px; " ></ th >
< td > <?php echo $r -> name ; ? > </ td >
< td style = " text-align:right; " >< s > <?php echo number_format ( $r -> price ); ? > 원 </ s >
<?php if ( $r -> sale_price > 0 ){ ? >
< br >
<?php echo number_format ( $r -> sale_price ); ? > 원
<?php } ? >
</ td >
< td style = " text-align:right; " > <?php echo number_format ( $r -> cnt - $r -> sale_cnt ); ? > EA </ td >
< td style = " text-align:center; " >< input type = "checkbox" name = "ismain[ <?php echo $r -> pid ; ? > ]" id = "ismain_ <?php echo $r -> pid ; ? > " value = "1" <?php if ( $r -> ismain ){ echo "checked" ;} ? > ></ td >
< td style = " text-align:center; " >< input type = "checkbox" name = "isnew[ <?php echo $r -> pid ; ? > ]" id = "isnew_ <?php echo $r -> pid ; ? > " value = "1" <?php if ( $r -> isnew ){ echo "checked" ;} ? > ></ td >
< td style = " text-align:center; " >< input type = "checkbox" name = "isbest[ <?php echo $r -> pid ; ? > ]" id = "isbest_ <?php echo $r -> pid ; ? > " value = "1" <?php if ( $r -> isbest ){ echo "checked" ;} ? > ></ td >
< td style = " text-align:center; " >< input type = "checkbox" name = "isrecom[ <?php echo $r -> pid ; ? > ]" id = "isrecom_ <?php echo $r -> pid ; ? > " value = "1" <?php if ( $r -> isrecom ){ echo "checked" ;} ? > ></ td >
< td style = " text-align:center; " > <?php echo isStatus ( $r -> status ); ? > </ td >
</ tr >
<?php } ? >
</ tbody >
</ table >
< a href = "product_up.php" >
< button class = "btn btn-primary" type = "button" > 제품등록 </ button >
</ a >
< link rel = "stylesheet" href = "//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" >
< script >
$ ( function (){
$ ( "#sale_end_date" ). datepicker ({ dateFormat: 'yy-mm-dd' });
});
$ ( "#cate1" ). change ( function (){
var cate1 = $ ( "#cate1 option:selected" ). val ();
var data = {
cate1 : cate1
};
$ . ajax ({
async : false ,
type : 'post' ,
url : 'category2.php' ,
data : data ,
dataType : 'html' ,
error : function () {} ,
success : function ( return_data ) {
$ ( "#cate2" ). html ( return_data );
}
});
});
$ ( "#cate2" ). change ( function (){
var cate2 = $ ( "#cate2 option:selected" ). val ();
var data = {
cate2 : cate2
};
$ . ajax ({
async : false ,
type : 'post' ,
url : 'category3.php' ,
data : data ,
dataType : 'html' ,
error : function () {} ,
success : function ( return_data ) {
$ ( "#cate3" ). html ( return_data );
}
});
});
</ script >
<?php
include $_SERVER [ "DOCUMENT_ROOT" ] . "/inc/footer.php" ;
? >
checkbox는 체크가 되면 값이 있고 없으면 값이 없으므로 값이 있는 경우에만 검색에 추가한다. 값이 있으면 체크박스를 체크해준다.
이번엔 날짜 검색과 텍스트 검색이다.
<?php
include $_SERVER [ "DOCUMENT_ROOT" ] . "/inc/header.php" ;
if (! $_SESSION [ 'AUID' ]){
echo "<script>alert('권한이 없습니다.');history.back();</script>" ;
exit ;
}
$pageNumber = $_GET [ 'pageNumber' ]?? 1 ; //현재 페이지, 없으면 1
if ( $pageNumber < 1 ) $pageNumber = 1 ;
$pageCount = $_GET [ 'pageCount' ]?? 10 ; //페이지당 몇개씩 보여줄지, 없으면 10
$startLimit = ( $pageNumber - 1 )* $pageCount ; //쿼리의 limit 시작 부분
$firstPageNumber = $_GET [ 'firstPageNumber' ];
$cate1 = $_GET [ 'cate1' ];
$cate2 = $_GET [ 'cate2' ];
$cate3 = $_GET [ 'cate3' ];
$ismain = $_GET [ "ismain" ];
$isnew = $_GET [ "isnew" ];
$isbest = $_GET [ "isbest" ];
$isrecom = $_GET [ "isrecom" ];
$sale_end_date = $_GET [ "sale_end_date" ];
$search_keyword = $_GET [ "search_keyword" ];
$query = "select * from category where step=1" ;
$result = $mysqli -> query ( $query ) or die ( "query error => " . $mysqli -> error );
while ( $rs = $result -> fetch_object ()){
$cate1array []= $rs ;
}
if ( $cate1 ){
$query = "select * from category where step=2 and pcode='" . $cate1 . "'" ;
$result = $mysqli -> query ( $query ) or die ( "query error => " . $mysqli -> error );
while ( $rs = $result -> fetch_object ()){
$cate2array []= $rs ;
}
}
if ( $cate2 ){
$query = "select * from category where step=3 and pcode='" . $cate2 . "'" ;
$result = $mysqli -> query ( $query ) or die ( "query error => " . $mysqli -> error );
while ( $rs = $result -> fetch_object ()){
$cate3array []= $rs ;
}
}
$scate = $cate1 . $cate2 . $cate3 ; //카테고리를 모두 묶음
if ( $scate ){
$search_where = " and cate like '" . $scate . "%'" ; //like 검색으로 검색
}
if ( $ismain ){ //값이 있는 경우에만 검색 조건에 추가한다.
$search_where .= " and ismain=1" ;
}
if ( $isnew ){
$search_where .= " and isnew=1" ;
}
if ( $isbest ){
$search_where .= " and isbest=1" ;
}
if ( $isrecom ){
$search_where .= " and isrecom=1" ;
}
if ( $sale_end_date ){
$search_where .= " and sale_end_date<='" . $sale_end_date . "'" ;
}
if ( $search_keyword ){
$search_where .= " and (name like '%" . $search_keyword . "%' or content like '%" . $search_keyword . "%')" ; //like 검색으로 검색
}
$sql = "select * from products where 1=1" ;
$sql .= $search_where ;
$order = " order by pid desc" ; //마지막에 등록한걸 먼저 보여줌
$limit = " limit $startLimit , $pageCount " ;
$query = $sql . $order . $limit ;
echo "query=>" . $query . "<br>" ;
$result = $mysqli -> query ( $query ) or die ( "query error => " . $mysqli -> error );
while ( $rs = $result -> fetch_object ()){
$rsc []= $rs ;
}
//전체게시물 수 구하기
$sqlcnt = "select count(*) as cnt from products where 1=1" ;
$sqlcnt .= $search_where ;
$countresult = $mysqli -> query ( $sqlcnt ) or die ( "query error => " . $mysqli -> error );
$rscnt = $countresult -> fetch_object ();
$totalCount = $rscnt -> cnt ; //전체 갯수를 구한다.
$totalPage = ceil ( $totalCount / $pageCount ); //전체 페이지를 구한다.
if ( $firstPageNumber < 1 ) $firstPageNumber = 1 ;
$lastPageNumber = $firstPageNumber + $pageCount - 1 ; //페이징 나오는 부분에서 레인지를 정한다.
if ( $lastPageNumber > $totalPage ) $lastPageNumber = $totalPage ;
function isStatus ( $n ){
switch ( $n ) {
case - 1 : $rs = "판매중지" ;
break ;
case 0 : $rs = "대기" ;
break ;
case 1 : $rs = "판매중" ;
break ;
}
return $rs ;
}
? >
< div style = " text-align:center; " >< h3 > 제품 리스트 </ h3 ></ div >
< form method = "get" action = " <?php echo $_SERVER [ "PHP_SELF" ] ? > " >
< div class = "row g-3" style = " padding-bottom:10px; " >
< div class = "col-md-4" >
< select class = "form-select" name = "cate1" id = "cate1" aria-label = "Default select example" >
< option value = "" > 대분류 </ option >
<?php
foreach ( $cate1array as $c ){
? >
< option value = " <?php echo $c -> code ; ? > " <?php if ( $cate1 == $c -> code ){ echo "selected" ;} ? > > <?php echo $c -> name ; ? > </ option >
<?php } ? >
</ select >
</ div >
< div class = "col-md-4" >
< select class = "form-select" name = "cate2" id = "cate2" aria-label = "Default select example" >
< option value = "" > 중분류 </ option >
<?php
foreach ( $cate2array as $c ){
? >
< option value = " <?php echo $c -> code ; ? > " <?php if ( $cate2 == $c -> code ){ echo "selected" ;} ? > > <?php echo $c -> name ; ? > </ option >
<?php } ? >
</ select >
</ div >
< div class = "col-md-4" >
< select class = "form-select" name = "cate3" id = "cate3" aria-label = "Default select example" >
< option value = "" > 소분류 </ option >
<?php
foreach ( $cate3array as $c ){
? >
< option value = " <?php echo $c -> code ; ? > " <?php if ( $cate3 == $c -> code ){ echo "selected" ;} ? > > <?php echo $c -> name ; ? > </ option >
<?php } ? >
</ select >
</ div >
</ div >
< div class = "input-group mb-12" style = " width:100%;padding-bottom:10px; " >
< input class = "form-check-input" type = "checkbox" name = "ismain" id = "ismain" value = "1" <?php if ( $ismain ){ echo "checked" ;} ? > > 메인
< input class = "form-check-input" type = "checkbox" name = "isnew" id = "isnew" value = "1" <?php if ( $isnew ){ echo "checked" ;} ? > > 신제품
< input class = "form-check-input" type = "checkbox" name = "isbest" id = "isbest" value = "1" <?php if ( $isbest ){ echo "checked" ;} ? > > 베스트
< input class = "form-check-input" type = "checkbox" name = "isrecom" id = "isrecom" value = "1" <?php if ( $isrecom ){ echo "checked" ;} ? > > 추천
판매종료일: < input type = "text" class = "form-control" style = " max-width:150px; " name = "sale_end_date" id = "sale_end_date" value = " <?php echo $sale_end_date ? > " >
< input type = "text" class = "form-control" name = "search_keyword" id = "search_keyword" placeholder = "제목과 내용에서 검색합니다." value = " <?php echo $search_keyword ; ? > " aria-label = "Recipient's username" aria-describedby = "button-addon2" >
< button class = "btn btn-outline-secondary" type = "submit" id = "search" > 검색 </ button >
</ div >
</ form >
< table class = "table table-sm table-bordered" >
< thead >
< tr style = " text-align:center; " >
< th scope = "col" > 사진 </ th >
< th scope = "col" > 제품명 </ th >
< th scope = "col" > 가격 </ th >
< th scope = "col" > 재고 </ th >
< th scope = "col" > 메인 </ th >
< th scope = "col" > 신제품 </ th >
< th scope = "col" > 베스트 </ th >
< th scope = "col" > 추천 </ th >
< th scope = "col" > 상태 </ th >
</ tr >
</ thead >
< tbody >
<?php
foreach ( $rsc as $r ){
? >
< tr >
< th scope = "row" style = " width:100px; " >< img src = " <?php echo $r -> thumbnail ; ? > " style = " max-width:100px; " ></ th >
< td > <?php echo $r -> name ; ? > </ td >
< td style = " text-align:right; " >< s > <?php echo number_format ( $r -> price ); ? > 원 </ s >
<?php if ( $r -> sale_price > 0 ){ ? >
< br >
<?php echo number_format ( $r -> sale_price ); ? > 원
<?php } ? >
</ td >
< td style = " text-align:right; " > <?php echo number_format ( $r -> cnt - $r -> sale_cnt ); ? > EA </ td >
< td style = " text-align:center; " >< input type = "checkbox" name = "ismain[ <?php echo $r -> pid ; ? > ]" id = "ismain_ <?php echo $r -> pid ; ? > " value = "1" <?php if ( $r -> ismain ){ echo "checked" ;} ? > ></ td >
< td style = " text-align:center; " >< input type = "checkbox" name = "isnew[ <?php echo $r -> pid ; ? > ]" id = "isnew_ <?php echo $r -> pid ; ? > " value = "1" <?php if ( $r -> isnew ){ echo "checked" ;} ? > ></ td >
< td style = " text-align:center; " >< input type = "checkbox" name = "isbest[ <?php echo $r -> pid ; ? > ]" id = "isbest_ <?php echo $r -> pid ; ? > " value = "1" <?php if ( $r -> isbest ){ echo "checked" ;} ? > ></ td >
< td style = " text-align:center; " >< input type = "checkbox" name = "isrecom[ <?php echo $r -> pid ; ? > ]" id = "isrecom_ <?php echo $r -> pid ; ? > " value = "1" <?php if ( $r -> isrecom ){ echo "checked" ;} ? > ></ td >
< td style = " text-align:center; " > <?php echo isStatus ( $r -> status ); ? > </ td >
</ tr >
<?php } ? >
</ tbody >
</ table >
< a href = "product_up.php" >
< button class = "btn btn-primary" type = "button" > 제품등록 </ button >
</ a >
< link rel = "stylesheet" href = "//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" >
< script >
$ ( function (){
$ ( "#sale_end_date" ). datepicker ({ dateFormat: 'yy-mm-dd' });
});
$ ( "#cate1" ). change ( function (){
var cate1 = $ ( "#cate1 option:selected" ). val ();
var data = {
cate1 : cate1
};
$ . ajax ({
async : false ,
type : 'post' ,
url : 'category2.php' ,
data : data ,
dataType : 'html' ,
error : function () {} ,
success : function ( return_data ) {
$ ( "#cate2" ). html ( return_data );
}
});
});
$ ( "#cate2" ). change ( function (){
var cate2 = $ ( "#cate2 option:selected" ). val ();
var data = {
cate2 : cate2
};
$ . ajax ({
async : false ,
type : 'post' ,
url : 'category3.php' ,
data : data ,
dataType : 'html' ,
error : function () {} ,
success : function ( return_data ) {
$ ( "#cate3" ). html ( return_data );
}
});
});
</ script >
<?php
include $_SERVER [ "DOCUMENT_ROOT" ] . "/inc/footer.php" ;
? >
큰 차이는 없지만 날짜도 크다 작다고 검색이 된다는 것과 제목과 내용을 동시에 검색하기 위해 like검색에 or를 사용한 부분을 보면 된다.
select * from products where 1=1 and sale_end_date<='2022-10-26' and (name like '%노트북%' or content like '%노트북%') order by pid desc limit 0, 10
모든 검색을 다 넣어보자.
select * from products where 1=1 and cate like 'A0001B0001C0001%' and ismain=1 and isnew=1 and isbest=1 and isrecom=1 and sale_end_date<='2022-10-26' and (name like '%노트북%' or content like '%노트북%') order by pid desc limit 0, 10
이렇게 된다.
개발하다 힘들면 눌러.
https://www.zzarbang.com