sk  
 


 

 
Useful Codes
 

 
<?php
#
## print nod contents with all tags by php dom
#
// XML data
$xml_string = "<?xml version='1.0' ?>
<sentence>
How to get contents of node with all tags by PHP Dom: <br/>
<b>cabbages</b>, <i>tomatoes</i>,
  <i>apples</i>, <font color='purple'>aubergines</font>
<ol>
   <li> row 1 </li>
   <li> row 2 </li>
   <li> row 3 </li>
 </ol>
 <table border = '1'>
  <tbody>
    <tr>
      <td>I am</td>
    </tr>
    <tr>
      <td>Programmer</td>
    </tr>
  </tbody>
</table>
You see my projects at:
<strong><a href='http://www.itdeveloping.eu' >http://www.itdeveloping.eu</a></strong>
</sentence>";

// parse it
$doc= new DOMDocument();
if (!$doc->loadXML($xml_string))
{
   echo ("Error in XML document");
}

$nody = $doc->getElementsByTagName('sentence'); // gets NodeList

$nod=$nody->item(0); // Node 
getContent($Content,$nod);
echo $Content;

// getContent function definition
function getContent(&$NodeContent="",$nod)
{  $NodList=$nod->childNodes;
   for( $j=0 ;  $j < $NodList->length; $j++ )
   {  $nod2=$NodList->item($j); // Node j
      $nodeActualName=$nod2->nodeName;
      $nodevalue=$nod2->nodeValue;
      if($nod2->nodeType == XML_TEXT_NODE)
        $NodeContent .=  $nodevalue;
      else 
      {  $NodeContent .= "<$nodeActualName "; 
         $attAre=$nod2->attributes;
         foreach ($attAre as $value)
           $NodeContent .="{$value->nodeName}='{$value->nodeValue}'" ;
         $NodeContent .=">";					 
         getContent($NodeContent,$nod2);					 
         $NodeContent .= "</$nodeActualName>";
      }
   }	
}
?>
The function string_analyze(...) calculate mathematical string and returns his value.
/*
The function double string_analyze("Mathematical_string_no_space", x value )
calculates a x value of a string.
Head:	double string_analyze(char *, double  );  

Using:
     bad:
   	string_analyze( "3 *x+4/ (cos(x*8.11)*12)" , 3.14 )

                          ^     ^ 
                      error, no spaces
     correct:
        string_analyze( "3*x+4/(cos(x*8.11)*12)" , 3.14 )
     where x=3.14

You can use these mathematical functions.

sin(..), cos(..), log(..), exp(..), sqrt(..), tan(..).
*/

#include <math.h>
#include <string.h>
#include <malloc.h>

#include <stdlib.h>

double string_analyze( char *, double   );  

char   *number( char *, int , double *  ); 

int    value( char );

double string_analyze( char *znak, double x_value )  

{  char *z, *s, *Z, *chars;

   double *num;
   int  oi, sign=1, n, i=0;

chars=malloc((strlen(znak)+3)*sizeof(char * ));

strcpy( chars, znak);

if( znak[strlen(chars)-1] != ';' )     /* The  character ';' must be at the end of the string. */

{ strcat(chars, ";");
} 

num=malloc((strlen(chars)+4)*sizeof(double* ) );

Z=malloc(strlen(chars)*sizeof(char * ) );         

z=&chars[0];
*(z-1)='p';
s=&Z[0];                 /* Z[] is the slave stack of the characters.*/

*s='p';

for( oi=0 ; *z != ';' ;  z++  )

{ 
     switch( *z )
     {
       case '+': *++s = '+';

                 break;
       case '(': *++s= '(';

                 break;
       case ')': break; 
       case '-': 
                 if( *(z-1) == '(' || *(z-1) == 'p')

                   sign = -1;
                 else
                  *++s='-'; 
                 break;

       case '*': *++s = '*';
                 break;

       case '/': *++s='/';
                 break;
       case 's': 
                 if( *(z+1) == 'q' )

                 { *++s='q';		/* Function sqrt(...) */
                   z+=3;

                 }
                 else 
                 { *++s='s'; 		/* Function sin(...) */
                   z+=2;  
                 }

                 break;
       case 'c': *++s='c';          	/* Function cos(...) */

                 z+=2;
                 break;
       case 'e': *++s='e';         	/* Function exp(...) */ 
                 z+=2;

                 break;
       case 'l': *++s='l';		/* Function log(...) */ 
                 z+=2;

                 break;
       case 't': *++s='t';		/* Function tan(...) */ 
                 z+=2;

                 break;
       default:
         if(*z=='x')
           num[oi]=sign*x_value;

         else
           z=number( z, sign, &num[oi]);

         sign=1;  
         for( n=1, i=0; (  *s != 'p') &&  ( value( *s ) >= value(*(z+1)) ) && (i<n); i++)

         {
                switch(  *s )
                { case '*': num[oi-1] *= num[oi]; oi--;      /*  Multiplication */

                            s--;
                            break;
                  case '/': num[oi-1] /= num[oi]; oi--;      /*  Distribution */

                            s--;
                            break;
                  case '+': num[oi-1] += num[oi]; oi--;      /*  Addition */

                            s--;
                            break;             
                  case '-': num[oi-1] -= num[oi]; oi--;      /*  Subtraction */

                            s--;
                            break;
                  case 's': num[oi]=sin(num[oi]); s--; i=-1;  
                            break;           
                  case 'q': num[oi]=sqrt(num[oi]); s--; i=-1;  
                            break;

                  case 'c': num[oi]=cos(num[oi]); s--; i=-1;

                            break;
                  case 'e': num[oi]=exp(num[oi]); s--; i=-1;

                            break;
                  case 'l': num[oi]=log(num[oi]); s--; i=-1;

                            break;
                  case 't': num[oi]=tan(num[oi]); s--; i=-1;

                            break;
                }
		if ( ( *(z+1) == ')' || *(z+1) == ';'  ||  value(*(z+1)) == 1 ) && value(*s) == 1 )

            	   i=-1;

            	if( *s == '('  &&  *(z+1) == ')'  )

                {  s--;  z++; i=-1; 
                }
         }

         oi++; 
         break;
     }/*The end of switch*/
}

return num[0];

}

