Visualizzazione risultati 1 fino 3 di 3

Discussione: php mysql search query limit results

  1. #1
    Guest

    Exclamation php mysql search query limit results

    Salve a tutti, ho un problema con il seguente file php
    quando digito sulla ricerca una lettera, vorrei che escono soltanto fuori i 5 risultati e non 7, come posso fare?

    Codice PHP:
    <?php
    /************************************************
    The Search PHP File
    ************************************************/


    /************************************************
    MySQL Connect
    ************************************************/

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

    // 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 .= '<div class="show" align="left">
    <img src="author.PNG" style="width:50px; height:50px; float:left; margin-right:6px;" />nameString <br/><span>functionString</span></div> '
    ;
    $html .= '';
    $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://www.itworldlive.it/'.urlencode($result['function']).'';

    // 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);
    }
    }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);
    }
    }


    /*
    // Build Function List (Insert All Functions Into DB - From PHP)

    // Compile Functions Array
    $functions = get_defined_functions();
    $functions = $functions['internal'];


    foreach ($functions as $function) {
    $function_name = str_replace("_", " ", $function);
    $function_name = ucwords($function_name);

    $query = '';
    $query = 'INSERT INTO search SET id = "", function = "'.$function.'", name = "'.$function_name.'"';

    $tutorial_db->query($query);
    }
    */
    ?>
    Ultima modifica di AmOrEaMiCiZa : 13-03-2015 alle ore 21.20.26

  2. #2
    Guest

    Predefinito

    Non ho testato la soluzione ma dovrebbe essere alla linea 51 quando fai la query, imposti un limite a 5.

    quindi da
    Codice PHP:
    $query = 'SELECT * FROM search WHERE function LIKE "%'.$search_string.'%" OR name LIKE "%'.$search_string.'%"';
    a
    Codice PHP:
    $query = 'SELECT * FROM search WHERE function LIKE "%'.$search_string.'%" OR name LIKE "%'.$search_string.'%" LIMIT 0, 5';
    Fammi sapere se funge, bye!
    Ultima modifica di pxy : 16-03-2015 alle ore 13.03.40

  3. #3
    Guest

    Predefinito

    No, non funziona ancora :(

Regole di scrittura

  • Non puoi creare nuove discussioni
  • Non puoi rispondere ai messaggi
  • Non puoi inserire allegati.
  • Non puoi modificare i tuoi messaggi
  •