Jump to content

Anthop

Members
  • Posts

    14
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

Anthop's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Thanks for the reply, premiso, and sorry for my own late reply. The echo __CLASS__ works fine when its within the code for the class I'm trying to discover, but it isn't really the general solution that I'm looking for. For example, see the following code: interface Classable { public function getClass(); } class Test implements Classable { public function getClass() { return __CLASS__; } } class SubTest extends Test {} $o = new Test(); echo($o->getClass() . "\n"); // Returns "Test". $o = new SubTest(); echo($o->getClass() . "\n"); // Returns "Test". First of all, it limits me to finding the class of objects which implement the Classable interface. Secondly, I can't just define the function once and cause it to work correctly for all subsequent sub-classes, as the function returns the name of the class in which the function was last defined. That means, I have to trust that every sub-class has a correct implementation of the Classable interface. I'll think about this a bit more, but for now, I'm going to assume that this is just how it is going to be.
  2. While I'm familiar with object-oriented programing, in Java and other languages, I'm not sure how to do this simple task in PHP: If I have an instance of some object, how do I find out what its class is (perhaps returned to me as a string)? Also, I know PHP isn't strongly typed, but is there some way to "sanity check" in my code that an object is an instance of class X or a subclass of X? Thanks in advance for your time .
  3. @ Barand: Wouldn't that just create a ton of rows, one for each post, with duplicate entries for each thread and each forum?
  4. I have a particular query that I'm wondering will work as I hope. Could a helpful forum-goer take a look, please? Let's say I'm trying to implement a forum, and I have a Forum table like so: ┌─────────────┐ │ Forum │ │-------------│ │ ForumID │ │ Title │ │ Description │ └─────────────┘ And a Thread table like so: ┌─────────────┐ │ Thread │ │-------------│ │ ThreadID │ │ ForumID │ │ Title │ └─────────────┘ And finally a Post table like so: ┌─────────────┐ │ Post │ │-------------│ │ PostID │ │ ThreadID │ │ User │ │ DatePosted │ └─────────────┘ With the obvious foreign-key relationships. Now, I need a query that displays the threads in a particular forum reverse-sorted by date of a thread's most recent post. The best way to do this would be if the rows returned also contained the columns from the post table for that most recent post. I've gotten this far, but I'm not sure if the following query does what I want it to do. Is there any guarantee that the following query will correctly select the most recent post in association with a distinct thread or just any post from that thread? Also, since this query returns columns with the same name, will referencing that column in the PHP associative array have different or special behavior? SELECT Thread.*, Post.*, DISTINCT Thread.ThreadID FROM Thread INNER JOIN Post ON Thread.ThreadID = Post.PostID WHERE Thread.ForumID='somegivenforumid' ORDER BY Post.DatePosted DESC If it matters any, I'm using MySQL 4. Any help would be greatly appreciated! Thank you beforehand .
  5. Thank you! I certainly feel a bit silly now for not knowing that. I still don't quite understand why those particular values would be set at all. (By dereferencing $variable1, I was basically trying to store the value to a variable specified by the value of variable1.... But $variable1 itself was never set, so you'd think I would be trying to store to $this->null or something, and nothing would happen.... ???) Mysterious indeed. But in any case, thanks for clearing that up! ... Now to go back and change all the instance variable references in my code ....
  6. @ Daniel: Didn't know that! There are some nice things about PHP ! My background is in desktop programming languages, especially strict, strongly typed ones like Java and C#, and in those cases, closing your connections is an iron rule of sorts. (Not as bad as deallocating memory in C, but still pretty strict.) So, I'm just very used to closing things when I'm done with it and making sure not to use it again. (I suppose I might get a very slight performance increase for it though?) In any case, PHP is very different in a lot of ways, so it's been throwing me for a loop .
  7. deadimp's suggestion seems very promising. Also, I'm not sure, but I think to correctly call a static function, C::foo() in this case, you would have to actually use: call_user_func(array($class, "foo"));
  8. I'm not exactly sure how these boat races work, but if I'm understanding this correctly, it might actually do you more good to have a "Results" or "Points" table. In it, you would store the number of points that a skipper got on a particular race. It would look something like this: |------------| | Results | |------------| | ResultsID | | SkipperID | | RacePoints | |------------| One skipper would probably have several entries in the Results table. You could then total up the points to give a cumulative score. This would require a slightly fancier front-end, but would give you the added benefit of seeing individual results as well as a cumulative score. Also, this really doesn't make that much of a difference, I guess, but I would really use ints instead of varchars to store my primary and foreign keys....
  9. I agree with thorpe. I would probably just do it functionally and return a connection pointer. If you intend to add more functions, you still don't need to make a class; you could simply just have a bunch of functions in connect.php file to help you manage and mess with your connection. Otherwise, there's really no problem with how you're doing it. In fact, if you define the __destruct() method to close your connection automagically, then this may actually be better! (ie: Add the following function to your class:) __destruct() { mysql_close($this->connection); }
  10. You could also serialize the page object and store the object itself into a database and unserialize when you want to use it on another page.
  11. I'm not even sure what to call this problem as I seem to be doing everything correctly (though, of course, I'm still very new at this). I wrote a very simple test script to see what I could do with classes in PHP. The wrong values seem to be stored in the instance variables. Incidentally, I'm using PHP 4 (ugh >P). Here is the code: <?php class Constructor { var $variable1; var $variable2; function Constructor() { $num_args = func_num_args(); $arg_list = func_get_args(); if($num_args >= 1) {$this->$variable1 = $arg_list[0];} if($num_args >= 2) {$this->$variable2 = $arg_list[1];} else {$this->$variable2 = 10;} } function CreateNewStatically() {return new Constructor(2, 9);} function display() { echo $this->$variable1; echo ", "; echo $this->$variable2; } } $p1 = new Constructor(1); $p1->display(); echo("<br />"); $p2 = Constructor::CreateNewStatically(); $p2->display(); echo("<br />"); $p3 = new Constructor(); $p3->display(); echo("<br />"); ?> What I get back is the following: 10, 10 9, 9 10, 10 What I expect to get is: 1, 10 2, 9 , I tested it a bit and I found that the code is being traversed correctly and that there seems to be no problem with the variable number of variables to the constructor. I'm at a complete loss as to why I'm getting these values. I'm guess that this has to do with some facet of PHP that I don't know about. I would be grateful for any help! Thank you .
  12. O_O.... It worked!  Wow.  So I've learned that you have to set all the values of a cookie and not use any whitespace to make a cookie work in Firefox <_<;;. Anyway, thanks for your help, btherl and corbin :D.
  13. Though I've done a lot of desktop development, I'm new to PHP and to web-development in general, so please treat me kindly :D.  In any case, I have a login script that won't set a cookie.  Attempting to set the cookie returns a true on success (see [b]$success[/b]), so I don't think it's a header issue.  However, checking [b]isset($_COOKIE['User ID'])[/b] and just refreshing the page to see if I'm still logged on shows that there is no cookie to be found.  (I also checked my brower's list of cookies to no avail -_-;.) In any case, here is my code.  I've deleted the non-cookie parts for quicker browsing (My code isn't really this sparse XD).  The cookie isn't set whether I run index.php or login.php directly. This is index.php. [code]<?php ob_start() ?> <html> <head> </head> <body> <?php // Here is a form for other input.?> <br /><br /> <?php include 'login.php'; ob_flush(); ?> </body> </html> [/code] This is login.php. [code]<?php // If user is logging out, remove cookie. if ($_POST['logout']) { setcookie("User ID", "", mktime(12,0,0,1,1,1990)); } // If information submitted, and no cookie is found, check login information and set cookie. if ($_POST['submitted'] && !isset($_COOKIE['User ID'])) { // Connect to the User database using a public account. // Search to see if user name matches, and fetch password. // If password matches that given, set a cookie which expires in 12 hours. if($password == $row['Password']) { $success = setcookie('User ID', $row['User ID'], time()+43200, '/', '.light-within.org', 0); $new_session = 1; } } ?> <html> <head> </head> <body> <?php // If not already logged in, if (!$_POST['submitted'] && !isset($_COOKIE['User ID'])) { // Display the Login Form (the target is PHP_SELF) // Otherwise, display welcome message. }else{ // If this is a new login, display welcome message. if($new_session) { // If there is a cookie, retrieve user information. } else if (isset($_COOKIE['User ID'])) { // Otherwise, return invalid login message. } else { echo("<br><p>Screen Name and Password combination not valid.</p>"); } // This checks to see if there is a cookie installed.  Success returns true,  but isset returns false. if(!$success || !isset($_COOKIE['User ID'])) { echo('NO COOKIE SET!! <br>'); } // Close database connection. } ?> </body> </html> [/code]
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.