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);
}
}
}