Pagination number elements do not work - jQuery - php

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()

Related

jQuery .load issue when switching content

Tristann.tk
On this site click About me and then click back to Home, and then try switching the pages at the bottom, It doesn't change. Why?
jQuery code:
$(document).ready(function(){
$(".page_links .page li").click(function(){
$(".page_links .page li").css({'background-color' : ''});
$(this).css({'background-color' : '#A5CDFA'});
$(".posts").load("/includes/data.php?data=pages&page=" + this.className);
});
$(".navlist #about_me").click(function(){
$(".navlist #home").css({'text-decoration' : 'none'});
$(".navlist #about_me").css({'text-decoration' : 'underline'});
$(".vsebina").load("/includes/data.php?data=about_me");
});
$(".navlist #home").click(function(){
$(".navlist #about_me").css({'text-decoration' : 'none'});
$(".navlist #home").css({'text-decoration' : 'underline'});
$(".vsebina").load("/includes/data.php?data=home");
});
$(".navlist #home").css({'text-decoration' : 'underline'});
$(".1").css({'background-color' : '#A5CDFA'});
});
The divs are like that:
<div class="zunanji">
<div class="glava">
<div class="meni">
<ul class="navlist">
<li><a id="home" href="javascript:void(0)">Home</a></li>
<li><a id="about_me" href="javascript:void(0)">About Me</a></li>
</ul>
</div>
</div>
<div class="container">
<div class="vsebina">
<? get_posts();
get_pages();
?>
</div>
<div class="stranska">
<h2>Archive</h2>
</div>
</div>
</div>
The 2 PHP functions return the 5 posts at the start and the page numbers at the bottom.
You'll need to rebind those events after every load because the page_links are being replaced. An easier and cleaner way to do it is to use event delegation. The best way is to use $.fn.on, which performs better and is less buggy than $.fn.live. Also, since the load will replace the page_links you have to do the background-color stuff in a callback:
$(".vsebina").on('click', '.page_links .page li', function(){
var $page = $(this); // save the page link that was clicked
var path = "/includes/data.php?data=pages&page=" + this.className;
$(".posts").load(path, function(){
$(".page_links .page li").css({'background-color' : ''});
$page.css({'background-color' : '#A5CDFA'});
});
});
This binds a click event to the .vsebina container and checks to see if the clicked item was a page link. If it was, it will fire your handler. This way, even if the content is replaced, your handler will run.
Read more about $.fn.on vs $.fn.live vs $.fn.delegate vs $.fn.bind here
Your click events are getting disconnected when you load new content. Change your page nav code to use the live() method:
$(".page_links .page li").live('click',function(){
$(".page_links .page li").css({'background-color' : ''});
$(this).css({'background-color' : '#A5CDFA'});
$(".posts").load("/includes/data.php?data=pages&page=" + this.className);
});

How can I show array and button by click?

I have a simple table with buttons: <button class='btn btn-info' href='#'>More</button>, and I need that when user click by "More" button then array ($records, for example) and "Less" button will be shown under "More" button. For example, $records[0]="1", $records[1]="2". Please, I don't know JavaScript and JQuery very well, but I need to do it using Ajax. So, I very need your help. Thank you.
UPDATE:
$("button.btn-info").click(function() {
alert('click');
});
It's code for clicking by "More button", but I don't know how to write $records array under this button with "Less" button (which will hide the array by click) at the end.
PHP (sample only):
<?php
$records = array(1,2,3,4,5,6,7);
?>
HTML:
<html>
<body>
<div id="more">
<button>More</button>
</div>
<div id="less" style="display: none;">
<div id="codes" style="border-top: 1px dotted #d0d0d0;">
<?php
for($i=0; $i<count($records); $i++) {
printf("$records[%d] = %d<br />",$i,$records[$i]);
}
?>
</div>
</div>
</body>
</html>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
JS:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var i = 1;
$('div#more button').click(function() {
$('div#less').toggle();
if (i) { $(this).html('Less'); i=0; } else { $(this).html('More'); i=1; }
});
});​
</script>
P.S. Do not forget to include the jQuery plugin or this won't work.
EDIT: There it is jquery.min.js
Demo fiddle: http://jsfiddle.net/fe9wv/
You can use Jquery for this, below is a rough example
Assumptions:
ALL the buttons have an ID(required for fetching data through ajax)
The table cell in which you have the button ,also has a with display style as hidden and class as 'dataspan'
$('.btn-info').live('click',function(){
var id = $(this).attr('id');
$.ajax({
type : "POST",
url: "YOUR TARGET URL",
dataType : "HTML",//you can also use JSON
data : 'id=' + id,
success: function(data)
{
$(this).siblings('.dataspan').html(data);
$(this).siblings('.dataspan').append('<button class="hidedata" value="HIDE"/>');
$(this).siblings('.dataspan').show();
}
});
});
//FOR hiding the data
$('.hidedata').live('click' , function(){
$(this).siblings('.dataspan').hide();
});
Please note that as per the latest JQuery release the .live function has been deprecated, if you are using the latest version I will strongly recommend you to switch to delegate function

