WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Dynamic Loading of XML array data into ComboBox and Display XML data using PHP + DOM + Javascript.
Categories : PHP, Java Script, DOM XML, XML, Arrays Update Picture
Src Bie
Date : Dec 23rd 2003
Grade : 2 of 5 (graded 16 times)
Viewed : 28685
File : 3775.zip
Images : No Images for this code example.
Search : More code by Src Bie
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

The concept is to search and display the data stored in a XML file using PHP+ XMLDOM+ JavaScript.(Sorry for bad English guys).

Index.php
The search is done using 3 types of values where JavaScript+XMLDOM is used to get the XML elements from the XML file and do dynamic loading of those values into ComboBox.
<html>
<head>
<basefont face="Arial">
<script type="text/javascript">

function changereg(selprov)
{

    var NodeList
    var region = new Array()
       
    var selvalue = selprov.options[selprov.selectedIndex].value

    var xmldoc = new ActiveXObject("Microsoft.XMLDOM")
        xmldoc.async = false
        xmldoc.load("REGION.xml")
                     
    var Elem = xmldoc.getElementsByTagName("PROVINCE") ;
        Provlength = Elem.length;                 
                                                   
      for(i=0;i<=(Provlength-1);i++)
        {
            var prov = Elem.item(i).attributes.getNamedItem("NAME").text;
           
            if (prov == selvalue)
            {
                var RegElem = Elem.item(i).getElementsByTagName("REGION") ;
                           
                Reglength = RegElem.length;                 
                                                   
                  for(i=0;i<=(Reglength-1);i++)
                {
                    region = RegElem.item(i).attributes.getNamedItem("NAME").text;               
                    var ToCombo = document.frmcgo.selreg;
                    var addtext=region;
                    var addvalue=region;
                    ToCombo.options[i]=new Option(addtext,addvalue);                   
                }
            }
        }             
}

function changedest(selreg,selprv)
{

    var NodeList
    var destination = new Array()
   
    var selvalue = selreg.options[selreg.selectedIndex].value
   
    var xmldoc = new ActiveXObject("Microsoft.XMLDOM")
        xmldoc.async = false
        xmldoc.load("REGION.xml")
                     
    var Elem = xmldoc.getElementsByTagName("PROVINCE") ;
        Provlength = Elem.length;                 
                                                   
      for(i=0;i<=(Provlength-1);i++)
        {
            var prov = Elem.item(i).attributes.getNamedItem("NAME").text;
           
            if (prov == selprv)
            {
                   var RegElem = Elem.item(i).getElementsByTagName("REGION") ;
                Reglength = RegElem.length;
               
                for(j=0;j<=(Reglength-1);j++)
                {
                    var reg = RegElem.item(j).attributes.getNamedItem("NAME").text;
                   
                    if (reg == selvalue)
                    {
                        var DestElem = RegElem.item(j).getElementsByTagName("DESTINATION") ;
                        Destlength      = DestElem.length;
                       
                        for(k=0;k<=(Destlength-1);k++)
                        {
                            destination = DestElem.item(k).text;
                               var ToCombo = document.frmcgo.seldest;
                            var addtext=destination;
                            var addvalue=destination;
                           
                            ToCombo.options[k]=new Option(addtext,addvalue);                 
                        }
                    }
                }                                                                   
              }             
        }                     
}
</script>

<TITLE>Dynamic Loading Example</TITLE>

<LINK href="cgo_images/main.css" type=text/css rel=stylesheet>

</head>

<BODY bgColor=#ffffff onload="document.frmcgo.selprov.focus()" leftMargin=0 topMargin=0 MARGINWIDTH="0" MARGINHEIGHT="0">

<?php
    $xml_file
= "http://jj/www/noz/cargo/REGION.XML"; // Exact path of the XML file

   
$doc = xmldocfile($xml_file);

   
$root = $doc->root();  //CARGO
   
