Jump to content

Converting a MYSQL Query Script to Reading from a text file


Grodo

Recommended Posts

Hello fellow PHP Freaks,

    Im having a hard time converting the following tree menu script to read nodes from a text file, rather then a database.  This script works perfectly and is exactly what i am looking for, however it relies on query's from a MySQL database (I don't know how to set one up is the real problem).  I would like to keep the feature where you can pass a parameter through the script and its expands the corresponding folder.  If some one could help me convert this script to read data from a text file; or show me how to setup a MySQL database that would be awesome.  Here is the demo of the script im trying to convert / learn how to operate.  http://www.myiosoft.com/products/EasyFree/easyphptree/demo/  and is also available for download here: http://www.myiosoft.com/products/EasyFree/easyphptree/easyphptree.zip

 

I have also posted the code for the .php file that is located in the zip file.

<?
   session_start(); error_reporting(E_ALL); // error_reporting(0);
// ********************************************************
// ************************ EasyPHPtree *******************
// *********************************************** MyioSoft
// ************************** http://www.myiosoft.com/
//
// ****************************
// Edit Your Myqsl Server Data
// ****************************
// Server data myio.net
$Myqsl["Server"]="localhost";
$Myqsl["User"]="user";
$Myqsl["Pass"]="pass";
$Myqsl["DB"]="dbase";
$Myqsl["Table"]="table";
$install=0; //In order to Create a Tree Example run once with $install=1; then return to $install=0;
//
// **************************************************************************************
// In order to Create a Tree Example run once with $install=1; then return to $install=0;
// **************************************************************************************
$nodes=500; $groups=9;
if($install==1){
$Description=""; $CreateUser=""; $CreateDate=""; $ModifyDate=""; $LastVisit=""; $PublicUse=0;
$MyqslTable=$Myqsl["Table"]; @mysql_connect($Myqsl["Server"],$Myqsl["User"],$Myqsl["Pass"]) or Die ("<div align=center><font color='red'><b>Error! <br>Cannot make MySQL server  connection.</b></font></div>"); mysql_select_db($Myqsl["DB"]);
$sql="DROP TABLE IF EXISTS $MyqslTable"; mysql_query($sql);
$sql="CREATE TABLE IF NOT EXISTS `$MyqslTable` (  `ID` bigint(20) NOT NULL auto_increment,  `Name` varchar(255) NOT NULL default '',  `Description` text NOT NULL,  `CreateUser` bigint(20) NOT NULL default '0',  `CreateDate` datetime NOT NULL default '0000-00-00 00:00:00',  `ModifyDate` datetime NOT NULL default '0000-00-00 00:00:00',  `LastVisit` datetime NOT NULL default '0000-00-00 00:00:00',  `Parent` bigint(20) NOT NULL default '0',  `PublicUse` tinyint(1) NOT NULL default '0',  PRIMARY KEY  (`ID`))"; mysql_query($sql);
srand((double)microtime()*1000000);
for ($i=1;$i<=$nodes;$i++){ if ($i>$groups){ $parcat=rand(1,100); }else{ $parcat=0; }; $sql="INSERT INTO $MyqslTable(ID,Name,Description,CreateUser,CreateDate,ModifyDate,LastVisit,Parent,PublicUse) VALUES  (Null,'Easy $i','$Description','$CreateUser','$CreateDate','$ModifyDate','$LastVisit','$parcat','$PublicUse')"; mysql_query($sql); }
print "Created Tree with ".$nodes." nodes in ".$groups." groups";
}
//
// ************
// HTML OUTPUT
// ************
// register_globals = Off compatibility
if ($_GET) {foreach (array_keys($_GET) as $var){$$var=strip_tags($_GET[$var]);}} ?>
<HEAD><TITLE>Easy PHP Tree</TITLE></HEAD>
<BODY>
<table cellspacing="0" cellpadding="0" width="750"  border="0">
<tr>
    <?
    // benchamrk (optional) start time
    $timeparts = explode(" ",microtime());
    $starttime = $timeparts[1].substr($timeparts[0],1);
    ?>
  <td width="200" valign="top"><?=MakeTree($Page,$Name,$Myqsl); ?></td>
    <?
    // benchamrk (optional) end time
    $timeparts = explode(" ",microtime());
    $endtime = $timeparts[1].substr($timeparts[0],1);
    $totaltime=bcsub($endtime,$starttime,6);
    ?>
  <td valign="top" align="left" width="550">
   <div align="center">
    <h1>Easy PHP Tree</h1><h2>This is a page for the node</h2>
    <h1><?=$Name; ?></h1>
    <h2> Tree with <?=$nodes; ?> nodes in <?=$groups; ?> groups <br>build in <?=$totaltime; ?> sec</h2>
    <a href="http://myiosoft.com">MyioSoft</a>
   </div>
  </td>