jquery tab cookie ...?

Hy ,
I have the following code for mai tabs ...
$(document).ready(function() {
$("#continut_right div.tab").hide();
$("#continut_right div.tab:first-child").show();
$("#navigatie_left ul li").click(function() {
var id_tab = $(this).attr("id");
$("#continut_right div.tab").hide();
$("#continut_right").children("#" + id_tab).show();
});
and the markup is something like this :
<div id="continut_right">
<!-- tab 1 -->
<div class="tab" id="id1">
content ... bla bla bla
</div>
</div>
and the menu that trigger tabs to change :
<div id="navigatie_left">
<a style="text-decoration: none; color:#000; font-family:Arial; font-size:15px; font-weight:bold;" href="<?php echo $_SERVER['PHP_SELF']; ?>">[ REFRESH ]</a>
<br /><br />
<ul>
<li id="id1">Categorii principale</li>
<li id="id2">Categorii secundare</li>
<li id="id2">Texte categorii secundare</li>
</ul>
</div>
How can i implement a code ... to remember what tab was opened because when i do a form submit to php_self to keep that tab open to see changes ... ??
Thanks a lot
You can use window.location.hash to store the current tab id. You have to write code that reads the window.location.hash property for the tab id on $(document).ready() and automatically switch to that tab.
An example:
$(document).ready(function(){
var id_tab = window.location.hash;
$("#continut_right div.tab").hide();
if(id_tab){
$("#continut_right").children(id_tab).show();
}else{
$("#continut_right div.tab:first-child").show();
}
$("#navigatie_left ul li").click(function() {
var id_tab = $(this).attr("id");
$("#continut_right div.tab").hide();
$("#continut_right").children("#" + id_tab).show();
window.location.hash = id_tab;
});
});
You could look into .appendTo()'s JSON Cookie solution. it would offer an easy method of storing and retrieving the last-active tab.
The usage is documented as follows:
$.cookie( string key, mixed value [, hash options ] )
So you could store the index of the active tab:
$.cookie( 'activeTab', '3' );
And then retrieve it on page load with:
$.cookie( 'activeTab' ); //returns the set value
So let's look at how you could implement it:
$("#navigatie_left ul li").click(function() {
var id_tab = $(this).attr("id");
$.cookie( 'activeTab', id_tab ); // storing the new active tab
$("#continut_right div.tab").hide();
$("#continut_right").children("#" + id_tab).show();
});
Now for us to automatically show the tab on page load:
$(document).ready(function() {
$("#continut_right div.tab").hide();
$("#continut_right div.tab:first-child").show();
// Which tab should we show on document.ready?
$("#continut_right").children("#" + $.cookie('activeTab')).show();
});

how to show repeating divs on mouse over with jquery?

for example ive got a div like:
<div class="y1">
<img src="i/o.png" />
</div>
and
<!-- pre load -->
<div class="p1" style="display:none">
<h5 class="ob">Title</h5>
<img class="ob" src="i/ob.png" />
<small class="ob">Description</small>
PLAY
</div>
and this jquery
<script type="text/javascript">
$("div.y1").hover(
function () {
$('div.p1').slideDown('slow', function() {
});
}
);
</script>
my question is how can i repeat it for 12 times. i mean when i hover on y1, show p1, y2 => p2, y3 => p3 ... y12 => p12. i hope you guys understand me. thank you so much!!!!
Should look like:
$(function(){
$('div[class^=y]').hover(function(){
var self = $(this),
number = this.className.slice(1);
$('div.p' + number).slideDown('slow', function() {
});
}, function() {
var self = $(this),
number = this.className.slice(1);
$('div.p' + number).slideUp('slow', function() {
});
});
});
Example: http://www.jsfiddle.net/Q5Ug2/
I'm going to assume that your y# divs are inside a div with the id container. Secondly, I'm going to assume that none of your y# divs have any other classes applied to them.
$('#container').delegate('div[class^=y]','mouseenter', function(){
$('div.p' + this.className.slice(1)).slideDown('slow');
}).delegate('div[class^=y]', 'mouseleave', function(){
$('div.p' + this.className.slice(1)).slideUp('slow');
});
This uses delegate to avoid the cost of binding to multiple DOM elements.
Edit To hide the p# div on hovering on another element, you can use this code.
$('#container').delegate('div[class^=y]','mouseenter', function(){
$('div.p' + this.className.slice(1)).slideDown('slow').siblings().slideUp();
});
Do things a bit differently..
Use ID's for the p# and y# series indicators.
On your DIV tags for the p# series, add a title of the y# series.. so <div id="p1" title="y1">
On your DIV tags for the y# series, add a class.. <div id="y1" class="hoverMe">
$('div.hoverMe').hover( function() {
$('[title=' + $(this).attr('id') + ']').slideDown('slow');
});

Making script more dynamic

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"'); ?>

Categories