Page 1 of 2

php help :S

Posted: Sun Mar 19, 2006 4:09 pm
by dmmh
my page numbering function just doesnt seem to wanne work right

anyone can spot the error quickly?

the way it basically works:

- I pass it an array of url variables with the corresponding values, the current page number and the total number of pages to be created, depending on the query/ user settings
- the url is created dynamically
- the function highlights the current active page depending on the url
- it displays the next 2 page numbers and the last 2 page numbers for each page viewed

[lvlshot]http://www.cinemasiafreaks.com/pimages/naamloos.jpg[/lvlshot]

- it will stop drawing the dots when the there is no use for it anymore

[lvlshot]http://www.cinemasiafreaks.com/pimages/naamloos3.jpg[/lvlshot]

now thos goes wrong when I am on the second last or last page, it will echo out twice the current page number, in bold text

[lvlshot]http://www.cinemasiafreaks.com/pimages/naamloos4.jpg[/lvlshot]

I cant find it :(

any help? :D

CODE (syntax highlighted): http://bb.cinemasiafreaks.com/view/?tid=15

Posted: Sun Mar 19, 2006 4:14 pm
by Foo
Link goes to blank page.

Posted: Sun Mar 19, 2006 4:19 pm
by dmmh
no it doesnt :)

Posted: Sun Mar 19, 2006 4:27 pm
by Foo
The last shot shows this setting in action:
$end_key = ($total_pages);
$end_key2 = ($total_pages-1);

When you get to the penultimate page, it's both the active page and the second to last page, both of which are always echoed to the document.

Your system of bolding the current page means than both of them turn out bolded, of course.

To get around this you can check if ($page == $end_key2) and stop one of the 2 being printed.

You're already doing this check for the 'next key' page number, but not for the active page.

Posted: Sun Mar 19, 2006 4:29 pm
by Freakaloin
lol...smart ppl r stoopid...

Posted: Sun Mar 19, 2006 6:28 pm
by dmmh
Foo wrote:The last shot shows this setting in action:
$end_key = ($total_pages);
$end_key2 = ($total_pages-1);

When you get to the penultimate page, it's both the active page and the second to last page, both of which are always echoed to the document.

Your system of bolding the current page means than both of them turn out bolded, of course.

To get around this you can check if ($page == $end_key2) and stop one of the 2 being printed.

You're already doing this check for the 'next key' page number, but not for the active page.
I know what you mean, but there is only one instance where a page number is echoed without a link attached, when I add conidtions the line, it doesnt show at all :(

Posted: Sun Mar 19, 2006 6:31 pm
by dmmh
fuck it, to tired :D
will try to sort it later

Posted: Sun Mar 19, 2006 7:40 pm
by Sevensins
when you get to the last pages (3rd screenshot) this is what the keys are

prev_key = 8
prev_key2 = 7
page = 9
end_key = 10
end_key2 = 9

you see?

the condition on line 43 is true for both page and end_key2 causing it to print twice...

Posted: Sun Mar 19, 2006 10:47 pm
by Ryoki
Freakaloin wrote:lol...smart ppl r stoopid...
haha

Posted: Mon Mar 20, 2006 12:13 pm
by glossy
wow, i come back to this? gimmie a sec, i'll give it a shot

Posted: Mon Mar 20, 2006 12:50 pm
by glossy
Wow, difficult code. I'd almost be suggesting re-writing the whole thing to not include wierd arrays and such... because i'm having a real hard time stepping through your code in my head. Some suggestions though:

should " && ($next_key<$total_pages) "(44) and " && ($next_key<$total_pages) "(48) be <= instead of < ? it shouldn't matter, but worth a shot i suppose.

otherwise, try debugging by appending 'curkey'/'nextkey2' etc. to all your echo's, and see which little bit is fucking with your code.

otherwise, ditch the wierd array&foreach thing, and try a more elegant for($i=0;$i<=$total_pages;$++i;)-style loop? good luck.

Posted: Mon Mar 20, 2006 12:54 pm
by 4days
and then stick the functions in a class..

there are a bunch of ready-rolled ones, or tutorials for pagination classes.

Posted: Mon Mar 20, 2006 1:11 pm
by glossy
eugh, classes. although i'm getting into using them properly now (albeit only for my database and global functions), it's a pain in the ass when a global function has to do a mysql query, and the sql query function needs to refer to it's own variables -- it all falls down, because "$this" becomes unusable.

although i could replace all the calls to $this->link with call_user_class_variable('mysql', 'link'); or something to that effect, i'd rather not go completely overkill on my code.

Posted: Mon Mar 20, 2006 1:39 pm
by JulesWinnfield
Totally unrelated to your PHP, but at first glance your graphical elements are completely confusing. The vertical line next to the cover graphic makes the eye travel in trying to coorelate the info to the image. Get rid of it :up:

Posted: Mon Mar 20, 2006 3:29 pm
by Foo
glossy wrote:eugh, classes. although i'm getting into using them properly now (albeit only for my database and global functions), it's a pain in the ass when a global function has to do a mysql query, and the sql query function needs to refer to it's own variables -- it all falls down, because "$this" becomes unusable.

although i could replace all the calls to $this->link with call_user_class_variable('mysql', 'link'); or something to that effect, i'd rather not go completely overkill on my code.
....what?

You can't get at those variables from outside the class because that's part and parcel of the security model of classes.

If you need to access an item from outside a class then you need to create a method within that class to return the variable. It just means adding 3 lines of extra code into the class for each variable you want to be accessible outside the class.

Or declare them as public.

Or am I missing your point here?

Posted: Mon Mar 20, 2006 4:22 pm
by dmmh
OOP :S

Posted: Tue Mar 21, 2006 5:43 am
by glossy
Foo wrote:
glossy wrote:eugh, classes. although i'm getting into using them properly now (albeit only for my database and global functions), it's a pain in the ass when a global function has to do a mysql query, and the sql query function needs to refer to it's own variables -- it all falls down, because "$this" becomes unusable.

although i could replace all the calls to $this->link with call_user_class_variable('mysql', 'link'); or something to that effect, i'd rather not go completely overkill on my code.
....what?

You can't get at those variables from outside the class because that's part and parcel of the security model of classes.

If you need to access an item from outside a class then you need to create a method within that class to return the variable. It just means adding 3 lines of extra code into the class for each variable you want to be accessible outside the class.

Or declare them as public.

Or am I missing your point here?
nono, i'm talking about this:

Code: Select all

class mysql {
  protected $link;
  
  public function query($sql) {
    if (!$this->link) // line 5, errors.
      return FALSE;
    return mysql_query($sql);
  }
  public function fetch_array($result) {
    if (!$this->link) // also errors, but doesn't get a chance.
      return FALSE;
    return mysql_fetch_assoc($result);
  }
}
class globalsettings {
  public function getuser($uid) {
    $result = call_user_func(array('mysql', 'query'), 'SELECT * FROM `users` WHERE `uid`='. $uid);
    return call_user_func(array('mysql', 'fetch_array'), $result);
  }
}

$db = new mysql;
$gs = new globalsettings;
print_r($gs->getuser(1));

running this code will result in an error on line 5 stating "$this->link" doesn't exist because the query function isn't being run in the usual context, and $link only applies inside $db.

Posted: Tue Mar 21, 2006 5:46 am
by glossy
also will note: i worked around this, by changing the getuser() function to:

Code: Select all

  public function getuser(&$db, $uid) {
    $result = $db->query('SELECT * FROM `users` WHERE `uid`='. $uid);
    return $db->fetch_array($result);
  } 
works peachy, although it's not as elegant as i might have liked.

Posted: Tue Mar 21, 2006 6:09 am
by mjrpes
glossy, in your global settings class you could require the db object to be passed in the constructor.

$gs = new globalsettings ($db); // die if no valid db object passed
print_r($gs->getUser(1));

Posted: Tue Mar 21, 2006 6:18 am
by glossy
oh really! news to me, i will try it out.

thankyou mister wizard !

Posted: Tue Mar 21, 2006 6:25 am
by mjrpes
it would go something like this...

Code: Select all


class globalsettings
{ 
     private $db;

     function globalsettings(&$db)
     {
          // add error checking to make sure db is valid object
          $this->db = $db;
     }  

     function getuser($id)
     {
         $result = $this->db->query('SELECT * FROM `users` WHERE `uid`=' . $id);
         return $this->db->fetch_array($result);       
     }
}


Posted: Tue Mar 21, 2006 6:42 am
by glossy
that's exactly what i've wrote in :) :

Code: Select all

class globalsettings {
  // settings
  public $gzip = true;
  private $db = NULL;
   
  // constructor
  public function globalsettings(&$db) {
    $this->db = $db;
  }
error checking shouldn't be necessary, because if the database connection fails, or if the database object is fucked anyway, it'll throw a critical error anyway (since the whole application is database-orientated it won't work at all without)

works a treat... thanks again!

Posted: Tue Mar 21, 2006 7:04 am
by mjrpes
cool.

I've gotten into the habit of putting in error checking in all sort of places in my code as a way to uphold my own internal model of how the program should work. i've found this helps me greatly months later when i'm debugging and i've forgetten how i set everything up.

in the above example, i'd put a check in the constructor that would make sure i wasn't passing the class a null db. this could come back to haunt me later when i get mysterious database error messages and can't figure out why. but if i put a two line piece of code in the constructor,

Code: Select all

if (isset($db) == false)
   die ("YOU DIDN'T PASS A VALID DB OBJECT TO THE GLOBAL SETTINGS CLASS YOU FUCKING IDIOT");
then it'll be clear where exactly the problem lies. perhaps this isn't the best example, but more and more often (especially with php and it's lack of an active debugger) i've been sprinkling these vulgar die messages in my code as a way to make sure the logic that's flowing in my head while writing code will be followed ten months down the line when i've forgotten everything.

EDIT: actually, the db reference wouldn't work if you set it after the constructor call because there is no valid reference to begin with. duh :D

Posted: Tue Mar 21, 2006 7:16 am
by dmmh
mjrpes wrote:

Code: Select all

if (isset($db) == false)
   die ("YOU DIDN'T PASS A VALID DB OBJECT TO THE GLOBAL SETTINGS CLASS YOU FUCKING IDIOT");
ROFL

Posted: Tue Mar 21, 2006 7:16 am
by dmmh
btw, thx for teh help people, will tinkle about it 2day :)