2017-03-02 3 views
0

So zeigen Sie nur gefilterte Datensatzanzahl an (Anzahl der zurückgegebenen Daten aus der Suche, num_rows()) im Codezeichen, jTabelle.So zeigen Sie nur die gefilterte Datensatzanzahl (Anzahl der zurückgegebenen Daten aus der Suche, num_rows()) im Code an, jTabelle

Wie jetzt zeigt alle Zeile Nummer aus der Datenbank, egal wie viele Suchergebnisse ich habe. Ich weiß nicht, wie ich das lösen soll. Alles was ich will, ist nur die gefilterte Datensatzanzahl anzuzeigen. Kann mir jemand helfen?

Hier ist meine view.php:

<div class="JtableCrud"> 
<div class="filtering"> 
    <form class="searchbox_1"> 
     Country Name: <input type="text" class="search_1" name="country_name" id="country_name" /> 
     City Name: <input type="text" class="search_1" name="capital_city" id="capital_city" /> 
     Date from <input class="search_1" type="date" name="from_date" id="from_date"/> 
     Date to <input class="search_1" type="date" value = "<?php echo date('Y-m-d')?>" name="to_date" id="to_date"/> 
     <center><button class="submit_1" type="submit" id="LoadRecordsButton">Search</button> 
      <input class ="submit_1" type="reset" value="Clear fields!"></center> 

    </form> 
</div> 
<div id="countryTable"></div> 
<script type="text/javascript"> 

    $(document).ready(function() { 

     //Prepare jTable 
     $('#countryTable').jtable({ 
     title: 'Country\'s', 
       paging: true, 
       pageSize: 10, 
       sorting: true, 
       defaultSorting: 'country_name ASC', 
       selecting: true, 
       multiselect: true, 
       selectingCheckboxes: true, 
       selectOnRowClick: true, 
       actions: { 
         listAction: 'get_country', 
         createAction: 'create_country', 
         updateAction: 'update_country', 
         deleteAction: 'delete_country' 
       }, 
       fields: { 
        country_id: { 
        key: true, 
         list: false 
        }, 
         country_name: { 
         title: 'Country Name' 
         }, 
         country_code: { 
         title: 'Country Code' 
         }, 
         surface_area: { 
         title: 'Surface Area (m<sup>2</sup>)', 
         width: '13%' 
         }, 
         continent_name: { 
         title: 'Continent Name' 
         }, 
         continent: { 
         title: 'Continent Code' 
         }, 
         population: { 
         title: 'Population' 
         }, 
         capital_city: { 
         title: 'Capital City' 
         }, 
         record_date: { 
         title: 'Record Date', 
           type: 'date', 
           displayFormat: 'mm/dd/yy', 
           create: false, 
           edit: false, 
           sorting: false 
         } 
       }, 
    formCreated: function (event, data) { 
    data.form.find('input[name="country_name"]').addClass('validate[required]'); 
      data.form.find('input[name="country_code"]').addClass('validate[required], custom[onlyLetterSp][maxSize[2]]]'); 
      data.form.find('input[name="surface_area"]').addClass('validate[required,custom[number]]'); 
      data.form.find('input[name="continent_name"]').addClass('validate[required]'); 
      data.form.find('input[name="continent"]').addClass('validate[required], custom[onlyLetterSp][maxSize[2]]'); 
      data.form.find('input[name="population"]').addClass('validate[required], custom[number]]'); 
      data.form.find('input[name="capital_city"]').addClass('validate[required]'); 
      data.form.validationEngine(); 
    }, 
    formSubmitting: function (event, data) { 
    return data.form.validationEngine('validate'); 
    }, 
    formClosed: function (event, data) { 
    data.form.validationEngine('hide'); 
      data.form.validationEngine('detach'); 
    } 
     }); 

       $('#country_name').autocomplete({ 
        source: 'list_country', 
        minLength: 0, 
        scroll: true, 
        autoFocus: true 
       }).focus(function() { 
       $(this).autocomplete("search", "") 
         .autocomplete("widget") 
         .addClass("country_field"); 
      }); 

      $('#capital_city').autocomplete({ 
       source: 'list_city', 
       minLength: 0, 
       scroll: true, 
       autoFocus: true 
      }).focus(function() { 
       $(this).autocomplete("search", "") 
         .autocomplete("widget") 
         .addClass("country_field"); 
      }); 

     $('#LoadRecordsButton').click(function (e) { 
      e.preventDefault(); 
      $('#countryTable').jtable('load', { 
       country_name: $('#country_name').val(), 
       capital_city: $('#capital_city').val(), 
       from_date: $('#from_date').val(), 
       to_date: $('#to_date').val() 
      }); 

     }); 

     $('#LoadRecordsButton').click(); 

    }); 

