Avatar of pingeyeg
pingeyeg
 asked on

Defaulting a limit to display

Is there a way to make a default limit unless otherwise specified via a sql statement?  I am using JQuery and AJAX to change the limit to list on the page and I would like for 10 to be the default limit unless the user changes this.

if($_GET['act'] == 'getNum') {
            $number = $_GET['change'];
            
            $getUsers = sprintf("SELECT id, username, answer_point, howto_point
                  FROM login
                  LIMIT %d", $number);
            $showUsers = mysql_query($getUsers) or die("Users not showing because: " . mysql_error());
                  while($u = mysql_fetch_array($showUsers)) {
                        if(!empty($u['answer_point']) AND !empty($u['howto_point'])) {
                              $points = $u['answer_point'] + $u['howto_point'];
                                    echo "      <div class='performer'>
                                                            <div class='user'>{$u['username']}</div>
                                                            <div class='points'>$points</div>
                                                      </div>
                                                ";
                        }
                  }
      }
PHPSQL

Avatar of undefined
Last Comment
pingeyeg

8/22/2022 - Mon
Ionut A. Tudor

replace this
$number = $_GET['change'];
with this
if(($number = $_GET['change']) AND $number=="10") $number = "10";

SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ionut A. Tudor

my above condition won't work, this should be ok
if(($number = $_GET['change']) OR $number=="") $number = "10";
 
Ionut A. Tudor

or better to check if its an digit
<?php
 
if(isset($_GET['change']) AND $number = $_GET['change'] AND ctype_digit($number)){}
else {
$number = "10";
}
 
?>

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
pingeyeg

ASKER
None of those are working.  I think it has more to do with the JQuery itself.

$(document).ready(function() {
      $('#change').change(function() {
            $.get('/php-ajax/getnumber.php', {act: 'getNum', change: $(this).val()}, function(num) {
                  if(num) {
                        $('#list').html(num);
                  }
            });
      });
});
Ionut A. Tudor

well PHP controls the mysql rows being displayed, so the below should work and display the default 10 rows from that query or how many you're setting.
 

<?php
if($_GET['act'] == 'getNum') {
            if(isset($_GET['change']) AND $number = $_GET['change'] AND ctype_digit($number)){}
			else {
			$number = "10";
			}
            
            $getUsers = sprintf("SELECT id, username, answer_point, howto_point
                  FROM login
                  LIMIT %d", $number);
            $showUsers = mysql_query($getUsers) or die("Users not showing because: " . mysql_error());
                  while($u = mysql_fetch_array($showUsers)) {
                        if(!empty($u['answer_point']) AND !empty($u['howto_point'])) {
                              $points = $u['answer_point'] + $u['howto_point'];
                                    echo "      <div class='performer'>
                                                            <div class='user'>{$u['username']}</div>
                                                            <div class='points'>$points</div>
                                                      </div>
                                                ";
                        }
                  }
      }
?>

Open in new window

pingeyeg

ASKER
I don't know, it just sits there.  My JQuery statement has an onChange function so I wonder if that is causing this to not work properly?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ionut A. Tudor

probably, can we take a look at it online ?
pingeyeg

ASKER
I posted it above ^

$(document).ready(function() {
      $('#change').change(function() {
            $.get('/php-ajax/getnumber.php', {act: 'getNum', change: $(this).val()}, function(num) {
                  if(num) {
                        $('#list').html(num);
                  }
            });
      });
});

http://beta.autocahoots.com/top-performers.php
ASKER CERTIFIED SOLUTION
Ionut A. Tudor

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
pingeyeg

ASKER
Man this is difficult.  I didn't realize how much would be involved.  I thought this would be a simple thing, but the site you sent me to is a little difficult as well.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
pingeyeg

ASKER
This is what I did with that site's solution, but it doesn't work and frankly, I don't understand it myself.

$(document).ready(function() {
      if (!$("#mySelect option:selected").length)
        $("#mySelect option[value='10']").attr('selected', 'selected');
      $('#change').change(function() {
            $.get('/php-ajax/getnumber.php', {act: 'getNum', change: $(this).val()}, function(num) {
                  if(num) {
                        $('#list').html(num);
                  }
            });
      });
});
SOLUTION
Ionut A. Tudor

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
pingeyeg

ASKER
It's clear, but it's still not working.

$(document).ready(function() {
      if (!$("#change option:selected").length)
    $("#change option[value='10']").attr('selected', 'selected');
      $('#change').change(function() {
            $.get('/php-ajax/getnumber.php', {act: 'getNum', change: $(this).val()}, function(num) {
                  if(num) {
                        $('#list').html(num);
                  }
            });
      });
});
Ray Paseur

@pingeyeg: I think you are making this harder than it needs to be!  Have you tried using var_dump($_GET) to print out the contents of the AJAX query string? If you do that, you can see right away if the solutions posited at the top of this Question would work.

I think if you can show us $_GET in the backend script, we can show you the right answer at once.

Best regards, ~Ray
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
pingeyeg

ASKER
Ray_Paseur: What portion of my code would I place that?
pingeyeg

ASKER
How can I tell if this is working properly?

$(document).ready(function() {
      if($('#change').selectedValues() == false) {
            $('#change').selectOptions('10');
            $.get('/php-ajax/getnumber.php', {act: 'getNum', change: $(this).val()}, function(num) {
                  if(num) {
                        $('#list').html(num);
                  }
            });
      } else {
            $('#change').change(function() {
                  $.get('/php-ajax/getnumber.php', {act: 'getNum', change: $('#change').val()}, function(num) {
                        if(num) {
                              $('#list').html(num);
                        }
                  });
            });
      };
});
pingeyeg

ASKER
Ok, I've finally got the top portion working, but the second portion is not.  If the user does select an option I need it to change to that number.

$(document).ready(function() {
      if(!$("#change options:selected").length) {
            $("#change option[value='10']").attr('selected', 'selected');
            $.get("/php-ajax/getnumber.php", {act: 'getNum', change: $('#change').val()}, function(num) {
                  if(num) {
                        $("#list").html(num);
                  }
            });
      } else {
            $('#change').change(function() {
                  $.get('/php-ajax/getnumber.php', {act: 'getNum', change: $('#change').val()}, function(num) {
                        if(num) {
                              $('#list').html(num);
                        }
                  });
            });
      };
});
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
pingeyeg

ASKER
Nevermind, I figured it out.  Thanks for the help!