Making script more dynamic - php

I am trying to make my pagination script a bit more dynamic and am needing some help with the calculating of the page numbers part.
I have this script below which calculates the number of pages using php, however I would like to add other buttons that when clicked will filter and sort out data which means the pagination numbers need to be calculated again.
The script I have does not allow for this to happen, so I am wondering if there is a more efficient/dynamic way of calculating the pagination numbers then the way I am doing it now.
This is the main page (pagination.php)
<?php
include('config.php');
$per_page = 3;
//Calculating no of pages
$sql = "select * from explore";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
$pages = ceil($count/$per_page)
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1`` /jquery.min.js"></script>
<script type="text/javascript" src="jquery_pagination.js"></script>
<div id="loading" ></div>
<div id="content" data-page="1"></div>
<ul id="pagination">
<?php
//Pagination Numbers
for($i=1; $i<=$pages; $i++)
{
echo '<li class="page_numbers" id="'.$i.'">'.$i.'</li>';
}
?>
</ul>
<br />
<br />
And here is the JS script (jquery.pagination.js)
$(document).ready(function(){
//Display Loading Image
function Display_Load()
{
$("#loading").fadeIn(900,0);
$("#loading").html("<img src='bigLoader.gif' />");
}
//Hide Loading Image
function Hide_Load()
{
$("#loading").fadeOut('slow');
};
//Default Starting Page Results
$("#pagination li:first").css({'color' : '#FF0084'}).css({'border' : 'none'});
Display_Load();
$("#content").load("pagination_data.php?page=1", Hide_Load());
//Pagination Click
$("#pagination li").click(function(){
Display_Load();
//CSS Styles
$("#pagination li")
.css({'border' : 'solid #dddddd 1px'})
.css({'color' : '#0063DC'});
$(this)
.css({'color' : '#FF0084'})
.css({'border' : 'none'});
//Loading Data
var pageNum = this.id;
$("#content").load("pagination_data.php?page=" + pageNum, function(){
Hide_Load();
$(this).attr('data-page', pageNum);
});
});
});
I tried to put the calculation part of the script in 'pagination_data.php' file so when I clicked a button or link to go to that page it would generate everything, but it did not work.
So, any help on figuring out a good way of doing this would be great. Thanks.

Your page generation code is fine, it's the fixed query that it's using that needs to be changed. I would suggest changing your pagination code into a function that takes an $sql parameter so that you could pass in the correct query adjusted by any filters:
function generate_pagination($sql) {
include_once('config.php');
$per_page = 3;
//Calculating no of pages
$result = mysql_query($sql);
$count = mysql_num_rows($result);
$pages = ceil($count/$per_page)
//Pagination Numbers
for($i=1; $i<=$pages; $i++)
{
echo '<li class="page_numbers" id="'.$i.'">'.$i.'</li>';
}
}
and then call it like this:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1`` /jquery.min.js"></script>
<script type="text/javascript" src="jquery_pagination.js"></script>
<div id="loading" ></div>
<div id="content" data-page="1"></div>
<ul id="pagination">
<?php generate_pagination('SELECT * FROM explore'); ?>
</ul>
<br />
<br />
or with a filter:
<?php generate_pagination('SELECT * FROM explore WHERE key="value"'); ?>

Related

php jquery-load more data inside a loop

