0

我有一个使用 mysql & meekrodb 从数据库中提取信息的页面。结果限制为每页 15 个和数据库的字母数字子集 - 在此示例中,1-3 和 AB

我现在想在单击#goleft 或#gort 时添加分页,然后它将从数据库中提取正确的项目。

如何调用 ajax 调用 pagination.php 并将变量 $start 传递给它?不确定所有内容,如果我有错误的地方,请纠正。不确定 $start,15 在 pagination.php 中是否正确

我已经做到了这一点:

主页:

<?php
require_once 'meekrodb.2.2.class.php';
require_once 'dconnect.php';  // database login info
// currently pulling $results from database on initial pg load
$results = DB::query("SELECT substr(theme, 1, 1) as Alphabet, theme, developer, thumb, thumb_lg FROM gallery ORDER BY (CASE Alphabet
  WHEN '1' THEN 1
  WHEN '2' THEN 2
  WHEN '3' THEN 3  
  WHEN 'A' THEN 4
  WHEN 'B' THEN 5
  ELSE 6
  END), theme
  LIMIT 15");
// get count of all relevant items
$tcount = DB::query("SELECT substr(theme, 1, 1) as Alphabet, theme, developer, thumb, thumb_lg FROM gallery ORDER BY (CASE Alphabet
  WHEN '1' THEN 1
  WHEN '2' THEN 2
  WHEN '3' THEN 3  
  WHEN 'A' THEN 4
  WHEN 'B' THEN 5
  ELSE 6
  END), theme");
// get count of all relevant items
$counter = DB::count();
$ipp = 15;  // items per page
$tpages = ceil($counter / $ipp);  // total pages

// write each entry to specific div;
$x = 0;
foreach ($results as $row) {
  $x++;
  if ($x == 1) {
    $t1 = $row['theme'];
    $d1 = $row['developer'];
    $th1 = $row['thumb'];
    $thlg1 = $row['thumb_lg'];
  }
  ... Additional if's through 15th item
}
?>

Basic Html:

<img src="<?php echo($th1); ?>" data-retina="<?php echo($thlg1); ?>" alt="<?php echo($t1); ?>" />
<span><p class="hname"><?php echo($t1); ?></p>
<div class="bull">&bull;</div>
<p class="hdev"><?php echo($d1); ?></p></span>
...
<div id="thumbnav"><div id="goleft"></div><div id="gort"></div></div>

主 pg jQuery:

// Previous button
var curpg = 1;
$('#goleft').mouseup (function() {
  var newpg = curpg - 1;
  if (newpg == 0) {newpg = 1} // reset page if going back to first page
  curpg = newpg;
  var $start = (newpg - 1) * 15 + 1;
  // how to pass $start to pagination.php ??
});

// Next button
$('#gort').mouseup (function() {
  var newpg = curpg + 1;
  var $totpgs = <?php echo $tpages; ?>;  // DOESN'T Echo anything !!!
  console.log('Total Pages: ' + $totpgs);
  if (newpg > $totpgs) {newpg = $totpgs}  // limit page to total pages
  curpg = newpg;
  var $start = (newpg - 1) * 15 + 1;
  console.log('Start: ' + $start);
  // how to pass $start to pagination.php ??
});

分页.php:

<?php
require_once 'meekrodb.2.2.class.php';
require_once 'dconnect.php';  // database login info
// pull from database using specific page items
$results = DB::query("SELECT substr(theme, 1, 1) as Alphabet, theme, developer, thumb, thumb_lg FROM gallery ORDER BY (CASE Alphabet
  WHEN '1' THEN 1
  WHEN '2' THEN 2
  WHEN '3' THEN 3  
  WHEN 'A' THEN 4
  WHEN 'B' THEN 5
  ELSE 6
  END), theme
  LIMIT $start,15");
?>

更新 1:分页.php

<?php
$start = $_POST['start'];  // capture input from AJAX
require_once 'meekrodb.2.2.class.php';
require_once 'dconnect.php';
// pull from database using specific page items
$navresults = DB::query("SELECT substr(theme, 1, 1) as Alphabet, theme, developer, thumb, thumb_lg FROM gallery ORDER BY (CASE Alphabet
  WHEN '1' THEN 1
  WHEN '2' THEN 2
  WHEN '3' THEN 3  
  WHEN 'A' THEN 4
  WHEN 'B' THEN 5
  ELSE 6
  END), theme
  LIMIT $start,15");
$x = 0;
foreach ($navresults as $row) {
  $x++;
  if ($x == 1) {
    $t1 = $row['theme'];
    $d1 = $row['developer'];
    $th1 = $row['thumb'];
    $thlg1 = $row['thumb_lg'];
  }
  ... up to 15th variable set
}

主要的 jQuery:

//Next pagination
$('#gort').mouseup (function() {
  var newpg = curpg + 1;
  var $totpgs = <?php echo $tpages; ?>; // Doesn't show echo !!!
  console.log('Total Pages: ' + $totpgs);
  if (newpg > $totpgs) {newpg = $totpgs}  // limit page to total pages
  curpg = newpg;
  var $start = (newpg - 1) * 15 + 1;
  console.log('Start: ' + $start);
  $.ajax({
    url:'pagination.php',
    type:'POST',
    data:{start:$start},
    dataType:'text'
  });
});
4

1 回答 1

0

You need to make an ajax call to pagination.php

$('#goleft').mouseup (function() {
    var newpg = curpg - 1;
    if (newpg == 0) {newpg = 1} // reset page if going back to first page
    curpg = newpg;
    var $start = (newpg - 1) * 15 + 1;
    $.ajax({
        url:'pagination.php',
        type:'POST',
        data:{start:$start},
        dataType:'json'
    }).done(function(response) {
        tableparser(response);
    });
});

So, you see, now you need a function (which I named tableparser) to parse JSON and fill your HTML table.

At the same time, you need your pagination.php script to answer with valid JSON

<?php
$start = $_POST['start'] // I'm capturing the input sent from Ajax
require_once 'meekrodb.2.2.class.php';
require_once 'dconnect.php';  // database login info
// pull from database using specific page items
$results = DB::query("SELECT substr(theme, 1, 1) as Alphabet, theme, developer, thumb, thumb_lg FROM gallery ORDER BY (CASE Alphabet
  WHEN '1' THEN 1
  WHEN '2' THEN 2
  WHEN '3' THEN 3  
  WHEN 'A' THEN 4
  WHEN 'B' THEN 5
  ELSE 6
  END), theme
  LIMIT $start,15");

echo json_encode($results);

I don't know the particular implementation of your DB Query, but once you have your results in an array, you need to output them json_encoded for jQuery to receive them as an object.

于 2014-06-09T20:51:33.417 回答