$province = $root->children();

    for (
$x=0; $x<sizeof($province); $x++)
    {
        if (
$province[$x]->type == XML_ELEMENT_NODE) //PROVINCE -- if empty dont show index
         
{
           
$attributes = $province[$x]->attributes();
            if (
is_array($attributes) && ($index = is_list_attribute_present($attributes)))
            {
                   
$provlist[] = $index;
               
$region = $province[$x]->children();
               
                for (
$y=0; $y<sizeof($region); $y++)
                {
                    if (
$region[$y]->type == XML_ELEMENT_NODE) //REGION
                   
{
                       
$attributes = $region[$y]->attributes();
                        if (
is_array($attributes) && ($index = is_list_attribute_present($attributes)))
                        {
                           
$reglist[] = $index;
                           
$dest = $region[$y]->children();
                           
                               for (
$z=0; $z<sizeof($dest); $z++)
                            {
                                if (
$dest[$z]->type == XML_ELEMENT_NODE)//DESTINATION
                               
{
                                       
$result = $dest[$z]->children();
                                   
$destlist[] = $result[0]->content;
                                }
                            }
                        }
                     }
                }
            }
         }
    }

function
is_list_attribute_present($attributes)
{
    foreach(
$attributes as $attrib)
    {
        if (
$attrib->name == "NAME")
        {
           
$element = $attrib->value;
            break;
        }
    }
    return
$element;
}
?>


<TABLE cellSpacing=0 cellPadding=0 border=0 align="center">
  <TBODY>
  <TR vAlign=top>
    <TD colSpan=6><IMG height=20 alt="" src="cgo_images/1pix.gif" border=0 width=1>
  </TD>
  </TR>
  <TR vAlign=top>
    <TD colSpan=6><IMG height=20 alt="" src="cgo_images/1pix.gif" border=0 width=1>
    </TD></TR>
   
  <TR><TD vAlign=center width=264 height="91">
            <form name="frmcgo" method="post" action="Ratelist.php">
             
           
      <TABLE width="254" align="center">
        <TR><TD>
            <p><font color="#003366"><b>Province </b></font>
            </TD>
            <TD><select name="selprov" style="WIDTH: 150px"  onfocus="changereg(this);">
               
                <?php
                   
foreach($provlist as $p)
                    {
                        echo
"<OPTION selected value= $p >". $p ."</OPTION>";
                    }
               
?>
               
                </select>
            </TD></TR>
            <TR><TD>
            <p><font color="#003366"><b>Region </b></font>
            </TD>
            <TD><select name="selreg" style="WIDTH: 150px" onchange="changedest(this,selprov.options[selprov.selectedIndex].value);">
                </select>
            </TD></TR>
            <TR><TD>
            <font color="#003366"><b>Destination</b></font>
            </TD>
            <TD><select name="seldest" style="WIDTH: 150px">
                </select>
            </TD></TR>
                </TABLE>
                <TABLE align="center"  width="100%"><TR>
                <TD align="center"><INPUT type=image height=24 alt="Search now!"
                    width=46 src="cgo_images/vs_top_go.gif" value=submit>
                </TD></TR>
            </TABLE>
            </form>
    </TR></TABLE>
   
<TABLE cellSpacing=0 cellPadding=0 width=779 border=0>
  <TBODY>
 
  <TR>
    <TD><IMG height=10 alt="" src="cgo_images/1pix.gif" width=1 border=0></TD></TR>
  <TR>
    <TD class=separators><IMG height=1 alt="" src="cgo_images/1pix.gif" width=1 border=0></TD></TR>
  <TR>
    <TD><IMG height=10 alt="" src="cgo_images/1pix.gif" width=1 border=0></TD></TR></TBODY></TABLE>
<!-- THUNDERBOLT --></BODY>
</html>



RateList.php
It collects the form values from the Index.php and browse thru the XML file using PHP+XMLDOM and display it as a Table(different than ordinary table).
<html>
<head></head>
<basefont face="Arial">
<TITLE>XML Data Display using PHP+DOM+JavaScript</TITLE>

<BODY>

<TABLE cellSpacing=0 cellPadding=0 border=0 align="center">
  <TBODY>
<TR vAlign=top align="right">
    <TD colSpan=6><A href="../cargo/index.php" > Search Again..</A>
