Pagination - wie Daten ausgeben?

02/16/2016 18:46 .Barone#1
Hallo Epvp'ler. Ich stehe gerade extrem auf dem Schlauch. Ich würde gerne Pagination von Bootstrap nutzen.
PHP Code:
 <nav>
  <
ul class="pagination">
    <
li>
      <
a href="#" aria-label="Previous">
        <
span aria-hidden="true">&laquo;</span>
      </
a>
    </
li>
    <
li><a href="#">1</a></li>
    <
li><a href="#">2</a></li>
    <
li><a href="#">3</a></li>
    <
li><a href="#">4</a></li>
    <
li><a href="#">5</a></li>
    <
li>
      <
a href="#" aria-label="Next">
        <
span aria-hidden="true">&raquo;</span>
      </
a>
    </
li>
  </
ul>
</
nav
Aber ich kriegs gerade irgendwie nicht in meinen Kopf, wie ich zb pro Seite 9 einträge aus der Datenbank anzeigen lassen kann, hat da jemand ne Idee?
.Barone
02/16/2016 19:05 ~ JWonderpig ~#2
Das geht ganz einfach mit SQL. Einfach immer das Limit 9 setzen und durch die aktuelle Seite den Offset für eine SQL Query errechnen.

Ich habe hier noch eine PHP Klasse, die ich ab und zu nutze.

1. Parameter: 9
2. Parameter: Gesamtzahl aller Rows
3. Parameter: Aktuelle Seite (z.b. durch $_GET übergeben)

Danach einfach die Methode getPageInfo aufrufen und du kannst dir damit die Pagination generieren.

PHP Code:
class Paginator
{

    private 
$totalPages;
    private 
$page;
    private 
$limit;

    
/**
     * @param int $entriesPerPage How many items should be displayed
     * @param int $count How many items are there?
     * @param int $page Current page
     */
    
public function __construct($entriesPerPage$count$page)
    {
        
$this->limit = (int) $entriesPerPage;
        
$this->page = (int) $page;
        
$this->totalPages ceil((int) $count/$this->limit);
    }

    
/**
     * Returns an array with all information
     *
     * @return array
     */
    
public function getPageInfo()
    {
        return array(
            
'totalPages' => $this->totalPages,
            
'currentPage' => $this->page,
            
'limit' => $this->limit,
            
'offset' => $this->calculateOffset(),
        );
    }

    private function 
calculateOffset()
    {
        return (
$this->page 1) * $this->limit;
    }


02/17/2016 18:34 Devsome#3
Ich kann dir das nur empfehlen [Only registered and activated users can see links. Click Here To Register...]
Würde so aussehen [Only registered and activated users can see links. Click Here To Register...]

Code:
<link rel="stylesheet" href="/dataTables.bootstrap.min.css">
Code:
<div class="dataTable_wrapper">
	<table class="table table-striped table-bordered table-hover" id="news-table">
		<thead>
			<tr>
				<th>{{ trans('admin/news/default.show_table_id') }}</th>
				<th>{{ trans('admin/news/default.show_table_title') }}</th>
				<th>{{ trans('admin/news/default.show_table_created_at') }}</th>
				<th>{{ trans('admin/news/default.show_table_published_on') }}</th>
			</tr>
		</thead>
		<tbody>
			@foreach($news as $msg)
			<tr>
				<td>{{ $msg->id }}</td>
				<td>{{ $msg->title }}</td>
				<td>{{ $msg->created_at }}</td>
				<td>{{ $msg->published_at }}</td>
			</tr>
			@endforeach
		</tbody>
	</table>
</div>
Ist mit dem [Only registered and activated users can see links. Click Here To Register...]Template gemacht

//Edit: Script vergessen

Code:
<script src="/js/jquery.dataTables.min.js"></script>
<script src="/js//dataTables.bootstrap.min.js"></script>
<script>
  $(document).ready(function() {
	 $('#news-table').DataTable({
		"columnDefs": [
		   {
			  "targets": [ 2 ],
			  "searchable": false,
		   },
		   {
			  "targets": [ 3 ],
			  "searchable": false
		   },
		   {
			  "targets": [ 4 ],
			  "searchable": false,
			  "orderable": false
		   },
		   {
			  "targets": [ 5 ],
			  "searchable": false,
			  "ordering": false,
			  "orderable": false
		   }
		],
		responsive: true
	 });
  });
</script>