php - AJAX pagination w/ MySQL database subset -
i have page uses mysql & meekrodb pull info database. results limited 15 per page & alphanumeric subset of database - in example, 1-3 & a-b
i want add pagination when click either #goleft or #gort, pull correct items database.
how call ajax call pagination.php & pass variable $start it? not sure everything, please correct if have in wrong place. not sure if $start,15 correct in pagination.php
i've gotten far:
main page:
<?php require_once 'meekrodb.2.2.class.php'; require_once 'dconnect.php'; // database login info // pulling $results database on initial pg load $results = db::query("select substr(theme, 1, 1) alphabet, theme, developer, thumb, thumb_lg gallery order (case alphabet when '1' 1 when '2' 2 when '3' 3 when 'a' 4 when 'b' 5 else 6 end), theme limit 15"); // count of relevant items $tcount = db::query("select substr(theme, 1, 1) alphabet, theme, developer, thumb, thumb_lg gallery order (case alphabet when '1' 1 when '2' 2 when '3' 3 when 'a' 4 when 'b' 5 else 6 end), theme"); // count of relevant items $counter = db::count(); $ipp = 15; // items per page $tpages = ceil($counter / $ipp); // total pages // write each entry specific div; $x = 0; foreach ($results $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">•</div> <p class="hdev"><?php echo($d1); ?></p></span> ... <div id="thumbnav"><div id="goleft"></div><div id="gort"></div></div>
main pg jquery:
// previous button var curpg = 1; $('#goleft').mouseup (function() { var newpg = curpg - 1; if (newpg == 0) {newpg = 1} // reset page if going first page curpg = newpg; var $start = (newpg - 1) * 15 + 1; // how pass $start pagination.php ?? }); // next button $('#gort').mouseup (function() { var newpg = curpg + 1; var $totpgs = <?php echo $tpages; ?>; // doesn't echo !!! console.log('total pages: ' + $totpgs); if (newpg > $totpgs) {newpg = $totpgs} // limit page total pages curpg = newpg; var $start = (newpg - 1) * 15 + 1; console.log('start: ' + $start); // how pass $start pagination.php ?? });
pagination.php:
<?php require_once 'meekrodb.2.2.class.php'; require_once 'dconnect.php'; // database login info // pull database using specific page items $results = db::query("select substr(theme, 1, 1) alphabet, theme, developer, thumb, thumb_lg gallery order (case alphabet when '1' 1 when '2' 2 when '3' 3 when 'a' 4 when 'b' 5 else 6 end), theme limit $start,15"); ?>
update 1: pagination.php
<?php $start = $_post['start']; // capture input ajax require_once 'meekrodb.2.2.class.php'; require_once 'dconnect.php'; // pull database using specific page items $navresults = db::query("select substr(theme, 1, 1) alphabet, theme, developer, thumb, thumb_lg gallery order (case alphabet when '1' 1 when '2' 2 when '3' 3 when 'a' 4 when 'b' 5 else 6 end), theme limit $start,15"); $x = 0; foreach ($navresults $row) { $x++; if ($x == 1) { $t1 = $row['theme']; $d1 = $row['developer']; $th1 = $row['thumb']; $thlg1 = $row['thumb_lg']; } ... 15th variable set }
main 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 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' }); });
you need make ajax call pagination.php
$('#goleft').mouseup (function() { var newpg = curpg - 1; if (newpg == 0) {newpg = 1} // reset page if going 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, see, need function (which named tableparser
) parse json , fill html table.
at same time, need pagination.php script answer valid json
<?php $start = $_post['start'] // i'm capturing input sent ajax require_once 'meekrodb.2.2.class.php'; require_once 'dconnect.php'; // database login info // pull database using specific page items $results = db::query("select substr(theme, 1, 1) alphabet, theme, developer, thumb, thumb_lg gallery order (case alphabet when '1' 1 when '2' 2 when '3' 3 when 'a' 4 when 'b' 5 else 6 end), theme limit $start,15"); echo json_encode($results);
i don't know particular implementation of db query, once have results in array, need output them json_encoded jquery receive them object.
Comments
Post a Comment