#1
  1. No Profile Picture
    Super Moderator
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,563
    Rep Power
    112

    function() only has a return


    This project was started before my brain surgery, so either I've forgotten what the intent was here, or the 2 lines below DON'T match. Anyone with a basic reminder/pointer? (Single item while building an array)
    PHP Code:
    'Due Date' => function($row) { return $row['date']; }
    'Due Date' => $row['date'
    Last edited by Triple_Nothing; October 15th, 2018 at 09:49 PM.
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  2. #2
  3. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,903
    Rep Power
    9646
    Well, it's pretty clear that one is a function and the other is not.

    It's probably a matter of whether $row was available when the array was being set up. If so then the function is overkill. If not, and the array is more like a "spec" to create something later, then the function would be necessary - possibly called multiple times for each row processed.

    In other words, hard to tell without seeing the rest.
  4. #3
  5. No Profile Picture
    Super Moderator
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,563
    Rep Power
    112
    The included elements are a bit of an overkill with all the details in them, but just to toss it all out there...

    This is the main item that is the "leader" running the whole <TABLE> build. Calls construct_rows() near end.
    PHP Code:
      function build_testing($year) {

        
    // Make the fetch for and build the page/table
        
    $DBFetch = new DBFetch();
        
    $values $DBFetch->{$_SESSION['test_page']}($year);

        
    $tables = [];
        if(
    $values['title'] == 'Account Management' || $values['title'] == 'Paid Off/Closed Accounts') {
          
    $tables[] = $values['results'];
          
    $totals 0;
        } elseif(
    $values['title'] == 'Open Account Balances') {
          
    $tables[] = $values['results'];
          
    $totals 1;
        } elseif(
    $values['title'] == 'Invoices') {
          foreach(
    $values['results'] as $row) {
            
    $date = new DateTime($row['date']);
            
    $m $date->format('mm');
            if(!isset(
    $tables[$m])) { $tables[$m] = []; }
            if(!isset(
    $tables[$m]['month'])) { $tables[$m]['month'] = $date->format('F'); }
            
    $tables[$m][] = $row;
          }
          
    $totals 2;
        }

        
    $table '      <H1>' $values['title'] . "</H1>\n";
        foreach(
    $tables as $rows) {
          
    $table .= '      <TABLE>
    . (isset($rows['month']) ? '<TR><TH>' $rows['month'] . '</TH></TR>' NULL) . '
    construct_headers($values['headers']) . '
    construct_rows($values['headers'], $rows$totals) . '
          </TABLE>' 
    "\n";
        }

        return 
    $table;

      } 
    This is the whole construct_rows() function. The focus is more the list on the second half defining $display. One thing I noticed, which throws me off, $row isn't passed to the construct_rows() function.
    PHP Code:
      function construct_rows($keys$values$totals) {

        unset(
    $values['month']);

        
    $currency0 = new NumberFormatter('en_US'NumberFormatter::CURRENCY);
        
    $currency0->setAttribute(NumberFormatter::FRACTION_DIGITS0);
        
    $currency2 = new NumberFormatter('en_US'NumberFormatter::CURRENCY);
        
    $currency2->setAttribute(NumberFormatter::FRACTION_DIGITS2);

        
    // check/define our values
        
    $total $remain $adjust 0;
        
    $format = function($row) use ($currency0$currency2, &$total, &$remain, &$adjust) {
          
    $total += array_key_exists('value'$row) ? $row['value'] : 0;
          
    $remain += (array_key_exists('paid'$row) && $row['paid'] != 1) ? $row['value'] : 0;
          
    $adjust += (array_key_exists('paid'$row) && $row['paid'] != 1) ? ($row['adj_value'] != NULL $row['adj_value'] : $row['value']) : 0;
          
    $row['active'] = array_key_exists('active'$row) ? ($row['active'] == 'Active' 'Inactive') : 'RemoveThisElement';
          
    $row['paid'] = array_key_exists('paid'$row) ? ($row['paid'] == 'X' '') : 'RemoveThisElement';
          
    $row['comment'] = array_key_exists('comment'$row) ? ' (' $row['comment'] . ')' 'RemoveThisElement';
          
    $row['comments'] = array_key_exists('comments'$row) ? $row['comments'] : 'RemoveThisElement';
          
    $row['value'] = array_key_exists('value'$row) ? (strlen($row['value']) > $currency2->formatCurrency($row['value'], 'USD') : '') : 'RemoveThisElement';
          
    $row['adj_value'] = array_key_exists('adj_value'$row) ? (strlen($row['adj_value']) > $currency2->formatCurrency($row['adj_value'], 'USD') : '') : 'RemoveThisElement';
          
    $row['limit'] = array_key_exists('limit'$row) ? (strlen($row['limit']) > '/' $currency0->formatCurrency($row['limit'], 'USD') : '') : 'RemoveThisElement';
          
    $row['date'] = array_key_exists('date'$row) ? date_create($row['date'])->format('m/d/Y') : 'RemoveThisElement';
          return 
    $row;
        };
        
    // process a row
        
    $values array_map($format$values);
        
    // remove useless items
        
    foreach($values as $arr => $value) {
          foreach(
    $value as $key => $row) {
            if(
    is_numeric($key) || $row == 'RemoveThisElement') {
              unset(
    $values[$arr][$key]);
            }
          }
        }

        
    // format for display
        
    $display = array(
          
    // header => function($row) { return displayable value; }
          
    'Status' => function($row) { return $row['active']; },
          
    'Paid' => function($row) { return $row['paid']; },
          
    'Due Date' => function($row) { return $row['date']; },
          
    'Account (Comment)' => function($row) { return $row['name'] . $row['comment']; },
          
    'Amount' => function($row) { return $row['value']; },
          
    'Account Name' => function($row) { return $row['name']; },
          
    'Adj. Amount' => function($row) { return $row['adj_value']; },
          
    'Comments' => function($row) { return $row['comments']; },
          
    'Total Due / Limit' => function($row) { return $row['value'] . $row['limit']; },
          
    'As Of...' => function($row) { return $row['date']; }
        );

        
    $display array_intersect_key($displayarray_flip($keys));

        
    $rows NULL;
        foreach(
    $values as $value) {
          
    $rows .= '        <TR>';
          foreach(
    $display as $header => $formatter) {
            
    $rows .= '<TD>' $formatter($value) . '</TD>';
          }
          
    $rows .= '</TR>' "\n";
        }
        if(
    $totals == || $totals == 2) { $rows .= '        <TR><TD></TD><TD></TD><TD>' $currency2->formatCurrency($total'USD') . '</TD><TD>Total Balance</TD></TR>'; }
        if(
    $totals == 2) { $rows .= '        <TR><TD></TD><TD></TD><TD>' $currency2->formatCurrency($remain'USD') . '</TD><TD>Remaining Balance</TD><TD>' $currency2->formatCurrency($adjust'USD') . '</TD><TD>Remaining Balance After Adjustments</TD></TR>'; }

        return 
    $rows;

      } 
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  6. #4
  7. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,903
    Rep Power
    9646
    Trace it backwards:
    - the functions are in $display
    - $display is used in the inner foreach loop as $formatter
    - $formatter is called with $value as the "row"
    - $value comes from $values
    - $values was passed to construct_rows as the second argument
  8. #5
  9. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,274
    Rep Power
    4193
    Relevant thread: Universal Column Builder
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  10. #6
  11. No Profile Picture
    Super Moderator
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,563
    Rep Power
    112
    Thanks kicken. I guess the overall function was JUST before my brain surgery, so that's prolly why I'm a bit off, but I think I just happened to recognize it as I looked back, and even recognized 1 word: function. I looked at this from top down, but due to it being a list of functions, they aren't RUNNING from beginning to end, just being defined, correct? $format and $display is just simply a list of function definitions that aren't even processing until later. If that's correct, then it all makes sense, and I thought they were processing right away since I more saw it as JUST an array definition, not an array of FUNCTION definitions to be called/referenced later. ^_^
    Last edited by Triple_Nothing; October 16th, 2018 at 10:07 AM.
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  12. #7
  13. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,903
    Rep Power
    9646
    Originally Posted by kicken
    Relevant thread: Universal Column Builder
    I knew this all seemed familiar...

IMN logo majestic logo threadwatch logo seochat tools logo