php help :S

Open discussion about any topic, as long as you abide by the rules of course!
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

php help :S

Post 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
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
User avatar
Foo
Posts: 13840
Joined: Thu Aug 03, 2000 7:00 am
Location: New Zealand

Post by Foo »

Link goes to blank page.
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post by dmmh »

no it doesnt :)
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
User avatar
Foo
Posts: 13840
Joined: Thu Aug 03, 2000 7:00 am
Location: New Zealand

Post 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.
Freakaloin
Posts: 10620
Joined: Tue May 07, 2002 7:00 am

Post by Freakaloin »

lol...smart ppl r stoopid...
a defining attribute of a government is that it has a monopoly on the legitimate exercise of violence...
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post 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 :(
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post by dmmh »

fuck it, to tired :D
will try to sort it later
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
Sevensins
Posts: 444
Joined: Tue Jul 17, 2001 7:00 am

Post 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...
Ryoki
Posts: 13460
Joined: Wed Aug 01, 2001 7:00 am

Post by Ryoki »

Freakaloin wrote:lol...smart ppl r stoopid...
haha
glossy
Posts: 2285
Joined: Tue Apr 30, 2002 7:00 am

Post by glossy »

wow, i come back to this? gimmie a sec, i'll give it a shot
glossy
Posts: 2285
Joined: Tue Apr 30, 2002 7:00 am

Post 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.
4days
Posts: 5465
Joined: Tue Apr 16, 2002 7:00 am

Post by 4days »

and then stick the functions in a class..

there are a bunch of ready-rolled ones, or tutorials for pagination classes.
glossy
Posts: 2285
Joined: Tue Apr 30, 2002 7:00 am

Post 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.
JulesWinnfield
Posts: 625
Joined: Tue Feb 08, 2005 12:09 pm

Post 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:
User avatar
Foo
Posts: 13840
Joined: Thu Aug 03, 2000 7:00 am
Location: New Zealand

Post 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?
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post by dmmh »

OOP :S
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
glossy
Posts: 2285
Joined: Tue Apr 30, 2002 7:00 am

Post 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.
glossy
Posts: 2285
Joined: Tue Apr 30, 2002 7:00 am

Post 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.
mjrpes
Posts: 4980
Joined: Tue Nov 28, 2000 8:00 am

Post 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));
glossy
Posts: 2285
Joined: Tue Apr 30, 2002 7:00 am

Post by glossy »

oh really! news to me, i will try it out.

thankyou mister wizard !
mjrpes
Posts: 4980
Joined: Tue Nov 28, 2000 8:00 am

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

glossy
Posts: 2285
Joined: Tue Apr 30, 2002 7:00 am

Post 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!
mjrpes
Posts: 4980
Joined: Tue Nov 28, 2000 8:00 am

Post 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
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post 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
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post by dmmh »

btw, thx for teh help people, will tinkle about it 2day :)
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
Post Reply