I have the posts inside a while and post replays inside another while
I want to add a load more link or button for replays.
I have this codes but just first load more works:
<style> #myList li{ display:none;list-style: none;}</style>
<?php $conn=mysqli_connect("localhost","root","","dbace");
$psql="SELECT * FROM tbl_users_posts limit 20";
$qry=mysqli_query($conn,$psql);?>
<ul id="myList">
<?php while($fql=mysqli_fetch_assoc($qry)){
$pst=$fql['post'];
$pid=$fql['id'];
echo $pid." ".$pst;?><br>
<?php $rpql="SELECT * FROM re_pst WHERE subid='$pid'";
$qrep=mysqli_query($conn,$rpql);
while($qrw=mysqli_fetch_assoc($qrep)){
$remsg=$qrw['remsg'];?>
<li>
<?php echo $remsg;?><br>
<?php }?><br><br>
</li>
<div id="loadMore">load more</div>
<?php }?>
</ul>
and the jquery:
<script type="text/javascript" src="_js/jqmin.js"></script>
<script type="text/javascript">
$(document).ready(function () {
size_li = $("#myList li").size();
x=3;
$('#myList li:lt('+x+')').show();
$('#loadMore').click(function () {
x= (x+2 <= size_li) ? x+2 : size_li;
$('#myList li:lt('+x+')').show();
});
});
</script>
How can I fix that?
Thanks
Can I do it somehow by give number to posts and jquery command? some thing like this:
for ($i = 0; $i < X; $i++) {
Your problem is to you using id attribute for event. The value of id attribute must be unique. Instead of you can use class attribute.
The following code may be solution:
$('.loadMore').on('click', function(){
$(this).parent('li').show();
})
Can you try using "loadMore" button by class attribute with above code?

php pagination page without refreshing the page and losing post data

I am trying to create pagination code without refreshing the page using jquery, php and mysql. I found away to do it but I can't retrieve my post data because they are sent from the previous page the main.php page and the pagination code is in another page called pagination.php. I believe the post data wont be lost if I Integrate the two pages into one but when I tried to make it it didn't work.
main page
include('db.php');
$per_page = 1;
$select_table = "select * from pagination";
$variable = mysql_query($select_table);
$count = mysql_num_rows($variable);
$pages = ceil($count/$per_page)
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Qjuery pagination with loading effect using PHP and MySql</title>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
function Display_Load()
{
$("#load").fadeIn(1000,0);
$("#load").html("<img src='load.gif' />");
}
function Hide_Load()
{
$("#load").fadeOut('slow');
};
$("#paginate li:first").css({'color' : '#FF0084'}).css({'border' : 'none'});
Display_Load();
$("#content").load("pagination.php?page=1", Hide_Load());
$("#paginate li").click(function(){
Display_Load();
$("#paginate li")
.css({'border' : 'solid #193d81 1px'})
.css({'color' : '#0063DC'});
$(this)
.css({'color' : '#FF0084'})
.css({'border' : 'none'});
var pageNum = this.id;
$("#content").load("pagination.php?page=" + pageNum, Hide_Load());
});});
</script>
</head>
<body>
<div id="content" ></div>
<div class="link" align="center">
<ul id="paginate">
<?php
//Show page links
for($i=1; $i<=$pages; $i++)
{
echo '<li id="'.$i.'">'.$i.'</li>';
}
?>
</ul>
</div>
<div style="clear:both"> </div>
<div id="load" align="center" ></div>
</body>
</html>
pagination page
<?php
include('db.php');
$per_page = 1;
if($_GET)
{
$page=$_GET['page'];
}
$start = ($page-1)*$per_page;
$select_table = "select * from pagination where date = ' =$date' and number ='$number' $start,$per_page";
$variable = mysql_query($select_table);
?>
<table width="800px">
<?php
$i=1;
while($row = mysql_fetch_array($variable))
{
$name=$row['name'];
$design=$row['designation'];
$place=$row['place'];
?>
<tr>
<td style="color:#999;"><?php echo $i++; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $design; ?></td>
<td><?php echo $place; ?></td></tr>
<?php
}
?>
</table>
I get an error from pagination.php page in this line:
$select_table = "select * from pagination where date = ' =$date' and number ='$number' $start,$per_page";
that $date and $number variables are undefined variables
I solved the problem, the solution was to remove the pagination page.php file and to add a simple jquery pagination plugin inside the main page, which mean its not necessary to define the post data again.
I'd highly recommend you move from mysql_* to mysqli because you are vulnerable.
$select_table = "select * from `pagination` where `date` = '".$date."' and `number` ='".$number."' $start,$per_page";
I see an extra = in there at the very least, it is also not concatenated properly. I'm not sure what you are trying to do here $start,$per_page";. Also make sure you are escaping your fields and tables with back ticks.

jquery drag and drop with database

I'm working on drag and drop and store id number in database, i just finished that and is works great in all browser but the problem is that is NOT WORKING IN IE 8 or 9.
The problem is that in IE is not allow me to drag or move around that the problem that i can't figure out how to solve this, and rest of browser are works fine.
here is jquery code
<script type="text/javascript">
$(document).ready(function(){
function slideout(){
setTimeout(function(){
$("#response").slideUp("slow", function () {
});
}, 2000);}
$("#response").hide();
$(function() {
$("#list ul").sortable({ opacity: 0.8, cursor: 'move', update: function() {
var order = $(this).sortable("serialize") + '&update=update';
$.post("updateList.php", order, function(theResponse){
$("#response").html(theResponse);
$("#response").slideDown('slow');
slideout();
});
}
});
});
});
</script>
and the body code is
<div id="response"> </div>
<ul>
<?php
include("connect.php");
$query = "SELECT id, text FROM dragdrop ORDER BY listorder ASC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$id = stripslashes($row['id']);
$text = stripslashes($row['text']);
?>
<li id="arrayorder_<?php echo $id ?>"><?php echo $id?> <?php echo $text; ?>
<div class="clear"></div>
</li>
<?php } ?>
</ul>
</div>
</div>
can any one help me how to solve to make works drag and drop for IE, if is there is other sample that might support all browser!
AM
According to #jheilgeist here, adding a position:relative on the div, will sort it out, even if it acts a little weird.
It looks like a jQuery UI bug in those browsers.
Check more info here: http://bugs.jqueryui.com/ticket/7546

How to dynamically display MySQL data using jQuery and PHP?

