여기에 몇 가지가 있습니다. 우리가 해결할 수 있는지 알아 봅시다.
여기에는 검색을 수행해야하는 수정 된 기능이 있으며 여기에는 의견을 통해 설명이 나와 있습니다.
참고 :
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();
이 코드에서 수행하고자하는 것에 대한 설명이 가장 도움이 될 것입니다. 그것이 그대로, 우리는 당신이 무엇을 성취하려고하는지 전혀 모른다. –
안녕하세요, 저는 wordpress에 sc_products라는 데이터베이스를 설치하고 여기에 제품 데이터가 포함 된 3 개의 열이있는 사용자 정의 테이블이 있습니다. 이 표의 모든 데이터를 검색하는 검색 양식을 만들어야 양식에서 일치하는 결과 만 반환됩니다. 아무 것도 반환되지 않으면 연락처 양식에 대한 링크가있는 문자열이 반환됩니다. – KKBSE2016
좋습니다, 그게 더 유용합니다. 테이블에있는 세 개의 열은 무엇입니까? (그렇지 않으면, 우리는 어떻게 검색 질의를 작성 하는지를 어떻게 알 수 있습니까?) –