[SOLVED] Smarty: People who are Familiar, I was wo

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

[SOLVED] Smarty: People who are Familiar, I was wo

Post 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.
Image

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:
Image


Here is the code from frmUsersGrid.tpl, which is the template for the .php.
Image


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.
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

C'MON, alittle hint would be nice :)
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

I can't find the varible that declares how many times the line will print until it makes new page
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Looks like it could be $maxRecords to me. $offset would probably be passed via the url (on clicking Next/Back).
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post 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...?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post 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]
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post 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>]&nbsp;
{/if}
{tr}Page{/tr}: {$actual_page}/{$cant_pages}
{if $next_offset >= 0}
&nbsp;[<a class="link" href="frmUsersGrid.php?&offset={$next_offset}&sort_mode={$sort_mode}">{tr}NEXT{/tr}</a>]
{/if}
</div>

?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

$maxRecords must be defined somewhere. Find the file that contains something like $maxRecords = 10;
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post 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);

?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

YAYY :oops:

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?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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'.
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post 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
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post 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>


?>
Post Reply