Jump to content

Grayda

Members
  • Posts

    59
  • Joined

  • Last visited

Posts posted by Grayda

  1. When I need to compare times / dates, I use strtotime(). Basically you enter in a date and it returns it as a UNIX timestamp (a long number representing the number of seconds since midnight, January 1st, 1970). You then do a time() and subtract one from the other. For example:

     

    
    function isCheckedOut($startDate, $endDate) {
    
    // Get our times as a timestamp
    $checkOut = strtotime($startDate);
    $checkIn = strtotime($endDate);
    // Returns the current time as a timestamp
    $now = time();
    
    // Now, we do some comparisons. If the time now is later than the checkout date, but earlier than the checkin time, then there's a conflict
    if($now >= $checkOut and $now <= $checkIn) { echo "This item is already booked out!"; } else { echo "Your booking was confirmed!"; }
    
    }

     

    And to use it:

     

    isCheckedOut("2008-08-26", "2008-08-29");
    

     

    The neat thing with strtotime is, you can give it plain English sentences, such as:

     

    echo strtotime("Next Thursday"); // Outputs a timestamp that represents next Thursday
    echo strtotime("2nd January, 2009");
    echo strtotime("5 hours ago");
    echo strtotime("Last Thursday next month"); // Outputs a timestamp that represents Next month, calculated from last Thursday
    

     

    (I think the above examples are correct. Correct me if I'm wrong folks ;))

     

    But I'd be careful about using plain English with isCheckedOut, because you could potentially let people set "Next Thursday" as a date, and that day will never come (strtotime, unless told, uses the current time as it's point of reference)

     

    Another solution would be to have a second database that stores all the items that are currently on loan. In semi-pseudocode:

     

    function checkOut($item) {

     

    // When this code is run, $item (whether it's an ID number, item name, whatever) is copied to a table in your database called "onLoan". onLoan holds nothing but some basic information about the item, such as who loaned it, when they loaned it, the due date, the item number etc.

     

    }

     

    function isCheckedOut($item) {

     

    // When this code is run, the code looks in onLoan for $item (eg. SELECT borrower FROM onLoan WHERE itemNum = '$item' ) and if it's found, assume it's on loan

     

    }

     

    This has the advantage of being easier to keep track of loans when you're browsing the database manually, plus separates your item description from your item loan. However, I think both are acceptable answers ;)

     

    Hope this helps :)

  2. Thanks for the help ignace, but I'm still a little confused. Can I have:

     

    <?php
    
    // This is in base.php which is a file on my web-server
    abstract class Person {
    abstract public function getRealName($username);
    }
    
    ?>
    

     

    and

     

    <?php
    
    // This is code that a user copies and pastes into the online editor. This page is stored within a DATABASE so it needs to be eval()'d when it's grabbed from the database and the user is authenticated
    
    class Person {
    
    function getRealName($username) {
    echo "Hi, your real name is: " . $someclass->somefunction;
    }
    
    }
    
    ?>
    

     

    or do I still need to extend classes? Also, what about a default implementation? Could I have something like (and remember that this is pseudo-code and I don't know if this can or cannot be done):

     

    <?php
    
    // This goes into base.php which is located in a directory on the webserver
    
    abstract class Person {
    abstract public function getRealName($username) {
    echo "Hi, your real name is: " . $someOtherClass->someOtherFunction($username);
    }
    }
    
    ?>

     

    and:

     

    <?php
    
    // This is what gets copied and pasted into the database, so it must be eval()'d
    class Person {
    
    function getRealName($username) {
    return $someOtherClass->someOtherFunction($username);
    }
    
    }
    
    ?>
    

     

    So that way if no overwriting is done, it will use the default method, but if one is specified, it'll use that?

     

    I've seen Runkit for PHP (http://au2.php.net/manual/en/intro.runkit.php), which lets you do this, but it also provides extra stuff which I might not need, which is why I wondered if there was a native way to do this in PHP5

     

    I hope this makes sense, and thanks for taking the time to help out :)

  3. Not bad for a first website, but I'm not a fan of the bright colours, the font or the navigation ;)

     

    The colour needs to be toned down a little, especially for myself, who has an eye condition where any bright lights make a trail of light appear under the bright light, and that hangs over the text a little. I suggest a nice dark-ish shade of blue to go with the surfing theme you have going. Even a simple gradient from a lighter blue to a darker blue, set as the background, would look nice:

     

    body {
      background: url(blue_grad.jpg) repeat-x #31475B;
    }
    

     

    And have the gradient start off as #4B8DB1 (R: 75, G: 14, B: 17) then fade it down to #31475B (R: 49 G: 71 B: 91) over say, 300 pixels

     

    As for the font, I suggest a widely used font. I don't think Snap ITC is a widely used font on the internet, so perhaps Verdana, Helvetica or something would look good? It's easy on the eye too :D

     

    And finally the navigation. It might be good to have a plain navigation, or get rid of the "bend" in the text. Oh, and it could use a text-only navigation at the bottom too, for those who don't want to scroll all the way up, or for those using a screenreader. It all comes down to readability and user comfort I reckon ;)

     

    But, for your first site, it's coming along nicely, and good luck with CSS. It's tricky at first, but now, I can barely breathe without it ;)

  4. In my latest PHP project, I have a few classes that perform various things, for example, authenticating users, pulling information from databases and so forth. One of the big points of my project is allowing trusted users to eval() code, so they can integrate existing PHP code into their website. What I would like, is for trusted users to be able to "overwrite" parts of the code at run-time. For example, if this is my security class:

     

    <?php class Security {
    
    ...
    
    function authenticateUser($user, $password) {
        // Code to authenticate user goes here
    }
    
    ...
    
    } ?>

     

    I would like users to be able to do write something like this:

     

    <?php 
    
    class Security {
    
    ...
    
    function authenticateUsers($user, $password) {
       // A different authentication scheme in here
    }
    
    ...
    
    } ?>

     

    Much like "class NewSecurity extends Security", but instead of creating an extended class, merely re-defining / re-using existing methods in an existing class

     

    I suppose this is more of a personal curiosity thing rather than a serious idea for my application (although, releasing patches without editing real files on the server, has it's advantages ;)), but the question is, can it be done?

  5. Hi Jsus, the %20's are added by the web browser, not by the server. You can use urldecode() to remove the %20 and any other codes that come from people having accents and other bits in their name:

     

    
    $username = urldecode($_GET["UsernameFromURL"];
    // This will return USER NAME rather than USER%20NAME
    
    

     

    You can also force Boonex to convert spaces to underscores which would be better for applications and such, but a little tricker to implement I would think:

     

    createUsername("USER", "NAME");
    
    // ...
    
    function createUsername($firstname, $lastname) {
    
    $username = $firstname . "_" . $lastname;
    addUserToDatabase($username);
    echo "Thankyou for registering. You can now log in via www.domain.com/" . $username
    

     

    But urldecode and urlencode would help you sort out people with non-standard characters in their username / realname.

     

    Hope this helps :)

  6. I found a solution. Turns out I needed to use preg_match_all to grab all of the results, because preg_match only matches the first one and stops looking. This, however, throws in ALL the results:

     

     

    Array

            (

                [0] => {{header}}

                [1] => {{fact}}

                [2] => {{fact}}

                [3] => {{footer}}

                [4] => header

                [5] => fact

                [6] => fact

                [7] => footer

            )

     

    So I simply loop through the array and unset the first half of the array

     

    My code now looks like this (and is rather messy. I hope there's a quicker, cleaner way to do this):

     

    // First, we extract the text between the {{ and }} and return it as $a
    preg_match_all("|{{(.*)}}|", $retPage, $a);
    // Then we loop through the array, divided by two, and unset the first half of the array
    for ($i = 0; $i < count($a) / 2; $i++) {
    unset($a[$i]);
    }
    
    // Then we loop through again, this time exploding the arguments given to the temmplate, ignoring any errors
    for ($i = 0; $i < count($a[1]); $i++) {
    @$args = explode("|", $a[1][$i]); 
    // Next, we set up a sessions variable called "pagename_args" which contains our arguments for the template to parse 
    $_SESSION[$args[0] . "_args"] = $args;
    // Now we grab the page from the database and store it in $n
    $n = $this->getPage($args[0]);
    // Remove the template tag from the page, and replace it with our page name
    $retPage = preg_replace("|{{(.*)}}|", $n, $retPage);
    // And finally, we remove the $_SESSION variable we made, since the page no longer needs it
    $_SESSION[$args[0] . "_args"] = array();
    }
    

     

    I highly doubt this is the right way, or the fast way, to do this, but for now it works. I guess my regexes need a little work, too ;)

     

    I hope this helps someone out, because it's being doing my head in for a while  :P

  7. My latest big project is a website engine that lets people manage their entire website through their browser. It's aimed at website developers (myself) and people who don't know too much about HTML and PHP and stuff (the client). One idea I'm implementing is "templates". For example, if a user wanted to show the page called "hello", they would simply type {{hello}} and it would be included, very much like Wikipedia's {{stub}} templates and so forth.

     

    I would also like to pass arguments to the "template", so a user could type {{weather|New York}} and have the weather page generate some HTML that shows the weather in New York.

     

    I got this working after much fiddling, by:

    $t = preg_match("/\{\{(I.*)\}\}/", $thePage, $args);

    -ing the page pulled from the database then splitting the results from $args by the pipe delimiter. This worked great for several different templates on the one page, but if I wanted to re-use one template several times, for example to automate complex tasks, then it would silently fail, showing the first template, but blank spaces for the remaining templates. So for example, if I have 3 templates defined: header (the navigation for the website), fact (a [Citation Needed] template) and footer (for copyright info) and my code looks like this:

     

    {{header}}
    This statement is a lie{{fact}}
    While this statement is correct{{fact}}
    {{footer}}
    

     

    One would expect to see:

     

    Home | About | Contact

     

    This statement is a lie[Citation Needed]

    While this statement is correct[Citation Needed]

     

    ©2008 MyCompany

     

    But instead we get:

     

    Home | About | Contact

     

    This statement is a lie[Citation Needed]

    While this statement is correct

     

     

    ©2008 MyCompany

     

    Note the missing [Citation Needed] after the second statement, plus the extra whitespace?

     

    I've tried lots of ways to get this working, from preg_match_all (Just returns the whole page, minus the first {{ and last }}), preg_replace (But doesn't let me pass the page name to a custom function) and even preg_replace_callback which did gave me the error

    preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, 'doFix', to be a valid callback in <script filename> on line 71
    despite the function doFix existing, being a valid function, returning the right values etc.

     

    Am I missing something? Can I pass what's between the {{ and }} to my custom function, $this->getPage($thePage); ? I'm slowly starting to get the hang of regular expressions, but it's just so difficult to read sometimes..

     

    Any help is much appreciated :)

  8. As a part of my job, I'm required to build database-driven applications for collecting and maintaining hardware. So far things have been going well. Until I met unset() and loops. Take this sample code:

     

    <?php
    $arr = array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => ;
    
    foreach($arr as $val) {
    unset($val);
    }
    
    echo var_dump($arr);
    
    ?>

     

    Looking over it, it seems pretty easy. 8 items in an array. the foreach loop goes through the array, unsetting each item. What should we expect to see? To me, I expect to see a blank array. But instead, $arr remains untouched and var_dumping $arr produces:

     

    array( { [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(4) [5]=> int(5) [6]=> int(6) [7]=> int(7) [8]=> int( } 

     

    Am I missing something here? This happens with any sort of loop I try (foreach, for, do etc.) and with any sort of data I throw at it. The loop runs 8 times (stick an $i++; and an echo and watch it go), but refuses to unset the variable. I doubt this is a scope issue as there are no functions / classes / whatever to get in the way.

     

    Cheers :)

×
×
  • 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.