SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard silver trophy
    beley's Avatar
    Join Date
    May 2001
    Location
    LaGrange, Georgia
    Posts
    6,117
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Converting to PHP from old ColdFusion app

    I really need some help. I'm converting an almost 10-year-old CF app to PHP5 and ran into a snag. There is a section of code that sorts content sections on a page, and I can't figure out how it works.

    I know it's using modulus to take a display sort order value and do something with it to determine where they are sorted, and then loop through the content items.

    There are 3 different types of content and each type can be sorted above or below the other 2 types. Then within each type, each content item can be sorted.

    I've got the content, but I can't get it to sort properly and I need to understand how it works so I can build a new admin interface. We'd like to keep the sorting the same (old data that would have to be converted). Can anyone help figure out how this works?

    Here is the code on the front end:

    PHP Code:
    <!-- calculate display options -->
    <
    cfset pref Settings.ContentPref>
    <
    cfif pref is ""><cfset pref="0"></cfif>
    <
    cfset midIndex = ( pref MOD 2 ) + 1>
    <
    cfset pref pref 2>
    <
    cfset topIndex = ( pref MOD 4 ) + 1>
    <
    cfset pref pref 4>
        
        
    <
    cfloop list="RF,CF,OF" index="var">
       <
    cfset tmp SetVariable( var, pref MOD 2 )>
       <
    cfset pref pref 2>
    </
    cfloop>


    <!-- 
    calculate topmiddlebottom content -->
    <
    cfset content1 ListGetAt"O,C,R"topIndex )>
    <
    cfset content2 ListGetAtListDeleteAt"O,C,R"topIndex ), midIndex )>
    <
    cfset content3 ListDeleteAtListDeleteAt"O,C,R"topIndex ), midIndex )>

    <!-- 
    calculate display list -->
    <
    cfset displaylist "*,*,*">

    <!-- 
    loop over the content types -->
    <
    cfloop index="i" from="1" to="#ListLen( displaylist )#">

        <!-- 
    content prefix -->
        <
    cfset pre Evaluate"content#i#" )>

        <!-- 
    add the content if necessary -->
        <
    cfif Evaluate"#pre#F" is 1>
        
            <
    cfset displaylist ListSetAtdisplaylistipre )>
            
        <!-- 
    check for co-op override -->
        <
    cfelseif pre is "C" ) and ( Coop.ForceContent is 1 ) >

            <
    cfset displaylist ListSetAtdisplaylisti"C" )>

        </
    cfif>

    </
    cfloop
    Thanks!

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,322
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    <cfset pref = pref 2>

    that can't be right
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Wizard silver trophy
    beley's Avatar
    Join Date
    May 2001
    Location
    LaGrange, Georgia
    Posts
    6,117
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    It's copied and pasted from the live, working app.

    It's cold fusion though, and I don't know CF. What can't be right about it?

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,322
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    it's missing an operator

    all of these would be fine --

    <cfset pref = pref + 2 >
    <cfset pref = pref * 2 >
    <cfset pref = pref MOD 2 >

    but this is invalid --

    <cfset pref = pref 2 >
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Wizard silver trophy
    beley's Avatar
    Join Date
    May 2001
    Location
    LaGrange, Georgia
    Posts
    6,117
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Yep, you're right -- something got changed from the original code (I must have been playing with it on my local machine). Here is the code from the site:

    PHP Code:

    <!-- calculate display options -->
    <
    cfset pref Settings.ContentPref>
    <
    cfif pref is ""><cfset pref="0"></cfif>
    <
    cfset midIndex = ( pref MOD 2 ) + 1>
    <
    cfset pref pref 2>
    <
    cfset topIndex = ( pref MOD 4 ) + 1>
    <
    cfset pref pref 4>
    <
    cfloop list="RF,CF,OF" index="var">
       <
    cfset tmp SetVariable( var, pref MOD 2 )>
       <
    cfset pref pref 2>
    </
    cfloop>

    <!-- 
    calculate topmiddlebottom content -->
    <
    cfset content1 ListGetAt"O,C,R"topIndex )>
    <
    cfset content2 ListGetAtListDeleteAt"O,C,R"topIndex ), midIndex )>
    <
    cfset content3 ListDeleteAtListDeleteAt"O,C,R"topIndex ), midIndex )>

    <!-- 
    calculate display list -->
    <
    cfset displaylist "*,*,*">

    <!-- 
    loop over the content types -->
    <
    cfloop index="i" from="1" to="#ListLen( displaylist )#">

        <!-- 
    content prefix -->
        <
    cfset pre Evaluate"content#i#" )>

        <!-- 
    add the content if necessary -->
        <
    cfif Evaluate"#pre#F" is 1>
        
            <
    cfset displaylist ListSetAtdisplaylistipre )>
            
        <!-- 
    check for co-op override -->
        <
    cfelseif pre is "C" ) and ( Coop.ForceContent is 1 ) >

            <
    cfset displaylist ListSetAtdisplaylisti"C" )>

        </
    cfif>

    </
    cfloop
    Does that make more sense?

  6. #6
    SitePoint Wizard silver trophy
    beley's Avatar
    Join Date
    May 2001
    Location
    LaGrange, Georgia
    Posts
    6,117
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Just noticed that even though some of it is different, that line is the same. I have no idea why it's like that... or how it works. but it does.

  7. #7
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,322
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    well, as they say, "irregardless of that..."

    even if it did say something more sensible, like pref=pref/2, that wouldn't help me

    i write coldfusion but what he's doing there is a complete mystery

    in fact i'd say it's a brilliant example of obfuscation
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  8. #8
    SitePoint Wizard silver trophy
    beley's Avatar
    Join Date
    May 2001
    Location
    LaGrange, Georgia
    Posts
    6,117
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Okay, I found the problem. It''s not converting to Vbulletin correctly... let me try using a different code tag:

    Code:
    <!--- calculate display options -->
    <cfset pref = Settings.ContentPref>
    <cfif pref is ""><cfset pref="0"></cfif>
    <cfset midIndex = ( pref MOD 2 ) + 1>
    <cfset pref = pref \ 2>
    <cfset topIndex = ( pref MOD 4 ) + 1>
    <cfset pref = pref \ 4>
    <cfloop list="RF,CF,OF" index="var">
       <cfset tmp = SetVariable( var, pref MOD 2 )>
       <cfset pref = pref \ 2>
    </cfloop>
    
    <!--- calculate top, middle, bottom content -->
    <cfset content1 = ListGetAt( "O,C,R", topIndex )>
    <cfset content2 = ListGetAt( ListDeleteAt( "O,C,R", topIndex ), midIndex )>
    <cfset content3 = ListDeleteAt( ListDeleteAt( "O,C,R", topIndex ), midIndex )>
    
    <!--- calculate display list -->
    <cfset displaylist = "*,*,*">
    
    <!--- loop over the content types -->
    <cfloop index="i" from="1" to="#ListLen( displaylist )#">
    
        <!--- content prefix -->
        <cfset pre = Evaluate( "content#i#" )>
    
        <!--- add the content if necessary -->
        <cfif Evaluate( "#pre#F" ) is 1>
        
            <cfset displaylist = ListSetAt( displaylist, i, pre )>
            
        <!--- check for co-op override -->
        <cfelseif ( pre is "C" ) and ( Coop.ForceContent is 1 ) >
    
            <cfset displaylist = ListSetAt( displaylist, i, "C" )>
    
        </cfif>
    
    </cfloop>

  9. #9
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not a cold fusion programmer, but from online documentation I think this is a rough translation. Could have totally missed the mark but here is my translation.
    PHP Code:
    // calculate display options
    $pref Settings.ContentPref// Do not know how to get this variabble
    if ( $pref == "" ) {
        
    $pref 0;
    // This makes no sense, if you set to zero all of the rest of the computations will
    // be set to 1
    $midIndex fmod$pref,  ) + 1;
    $pref $pref 2;
    $topIndex fmod$pref) + 1;
    $pref $pref 4;
    $list = array("RF","CF","OF");
    foreach( 
    $list as $var ) {
       
    $var .= fmod$pref);
       
    $pref $pref 2;
    }

    // calculate top, middle, bottom content
    // not sure this will work in php, cold fusion may reorder this array as you unset elements
    // but php will not
    $position = array( "O","C","R" );
    $content[1] = $position[$topIndex];
    unset( 
    $position[$topindex] );
    $content[2] = $position[$midIndex];
    unset ( 
    $position[$midindex] );
    $content[3] = $position[0];

    //loop over the content types
    for ( $i=1$i<=3$i++ ) {
        
    //content prefix
        
    $pre substr$content[$i], 0);
        
    //add the content if necessary
        
    if ( $list[$pre] == ) {
            
    $displaylist[$i] = $list[$pre];
        }    
        
    // check for co-op override
        
    elseif ( $pre == "C"  &&  Coop.ForceContent == ) { // Do not know how to get this variable
            
    $displaylist[$i] = $list["C"];
        }

    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  10. #10
    SitePoint Wizard silver trophy
    beley's Avatar
    Join Date
    May 2001
    Location
    LaGrange, Georgia
    Posts
    6,117
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help guys... with the help of a co-worker I think we've deciphered the code. The programmer that wrote this (almost a decade ago) is a gifted mathematician and an extremely bright programmer, but he didn't leave us many notes behind!

    Anyway, he uses modulus (and a bug in software that allows a modulus of 1 into 2 to be 1) to use a single value to determine the sorting of 3 elements on a page (and whether they display or not).

    I think we've figured out how it works by reverse-engineering the code which should allow us to rewrite all of it (this is just the front-end, we have to have the admin section too).

    byron3 -- that's about what I have too (same as you I don't know CF but just read a bit online). Still after converting the code, I couldn't get it to work right so we had to reverse-engineer it to find out how in the heck he was doing it.

    The numbers (like 31 or 8) were created by a function in the admin and deciphered here in this code snippet. I'll post some of that tomorrow when I get into the office.

    Thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •