Smarty Help: Changing the AssignName=UserName to UserId

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

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

Smarty Help: Changing the AssignName=UserName to UserId

Post by NewfieBilko »

Hey. I have been working on this today.

I have my usergrid, in which i want to link up to a page, that shows all the information from my DB on the page like this:
Image

but the page that sends which user i wish to view is sending the 'login' field of my DB to show the rest of the fields. I want it to send userId, and thus display all other fields:

Image

So... Here is my code.
This code is from the DataGrid.TPL, WHICH gets data from DataGrid.PHP:

Code: Select all

<?php

{section name=user loop=$users}
{if $smarty.section.user.index % 2}
<tr>
<td bgcolor="#dddddd" class="text"><a class="link" href="frmUserInfo.php?assign_user={$users[user].user}">{$users[user].user}</a></td>
<td bgcolor="#dddddd" class="text">{$users[user].email}</td>
<td bgcolor="#dddddd" class="text">{$users[user].lastLogin|date_format:"%A %d of %B, %Y [%H:%M:%S]"}</td>
<td bgcolor="#dddddd" class="text">
{section name=grs loop=$users[user].groups}
{$users[user].groups[grs]}{if $users[user].groups[grs] != "Anonymous"}(<a class="link" href="frmUsersGrid.php?ruser={$users[user].user}&action=removegroup&group={$users[user].groups[grs]}">x</a>){/if}&nbsp;
{/section}
</td>
<td bgcolor="#dddddd" class="text">
<a class="link" href="frmUserInfo.php?assign_user={$users[user].user}">{tr}View Details{/tr}</a>&nbsp;&nbsp;
<a class="link" href="frmUserEdit.php?assign_user={$users[user].user}">{tr}Edit Details{/tr}</a>&nbsp;&nbsp;
<a class="link" href="frmUsersGrid.php?action=delete&user={$users[user].user}">{tr}Delete{/tr}</a>
</td>
</tr>
{else}
<tr>
<td bgcolor="#EEEECC" class="text"><a class="link" href="frmUserInfo.php?assign_user={$users[user].user}">{$users[user].user}</a></td>
<td bgcolor="#EEEECC" class="text">{$users[user].email}</td>
<td bgcolor="#EEEECC" class="text">{$users[user].lastLogin|date_format:"%A %d of %B, %Y [%H:%M:%S]"}</td>
<td bgcolor="#EEEECC" class="text">
{section name=grs loop=$users[user].groups}
{$users[user].groups[grs]}{if $users[user].groups[grs] != "Anonymous"}(<a class="link" href="frmUsersGrid.php?ruser={$users[user].user}&action=removegroup&group={$users[user].groups[grs]}">x</a>){/if}&nbsp;
{/section}
</td>
<td bgcolor="#EEEECC" class="text">
<a class="link" href="frmUserInfo.php?assign_user={$users[user].user}">{tr}View Details{/tr}</a>&nbsp;&nbsp;
<a class="link" href="frmUserEdit.php?assign_user={$users[user].user}">{tr}Edit Details{/tr}</a>&nbsp;&nbsp;
<a class="link" href="frmUsersGrid.php?action=delete&user={$users[user].user}">{tr}Delete{/tr}</a>
</td></td>
</tr>
{/if}
{/section}

?>

This gets the varibles from this file, DataGrid.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"]);

// Display the template
$smarty->assign('tbody','frmUsersGrid.tpl');



?>


And finally, we have code from the OUTPUT/display of UsersView.tpl:

Code: Select all

<?php

<h2>User Information/Details</h2>
<table>
<tr><td class="textbl">USERID:</td><td class="text">{$user_info.userId}</td>
<tr><td class="textbl">Login:</td><td class="text">{$user_info.login}</td>
<tr><td class="textbl">Email:</td><td class="text">{$user_info.email}</td>
<tr><td class="textbl">Real Name:</td><td class="text">{$user_info.realname}</td>
<tr><td class="textbl">Country:</td><td class="text">{$user_info.country}</td>
<tr><td class="textbl">Home Page:</td><td class="text">{$user_info.homePage}</td>
<tr><td class="textbl">Groups:</td><td class="text">
{section name=grp loop=$user_info.groups}
{$user_info.groups[grp]}{if $user_info.groups[grp] != "Anonymous"}(<a class="link" href="assignuser.php?assign_user={$assign_user}&action=removegroup&group={$user_info.groups[grp]}">x</a>){/if}&nbsp;
{/section}
</td></tr>
</table>

?>

Also, this is some code from UsersView.php

Code: Select all

<?php

$user_info = $userlib->GetUserInfo($assign_user);
$smarty->assign('user_info',$user_info);

if(!isset($_REQUEST["sort_mode"])) {
  $sort_mode = 'groupName_desc'; 
} else {
  $sort_mode = $_REQUEST["sort_mode"];
} 
$smarty->assign('sort_mode',$sort_mode);

// If offset is set use it if not then use offset =0
// use the maxRecords php variable to set the limit
// if sortMode is not set then use lastModif_desc
if(!isset($_REQUEST["offset"])) {
  $offset = 0;
} else {
  $offset = $_REQUEST["offset"]; 
}
$smarty->assign('offset',$offset);

if(isset($_REQUEST["find"])) {
  $find = $_REQUEST["find"];  
} else {
  $find = ''; 
}

$users = $userlib->GetGroups($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); 
}


?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

please put:

Code: Select all

var_dump($users);
line after this one:

Code: Select all