char  *number( char *zz, int sign , double *num  )   

{   int i;
    char chnum[40];

    for(i=0; (*zz != '*')  &&  (*zz != '/') &&  (*zz != '-')  &&  (*zz != '+') &&   (*zz != ';') && ( *zz != ')' ) ; i++, zz++)

       chnum[i]= *zz ;

    chnum[i]='\0';

    *num=sign*atof(chnum); /* It converts The characters "2.9764876" into of the number (double). */

return ( --zz );           /* It returns the address of a last character. */   

}

int value(char ch)
{  /*
     The fuction assigns the number to the character. 

   */
  int out;

  if( ch == '+' || ch == '-')

     out= 1;
  if( ch == '*' || ch == '/' )

     out= 2;
  if( ch == '(' || ch == ')' )

     out= -3;
  if( ch == ';' || ch == 'p' )

     out= -4; 
  if( ch == 's' || ch =='c' || ch =='e' || ch =='l' )

     out=3;
 
return out;
}
 Node1
       Node4
       Node3
             Node6
             Node7
       Node2
             Node5
                   Node8 <?php $objTree=new Tree; $objTree->showTree(); class Tree { function __construct() { $database="db"; $mysqlserver="localhost"; $login=""; $passwd=""; mysql_connect($mysqlserver, $login, $passwd)or die('Could connect select database'); mysql_select_db($database) or die('Could not select database'); } private function info($idNode,&$text) { $result3 = mysql_query("SELECT * FROM Node WHERE Node.idNode='$idNode'"); $out=mysql_num_rows($result3); if($out >0) { $row3 = mysql_fetch_array($result3); $nazov=$row3[Title]; $text=" title='text: ".$row3[Text]."'"; } return $nazov; } private function display($parent, $level) { $result3 = mysql_query('SELECT idBranch FROM Tree WHERE idNode="'.$parent.'";'); while ($row = mysql_fetch_array($result3)) { $Title=$this->info($row['idBranch'],$title); echo str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',$level).
"<span $title style='cursor: pointer;background-color : #FFED9E;'> ".$Title
.
"</span><br>"; $this->display($row['idBranch'], $level+1); } } public function showTree() { $this-> display(0,0); } }; ?>
-- -- Table structure for table `Node` -- CREATE TABLE `Node` ( `idNode` int(11) NOT NULL auto_increment, `Title` varchar(100) NOT NULL default '', `Text` longtext NOT NULL, `datum` date NOT NULL default '0000-00-00', PRIMARY KEY (`idNode`) ) ENGINE=MyISAM DEFAULT CHARSET=cp1250; -- -- Dumping data for table `Node` -- INSERT INTO `Node` (`idNode`, `Title`, `Text`, `datum`) VALUES (1, 'Node1', 'text1', '0000-00-00'), (2, 'Node2', 'text2', '0000-00-00'), (3, 'Node3', 'text3', '0000-00-00'), (4, 'Node4', 'text4', '0000-00-00'), (5, 'Node5', 'text5', '0000-00-00'), (6, 'Node6', 'text6', '0000-00-00'), (7, 'Node7', 'text7', '0000-00-00'), (8, 'Node8', 'text8', '0000-00-00'); -- -- Table structure for table `Tree` -- CREATE TABLE `Tree` ( `idNode` int(11) NOT NULL default '0', `idBranch` int(11) NOT NULL default '0' ) ENGINE=MyISAM DEFAULT CHARSET=cp1250; -- -- Dumping data for table `Tree` -- INSERT INTO `Tree` (`idNode`, `idBranch`) VALUES (1, 4), (1, 3), (1, 2), (2, 5), (3, 6), (3, 7), (0, 1), (5, 8);