× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fetch data from DB
#1
Dear UserSpice Team ,

I am a newbie to user spice and just started working on PhP MYSQL . of course i love to work in this for the reason it gives me flexibility to make changes on the top of your framework without any restriction .

My query is i need to display the role of the user logged in . Like if he is Administrator then echo as Administrator

I am taking Permission id of the logged in user from users table and comparing it with the id in Permission table and trying to fetch the name of the role from the permission table . But it displays the following error .

<pre>
Code:
Notice: Array to string conversion in C:\xampp\htdocs\***\account.php on line 477
Array
</pre>


My code is as follows

<pre>
Code:
<?php
  $query =$db->query("select name from permission where permission.id=users.permissions");
  $results = $query->results();
  echo $results;
?>
</pre>
  Reply
#2
Since results is obviously an array i just noticed and used print_r($results); in replace of echo . still i am getting only as follows
Code:
Array ( [0] => stdClass Object ( [id] => 100 ) )
  Reply
#3
Then i tried to access that using

Code:
$results[0]->id;

but actually it outputs as
Code:
100
. The point is i want to output the
Code:
name
field in
Code:
Permission table
which is
Code:
"Administrator"
also by the way there is no such id as
Code:
100
. I am confused
  Reply
#4
No i tried the following
<pre>
Code:
<?php
  $sql= "select name from permissions where id=users.permissions";
  $rslt = $db->query($sql);
  foreach($rslt as $newArray){
  $roleName = $newArray['name'];
  echo "$roleName";
}
</pre>


It results nothing . Just blank . I am really really worried without knowing where i am doing mistake
  Reply
#5
This code will dump out the names of the groups the logged in user belongs to. Give it a try and come back with any questions.

<pre>
Code:
$db = DB::getInstance();
$query = $db->query("SELECT * FROM user_permission_matches WHERE user_id = ?", array($user->data()->id));
$x = $query->results(true);
//dump($x);
foreach ($x as $y){
    //dump($y);
    $perm_id=$y["permission_id"];
    $query = $db->query("SELECT * FROM permissions WHERE id = ?", array($perm_id));
    $z = $query->results(true);
    //dump($z[0]);
    $perm_name=$z[0]["name"];
    echo $perm_name.'<br/>';;
}
</pre>
  Reply
#6
Yeah. Off the top of my head, that looks right. Great answer. If that doesn't work, let me know. I can also make a little function to make that easier in the future.
  Reply
#7
The trick here is that the user still needs to "assign meaning" to the different levels. In my case, I run a query to see if they have permission level 2 or 3 (Admin and Moderator) while level 3 is only Moderator (no admin) and that allows or generates pages based on the level.

The more we know about what you need to do , the better we can tailor a response.
  Reply
#8
@brian , That worked perfectly . Thanks a ton .

@brian and @mudmin
One more query . Why come there are two ids in user_permission_matches table with same user_id . for ref check the demo db table after installing userspice 4.0 mate . User with 101 also has user_id 1 and Administrator with id 100 also has user_id 1
  Reply
#9
@brain and @mudmin

in addition to the previous reply i also have one more query

if i output
Code:
<?php echo $user->data()->join_date; ?>
it results as
Code:
2016-01-01 00:00:00
. how to code that to output like
Code:
January 2016
  Reply
#10
I solved the above with the following query mate

<pre>
Code:
<?php
$date = $user->data()->join_date;
$datetime = new DateTime($date);
echo $datetime->format('F Y');
?>
</pre>


However kindly answer for my other question before to that .
  Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)