Lets show off:)

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Lets show off:)

Post by Benjamin »

Ok, so here is the deal. I am really proud of this code so I am going to post it. It took me 3 days of just "thinking" because I couldn't figure out how to do it, or if it was even possible. Finally I was able to write down a rough draft on paper of how it would work, and a few days later I got it working.

This is what it does.

In one of the database tables, there are two fields. One is called parent, and the other is called child. Each child is an HTML tag. Each parent is the childs parent tag. There is no further information available, except what is in the tag, such as text.

Example:
<div> parent to next div
<div> = parent to p | child to div
<p> = child of previous div

This function opens tags until there are no more children and then starts closing them in reverse order from how they were opened.

</p>
</div>
</div>

This function, in conjunction with other functions opens and closes each tag and creates entire webpages from a database on the fly. It is used in a website development program I wrote which allows you to build your own standards compliant web pages with a web based interface. It even writes all the css;)

Anyway, This thing was really hard to write, because it calls itself, it has multiple if statements, and it calls other functions which sometimes call this function again. It's just a tangled mess but it runs like a swiss watch. I'm not going to include the other functions because I don't want this out.

I ask you, what code snippet did you find the most challenging to write? Please post it and maybe we can all gain some insight on how to tackle the impossible.

Code: Select all

function open_record($record_number) {
  global $page_number, $connect, $closed_tags, $opened_tags, $mode;

  if ($opened_tags[$record_number] == false) {
    render_object_open($record_number);
    $opened_tags[$record_number] = true;
  }

  if (has_children($record_number) == true) {
    $object_id = "select object_id from webpage_objects where record_number='" . $record_number . "'";
    $execute = mysql_query($object_id,$connect);
    $data = mysql_fetch_assoc($execute);
    $object_id = $data['object_id'];
    $open_children = true;
    $get_kids = "select record_number from webpage_objects where parent_object_id='" . $object_id . "' order by object_id asc";
    $execute = mysql_query($get_kids,$connect);
    while (($open_children == true) and ($data = mysql_fetch_assoc($execute))) {
      $record_to_check = $data['record_number'];
      if ($opened_tags[$record_to_check] != true) {
        $open_children = false;
        $mode = 'open';
        open_record($record_to_check);
      } else {
        $mode = 'close';
      }
    }
  } else {
    $mode = 'close';
  }
  if ($mode == 'close') {
    if ($closed_tags[$record_number] != true) {
    render_object_close($record_number);
    $closed_tags[$record_number] = true;
    }
    $get_parent = "select parent_object_id from webpage_objects where record_number='" . $record_number . "'";
    $execute = mysql_query($get_parent,$connect);
    $data = mysql_fetch_assoc($execute);
    if ($data['parent_object_id'] != 0) {
      $get_record_number = "select record_number from webpage_objects where object_id='" . $data['parent_object_id'] . "' and page_number='" . $page_number . "'";
      $execute = mysql_query($get_record_number,$connect);
      $data = mysql_fetch_assoc($execute);
      $record_number = $data['record_number'];
      open_record($record_number);
    }
  }
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The SHA256 algorithm.

I don't recall the exact amount of time it took to write, but I do remember about 24 hours of time going by.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

That is really cool feyd. What the he** does it do? That is a lil too advanced for me. I have never learned classes. In fact, I tried but it just haven't grasped them yet.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It is the first pure php implementation of the SHA256 hashing algorithm. It creates a 256-bit one-way hash of whatever data is passed to it. For more information you can read about the base algorithm here: http://en.wikipedia.org/wiki/SHA_hash_functions
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

That is really neat, and you made it open source too. Good job.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Oh lordy.. where do I start? http://www.ooer.com/onionraster/ .. that's my latest toy. It's kinda cool.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Umm... Umm... Yeah you need an award for that one. That is incredibly creative. Where can I get a copy?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Ok people brag. This is your 15 nanoseconds. This is fundemental to the overall health of humanity and may help prevent global warming. This is your chance to take action. POST your best work. SHOW it to the world. Be PROUD of your accomplishments. Don't be shy. The time has come. The time is now.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I don't have any single functions or classes that I consider marvels of coding.. I only have full projects that are a twinkle in my eye, and well.. that's just too much to post and they are not open source :P
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Moved to General Discussion
acidHL
Forum Commoner
Posts: 41
Joined: Wed Dec 07, 2005 7:38 am

Post by acidHL »

Uhm, work on a commercial project I can't really post the code for (my partner in crime would kill me).

Essentially a specialist ERP/CRM/Billing system.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

I married the most beautiful woman in the world :D

But my code still sucks
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Grim... wrote:I married the most beautiful woman in the world :D

But my code still sucks
Second only to mine. Oh yeah and she's a couple weeks out from her due date. :)
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

ARGH!

SHE DIDN'T EVEN TELL ME SHE WAS PREGNANT!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Lets show off:)

Post by Chris Corbyn »

agtlewis wrote:Anyway, This thing was really hard to write, because it calls itself, it has multiple if statements, and it calls other functions which sometimes call this function again.
That's recursion :) Once you've started using that and understand it you'll find yourself using it in many places where you would previously have used a spaghetti a while() loops :P Recursion rocks!

Nice idea, sounds great :D

//I'm just bundling up my latest offering so I'll "show off" once I have it packaged and documented
Post Reply