TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-06-2012, 02:39 PM   #1 (permalink)
Ivan Ivković, Croatia
 
ivanivkovich's Avatar
 
Join Date: May 2012
Posts: 7
Thanks: 0
ivanivkovich is on a distinguished road
Default Getting doubles in my AJAX query

The template file:

Code:
<?php include(WIDGETS . 'html_head.php'); ?>
	<link rel="stylesheet" type="text/css" href="/src/styles/masonry.css"/>
	</script>
	</head>
	<body id="<?php echo $this -> registry -> router -> page ?>">
		<div id="background"></div>
		<?php include(WIDGETS . 'navigation.php'); ?>
			<div id="container" style="position: absolute; top: 80px;">
				<?php 
					$result = $this -> registry -> db -> query('SELECT * FROM sc_pics ORDER BY pic_id DESC LIMIT 20');
					$world = new WorldDatabase($this -> registry -> db);
					$cat = new Categories($this -> registry -> db);
					include('includes/fbconfig.php');
					while($fetch = $result -> fetch_array()){ ?>
						<div class="element item" id="pic_<?php echo $fetch['pic_id'] ?>">
					<?php 
						echo '<img src="/thumb.php?pic=content/pics/' . $fetch['user_id'] . $fetch['src'] . '&h=200&w=210"/>';
						?>
						</div>
					<?php	
					} 
					 ?>
			</div>
			<input type="hidden" id="last_id"/>
			<script>
				$('#last_id').attr('value', $('.item:last').attr('id'));
			</script>
		<script type="text/javascript" src="/src/scripts/masonry.js"></script>
		<script  type="text/javascript">
		
		var container = document.getElementById('container');
		var wall = new Masonry( container, {isFitWidth: true });
			
		var boxMaker = {};
		
		boxMaker.makeBoxes = function(last_id){
			var boxes = [];
			
			var request = $.ajax({
				url: '/ajax/infinite_scroll/',
				type: 'POST',
				data: { last_id : last_id },
				dataType: 'json'
			});
				
			request.fail(function(jqXHR, textStatus){
				alert( "Request failed: " + textStatus);
			});
				
			request.done(function(data){
				ndata = data;
				return data;
			});

			if(!ndata.error){
				$('#last_id').val('pic_' + ndata.pic_id);
				alert('NEW ID: pic_' + ndata.pic_id + ',' + ' NEW LAST ID: ' + $('#last_id').val());
				var box = document.createElement('div');
				text = document.createElement('img');
				text.src = '/thumb.php?pic=content/pics/' + ndata.user_id + ndata.src + '&h=200&w=210"';
				
				box.className = 'element item masonry-brick';
				box.id = 'pic_' + ndata.pic_id;
				box.appendChild( text );
				boxes.push( box );
			}else{
				if(ndata.error != 'complete'){
					alert(ndata.error);
				}
			}

			return boxes;
		};
		
		window.onscroll = function(){	
			if(($(document).height() - $(window).height() - $(document).scrollTop()) < 300){
				var container = document.getElementById('container');
				var wall = new Masonry( container, {isFitWidth: true });
				var last_id = $('#last_id').val();
				alert('Start LAST ID: ' + last_id );
				var boxes = boxMaker.makeBoxes(last_id);
				
				for (var i=0; i < boxes.length; i++) {
					container.appendChild( boxes[i] );
				}
				wall.appended( boxes );
			}
			
		}

		</script>
	</body>
</html>
This is an infinite scroll I'm doing and I store my last picture ID in an input type hidden element for the AJAX -> PHP to know from which ID to continue outputting pictures. However, this last_id does not function properly and the app makes double pictures.

I put up alert boxes for testing purposes, hope they help you too.


PHP side:

Code:
	public function infinite_scroll(){
		
		header("Content-Type: application/json");
		
		if(!empty($_POST)){
			if(isset($_POST['last_id'])){
				$pic_id = end(explode('_', $_POST['last_id']));
				$security = new Security($this -> registry -> db);
				$pic = new Picture($this -> registry -> db);

				if($security -> checkNum($pic_id)){
					$items = $pic -> getInfiniteScrollData($pic_id);
					if($items !== false ){
						foreach($items as $key => $value){
							$data[$key] = $value;
						}
					}else{
						$data = array(
							'error' => 'complete'
						);
					}
				}else{
					$data = array(
						'error' => 'Invalid input!'
					);
				}
			 }
		}
		
		echo json_encode($data);
		
	}

URL : http://pixpresso.mycyberlove.com

Pls help.
Send a message via Skype™ to ivanivkovich
ivanivkovich is offline  
Reply With Quote
Old 06-25-2012, 08:33 PM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

I set this thread for "Reply later" since I haven't had a chance with work. But I came back today and saw your site to see if you have fixed it already. It seems that the autoscroll is working now and with a very cool animation.
tony is offline  
Reply With Quote
Old 06-25-2012, 09:47 PM   #3 (permalink)
Ivan Ivković, Croatia
 
ivanivkovich's Avatar
 
Join Date: May 2012
Posts: 7
Thanks: 0
ivanivkovich is on a distinguished road
Default

Thanks. Queries are still being doubled dogh. This is my side project, so I'll fix it somehow l8er. If you have any idea, pls let me know.
Send a message via Skype™ to ivanivkovich
ivanivkovich is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and AJAX 9three Javascript, AJAX, E4X 0 09-27-2009 09:26 PM
Ajax Theme for CMS russellharrower Javascript, AJAX, E4X 5 07-22-2009 08:44 PM
Query With a C# Variable?‏ StevenF The Lounge 5 12-08-2008 05:32 AM
Ajax without ruining crawlability? Aaron Javascript, AJAX, E4X 5 01-28-2008 06:57 PM
Ajax & Creating Thumbnail Rendair Advanced PHP Programming 0 01-10-2008 03:28 PM


All times are GMT. The time now is 08:34 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design