</TD></TR>
<TR vAlign=top>
    <TD colSpan=6><IMG height=20 alt="" src="cgo_images/1pix.gif" border=0 width=1>
   
<?php

    $attvalprov
= $_POST['selprov'];
   
$attvalreg  = $_POST['selreg'];
   
$attvaldest = $_POST['seldest'];

    echo
"<p> ";
    echo
"<hr> ";
    echo
"<TABLE align='CENTER'>";
    echo
"<TR><TD><font color='35587E'><B>".$attvalprov." PROVINCE</TD><TD colSpan=15></TD></TR>";
    echo
"</TABLE> ";
    echo
"<hr>";
    echo
"<TABLE align='CENTER'>";
    echo
"<TR><TD><font color='35587E'><B>Region : ".$attvalreg."</TD><TD colSpan=15></TD>";
    echo
"<TD><font color='35587E'><B>Destination : ".$attvaldest."</TD></TR>";
    echo
"</TABLE> ";
    echo
"<hr> ";

   
$xml_file = "http://jj/www/noz/cargo/CARGO.XML";

   
$doc = xmldocfile($xml_file);

   
$root = $doc->root();  //CARGO
   
$provchild = $root->children();

    for (
$x=0; $x<sizeof($provchild); $x++)
    {
        if (
$provchild[$x]->type == XML_ELEMENT_NODE) //PROVINCE -- if empty dont show index
         
{
           
$attributes = $provchild[$x]->attributes();
            if (
is_array($attributes) && ($index = is_province_attribute_present($attributes, $attvalprov)))
            {
           
//-------------------------------------------------

               
$regchild = $provchild[$x]->children();
                   
                for (
$y=0; $y<sizeof($regchild); $y++)
                {
                    if (
$regchild[$y]->type == XML_ELEMENT_NODE) //REGION
                   
{
                       
$attributes = $regchild[$y]->attributes();
                               
                        if (
is_array($attributes) && ($index = is_region_attribute_present($attributes, $attvalreg)))
                        {

                         
//-------------------------------------------------

                         
$destchild = $regchild[$y]->children();

                         for (
$z=0; $z<sizeof($destchild); $z++)
                         {
                             if (
$destchild[$z]->type == XML_ELEMENT_NODE) //DESTINATION
                           
{
                               
$attributes = $destchild[$z]->attributes();
                               
                                 if (
is_array($attributes) && ($index = is_dest_attribute_present($attributes, $attvaldest)))
                                 {
                                 
//-------------------------------------------------

                                       
$carrchild = $destchild[$z]->children();
                                       
                                       for (
$i=0; $i<sizeof($carrchild); $i++)
                                     {
                                         if (
$carrchild[$i]->type == XML_ELEMENT_NODE) //CARRIER
                                       
{
                                           
$attributes = $carrchild[$i]->attributes();

                                            if (
is_array($attributes) && ($index = is_carrier_attribute_present($attributes)))
                                               {
                                               
$carr[] = $index;

                                            }

                                   
//-------------------------------------------------
                                           
$carrval = $carrchild[$i]->children();
                               
                                            for (
$a=0; $a<sizeof($carrval); $a++)
                                            {
                                                if (
$carrval[$a]->type == XML_ELEMENT_NODE) //CARRIER VALUES(WEIGHT)
                                               
{
                                                   
$rpwb[] = $carrval[$a]->name();

                                                   
$result = $carrval[$a]->children();
                                                   
$cdata[] = $result[0]->content;

                                   
//-------------------------------------------------
                                               
}
                                            }
                                        }
                                     }
                                 }
                            }
                         }
                        }
                    }
                }
            }
         }
    }
//----------------------------------------------------------------------------------------------
function is_province_attribute_present($attributeList,$attvalprov)
{
     foreach(
$attributeList as $attrib)
     {
          if (
$attrib->value == $attvalprov)
          {
               
$element = $attrib->value;
               break;
          }
     }
     return
$element;
}
//----------------------------------------------------------------------------------------------
function is_region_attribute_present($attributeList,$attvalreg)
{
     foreach(
$attributeList as $attrib)
     {
          if (
$attrib->value == $attvalreg)
          {
               
$element = $attrib->value;
               break;
          }
     }
     return
$element;
}
//----------------------------------------------------------------------------------------------

