Programming Challenge |
[eluser]seanloving[/eluser]
How do I write an algorithm that takes an array like this... Code: $regions = array ( 'box', and generates HTML like this... Code: <div id="box"> Need the algorithm to understand the (hopefully obvious) hierarchical naming convention. Here's what I have so far Code: foreach( $regions as $region ) --Sean Loving Thanks
[eluser]BrianDHall[/eluser]
For hierarchy I'd use preg function to search the string for the chosen id, , such as 'top', then search in reverse and count all the instances of '<div'. 1 means top level, 3 means it is nested inside 2 other divs, etc. To simplify the preg function since regex isn't everyone's favorite thing in the world, you could use strpos() to get the position of the string and then use substr() to isolate only your chosen id and any part of the string that comes before it. Note however that it's a hack and is not 100% robust, as it would be fooled by "<div></div><div id='id'>" - it would think id is nested instead of being top-level. To address that you'd need to parse as you go, incrementing for each <div> but subtracting for each </div> until you get to your ID. The other solution is to parse it like XML of sorts and then use pre-built functions, but you'd need to get pretty chummy with XML functions to enjoy that task.
[eluser]sophistry[/eluser]
switch to YAML to represent your block element hierarchy and use this nice YAML parser on the wiki :-)
[eluser]helmutbjorg[/eluser]
[quote author="alboyd" date="1260394733"]OMG.. Begs the question.. WHY?[/quote] hhahaa.. nice.
[eluser]helmutbjorg[/eluser]
Actually i felt bad... You will end up pulling your hair out. You should have a better layout for your array if you want it to work like that. And then use a recursive function that will output the html no matter how big or nasty your array becomes. Code: function create($segments, $html='') {
[eluser]helmutbjorg[/eluser]
Actually sitting here I figured a way you can actually turn your array into my array so I created a (messy as hell) function to do it. It only goes to three levels (like your example) but could go to more. If you had time you could make that a recursive function too which will allow you to go to infinite levels. See below Code: // This is a shit function and I recommend you simply change your array layout
[eluser]seanloving[/eluser]
@sophistry - YAML might be the right way to prepare the $regions array @helmutbjorg - I think you are right about the need for a recursive function @BrianDHall - I think I'm doing something like you are suggesting - with strstr() --------------- I ALMOST got this working... can somebody PLEASE help? Once again, here's my desired output... Code: <div id="box"> But here is the actual output of my code Code: <div id="box"> And here is my code: Code: // pass this function an array of view partials (a.k.a. regions, a.k.a. strings) I call that function like this: Code: // This is a test array. Remove it. Anyone? Please!! Thanks! --Sean Loving
[eluser]sophistry[/eluser]
@seanloving i think you misunderstood what i meant. the suggestion wasn't to prepare the flat regions array with YAML, it was to prepare a nested array so that all your code had to do was iterate the array and output your divs. the problem is that the regions array with strings (as it stands now) is using underscore to delimit nesting level; this is a non-standard way to represent hierarchy. all i was suggesting was to use a standard way of representing hierarchy right from the start: YAML (using indentations and dashes) see http://www.yaml.org/
[eluser]seanloving[/eluser]
@sophistry- thanks for taking the time to clarify. The key names in my $regions array are actually extracted from the (arbitrarily predefined) field names of a database table. The name of the table is the name of the template. The field names are the region names. Each table row is a unique page. The naming convention of the field names dictates the hierarchical structure. I can't name MySQL fields with YAML, although I could build a translation layer if it made sense. I suppose the database I describe could be generated from the same YAML file that would presumably be used to generate the <div> hierarchy. Sounds like a good approach. The pro is that YAML is easy to read. The con is that you need to add one or two extra processing steps. Anyway, yikes trying to write this recursively is tricky. -SL |
Welcome Guest, Not a member yet? Register Sign In |