$users = $userlib->GetUsers($offset,$maxRecords,$sort_mode,$find);
and post here its output. From the code you've posted it's hard to know the structure of $users array.
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

LOL <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>


I got :

array(2) { ["data"]=> array(3) { [0]=> array(3) { ["groupName"]=> string(10) "Registered" ["groupDesc"]=> string(28) "Users logged into the system" ["perms"]=> array(3) { [0]=> string(14) "p_read_article" [1]=> string(10) "p_register" [2]=> string(18) "p_view_submissions" } } [1]=> array(3) { ["groupName"]=> string(4) "News" ["groupDesc"]=> string(86) "Group of users that will have access to post news and articles, but no admin features." ["perms"]=> array(10) { [0]=> string(20) "p_approve_submission" [1]=> string(14) "p_edit_article" [2]=> string(17) "p_edit_submission" [3]=> string(14) "p_post_article" [4]=> string(14) "p_read_article" [5]=> string(10) "p_register" [6]=> string(16) "p_remove_article" [7]=> string(19) "p_remove_submission" [8]=> string(16) "p_submit_article" [9]=> string(18) "p_view_submissions" } } [2]=> array(3) { ["groupName"]=> string(9) "Anonymous" ["groupDesc"]=> string(23) "Public users not logged" ["perms"]=> array(1) { [0]=> string(14) "p_read_article" } } } ["cant"]=> string(1) "3" }



Oh boy...

What I can tell you is that ;
in userslib, it picks up the data:

Code: Select all

<?php
    function GetUserInfo($user) 
    {
        $query = "SELECT * FROM users_users WHERE login='$user'";
        $result = $this->db->query($query);
        if(DB::isError($result)) $this->SqlError($query,$result);

        $res = $result->fetchRow(DB_FETCHMODE_ASSOC);

        $aux = Array();
        foreach ($res as $key => $val) {
            $aux[$key] = $val;  
        }

        $groups = $this->GetUserGroups($user);
        $res["groups"] = $groups;

        return $res;
    }

?>

^^^ That is where it gets it from DB,
The frmUsersInfo.PHP links by calling to the function:

Code: Select all

<?php

$user_info = $userlib->GetUserInfo($assign_user);
$smarty->assign('user_info',$user_info);

?>


Then spits user_info out to UserInfo.TPL:

USERINFO.TPL:

Code: Select all

<?php

<tr><td class="textbl">USERID:</td><td class="text">{$user_info.userId}</td>
<tr><td class="textbl">Login:</td><td class="text">{$user_info.login}</td>
<tr><td class="textbl">Email:</td><td class="text">{$user_info.email}</td>
<tr><td class="textbl">Real Name:</td><td class="text">{$user_info.realname}</td>
<tr><td class="textbl">Country:</td><td class="text">{$user_info.country}</td>
<tr><td class="textbl">Home Page:</td><td class="text">{$user_info.homePage}</td>
<tr><td class="textbl">Groups:</td><td class="text">



?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Actually I was interested in GetUsers() method ;) You have posted GetUserInfo().
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

yea sorry, this is it here... This GetUsers is called from the Grid...

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

        if(DB::isError($result)) $this->SqlError($query, $result);

        $cant = $this->db->getOne($query_cant);
        $ret = Array();

       while($res = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
            $aux = Array();
            $aux["user"] = $res["login"];
            $user = $aux["user"];
            $aux["email"] = $res["email"];
            $aux["lastLogin"] = $res["lastLogin"];
            $groups = $this->GetUserGroups($user);
            $aux["groups"] = $groups;
            $ret[] = $aux;
        }

        $retval = Array();
        $retval["data"] = $ret;
        $retval["cant"] = $cant;

        return $retval;
    }

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

Post by NewfieBilko »

The more i think about it though, the more i feel like deleting it and starting it from scratch.

If i were to have a simple call to getusers(), then do a link to every userId, then pass that to the INFO page, then in the info page i'd have it go, request from userId, then .all the fields...


This logic seems correct, but it should be easy for me to swab varibles outta what i already have done then. But i cannot get around it..
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

bumpme.com
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

you need to modify the query inside GetUsers method to fetch user_id along with the information it fetch, then use that id in your template
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

All I have to say is, this script is over complicated for what you are trying to do. THe best scripts I have made were not my first run throughs... they were scripts that I redid..over..and over...and over.
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

Yea, i hear what your saying,.. Onoly thing is, boss wants it done, and hes also gonna want me to do ALOT more with this system :(
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

Yea and i was looking at that problem some more.. its as if whatever the "assign_user" var is, goes to the GetUsersInfo() function passing it the name.. then in the function it does a SELECT for * login= Assign_user input.

So, i guess instead of re-writing the function, i will make a new function here... Make it so that it will look for all FROM ID=Input. Then return all.
...... Easier said then done. I know what u mean by complicated system... But boss wants this system
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

damn, now hes got me working on the MOODLE system now too.. Wants me to intergrate both systems togther! WOw wtf
User avatar
NewfieBilko
Forum Contributor
Posts: 189
Joined: Sun Jun 06, 2004 6:45 pm
Location: Newfoundland
Contact:

Post by NewfieBilko »

BUMP



Im trying to make it so that the assign_user variable which is actually login=assign_user, is now iD=assign_user.. This will pass the user ID, to the view page. I need to get it out of the database with a function, then pass all the user info to the view page and display the users ID along with every other field. . Only for that one user, referenced by ID instead of login.

HELP
Post Reply