Code Share - Page 5
Page 5 of 8 FirstFirst ... 34567 ... LastLast
Results 61 to 75 of 109

Thread: Code Share

  1. #61
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Running Gear"
    Posts
    22,573

    Roman Numerals

    The algorithm smells like Knuth to me. Anyway, roman_numeral(1997) returns mcmxcvii.

    PHP Code:
    function roman_numeral($n)
    {
        
    settype($n'integer');
        
    $ret '';
        
    $j 0;
        
    $str='mdclxvi';
        
    $v 1000;

        for(;;)
        {
            while(
    $n>=$v)
            {
                
    $ret.=$str{$j};
                
    $n -= $v;
            }
            if(
    $n<=0) return $ret;

            
    $u intval($v/((($k=$j)&1)?5:2));
            if(!(
    $k&1))
            {
                
    $u intval($u/(((++$k)&1)?5:2));
            }

            if(
    $n+$u>=$v)
            {
                
    $ret .= $str{$k+1};
                
    $n += $u;
            }
            else
            {
                
    $v intval($v/(((++$j)&1)?2:5));
            }
        }

    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  2. #62
    Web Interface Architect
    Join Date
    Jan 2002
    Location
    Central Texas
    Posts
    989

    How to generate a list of "missing checks"

    Here is something that might be helpful to someone and it's dirt simple to implement. Often you want to do a report of "missing numbers" in a sequence, like missing checks. I'm not going to explain too much on this because the coding is very obvious once you see the trick involved, which is a LEFT JOIN of the table to itself on either the next greatest check number (provides a maximum if the join is null) or the next lesser check number (provides minimum if join is null). the SQL table creation and data is as follows:
    Code:
    CREATE TABLE `_breaks` (
      `ID` int(9) unsigned NOT NULL auto_increment,
      `checknumber` int(9) unsigned NOT NULL default '0',
      PRIMARY KEY  (`ID`)
    ) TYPE=MyISAM AUTO_INCREMENT=12 ;
    #
    # Dumping data for table `_breaks`
    #
    INSERT INTO `_breaks` VALUES (1, 1000);
    INSERT INTO `_breaks` VALUES (2, 1001);
    INSERT INTO `_breaks` VALUES (3, 1007);
    INSERT INTO `_breaks` VALUES (4, 1008);
    INSERT INTO `_breaks` VALUES (5, 1009);
    INSERT INTO `_breaks` VALUES (6, 1010);
    INSERT INTO `_breaks` VALUES (7, 1011);
    INSERT INTO `_breaks` VALUES (8, 1012);
    INSERT INTO `_breaks` VALUES (9, 1014);
    INSERT INTO `_breaks` VALUES (10, 1020);
    INSERT INTO `_breaks` VALUES (11, 1021);
    Be sure and index your checknumber field!!!!


    Next we query the table twice as follows:
    Code:
    /** Range Minimums **/
    SELECT a.checknumber
    FROM _breaks a
    LEFT  JOIN _breaks b ON
    a.checknumber = b.checknumber +1
    WHERE b.checknumber IS NULL
    
    /** Range Maximums **/
    SELECT a.checknumber
    FROM _breaks a
    LEFT  JOIN _breaks b ON
    a.checknumber = b.checknumber -1
    WHERE b.checknumber IS NULL
    I'll skip the php-mysql scripting to contact the table, you can figure that out, but you'll need to get the minimums and maximums as two arrays as follows:

    PHP Code:
    $minimums= array(1000,1007,1014,1020);
    $maximums= array(1001,1012,1014,1021); 
    Now here's the simple code to put the contiguous sequences into an array, and an example of its use:

    PHP Code:
    foreach($minimums as $v){
        
    $i++;
        
    $ranges[$i]['min']=$v;
        do{
            
    //nothing needed here
        
    }while( (list(,$w)=each($maximums))<$v);
        
    $ranges[$i]['max']=$w;
    }
    echo 
    'Contiguous Sequences:<br>';
    foreach(
    $ranges as $n=>$v){
        echo (
    $v['min']==$v['max']) ? 'Check '.$v['min'] : 'Checks '.$v['min'].'-'.$v['max'];
        echo 
    '<br>';

    The end result:

    Contiguous Sequences:
    Checks 1000-1001
    Checks 1007-1012
    Check 1014
    Checks 1020-1021


    That's it! So if your client ever wants a report on a range of contiguous numbers like a missing check report, this will be helpful.

    Samuel
    Last edited by sfullman; 03-19-2006 at 01:28 PM.
    Hey! You may have inadvertently stumbled onto some genius there! :-)

  3. #63
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Running Gear"
    Posts
    22,573
    Okay, so num2name() converts the decimal representation of a natural number into its English representation. But there are two types of natural numbers. There are the cardinals (which are used for counting how many there are of something), and there are the ordinals (which are used for ordering). The fact is that English distinguishes between the two.

    The cardinals run "zero", "one", "two", "three", and so forth. The ordinals are called (in order!) "first", "second", "third", "forth", etc. Note that in conventional English there is no ordinal corresponding to "zero". I need only mention zero-indexed arrays and you'll recognise some of the issues this difference can cause. There is a need for "zeroth" and so this function supplies it. (Plus, it's the obvious return value when 0 is passed in.

    For convenience this function takes the same input as num2name().
    PHP Code:
    num2ordinal($number)
    {
        
    $cardinal num2name($number);
    $replacements = array(
        
    'one'=>'first',
        
    'two'=>'second',
        
    'three'=>'third',
        
    'five'=>'fifth',
        
    'nine'=>'ninth',
        
    'twelve'=>'twelfth',
        
    'y'=>'ieth');
        foreach(
    $replacements as $card=>$ord)
        {
            
    $len=-strlen($card);
            if(
    substr($cardinal,$len)==$card)
                return 
    substr($cardinal,0,$len).$ord;
        }
        return 
    $cardinal.'th';

    Last edited by Weedpacket; 01-03-2007 at 05:47 AM.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  4. #64
    Member of the Year phpshift's Avatar
    Join Date
    Jun 2006
    Posts
    75
    I am putting together a website that is supposed to have a large collection of free PHP scripts. If you want to, you can post any scripts you want there along with any copyright or contact information you want to go with it. I would greatly appreciate it. The website is here:

    http://phpshift.fireproductions.co.uk

    It gets a good amount of traffic, so if you want people to see your codes, it's a good place to put it.

  5. #65
    Copyleft Commie bubblenut's Avatar
    Join Date
    Oct 2003
    Location
    London
    Posts
    2,318
    Hey, I've not seen this thread in ages. I've just been taking a look back through and saw my rather ropey PHP4 static property implementation. PHP4 does not have static class properties but it does have static function properties so we can use that instead, and the best bit is, it's painfully simple. The majority of the code is just a convaluted test.
    PHP Code:
    <?php

    class MyTest {
        var
            
    $dynamic,
            
    $static;
        function 
    MyTest() {
            static 
    $static;
            
    $this->static =& $static;
        }
    }

    $obj1 = new MyTest;
    $obj2 = new MyTest;

    $obj1->static 'Blah';
    $obj1->dynamic'Blah';
    echo <<<END_TEXT
    \$obj1->static:  {$obj1->static}
    \$obj1->dynamic: 
    {$obj1->dynamic}
    \$obj2->static:  
    {$obj2->static}
    \$obj2->dynamic: 
    {$obj2->dynamic}


    END_TEXT;

    $obj2->static 'Whoa!';
    $obj2->dynamic'Whoa!';
    echo <<<END_TEXT
    \$obj1->static:  {$obj1->static}
    \$obj1->dynamic: 
    {$obj1->dynamic}
    \$obj2->static:  
    {$obj2->static}
    \$obj2->dynamic: 
    {$obj2->dynamic}

    END_TEXT;
    If at first it doesn't work, slap it with a dirty hack.

    Moral of the week: Never let a moral of the week go on for more than a week, it's even sillier than feeding the admins.

    My Blog

  6. #66
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Running Gear"
    Posts
    22,573
    Quote Originally Posted by phpshift
    It gets a good amount of traffic, so if you want people to see your codes, it's a good place to put it.
    Why not post it here? This site gets a good amount of traffic, too. Or was this just an excuse to pimp your site again?
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  7. #67
    Member of the Year phpshift's Avatar
    Join Date
    Jun 2006
    Posts
    75
    Quote Originally Posted by Weedpacket
    Why not post it here? This site gets a good amount of traffic, too. Or was this just an excuse to pimp your site again?
    Well, I'm sure these forum posts are eventually deleted. If you put it on my site, it'll be there permanently.

  8. #68
    Construction zone
    Join Date
    Apr 2004
    Posts
    1,833
    No, you will be remembered forever here now. But, what's with the design on your site...ouch.
    Let 'x' make a statement...

  9. #69
    Member of the Year phpshift's Avatar
    Join Date
    Jun 2006
    Posts
    75
    A lot of the people here need to cool down a bit....

  10. #70
    Not Yet Involved Shrike's Avatar
    Join Date
    Oct 2003
    Location
    The Eighth, Sursamen
    Posts
    2,278
    Awww it reminds me of 1996, HTML to match too

    I think whatever code your using to display your PHP is escaping quotes where it shouldn't too - see the password editor one.
    The Hundredth Idiot

  11. #71
    PHP Witch laserlight's Avatar
    Join Date
    Apr 2003
    Location
    Singapore
    Posts
    13,918
    Well, take for example your 'Random 10-Digit Password Creator'. It screams "written by a newbie!" at me. bubblenut and myself have already given you suggestions to improve it, but these suggestions have been ignored.

    The thing is, your website lacks good content, and the poor design doesnt help to alleviate that. It is far from the 'supposed to have a large collection of free PHP scripts' goal, and doesnt look worth contributing to since what it does have isnt worth using. This is turn makes the 'gets a good amount of traffic' sound rather dubious, even if it is true.

    My suggestion is to stay around here and help out a little more. By helping out, you learn. By reading the suggestions made to help others, you improve too. You should also seek help in your existing scripts, get them improved and well formatted, and have more scripts in your current collection.
    Use Bazaar for your version control system
    Read the PHP Spellbook
    Learn How To Ask Questions The Smart Way

  12. #72
    Member of the Year phpshift's Avatar
    Join Date
    Jun 2006
    Posts
    75
    If a script works, why do you care how it's written?

  13. #73
    PHP Witch laserlight's Avatar
    Join Date
    Apr 2003
    Location
    Singapore
    Posts
    13,918
    If a script works, why do you care how it's written?
    Well written (and documented) scripts are easier to understand, optimise and maintain.
    Use Bazaar for your version control system
    Read the PHP Spellbook
    Learn How To Ask Questions The Smart Way

  14. #74
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Running Gear"
    Posts
    22,573
    Quote Originally Posted by phpstuff
    Well, I'm sure these forum posts are eventually deleted. If you put it on my site, it'll be there permanently.
    Well, whether this site (and hence the posts on it) will outlive yours is of course one matter. But you're right about one thing. Posts that are in violation of these boards' Acceptable Use Policy (especially those that consist of advertising) do get deleted.
    If a script works, why do you care how it's written?
    If you didn't care how it was written, why did you post in the Code Critique forum and ask for comments? Or was it just so that you could boost your page ranking with another link here? Your motives are in question.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  15. #75
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Running Gear"
    Posts
    22,573

    Rolling an array's elements without a loop

    Roll an array $array[$first, ..., $mid, ..., $last] so that $mid is the first element. I.e., $array[$mid, ..., $last, $first, ...].

    PHP Code:
    $array array_reverse(array_merge(
                    
    array_reverse(array_slice($array0$mid)),
                    
    array_reverse(array_slice($array$mid))
            )); 
    EDIT: Should have given the description. Oops. Basically, take the first $mid elements of the array and reverse them. Take the rest of the array and reverse that. Stick the two chunks back together, then reverse the whole thing. (Missy Elliot helped me with this algorithm.)

    Rolling [a,b,c,d,e,f,g,h,i] left (did I mention it was left?) by five places:
    Code:
    [a,b,c,d,e,f,g,h,i]
    [a,b,c,d,e][f,g,h,i]  slice
    [e,d,c,b,a][i,h,g,f]  reverse
    [e,d,c,b,a,i,h,g,f]   merge
    [f,g,h,i,a,b,c,d,e]   reverse
    Last edited by Weedpacket; 05-25-2008 at 06:56 AM. Reason: tuypo
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •