Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have a database which has about 5000 books. When user comes on the page, I don't want to load the books by using simple select * from BOOkTABLE, my approach is to go as the user needs it, so I want that as the user scrolls down I could fetch data from the Database. May be in chunks of 15-20 books, is that a good approach? Any alternative?
Secondly I have alphabets on top of div so if a user clicks on "Z" we could fetch the books starting with Z and show him in the DIV.
Yes you can do this by using Ajax on scroll down.Something like that:
$(window).scroll(function () {
if ($(window).height() + $(window).scrollTop() == $(document).height()) {
$.ajax({
type: "POST",
url: 'abc.php',
async:false,
data:"pagedata="+pageData ,
cache: false,
success: function(data){
}
});
}
});
In abc.php you can set the query according to your requirement and render the html(response) to be shown when scrolled down.
For the first question, using ScrollTop() you can load iterations of data from your PHP after an interval of pixels has been scrolled.
You will need a PHP file, and JavaScript.
This is written freehand with no testing so I can't guarantee these will work, but try something along these lines:
JavaScript
jQuery(document).ready(function($){
// Set the number of pixels from the top you want the event to fire
// Select the number of books you would like to load each time
// i = number of loads you've called
var reloadAfter = 500;
var numberBooks = 20;
var i = 1;
$(window).scroll(function() {
if ( $(this).scrollTop() >= reloadAfter )
{
$.get("getbooks.php", {number:numberBooks , counter:i} )
.done(function(data){
$('#books').append("Title:", data.title)
.append("Author:", data.author);
});
reloadAfter = reloadAfter + reloadAfter;
i++;
}
});
PHP
$numberBooks = $_GET['number'];
$i = $_GET['counter'];
$upper = ( $numberBooks * $i );
$lower = ( $upper - $numberBooks );
$get_books = mysql_query('SELECT * FROM BOOkTABLE WHERE id > $lower AND id < $upper');
echo json_encode($get_books);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
EXAMPLE
I want to insert the data automatic everyday without press the button action or whatever.
In my case,
my data is ready to input every day on system but i must press the button action every day too.
thakyou
Handle an auto-click parameter in URL:
in URL: ?automode=1
in Javascript:
var is_automode = false,
$btn = YOUR_BUTTON,
$form = YOUR_FORM,
$mode = HIDDEN_INPUT_TEXT;
location.search.replace(/^\?/,'').split('&').forEach(function(val) {
var pair = val.split('=');
if (pair.length && pair[0]==='automode' && pair[1]!=0) {
is_automode = true;
}
});
if (is_automode) {
setInterval(function () {
$form.target = '_blank';
$mode.value = 'automode'; // detect this value in the target page, call window.close() if is 'automode'
$btn.click(); // or $form.submit()
}, 86400*1000);
}
Or add a cron job to open the URL (YOUR_URL/action.php?automode=1) without setInterval()
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So, let's say I want to have a piece of text on my website, that changes color every second. I do know how to use JS to change the color of the text, but not how to continue to auto-change it based on certain parameters. For example, cycling through thousands of colors, not just one or two.
ok here is a simple way to blink text with colors
Js :
// List of colors
var spectrum = ['#f00', '#f66', '#969', '#00F', '#0FB53F'];
var cycle = spectrum.length-1;
// Cycle speed
var speed = 300;
var i = 0;
window.setInterval(function(){
document.getElementById('index').style.color = spectrum[i];
if (i < cycle) i++;
else i = 0;
}, speed);
HTML
<p id="index">Flashing text</p>
Demo
You can simple use html inside PHP:
echo "<font color='red'>Hello World</font>";
for a loop you can simple create an array with all the html collors:
$colors=array();
$colors[1]="red";
$colors[2]="blue";
// soo on....
foreach($colors as $color){
echo "<font color='$color'>Hello World</font>";
}
setInterval is what you want.
Fiddle
http://www.w3schools.com/jsref/met_win_setinterval.asp
var myColours = [...];
var index = 0;
var myElement = /*get your element*/
setInterval(function() {
/* Set your element. Are you using Javascript to get your element? */
myElement.style.color = myColours[index];
/* Are you using jQuery? */
myElement.css('color', myColours[index]);
index++;
if(index > myColours.length - 1){
index = 0;
}
}, 1000);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to make an auto complete forum (like the tags below) that works in lime-survey. I'm new to this so please explain everything like i'm 5 :)
My goals are:
auto complete to work
work with lime-survey
have an excel file or maybe a database using PHP/MySQL that anyone can manage by editing. It would only be ONE row. Please point me how to do this. I use a mac :)
Here is the code:
<!DOCTYPE HTML>
<html>
<body>
<div >
<input type="text" id="raw_input" width="100" height="30" name="box" onkeyup=show(this)>
</div>
<div id="drop_down" style="display:none">
<SELECT id=box size=3 name="box" onclick=show(this)></SELECT>
</div>
<script>
function drop_the_box() {
document.getElementById("drop_down").style.display = "none";
document.getElementById('box').length = 0;
if (!randomcharactersblablabla).test(document.getElementById("raw_input").value){
document.getElementById("drop_down").style.display="block";
var database = new Array("object_1","item_1","object_2","item_2");
var string = document.getElementById("raw_input").value;
for (var s = 0; s < database.length; s+= 1 ) {
var t += 1
if (database[s].indexOf(string) != 0) {
addItem(string[s],database[s]);
scan(streetArray[s],streetArray[s]);
}
}
}
}
function scan(x,y) {
var ghost_tag = document.createElement("ghost");
document.getElementById("box").options.add(ghost_tag);
ghost_tag.text = x;
ghost_tag.value = y;
}
function show(visable) {
document.getElementById("dropdown").value = visable.value;
}
</script>
</body>
</html>
Keep you data in mysql database. Create php file which will handle queries. Use jquery.ajax() to send queries and retrieve responses from php file.
Use this example
jQuery file
$('#search').change(function(){
var name = $('#search').val();
$.ajax({
type: 'POST',
url: 'request.php',
data: 'some data(may be variable)',
success: function(response){
$('#searach').val(response);
}
});
})
php file
if(isset($_POST['some_data'])){
$query = 'SELECT your_table_field FROM your_table WHERE your_table_field LIKE %$_POST['some_data']% LIMIT 1';
$result = mysql_query($query);
$myrow = mysql_fetch_array($result);
echo $myrow[0];
}
My question has part solutions on this site but not a complete answer.
On my wordpress homepage I display a counter of the number of questions answered within our webapp. This is displayed using jQuery and AJAX to retrieve the question count from a php file and works fine with this code.
jQuery(document).ready(function() {
function load() {
jQuery.get('/question_count.php', function(data) {jQuery('#p1').html( data ); });
}
load();
setInterval(load,10000);
});
Is there a way to display counting up to the new number retrieved rather than just immediately displaying it?
Something like this?
function countTo(n) {
var p = $("#p1"),
c = parseInt(p.html(), 10) || 0,
dir = (c > n ? -1 : 1); // count up or down?
if (c != n) {
p.html((c + dir) + "");
setTimeout(function() {
countTo(n);
}, 500);
}
}
Call it in your success handler
jQuery.get('/question_count.php', function(data) {
var n = parseInt(data, 10);
countTo(n);
});
Example
You will need to do a setInterval event so that the count up is visable to human eyes.
This may be a problem if you eventually reach enough questions where the count takes a long time to reach the end.
Code will look like this:
function load(){
jQuery.get('/question_count.php', function(data){
var curr = 0;
var max = parseInt(data);
var interval = setInterval(function(){
if(curr==max){
clearInterval(interval);
}
jQuery('#p1').html( curr );
curr+=1; //<-- if the number of questions gets very large, increase this number
},
10 //<-- modify this to change how fast it updates
});
}
}
This question already has an answer here:
How to paginate query results for Infinite Scroll?
(1 answer)
Closed 8 years ago.
Infinite scroll should stop after 10 rows of records. If there are more records, ajax pagination should be displayed. For example, consider this infinite scrolling example.
So after 20 records I want to display pagination and same should be done on next page. Please let me know if you have any of this ideas or solution.
Here is my code on which I am working:
//On doMouseWheel = 1 I have taken the currentdealoffset value and check it with the total no of deals present
//If count is less, then simply calculating the window position displaying the allotted records say 10
//On next scroll just doing the same process and fetching records using ajax until end of the deals
//Now the problem is I am not able to code a logic where after say 10 records show a pagination and
//When click on next page the same process should be managed by fetching the offset count of scrol and offset of pagination bar
doMouseWheel = 1 ;
$(window).scroll(function() {
if($('#facebox_overlay').is(':visible')==false){
$('#endofdeals').show();
$('#endofdeals').html("<center><img src='"+SITEIMG +"ajax-loader_1.gif' ><center>");
//console.log("Window Scroll ----");
var currentdealoffset = 0; //alert(currentdealoffset);
var currentdealoffset = parseInt(document.getElementById("countval").value); //alert(currentdealoffset);
var displaymode = parseInt($('#displaymode').val());
var totalcountdeal = parseInt($('#totaldeals').val()); //alert(totalcountdeal);
if(currentdealoffset<totalcountdeal){
if (!doMouseWheel) {
return ;
} ;
var distanceTop = $('#last').offset().top - $(window).height();
if ($(window).scrollTop() > distanceTop){
//console.log("Window distanceTop to scrollTop Start");
doMouseWheel = 0 ;
$('div#loadMoreComments').show(5000);
//console.log("Another window to the end !!!! "+$(".postedComment:last").attr('id'));
$.ajax({
type : 'POST',
dataType : "html",
data: {
typeday : $('#remdumtype').val(),
catid : $('#catid').val(),
},
url: "<?php echo https_url($this->config->item('base_url'))?>popup/dealsearch",
success: function(html) {
doMouseWheel = 1;
if(html){
if(displaymode==12)
$('#listTable tr:last').after(html);
else
$("#postedComments").append(html);
//console.log("Append html--------- " +$(".postedComment:first").attr('id'));
//console.log("Append html--------- " +$(".postedComment:last").attr('id'));
$("#last").remove();
$("#postedComments").append( "<p id='last'></p>" );
$('div#loadMoreComments').hide();
$('#endofdeals').hide();
}
}
});
}
}
else
{
if($('#endofdeals')!='')
{
$('#endofdeals').hide();
if(currentdealoffset < displaymode)
{
$('#endofdeals').hide();
}else if(currentdealoffset > displaymode)
{
$('#endofdeals').show();
$('#endofdeals').html("<center><h2 style='color:#4C335B'>End of deals !!!!!!!</h2></center>");
}
}
}
}
});
According to me, You want page numbers but the pages should load slowly as you scroll.
If this you want then, you should not use Infinite scroll technique but LazyScroll will help you and if you want 20 records then make your query for 20 records also create pagination below
Here, you can have the Plug-in and Demo.