Jump to content

Inconsistent function


davo
 Share

Recommended Posts

function myscore($me) {



$this_user = wire('users')->get($me) ; // $me is passed from the referring page and should be the id of the current user
$myscore = $this_user->score;          // there is a field on the users page called which this grabs

echo "<p>Your score is $myscore</p> "; // it works, the score is output

}

function myrank($me) {

$this_user = wire('users')->get($me);  //$me is again passed  as the value of the current user
$myscore = $this_user->score;          // my score grabs the score from that users page

$users_greater_than_me = wire('users')->find("score>=$myscore");   // this finds users who have a higher score than the current user
$myrank = count($users_greater_than_me);                           // myrank counts those users

echo "<p>Your rank is $myrank my and score is $myscore </p> ";    // rank is output correct but myscore doesnt output anything

}

I've got two function which should be doing exactly the same job but one works and yet the other doesn't I don't even really need the second one but it's frustrating me why it's behaving like this.

As you can see above, myscore works in the first function

It also works in the second function for comparing users

but when I ask it to output it a third time it's blank. Why oh why?

Link to comment
Share on other sites

Another night wasted; another lesson learned....

I think I have solved my own problem. I only hope this may help someone else one day.

There were a lot of things wrong with the code above and the problem I initially thought existed really wasn't the problem at all.

$this_user = wire('users')->get($me)

That was my problem i think. That returned all users to the array. 'Get ' of course returned the first user, which always happened to be me. And that was why I thought it was working, when actually nothing was working at all!

$this_user_id = wire('users')->get("id=$me")

That is how it should have been I think....

Link to comment
Share on other sites

You shouldn't need the "id="

These should all work:

$this_user_id = wire('users')->get(41);
$this_user_id = wire('users')->get("username");
$this_user_id = wire('users')->get("id=41");
$this_user_id = wire('users')->get("name=username");
Link to comment
Share on other sites

It was the first example that seemed to cause me so many problems. Whenever I used get($me), it always returned details for accounts 41, presumably because that was the first (lowest) user, unless problem was actually somewhere else.

Link to comment
Share on other sites

It was being defined on another page. I was trying to state if a user was logged in then to use the current user, but if they weren't but  had been and an id cookie had been dropped then to use the id of the cookie instead. Like this:

<?php

if($user->isLoggedin()) {
$me = $user;

$score_me = $me->id;
$log_message = "you are logged in";
} else{
$me = $_COOKIE[id];
$score_me = $me;
$log_message = "you are not logged in  ";}

$myname = $pages->get("id=$me");

myscore($score_me);
echo "<a href='/logout'>Not {$myname->name}?  </a>";

myrank($me);
Link to comment
Share on other sites

Makes sense, but what happens if they aren't logged in and there is no cookie set ?

One thing I just noticed - if the user is logged in, you set $me to the user object and not it's id, so when you do myrank($me), it won't work!

myrank($score_me) should work in both cases though.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...