function is_dest_attribute_present($attributeList,$attvaldest)
{
     foreach(
$attributeList as $attrib)
     {
           if (
$attrib->value == $attvaldest)
          {
                 
$element = $attrib->value;
               break;
          }
     }
     return
$element;
}
//----------------------------------------------------------------------------------------------

function is_carrier_attribute_present($attributeList)
{
     foreach(
$attributeList as $attrib)
     {
          if (
$attrib->name == "NAME")
          {
               
$element = $attrib->value;
               break;
          }
     }
     return
$element;
}

//----------------------------------------------------------------------------------------------
?>

<table border="1" cellspacing="5" cellpadding="5" align="center">

<tr align=center>

<?php
   
echo "<td bgcolor='35587E'><FONT color=#ffffff><B>".$attvaldest."</td> ";

    for (
$z=0;$z<=7;$z++)
    {
        echo
"<td bgcolor='4485CD'><FONT color=#ffffff><B>".$rpwb[$z]."</td>";
    }

    echo
"</tr> ";

   
$j = 0;
   
$k = 7;

    foreach(
$carr as $c)
    {
       
        echo
"<tr align=center>";
        echo
"<td bgcolor='4485CD'><FONT color=#ffffff><B>".$c."</td>";

        for(
$i=$j;$i<=$k;$i++)
        {
            echo
"<td><FONT color='4485CD'>".$cdata[$i]."</td>";
        }

        echo
"</tr>";
       
$j = $k + 1;
       
$k = $k + 8;
    }

?>
</table>

</TD></TR>

  </TBODY>
</TABLE>
</BODY>
</html>



XPath for PHP without the DOM XML extension
Categories : DOM XML, XML, XSLT, PHP Classes, PHP
XML To Array
Categories : PHP, PHP Classes, XML, Arrays
enhanced date picker with jcript checking for a dynamic date input
Categories : PHP, Java Script, Date Time, Calendar, Arrays
Array values from javascript to php
Categories : PHP, Java Script, Arrays
PHP Array to Javascript Object
Categories : PHP, Arrays, Java Script
The meaning of your name
Categories : PHP, Arrays, Misc
Local-to-user date and time display regardless of time zone or where the website's server is located
Categories : PHP, Date Time, HTML and PHP, Java Script
Parse string to find sub-string between two arbitrary strings
Categories : PHP, Strings, HTML and PHP, Arrays
Parsing html tags with php. Get an array from this function
Categories : PHP, HTML and PHP, Arrays, Tag Extractors
Glossword - glossary compiler
Categories : Content Management, PHP, MySQL, XML
Local Time clock and Server time usign PHP and JavaScript
Categories : PHP, Java Script, Date Time, Beginner Guides
KuPro1.0 - This application is for Libraries, To see their books and to maintain users books. This version is in Turkish, future vers may have English Support.
Categories : PHP, Java Script, DHTML
XMLManipulation
Categories : PHP, XML, SimpleXML
Linked comboboxes with php-mysql & javascript
Categories : PHP, Java Script, Databases, MySQL
Sort the results from a SELECT query (any number of columns) into an array automatically.
Categories : PHP, PHP Classes, Arrays, Databases, MySQL
 Jose Santos wrote : 1039
It`s an alternative that output data to XML.
Another good ideia is to use the XML_Tree package of the pear of php (see http://pear.php.net/manual/en/package.xml.xml-tree.php). It`s very simple and easy to use. With XML_Tree, don`t need close XML tag, the XML_Tree close tags automatically. It`s has an hierarchy of nodes.
 
 Jose Santos wrote :1040
It`s an alternative that output data to XML.
Another good ideia is to use the XML_Tree package of the pear of php (see http://pear.php.net/manual/en/package.xml.xml-tree.php). It`s very simple and easy to use. With XML_Tree, don`t need close XML tag, the XML_Tree close tags automatically. It`s has an hierarchy of nodes.