|
|
|
|
|
|
| |
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> | | |
|
| XML To Array Categories : PHP, PHP Classes, XML, Arrays | | | Array values from javascript to php Categories : PHP, Java Script, Arrays | | | enhanced date picker with jcript checking for a dynamic date input Categories : PHP, Java Script, Date Time, Calendar, Arrays | | | PHP Array to Javascript Object Categories : PHP, Arrays, Java Script | | | XPath for PHP without the DOM XML extension Categories : DOM XML, XML, XSLT, PHP Classes, PHP | | | clearing variables in php3 Categories : Variables, Arrays, PHP | | | Select with current month Categories : PHP, HTML and PHP, Date Time, Arrays | | | PHP Random rss feeds - selects 49 random feeds from an unlimited list and displays them on your website. It's Ideal for those moments when you got 5 minutes and dont know which one of your feeds to read. Categories : PHP, Rich Site Summary (RSS), Arrays | | | Dynamic Calender in PHP, Javascript and HTML. Categories : PHP, Java Script, HTML and PHP, Calendar | | | RSS parser.
Parses RSS into an array. Quick and nasty but does the job.
No checking is done for correct Tags, only correct XML.
PHP4 needed to display result (uses print_r). Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS) | | | Display list of files within current and subdirectories (recursively) showing
each file as an anchored link and each directory as a category header. Categories : Filesystem, Directories, Arrays, PHP | | | Building dynamic menus with PHP & MySQL (ADO), JavaScript and CSS Categories : PHP, Databases, MySQL, Java Script, User Interface | | | Stock quotes from yahoo! Categories : PHP, Web Services, Arrays | | | PHP Dump in html format the contents of one array variable with a recursive list of the nested array variables inside. Categories : PHP, Arrays, Variables | | | translate.php - Assocciative array example, passing a reference to a function. Categories : PHP, Arrays, Languages, Variables | |
| | | | 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.
| |
|
|
|