Forum Moderators: coopster

Message Too Old, No Replies

Dynamic number of records per page

Displaying a dynamic number of records from a select list

         

dbarasuk

6:05 am on Apr 15, 2008 (gmt 0)

10+ Year Member



Hi!

I have a line of code to define how many records should be returned from database to be displayed to a webpage like this:

define(SHOWMAX, 10);

If I put on the top of the page a select list element to allow the user to select a different number of records to use to navigate throughtout the whole number of recordsets, how should I pass the value of that select list so that by selecting a different value the number of records per page should be adjusted to reflect what the user selected?,

NOTE: I am not hittig a button for that purpose, only selecting a different value should accomplish that goal

Regards

eelixduppy

9:17 pm on Apr 15, 2008 (gmt 0)



One way or another you are going to have to use an AJAX solution to do what you want here. Without hitting a button, you are going to have to invoke the function whenever a new value is clicked, and then in the background, query the database and display the data appropriately on your page.

There is a great little intro to AJAX here: [news.php.net...]

Read it well and try to implement it. If you need further assistance, the Javascript and AJAX forum might be of more help to you.

dbarasuk

1:14 am on Apr 16, 2008 (gmt 0)

10+ Year Member



I decide to include a button before the select list.

The as you said I put a script in the background to check whether the submit button was submitted or not and define a query consequently

However, I must be honest, writing functions is my weakest point.

I defined the function as follows:

function nr_of_records()
{
// It retrieves the number of records indicated in the select list box.
if(!array_key_exists('nr_of_records',$_GET))
{
$query = "SELECT expense_id, date, DAYNAME(date), amount, expense_reason, payment_means, location FROM expenses ORDER BY date ASC LIMIT $start_row_number, ".MAXSHOW;
}
elseif(array_key_exists('nr_of_records', $_GET))
{
$selectRecords = strip_tags(trim($_GET['selectRecords'])));
$query = "SELECT expense_id, date, DAYNAME(date), amount, expense_reason, payment_means, location FROM expenses ORDER BY date ASC LIMIT $start_row_number, $selectRecords ";
}

return $query;
}

// the next line follows immediately the function.
$result = mysqli_query($connection, $query) or die(mysql_error($connection));
$start_row_number was defined before this function as
$start_row_number = $current_page*MAXSHOW;

WHEN I run the script I get the following error:

Undefined variable: query in C:\htdocs\MySQL_queries\all_expenses.php on line 80

Warning: mysqli_query() [function.mysqli-query]: Empty query in C:\htdocs\MySQL_queries\all_expenses.php on line 80

Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in C:\htdocs\MySQL_queries\all_expenses.php on line 80

I thought $query was received from the function but this is not the case.

Any help?