</tr>
</table>
</BODY></HTML>
<?
//
// ********************************
// EsayPHPtree functions (73 lines)
// ********************************
function MakeTree(&$Page,&$Name,$Myqsl){
          $MyqslTable=$Myqsl["Table"];
          @mysql_connect($Myqsl["Server"],$Myqsl["User"],$Myqsl["Pass"]) or Die ("<div align=center><font color='red'><b>Error! <br>Cannot make MySQL server  connection.</b></font></div>");
          mysql_select_db($Myqsl["DB"]);
          $usage="PublicUse=0 Or PublicUse=1";
          $result=mysql_query("SELECT * FROM ".$MyqslTable." WHERE ".$usage." Order by Name");
          if(@mysql_num_rows($result)==0 ) {return "<div align=center>No Folders Available</div><br><br>";}
          while($Folder0=list($ID,$Name,$Description,$CreateUser,$CreateDate,$ModifyDate,$LastVisit,$Parent,$PublicUse)=mysql_fetch_array($result)){$table[$Parent][$ID]=$Name; $partable[$ID]=$Parent;  $Folder[$ID]=$Folder0; }; unset($Folder0);
          $levels=TreeLevels(0,$table,0);
          if(!isset($Folder[$Page]["Name"]))  {$Folder[$Page]["Name"]="Home";}
          $Name=$Folder[$Page]["Name"];
          if($Page==0){
           if(!isset($_SESSION["expandtree"])) {$_SESSION["expandtree"]=1;}
           $_SESSION["expandtree"]=-$_SESSION["expandtree"];
           if($_SESSION["expandtree"]<0) { $levelexpand=1; } else { $levelexpand=100000; }
           $results="<img src='folderopen.gif' width='20' height='20'><span style='height: 20px; font: 11px Arial, Helvetica;  font-weight:bold;'> <a href='?Page=0'>Home</a></span><br>";
           $results.=TreeExpand(0,$table,$partable,$Folder,$Page,0,$levelexpand);
          }else{
           $results="<img src='folder.gif' width=20 height=20><span style='height: 20px; font: 11px Arial, Helvetica;'> <a href='?Page=0'>Home</a></span><br>";
           $ID=$Page;
           $results.=TreeNodes($ID,$table,$partable,$Folder,$Page,$levels);
           $_SESSION["expandtree"]=1;
          }
return $results."<br><br>";
}
function  TreeLevels($Parent,$table,$level){
          $_SESSION["levels"][$Parent]=$level;
          while(list($key,$val)=each($table[$Parent])){if (isset($table[$key])){TreeLevels($key,$table,$level+1); } }
return $_SESSION["levels"];
}
function TreeNodes($ID,$table,$partable,$Folder0,$Page,$levels){
         $rez0="";
         $imgfolder0="<img src='folder.gif' alt='' width='20' height='20'> <span style='height: 20px; font: 11px Arial, Helvetica; '>";
         $imgfolder1="<img src='folderopen.gif' alt='' width='20' height='20'> <span style='height: 20px; font: 11px Arial, Helvetica; font-weight:bold;'>";
         if(isset($table[$ID])) {
          $imgwidth="<img src='tr.gif' width='".(($levels[$ID]+1)*10)."' height='1' alt=''>";
          $lisd=$table[$ID];
          $ID0=$ID; $fiplevel=$levels[$ID];
          while(list($key,$val)=each($lisd)){
           if($Page==$key){
            $rez0.=$imgwidth.$imgfolder1."<a href='?Page=".$Folder0[$key]["ID"]."'>".$Folder0[$key]["Name"]."</a></span><br>";
           } else {
            $rez0.=$imgwidth.$imgfolder0."<a href='?Page=".$Folder0[$key]["ID"]."'>".$Folder0[$key]["Name"]."</a></span><br>";
         }}}
         $res="";
         while ($ID!=0) {
          $fip=$partable[$ID];
          $fiplevel=$levels[$fip];
          $fiplist=$table[$fip];
          $imgwidth="<img src='tr.gif' width='".(($fiplevel+1)*10)."' height='1' alt=''>";
          $rez="";
          while(list($key,$val)=each($fiplist)){
           if($Page==$key){
            $rez.=$imgwidth.$imgfolder1."<a href='?Page=".$Folder0[$key]["ID"]."'>".$Folder0[$key]["Name"]."</a></span><br>".$rez0;
           } else {
            $rez.=$imgwidth.$imgfolder0."<a href='?Page=".$Folder0[$key]["ID"]."'>".$Folder0[$key]["Name"]."</a></span><br>";
           }
           if($ID==$key) { $rez.=$res; $res="";}
          }
          $res.=$rez;
          $ID=$partable[$ID];
         }
return $rez;
}
function TreeExpand($Parent,$table,$partable,$Folder0,$Page,$level,$levelexpand){
          $list=$table[$Parent]; $width=($level+1)*10; $result='';
          while(list($key,$val)=each($list)){
           if($level<$levelexpand) { $result.="<img src='tr.gif' width='".$width."' height='1'><img src='folder.gif'><span style='height: 20px; font: 11px Arial, Helvetica;'> <a href='?Page=".$Folder0[$key]["ID"]."'>".$Folder0[$key]["Name"]."</a></span><br>"; }
           if (isset($table[$key])){ $result.=TreeExpand($key,$table,$partable,$Folder0,$Page,$level+1,$levelexpand); }
          }
return $result;
}
?>

 

Once again thank you for all your efforts,

 

Grodo

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.