</script> 
</div> 

Hier mein model.php ist:

<?php 

class Country_model extends CI_Model { 

public function get_country($country_name,$from_date,$to_date,$capital_city)  { 

    $result = $this->db->query("SELECT SQL_CALC_FOUND_ROWS country_id, country_code, country_name, surface_area, 
     continent_name, continent, population, capital_city, record_date 
     FROM country 
     WHERE (deleted = 0) 
     AND (country_name LIKE '%" .$this->db->escape_like_str($country_name) . "%') " 
    . "AND (capital_city LIKE '%". $this->db->escape_like_str($capital_city) . "%') " 
    . "AND (record_date BETWEEN'" . $from_date . "' AND date_add('" . $to_date . "', INTERVAL 1 DAY)) " 
    . "ORDER BY " . filter_input(INPUT_GET, "jtSorting") . " 
     LIMIT " . filter_input(INPUT_GET, "jtStartIndex") . "," . filter_input(INPUT_GET, "jtPageSize") . ";"); 

    $rows = array(); 
    foreach ($result->result() as $row) { 

     $rows[] = $row; 

    } 

    return $rows; 
} 

Hier ist meine controller.php

<?php 

defined('BASEPATH') OR exit('No direct script access allowed'); 

class country_tas extends CI_Controller { 

public function get_country() { 

    if (empty($this->input->post('to_date'))){ 
     $to_date = date('Y-m-d'); 
    }else { 
     $to_date = $this->input->post('to_date');   
    } 

    $country_name = $this->input->post('country_name'); 
    $capital_city = $this->input->post('capital_city'); 
    $from_date = $this->input->post('from_date'); 
    $this->load->model('Country_model'); 
    $rows = $this->Country_model->get_country($country_name,$from_date,$to_date,$capital_city); 
    $num_rows = $this->db->count_all_results('country'); 
    $jTableResult = array(); 
    $jTableResult['Result'] = "OK"; 
    $jTableResult['TotalRecordCount'] = $num_rows; 
    $jTableResult['Records'] = $rows; 
    print json_encode($jTableResult); 

} 
+0

Sie müssen eine weitere Abfrage auslösen, um die Anzahl der Datensätze mit Ihren Filterbedingungen zu erhalten, aber ohne Anfangs- und Endlimit zu übergeben, verwenden Sie diese Zahl als Gesamtzahl der gefilterten Datensätze – Javed

Antwort

0

Okay, ich Ich habe es gelöst!

Um diese Zeilenanzahl zu erhalten, fügen Sie in der SELECT-Anweisung die Option SQL_CALC_FOUND_ROWS ein, und rufen Sie anschließend FOUND_ROWS() auf.

Ich habe zu meiner SELECT-Abfrage "SQL_CALC_FOUND_ROWS" und dann habe ich diese Funktion:

public function record_count(){ 

    $num_rows = $this->db->query('SELECT FOUND_ROWS() count;')->row()->count; 

    return $num_rows;  
} 

und ich nannte es auf meinem Controller:

$jTableResult['TotalRecordCount'] = $this->Country_model->record_count(); 

Das ist mein Problem gelöst. Wenn Sie eine bessere Lösung haben, schreiben Sie es bitte.

0
$num_rows = $this->db->count_all_results('country'); 

//this will fetch all records from country table. 

//replace it with this: 
$num_rows = count($rows); 
+0

Ja, aber es ist nicht gut, weil es nur die Zeilennummer zeigt der aktuellen Seite. und wenn ich mehr ergebnis habe, zeigt es nur an, wie viele records ich auf einer seite habe. nicht die Gesamtanzahl. –

+0

Und deswegen habe ich keine Paging in meinem Tisch. –

+3

sollten Sie einige Details zu Ihrer Antwort hinzufügen. – Deep

Verwandte Themen