Code Share
Page 1 of 8 123 ... LastLast
Results 1 to 15 of 109

Thread: Code Share

  1. #1
    2($infinity) && $beyond BuzzLY's Avatar
    Join Date
    Nov 2002
    Location
    Star Command
    Posts
    2,435

    Code Share & Enjoy

    Ok, it's 4am, and I'm sitting here drinking my Code Red (what an appropriate name, on so many levels!), and I just finished coding a page that displays my tables for a sample application I am writing for a project (you can play with the app here if you want).

    I thought I'd start a little thread where you can share some cool code you've written or found. Doesn't matter if it's pretty or ugly, complex or simple. Feel free to spread the love. I'll start out with my table display app...

    --------------------------------------

    I have seen a few questions in various areas on PHPB about using PHP with the SHOW command, and since I had to use it for my tables, I thought I'd share it (in the true spirit of open source, and because I occasionally like to give back to the PHPB community).

    In a nutshell, I use the SHOW command to create arrays of table attributes, then iterate through the arrays to build tables. I am using it so that I visually see all of my tables, to spot any potential problems I might have with relationships.

    If you'd like to see it in action, click here. There is a link on the page to view the source if you like -- feel free to steal and use any code you see there.

    Anyway... it's time for bed. Please feel free to share your code with the community.

    Good night.
    Last edited by BuzzLY; 09-08-2003 at 05:07 PM.
    New to the board? Check out the guidelines
    | Color Picker | Blogification |
    §§§§§§§§§§§§§§§§§§§§§
    With all its sham, drudgery, and broken dreams, it's still a beautiful world.

  2. #2
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Running Gear"
    Posts
    22,573
    I think the topic should be called
    Share and Enjoy.

    I'll dig around. Pretty sure I'll turn something up...
    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

  3. #3
    Computers can do that? drawmack's Avatar
    Join Date
    Apr 2003
    Location
    Pocono Mtns PA
    Posts
    3,265
    cool I'm currently writting a generic script to place a single integrated forum on a site maybe I'll put that up here when I'm done with it.

  4. #4
    Has a PhD in Horribleness Elizabeth's Avatar
    Join Date
    Nov 2002
    Location
    In a meeting with the Bobs
    Posts
    2,294
    Buzz, I should have called you since I was awake then too LOL.

    Hey, what happened to "Hemorrhoid Man?" (And to prove that it was my hub's idea- he's the only one in the house that can spell "hemorrhoid" correctly.

    -Elizabeth

  5. #5
    2($infinity) && $beyond BuzzLY's Avatar
    Join Date
    Nov 2002
    Location
    Star Command
    Posts
    2,435
    I created a new function called "Prep H." As soon as I did, he disappeared!

    An interesting sidenote on my project, and something that some may not know. When adding the ability to sort on different columns, I discovered an interesting thing about ENUM columns. It seems that when you sort an ENUM column, it sorts your rows according to the options defined in your column.

    For example: Let's say you have a column defined: weekday, which is ENUM('Sun':'Mon':'Tue':'Wed':'Thu':'Fri':'Sat'). In your SELECT query, you specify "ORDER BY weekday ASC". You might think the sort order was "Fri, Mon, Sat, Sun, Thu, Tue, Wed." In fact, it's ordered the same exact way the choices are presented: "Sun, Mon, Tue, Wed, Thu, Fri, Sat." Yes, this behavior is in the manual, but it's not intuitive, and not something you would think to look up.
    Last edited by BuzzLY; 09-02-2003 at 11:30 AM.
    New to the board? Check out the guidelines
    | Color Picker | Blogification |
    §§§§§§§§§§§§§§§§§§§§§
    With all its sham, drudgery, and broken dreams, it's still a beautiful world.

  6. #6
    black sheep with red wool Merve's Avatar
    Join Date
    Jul 2003
    Location
    North of the 49th parallel
    Posts
    2,193
    I have a calculator script...I'll post the code later...

    I made it from scratch and it was pain the in the a** but now it's done...at least I think it's done...maybe I should make it open-source (winking smiley).

    It has a range of different functions, from simple addition to logarithms (but the server I use only has PHP 4.1.2 so with the log() function I couldn't supply the base argument and so there's only natural log and log of 10).

    Check it out here.

    Warning: cosine and tangent take a very long time to load (partly because the PHP cosine and tangent functions are slightly flawed).

    If you like the calculator, I'll post the code and you can edit it and do whatever the hell you want to do with it (including trading it for gummi worms).
    "A proof is a proof. What kind of a proof? It's a proof. A proof is a proof. And when you have a good proof, it's because it's proven." -- Jean Chrťtien

    The Three C's

  7. #7
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Running Gear"
    Posts
    22,573
    Attributed to Solomon W. Golomb; a method for swapping the values of two integer variables without using an intermediate variable (you can tell this dates from the Elder Days, when variables were expensive!). Thanks to PHP's syntax it's also a one-liner.
    PHP Code:
    $a^=$b^=$a^=$b
    Okay, here's how it goes (yeah, like I need to make content-free posts just for the sake of an increment...).

    First, simplify the line; noting that ^= is right-associative, which means that in that line the rightmost operator is evaluated first, that the assignment operators also return the value that they assign to their lvalue, and that foo^=bar is shorthand for foo=foo^bar:
    Code:
    $a^=$b^=$a^=$b;
    
    $a^=($b^=($a^=$b));
    
    $a=$a^b;
    $a^=($b^=$a);
    
    $a=$a^$b;
    $b=$b^$a;
    $a=$a^$b;
    Recall what ^ does. Takes each pair of corresponding bits from its arguments (the internal binary representation of its arguments, that is), and xors them together to produce the corresponding bit of the result ("corresponding" means that the first bits of both arguments produce the first bit of the result, the second bits of both arguments produce the second bit of the result, and so on. This is why, as BuzzLY noted, it's important that both variables are the same size - demons probably start flying out of your nose if one of them runs out of bits to xor before the other. So to figure out what ^ does to a pair of variables, we only need to recap what it does to single bits

    Code:
    x  y  | x^y
    ------+----
    0  0  | 0
    0  1  | 1
    1  0  | 1
    1  1  | 0
    Basically, x^y is true if x and y are different, and is false otherwise. In other words, x is true or y is true, but they're not both true. Hence, "exclusive-or".

    Now, taking those three lines, we see what happens when $a and $b start out with initial values $s and $t.

    Code:
    $a = $s;
    $b = $t;
    $a=$a^$b;
    $b=$b^$a;
    $a=$a^$b;
    
    // Since $b==$t, we substitute $t for $b for as long as $b doesn't change
    $a = $s;
    $a=$a^$t;
    $b=$t^$a;
    $a=$a^$b;
    
    // And likewise for $a
    $a=$s^$t;
    $b=$t^$a;
    $a=$a^$b;
    
    // But don't stop there!
    $b=$t^($s^$t);
    $a=($s^$t)^$b;
    
    $b=$t^($s^$t);
    $a=($s^$t)^($t^($s^$t));
    From the table above, it's obvious that $a^$b=$b^$a (if $a is different from $b, then $b must be different from $a), and that $a^$a=0 ($a is not different from itself). Using those two facts, that the fact that $a^0 = $a and $a^($b^$c) = ($a^$b)^$c (which I leave as exercises for the reader), we can simplify those two rather long expressions.
    Code:
    $b=$t^($s^$t);
    $a=($s^$t)^($t^($s^$t));
    
    $b=$t^($t^$s);
    $a=($t^$s)^($t^($t^$s));
    
    $b=($t^$t)^$s;
    $a=($t^$s)^(($t^$t)^$s);
    
    $b=0^$s;
    $a=($t^$s)^(0^$s);
    
    $b=$s;
    $a=($t^$s)^$s;
    
    $b=$s;
    $a=$t^($s^$s);
    
    $b=$s;
    $a=$t^0;
    
    $b=$s;
    $a=$t;
    So after starting out with $a=$s and $b=$t, we end up with $a=$t and $b=$s. In other words, ^ swapped the each pair of bits of $a and $b with each other. Do that for all the bits and the result is one of $a and $b being swapped.

    And after all that is it any mystery why (a) XOR is such a useful operation in cryptography, and (b) learning a bit of maths (boolean algebra in this case) can be useful in programming?

    Needs pretty colours
    Nah, just shorten it.
    Last edited by Weedpacket; 05-14-2012 at 11:25 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

  8. #8
    2($infinity) && $beyond BuzzLY's Avatar
    Join Date
    Nov 2002
    Location
    Star Command
    Posts
    2,435
    That's actually pretty cool... I've used that before, as you said, in the Elder Era, or as I like to call it, The Days When MS Word was a 20 Floppy Install. The dawn of CD's really was a boon to software distributors, but it sure made programmers sloppy, didn't it? Why tighten up your code if you have plenty of room on the CD?

    It's important to note that your trick really only applies to numbers. It will work with strings, too, but only if the strings are the same length. Of course, you could pad the shorter string to be the same length as the longer one, run it through your code, then trim the end variables, but that kind of defeats the purpose of minimal code. In that case, you might as well use an intermediate variable.

    It might be useful to explain what a bitwise operator is, and explain how XOR works. There are a few here that probably could use the primer. I would, but you are ever so much more eloquent than I when it comes to explaining code Perhaps you could do so by editing your post? Unless you are trying to pad your post count, that is
    Last edited by BuzzLY; 09-02-2003 at 09:53 PM.
    New to the board? Check out the guidelines
    | Color Picker | Blogification |
    §§§§§§§§§§§§§§§§§§§§§
    With all its sham, drudgery, and broken dreams, it's still a beautiful world.

  9. #9
    Better fan than rebelo! Moonglobe's Avatar
    Join Date
    Apr 2003
    Location
    brain://localhost:left-side
    Posts
    2,044
    i'll post my old newb-mistake-laden template class if you guys want it... or are you just looking for good interesting code?
    there's no place i can be, since i found serenity.

  10. #10
    2($infinity) && $beyond BuzzLY's Avatar
    Join Date
    Nov 2002
    Location
    Star Command
    Posts
    2,435
    Anything you think people might find useful or interesting. There are no egos here in this thread. Show off all you want, and we'll all ooh and ahh
    New to the board? Check out the guidelines
    | Color Picker | Blogification |
    §§§§§§§§§§§§§§§§§§§§§
    With all its sham, drudgery, and broken dreams, it's still a beautiful world.

  11. #11
    Missing in Action LordShryku's Avatar
    Join Date
    Aug 2002
    Location
    Occupational Hypnotherapy
    Posts
    7,227
    Well I'd really like to keep this thread going, and while I prolly have nothing in PHP of any real interest (yet), here's a lil Javascript clock...

    Code:
    <body onLoad="clock()">
    <span id="theClock" style="position:absolute;left:300;top:20;"></span>
    <script language="JavaScript">
    function clock() {
    	if (!document.layers && !document.all) return;
    	var digital = new Date();
    	var hours = digital.getHours();
    	var minutes = digital.getMinutes();
    	var seconds = digital.getSeconds();
    	var amOrPm = "AM";
    	if (hours > 11) amOrPm = "PM";
    	if (hours > 12) hours = hours - 12;
    	if (hours == 0) hours = 12;
    	if (minutes <= 9) minutes = "0" + minutes;
    	if (seconds <= 9) seconds = "0" + seconds;
    	dispTime = hours + ":" + minutes + ":" + seconds + " " + amOrPm;
    	if (document.layers) {
    		document.layers.theClock.document.write(dispTime);
    		document.layers.theClock.document.close();
    	}
    	else
    		if (document.all)
    			theClock.innerHTML = dispTime;
    	setTimeout("clock()", 1000);
    }
    </script>
    BTW...I know the feelin Buzz...it's about 1:30 AM right now, and I'm bored and drinking my Mountain Dew

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

    My name is Weedpacket and I am a bÝrker...

    Occasionally one wants to alter some text - except for parts that match some sort of pattern, e.g., URLs. Rather than faff around with regexps that try to figure out whether a certain piece of the text is part of the pattern or not and blahblahblah, it's probably easier to take the bits that shouldn't be changed out of the text, then put them back after the alterations.

    Here's a wee function that does the taking-out part of the job.

    PHP Code:
    function protect_from_borking($find, &$contents, &$placeholders, &$text)
    {    static 
    $n=0;
        
    $count=0;
        while(
    preg_match($find$text$matches))
        {    
    $contents[$count]=$matches[0];
            
    $placeholders[$count]="@@".$n."_".$count."@@";
            
    $text preg_replace($find$placeholders[$count], $text1);
            ++
    $count;
        }
        ++
    $n;

    The name is what it is because I lifted this bodily from my BÝrkinator ][ script, in which it is used to protect various pieces of vBulletin posts of various types from having their vowels bÝrked (e.g., the 'o' and 'a' of &oslash;, or the 'o' and 'e' of [code], or all the vowels within [php] blocks).

    Now, for it to be used, note that four parameters ought to be supplied:
    • $find contains a regexp pattern that matches the sort of text one wants to protect. If, for example, you don't want to touch anything between <b>...</b> tags, the pattern would be #<b>.*?</b>#. Note that the entire match is stored, so in this example, the b tags themselves would be protected.
    • &$contents is a reference to an array that should be empty when the function is called. After the function is completed, it will contain all the protected text that has been taken out.
    • &$placeholders is also initially empty, and afterwards contains copies of all the text that the corresponding entries in $contents have been replaced by.
    • &$text is of course a reference to the text being worked on.


    To Use:
    Well, the linked thread above shows an example of its use. First thing to note is that before using it, I did a little something to make sure that the string "@@" could never appear within the text. That is because I use "@@" to mark the beginning and end of the placeholders used to protect text. Afterwards of course, the "@" signs are all unescaped again.

    The function above takes text out - after altering the remainder we want to put those protected bits back in. That's easy. PHP has a function for it:
    PHP Code:
    $text str_replace($placeholders$contents$text); 
    The function may be used to protect several different classes of thing: this helps simplify the task of determining appropriate regexps, because you don't need to write one regexp to do everything. It automatically makes sure that each call to the function produces different placeholders by using a static variable $n which in effect counts how many times the function has run, and using that number in the placeholders it generates.

    Needless to say, this code is not limited to bÝrking. I actually got the kernel of the idea from reading the Bugzilla source, to which I added a couple of refinements of my own.
    Last edited by Weedpacket; 12-08-2017 at 08:03 PM.
    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

  13. #13
    Missing in Action LordShryku's Avatar
    Join Date
    Aug 2002
    Location
    Occupational Hypnotherapy
    Posts
    7,227
    Well....to get completely ouside the norm here.....here's a little code snippet I have to make an ascii chart in mIRC. Promise when I make some cool PHP I'll post it here too

    Code:
    //ascii chart dialog for mIRC
    on *:load: echo -s $colour *** ASCII Chart loaded
    alias ascii dialog -m ascii ascii
    dialog ascii {
      title "ASCII Chart"
      size -1 -1 300 400
      option pixels
      tab "32-127", 501, 10 10 280 350
      tab "128-255", 502
      list 503, 20 40 260 300, tab 501
      list 504, 20 40 260 300, tab 502
      text "Doubleclick on an item to copy to clipboard", 505, 10 375 210 20
      button "Close", 605, 225 375 50 20, ok 
    }
    alias -l _asciimake {
      var %a = 32, %b = 128
      while (%a < 128) { did -a ascii 503 %a $+ : $chr(%a) | inc %a }
      while (%b < 256) { did -a ascii 504 %b $+ ; $chr(%b) | inc %b }
    }
    on *:dialog:ascii:init:0: _asciimake
    on *:dialog:ascii:dclick:503: var %a = $did(ascii,503,$did(ascii,503).sel) | clipboard $gettok(%a,2,32)
    on *:dialog:ascii:sclick:504: var %a = $did(ascii,504,$did(ascii,504).sel) | clipboard $gettok(%a,2,32)

  14. #14
    2($infinity) && $beyond BuzzLY's Avatar
    Join Date
    Nov 2002
    Location
    Star Command
    Posts
    2,435
    Those are great snippets, LordShryku, but yes, I meant this thread to be related to PHP.

    Here is another tool some of you may remember from another thread:

    Color Picker &bull; View Source

    There is room for improvement (like using GD library to make images instead of using a Windows-only font). If you do modify it, let me know -- I'd love to see it!
    New to the board? Check out the guidelines
    | Color Picker | Blogification |
    §§§§§§§§§§§§§§§§§§§§§
    With all its sham, drudgery, and broken dreams, it's still a beautiful world.

  15. #15
    Missing in Action LordShryku's Avatar
    Join Date
    Aug 2002
    Location
    Occupational Hypnotherapy
    Posts
    7,227
    Just trying to keep the thread going. This is a great idea, so I'll go come up with something in PHP I can bring to the table

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
  •