2016-09-13 11 views
0

저는 PHP로 PHP 쿼리를하는 데 약간 익숙해 져있어서 제발 맨발로 나와주세요.WordPress의 사용자 정의 테이블 검색

저는 현재 클라이언트 용 사이트를 구축하고 있으며 제공하는 모든 제품을 검색하는 검색 기능을 원합니다.

데이터베이스에 sc_products라는 새 테이블을 만든 다음 WordPress 템플릿 페이지에서 아래 코드를 시도했지만 어디에도 표시되지 않습니다. 누구든지 내가 어떻게이 일을 얻을 수 있는지 알고 있다면 그것은 최고 일 것입니다!

<?php 
/* 

Template Name: myTemp 

*/ 

function search_it() { 
if (is_search() && isset($_GET['s'])) { 

    global $wpdb; 

    $address_table = $wpdb->prefix . "sc_products"; 
    $myrows = $wpdb->get_results('SELECT product FROM ' . $sc_products); 
    foreach ($myrows as $single_row) { 
     global $single_row; 
    } 
    $product = $single_row->product; 
} 
    return $product; 
} 
$city = search_it(); 
echo $city; 

get_search_form(); 

?>  
+0

이 코드에서 수행하고자하는 것에 대한 설명이 가장 도움이 될 것입니다. 그것이 그대로, 우리는 당신이 무엇을 성취하려고하는지 전혀 모른다. –

+0

안녕하세요, 저는 wordpress에 sc_products라는 데이터베이스를 설치하고 여기에 제품 데이터가 포함 된 3 개의 열이있는 사용자 정의 테이블이 있습니다. 이 표의 모든 데이터를 검색하는 검색 양식을 만들어야 양식에서 일치하는 결과 만 반환됩니다. 아무 것도 반환되지 않으면 연락처 양식에 대한 링크가있는 문자열이 반환됩니다. – KKBSE2016

+0

좋습니다, 그게 더 유용합니다. 테이블에있는 세 개의 열은 무엇입니까? (그렇지 않으면, 우리는 어떻게 검색 질의를 작성 하는지를 어떻게 알 수 있습니까?) –

답변

0

여기에 몇 가지가 있습니다. 우리가 해결할 수 있는지 알아 봅시다.

여기에는 검색을 수행해야하는 수정 된 기능이 있으며 여기에는 의견을 통해 설명이 나와 있습니다.

참고 :
1. $address_table을 선언했지만 $sc_products을 사용합니다. $sc_products이 정의되지 않았으므로 정확하지 않을 수 있습니다.
2.이 답변은 댓글에서 제공 한 열 이름을 기반으로합니다. 정확한 일치가 아닌 경우 일치 항목을 포함하여 업데이트해야합니다. Application이 아니라 application 인 경우 변경해야합니다.

function search_it() { 
// If this is a search, and the search variable is set 
    if (is_search() && isset($_GET['s'])) { 
     // Global in $wpdb so we can run a query 
     global $wpdb; 
     // Build the table name - glue the table prefix onto the front of the table name of "sc_products" 
     $address_table = $wpdb->prefix . "sc_products"; 
     // Get the search term into a variable for convenience 
     $search = $_GET['s']; 
     // To use "LIKE" with the wildcards (%), we have to do some funny business: 
     $search = "%{$search}%"; 
     // Build the where clause using $wpdb->prepare to prevent SQL injection attacks 
     // Searching ALL THREE of our columns: Product, Application, Sector 
     $where = $wpdb->prepare('WHERE Product LIKE %s OR Application LIKE %s OR Sector LIKE %s', $search, $search, $search); 
     // Execute the query with our WHERE clause 
     // NOTE: Your code originally used "$sc_products", but that variable was not defined - so have replaced to the proper $address_table here. 
     $results = $wpdb->get_results("SELECT product FROM {$address_table} {$where}"); 
     // return the results. No need to put into an array, it's already an array 
     return $product; 
    } 

    // For consistency, return an empty array if not a search/no search term 
    return array(); 
} 

// USAGE: 
// Run the query 
$cities = search_it(); 
// You can't echo an array! 
// echo $city; 
// var_dump to see the whole array 
var_dump($cities); 

// More useful USAGE: 
$cities = search_it(); 
// If there's any results 
if (! empty($cities)) { 
    // Output a title 
    echo '<h1>Product Search Results:</h1>'; 
    // Loop over the results 
    foreach($cities AS $city) { 
     // $wpdb returns an array of objects, so output the object properties 
     echo '<h2>' . $city->Product . '</h2>'; 
     echo '<p>' . $city->Application . '</p>'; 
     echo '<p>' . $city->Sector . '</p>'; 
    } 
} 

get_search_form(); 
+0

이 Cale을 공유해 주셔서 감사합니다. 아무 것도 출력하지 않았거나 아무 것도 출력하지 않습니다. 내 보관 템플릿을 사용하는 '/? s = searchterm'으로 리디렉션됩니다. 나는 아직도 뭔가를 세우지 않았거나 한 걸음도 놓치고있는 것처럼 느낍니다. 테이블 이름이 정확하고 테이블 머리글이 시작 부분의 대문자와 일치하므로 발생하는 것이 확실하지 않습니다. – KKBSE2016

+0

어디서 그 기능을합니까? "사용"코드는 어디에 있습니까? (어떤 파일/템플릿입니까?) –

+0

page-search.php라는 새로운 템플릿 파일을 만들었습니다. 이것은이 코드가 위치하여 실행될 위치입니다. WordPress에서 제품 검색이라는 페이지를 만들어 검색 창을 표시했습니다. – KKBSE2016