Page 1 of 2

Lets show off:)

Posted: Fri Feb 10, 2006 1:24 am
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);
    }
  }
}

Posted: Fri Feb 10, 2006 1:35 am
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.

Posted: Fri Feb 10, 2006 1:44 am
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.

Posted: Fri Feb 10, 2006 1:51 am
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

Posted: Fri Feb 10, 2006 1:59 am
by Benjamin
That is really neat, and you made it open source too. Good job.

Posted: Fri Feb 10, 2006 3:08 am
by onion2k
Oh lordy.. where do I start? http://www.ooer.com/onionraster/ .. that's my latest toy. It's kinda cool.

Posted: Fri Feb 10, 2006 3:29 am
by Benjamin
Umm... Umm... Yeah you need an award for that one. That is incredibly creative. Where can I get a copy?

Posted: Fri Feb 10, 2006 3:47 am
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.

Posted: Fri Feb 10, 2006 4:11 am
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

Posted: Fri Feb 10, 2006 4:14 am
by JayBird
Moved to General Discussion

Posted: Fri Feb 10, 2006 6:50 am
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.

Posted: Fri Feb 10, 2006 8:09 am
by Grim...
I married the most beautiful woman in the world :D

But my code still sucks

Posted: Fri Feb 10, 2006 8:19 am
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. :)

Posted: Fri Feb 10, 2006 8:49 am
by Grim...
ARGH!

SHE DIDN'T EVEN TELL ME SHE WAS PREGNANT!

Re: Lets show off:)

Posted: Fri Feb 10, 2006 9:26 am
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