php - 在php搜索框中显示url

标签 php html web

我试图让这个搜索页面在搜索栏下方显示 URL,而不是“产品”的实际名称。这是我走了多远,我目前被困住了。我还创建了一个主要的 html 页面,它只是搜索框的服务器。先谢谢您的帮助。

    ************************************************/

// Credentials
$dbhost = "localhost";
$dbname = "tutorial";
$dbuser = "root";
$dbpass = "root";

//  Connection
global $tutorial_db;
$tutorial_db = new mysqli();
$tutorial_db->connect($dbhost, $dbuser, $dbpass, $dbname);
$tutorial_db->set_charset("utf8");

//  Check Connection
if ($tutorial_db->connect_errno) {
printf("Connect failed: %s\n", $tutorial_db->connect_error);
exit();
}

/************************************************
 Search Functionality
************************************************/

// Define Output HTML Formating
 $html = '';
 $html .= '<li class="result">';
 $html .= '<a target="_blank" href="urlString">';
 $html .= '<h3>nameString</h3>';
 $html .= '<h4>functionString</h4>';
 $html .= '</a>';
 $html .= '</li>';

 // Get Search
 $search_string = preg_replace("/[^A-Za-z0-9@]/", " ", $_POST['query']);
 $search_string = $tutorial_db->real_escape_string($search_string);

 // Check Length More Than One Character
if (strlen($search_string) >= 1 && $search_string !== ' ') {
  // Build Query
 $query = 'SELECT * FROM search WHERE function LIKE "%'.$search_string.'%" OR name LIKE "%'.$search_string.'%"';

// Do Search
$result = $tutorial_db->query($query);
while($results = $result->fetch_array()) {
    $result_array[] = $results;
}

// Check If We Have Results
if (isset($result_array)) {
    foreach ($result_array as $result) {

        // Format Output Strings And Hightlight Matches
        $display_function = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['function']);
        $display_name = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['name']);
        $display_url = 'http://php.net/manual-lookup.php?pattern='.urlencode($result['function']).'&lang=en';

        // Insert Name
        $output = str_replace('nameString', $display_name, $html);

        // Insert Function
        $output = str_replace('functionString', $display_function, $output);

        // Insert URL
        $output = str_replace('urlString', $display_url, $output);

        // Output
        echo($output, $display url);
    }
}else{

    // Format No Results Output
    $output = str_replace('urlString', 'javascript:void(0);', $html);
    $output = str_replace('nameString', '<b>No Results Found.</b>', $output);
    $output = str_replace('functionString', 'Sorry :(', $output);

    // Output
    echo($output);
}
}

我已经尝试在“if 语句”中显示它,但它仍然无法正确显示:

   if (isset($result_array)) {
    foreach ($result_array as $result) {

        // Format Output Strings And Hightlight Matches
        $display_function = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['function']);
        $display_name = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['name']);
        $display_url = 'http://php.net/manual-lookup.php?pattern='.urlencode($result['function']).'&lang=en';

        // Insert Name
        $output = str_replace('nameString', $display_name, $html);

        // Insert Function
        $output = str_replace('functionString', $display_function, $output);

        // Insert URL
        $output = str_replace('urlString', $display_url, $output);

        // Output
        echo($output, $display url);
    }

这是 url 应该出现的图像以及搜索页面的样子:

enter image description here

最佳答案

尝试替换这个:

$html = '';
 $html .= '<li class="result">';
 $html .= '<a target="_blank" href="urlString">';
 $html .= '<h3>nameString</h3>';
 $html .= '<h4>functionString</h4>';
 $html .= '</a>urlString2';
 $html .= '</li>';

删除:

echo $output, $display_url;

并在上面添加:

$output = str_replace('urlString2', $display_url, $output);
echo $output;

关于php - 在php搜索框中显示url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33619312/

相关文章:

php - Web 表单中的外键

mobile - 如何运行网站的移动版?

php - 在取消设置 mysql 对象之前释放结果并关闭连接,或者只是取消设置会破坏对象 n 释放资源?

html - 为什么 select 的高度比 input[type=text] 稍大?

javascript document.write 和 insideHTML 输出不同的值

html - 为某些 NSXMLElements 保存 NSXMLDocument 转义 HTML

c++ - 如何将 libcurl 导入 C++ 项目?

Nginx 的根位置不同

php - 更新记录时来自 PHP/MySQL 的平面 HTML 文件

php - 在 codeigniter 中区分 session 超时