Page 1 of 2
[SOLVED] Smarty: People who are Familiar, I was wo
Posted: Tue Jun 08, 2004 1:13 pm
by NewfieBilko
Hi. I am getting pretty used to smarty, but have been working on this damn solution for a few hours now to no eval. I dont know if im just missing something small but maybe you can help.
As you can see here I have what is a Grid of the users in my database. This is page one.
I would like to increase the amount of users displayed, as well as have a variable to allow for the user to select how many users are shown per page.
Here is the code to the frmUsersGrid.php file thats called from the template:
Here is the code from frmUsersGrid.tpl, which is the template for the .php.
Any help would be graciously appericated.. I dont think its a hard problem, just one that needs solving. I hope there is a variable for the amount of users displayed somewhere.
Posted: Tue Jun 08, 2004 7:18 pm
by NewfieBilko
C'MON, alittle hint would be nice

Posted: Tue Jun 08, 2004 7:19 pm
by NewfieBilko
I can't find the varible that declares how many times the line will print until it makes new page
Posted: Tue Jun 08, 2004 8:44 pm
by markl999
Looks like it could be $maxRecords to me. $offset would probably be passed via the url (on clicking Next/Back).
Posted: Wed Jun 09, 2004 9:41 am
by NewfieBilko
right right, maxrecords seems to be the max amount from the database entirely.. and offset tells it to make anothe rpage or not...?
Posted: Wed Jun 09, 2004 9:50 am
by markl999
Well, the usual way of doing these type of queries is,
1. SELECT COUNT(*) FROM foo;
2. SELECT * FROM foo ORDER BY whatever LIMIT $offset, $howmany;
The first tells you how many entries there are in the database, the seconds select only the ones you want to display on that page.
In your case $maxRecords would probably be a defined number, say 10 (for 10 results per page). The next and back links will be $offset-10 and $offset+10 respectively. The PHP code will just take the value of $_GET['offset'] and work out the appropriate values for the next and back links (by adding or subtracting $maxRecords), if $offset is currently 0 (or not set) then you don't show the back link, if it's equal to (or above) the total number of records (from the first query) then don't show the next link.
Looking at the screenshot of the code it does look decently coded so it should (in theory) just be a case of finding where $maxRecords is defined and changing the number to suite how many you want per page, the rest of the code (next/back) should take care of itself.
Posted: Wed Jun 09, 2004 11:22 am
by NewfieBilko
weird.. can't figure where its to... this is my code from the .php:
Code: Select all
<?php
$users = $userlib->GetUsers($offset,$maxRecords,$sort_mode,$find);
$cant_pages = ceil($users["cant"] / $maxRecords);
$smarty->assign('cant_pages',$cant_pages);
$smarty->assign('actual_page',1+($offset/$maxRecords));
if($users["cant"] > ($offset+$maxRecords)) {
$smarty->assign('next_offset',$offset + $maxRecords);
} else {
$smarty->assign('next_offset',-1);
}
// If offset is > 0 then prev_offset
if($offset>0) {
$smarty->assign('prev_offset',$offset - $maxRecords);
} else {
$smarty->assign('prev_offset',-1);
}
// Get users (list of users)
$smarty->assign('users',$users["data"]);
?>
[/php_man]
Posted: Wed Jun 09, 2004 11:27 am
by NewfieBilko
and here is the code that calls those, in my .tpl
Code: Select all
<?php
<div class="mini">
{if $prev_offset >= 0}
[<a class="link" href="frmUsersGrid.php?&offset={$prev_offset}&sort_mode={$sort_mode}">{tr}PREVIOUS{/tr}</a>]
{/if}
{tr}Page{/tr}: {$actual_page}/{$cant_pages}
{if $next_offset >= 0}
[<a class="link" href="frmUsersGrid.php?&offset={$next_offset}&sort_mode={$sort_mode}">{tr}NEXT{/tr}</a>]
{/if}
</div>
?>
Posted: Wed Jun 09, 2004 11:27 am
by markl999
$maxRecords must be defined somewhere. Find the file that contains something like $maxRecords = 10;
Posted: Wed Jun 09, 2004 11:39 am
by NewfieBilko
Ok, very interesting.. It points to userslib.php in which the function GetUsers, sets $maxrecords.... Any ideas?
Code: Select all
<?php
function GetUsers($offset = 0,$maxRecords = -1,
$sort_mode = 'login_desc', $find='')
{
$sort_mode = str_replace("_"," ",$sort_mode);
if($find)
$mid=" WHERE login LIKE '%".$find."%'";
else
$mid='';
$query = "SELECT login, email, lastLogin FROM users_users $mid
ORDER BY $sort_mode LIMIT $offset,$maxRecords";
$query_cant = "SELECT count(*) FROM users_users";
$result = $this->db->query($query);
?>
Posted: Wed Jun 09, 2004 11:42 am
by markl999
Nope, that's not the place.
That function takes the value of $maxRecords which is passed it at
$users = $userlib->GetUsers($offset,$maxRecords,$sort_mode,$find);
So $maxRecords must be set/defined somewhere else.
Posted: Wed Jun 09, 2004 11:58 am
by NewfieBilko
YAYY
I FOUND IT, THX..
It was in my setup.php
SO... changing this constant changes the amount of displayed users/page. If i want the user to be able to change that constant vairable, do I just link up the input with equaling the variable? hmmm
On the input form have it called something, then assign my maxrecord varible to that input?
Posted: Wed Jun 09, 2004 12:04 pm
by markl999
Whereever you have $maxRecords = 10; you could replace that with:
Code: Select all
if(!empty($_POST['maxRecords']) && ctype_digit($_POST['maxRecords']) && $_POST['maxRecords'] > 0){
$maxRecords = $_POST['maxRecords'];
} else {
$maxRecords = 10;
}
Then just name your html field 'maxRecords'.
Posted: Wed Jun 09, 2004 12:12 pm
by NewfieBilko
Wow really.. nice thinking.. i had some code done up and put it in my UsersGrid.php, but i will put this in setup now and see how it works. thx
Posted: Wed Jun 09, 2004 12:15 pm
by NewfieBilko
hmm, no it didn't work.
It goes to the else clause everytime it seems
..
my form:
Code: Select all
<?php
<form method="get" action="frmUsersGrid.php">
<td align="center">
<input type="text" name="maxRecords" />
<input type="submit" value="Users Per Page"/>
</td>
</form>
?>