Return to top
<?php
// timer class included and STARTED in auto_prepend file
include("class_timer.php");
$timer = new timer(1);

error_reporting(E_ERROR); // for PHP5 error over-reporting

/* ---------------------------------------------------------------------

*        This script has lots of diagnostics included while i built this script, 
*     most of which would not be needed once you have every thing set up properly
*     
*     Make sure to check the db table sql source -- for this system to work you must 
*     have database fields properly defined (integer zerofill) for m_id and m_parent
*     this is needed for proper sorting of the lineage field.
*     
*     I tried to keep the database as similar to Skrol's example as possible
*     for those interested in the comparison. As it currently exists, Skrol's
^   script/template will work as written when using this slightly modified db table.
*       
*        This script is able to begin with no data (just the default '0000') 
*     in the m_newlineage field -- all that is required is the items have to
*     be generated with auto_incremnent for the m_id and to have you just set 
*     the m_parent for each Item.
*     
*   The first time you run this script it will populate the m_newlineage field
*        (the legacy field 'm_lineage' was only used to test the calculated value)
*      
*     See http://tomhenry.us/tbs3/  for more info and any updates to source
*/

//$table = "t_test_tree";
$table "t_test_tree_2";

// THIS PAGE is running with direct include -vs- autoprepend in .htaccess
include("../php_inc/tbs_ddwork_ezsql.inc"); // use TBS/ezSQL for access to "tbs_ddwork" database
$connect_time $timer->get($decimals=8);

// =====================  ========  =============================
$TBS = new clsTinyButStrong ;
$load_time $timer->get($decimals=8);
$TBS->LoadTemplate(basename($_SERVER['SCRIPT_NAME'], ".php").".html") ;


// We want to clean out ALL of the old values for "m_newlineage"
// so that entries can be added or reorganized easily  
$db->query(" UPDATE $table SET m_newlineage='0000' ");

// use MySQL's CONCAT to greatly reduce difficulty of sorting the records
$sql "SELECT m_parent,m_id,m_title,m_lineage,m_newlineage,CONCAT(m_newlineage,'-',m_id) AS sort_order FROM $table ORDER BY sort_order ";

$query_time $timer->get($decimals=8);
$result $db->get_results($sql);
$p_result=print_r($result,true);

$merge_time $timer->get($decimals=8);
$TBS->MergeBlock('m',$db);

$show_time $timer->get($decimals=8);
$TBS->show();

// ====================== ONDATA function =========================
function f_ondata_tree($BlockName,&$CurrRec,$RecNum) {
    global 
$table,$timer,$db_user,$db_password,$db_name,$db_host;
    
// --- Using "ezSQL" db layer plugin, need a second instance of ezSQL class
    
$db2 = new ezSQL_mysql($db_user,$db_password,$db_name,$db_host);
    
$db2->debug_echo_is_on false;

    
$m_id=$CurrRec['m_id'];
    
$m_parent=$CurrRec['m_parent'];
    
$m_lineage=$CurrRec['m_lineage'];
    
$m_newlineage=$CurrRec['m_newlineage'];
    
    
$ondata_time $timer->get($decimals=8);
    
$CurrRec['ondata_timer'] = $ondata_time;

    
// check the last (right-most) element of m_newlineage against the val of  m_parent
    
$pieces explode"-"$CurrRec['m_newlineage'] );
    
$CurrRec['p_pieces']=print_r($pieces,true); // debug:: show data arrays
    
is_array($pieces) ? $last=array_pop($pieces) : NULL ;
    
$CurrRec['last']=$last;
    
// set a flag to know wheether we had a match
    
$CurrRec['m_parent']=="$last" $flag="yes" $flag="no";
    
$CurrRec['flag']=$flag;

    
// --------------- if they do not match, update the value -----------------
    
if($CurrRec['m_parent']!="$last"){ // NOT EQUAL
        
$sql_parent "SELECT * from $table WHERE m_id='".$CurrRec['m_parent']."'";
        
$parent $db2->get_row($sql_parent);
        
$my_lineage=$parent->m_newlineage;
        
$CurrRec['my_lineage']=$my_lineage;
        
// Now, because the records were created sequentially, the parent items are always refreshed before the children need the m_newlineage value from the parent!!!!!
        // === Ooops === really need to update with the "m_lineage" of the "m_parent"
        
$second "UPDATE $table SET m_newlineage='".$my_lineage."-".$CurrRec['m_parent']."' WHERE m_id='".$m_id."'"//WORKS
        
$result=$db2->query($second);   // now works in single pass
        // $CurrRec['ondata_result']= print_r($db2->debug(),true); // debug:: show ezSQL debug
    
}     // === end === IF()  // ---------------
}     // === end === of ondata function
// ====================== ONDATA function =========================
    
?>