I have a PHP page that lists a bunch of words that it grabs from a MySQL database table. It displays the words in different sizes based on a count in the table:
<?php
$selectStr = "select * from test";
if ($results = MySQL($dbName, $selectStr))
{
$rowCount = MySQL_NUMROWS($results);
}
$i = 0;
while ($i < $rowCount)
{
echo '<div style="float: left; font-size:' . (MySQL_RESULT($results,$i,'count') * 5) . 'px;">' . MySQL_RESULT($results,$i,'word') . '</div>';
$i++;
}
?>
The trick is that I want the content to display dynamically. So if a user is sitting on the page, and one of the word counts goes up, I want the word to change size without the user refreshing the page.
I am a novice with jQuery. I have used it a bit before, but only using examples. Can someone steer me in a good direction to have my page dynamically change the content without refreshing?
You can auto refresh your page body like this ... give body id='body'
<html>
<head>
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#body').load('wordscount.php').fadeIn("slow");
}, 10000); // refresh every 10000 milliseconds
</script>
</head>
<body>
<div id='content'></div>
</body>
Dont forget to include jquery inside your head tag
It calls the file rowfunctie.php every 30000ms and fills the topbar diff with with the result of the getRows function.
<div id="center-rows">
<div id="links">Nu </div>
<div id="rows">
<div id="topbar"></div>
</div>
<div id="rechts"> aantal rijen</div>
</div>
<script type="text/javascript">
function doRequest() {
jQuery("#topbar").fadeOut('slow', function() {
jQuery.ajax({
type: "GET",
url: "rowfunctie.php",
cache: false,
success: function(html){
jQuery("#topbar").html(html);
jQuery("#topbar").fadeIn('slow',function() {
setTimeout('doRequest()',30000);
});
}
});
});
}
doRequest();
</script>
rowfunctie.php should look like this beneath:
<?php
$selectStr = "select * from test";
if ($results = MySQL($dbName, $selectStr))
{
$rowCount = MySQL_NUMROWS($results);
}
$i = 0;
while ($i < $rowCount)
{
echo '<div style="float: left; font-size:' . (MySQL_RESULT($results,$i,'count') * 5) . 'px;">' . MySQL_RESULT($results,$i,'word') . '</div>';
$i++;
}
?>

Pagination number elements do not work - jQuery

I am trying to get my pagination links to work properly. It seems when I click any of the pagination number links to go the next page, the new content does not load. literally nothing happens and when looking at the console in Firebug, nothing is sent or loaded.
I have on the main page 3 links to filter the content and display it. When any of these links are clicked the results are loaded and displayed along with the associated pagination numbers for that specific content.
Here is the main page so you can see what the structure looks like for the jquery:
<?php
include_once('generate_pagination.php');
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery_pagination.js"></script>
<div id="loading" ></div>
<div id="content" data-page="1"></div>
<ul id="pagination">
<?php generate_pagination($sql) ?>
</ul>
<br />
<br />
Marketing
Automotive
Sports
The jquery below is pretty simple and I don't think I need to explain what it does. I believe the problem may be with the $("#pagination li").click(function(){ since the li elements are the numbers that do not work when clicked. Even when I try to fadeOut or hide the content on click nothing happens. I'm pretty new to the jquery structure so I do not fully understand where the real problem is occurring, this is just from my observation.
The jquery file looks like this:
$(document).ready(function(){
//Display Loading Image
function Display_Load()
{
$("#loading").fadeIn(900,0);
$("#loading").html("<img src='bigLoader.gif' />");
}
//Hide Loading Image
function Hide_Load()
{
$("#loading").fadeOut('slow');
};
//Default Starting Page Results
$("#pagination li:first").css({'color' : '#FF0084'}).css({'border' : 'none'});
Display_Load();
$("#content").load("pagination_data.php?page=1", Hide_Load());
//Pagination Click
$("#pagination li").click(function(){
Display_Load();
//CSS Styles
$("#pagination li")
.css({'border' : 'solid #dddddd 1px'})
.css({'color' : '#0063DC'});
$(this)
.css({'color' : '#FF0084'})
.css({'border' : 'none'});
//Loading Data
var pageNum = this.id;
$("#content").load("pagination_data.php?page=" + pageNum, function(){
$(this).attr('data-page', pageNum);
Hide_Load();
});
});
// Editing below.
// Sort content Marketing
$("a.category").click(function() {
Display_Load();
var this_id = $(this).attr('id');
$.get("pagination.php", { category: this.id },
function(data){
//Load your results into the page
var pageNum = $('#content').attr('data-page');
$("#pagination").load('generate_pagination.php?category=' + pageNum +'&ids='+ this_id );
$("#content").load("filter_marketing.php?page=" + pageNum +'&id='+ this_id, Hide_Load());
});
});
});
If anyone could help me on this that would be great, Thanks.
EDIT:
Here are the innards of <ul id="pagination">:
<?php
function generate_pagination($sql) {
include_once('config.php');
$per_page = 3;
//Calculating no of pages
$result = mysql_query($sql);
$count = mysql_fetch_row($result);
$pages = ceil($count[0]/$per_page);
//Pagination Numbers
for($i=1; $i<=$pages; $i++)
{
echo '<li class="page_numbers" id="'.$i.'">'.$i.'</li>';
}
}
$ids=$_GET['ids'];
generate_pagination("SELECT COUNT(*) FROM explore WHERE category='$ids'");
?>
Try moving the code that is below // Sort content Marketing so it's above $("#pagination li").click()

Categories