Jump to content

Recommended Posts

Hey how can I search through an XML file for something specific? My XML file has "Customers" which have username and password elements. I need to check if a user exists when registering to ensure there are no duplicates.

 

At the moment I have

 

 

foreach($xml->customer as $customer) {
if ($customer->username=$username)
    echo 'User exists, please use another username';
    else
    echo 'Username is fine';
Edited by CatDogAB
Link to comment
https://forums.phpfreaks.com/topic/283493-searching-an-xml-file/
Share on other sites

You can use xpath. Example usage

<?php 

$xml = simplexml_load_file('users.xml'); 

if(isset($_POST['submit']))
{
   $username = $_POST['username'];
   $result = $xml->xpath('//users/user/username[. ="'. $username .'"]'); // search for the username in the <username></username> node

   // if xpath returned empty result (array)
   if(empty($result))
   {
      echo 'Username is fine';                         // username not found
   }
   else
   {
      echo 'User exists, please use another username'; // username found
   }
}

?>

<form method="post">
  Username: <input type="text" name="username" /><br />
  <input type="submit" name="submit" value="Search" />
</form>

Example users.xml data

<users>
 <user>
  <username>FooBar</username>
  <password>bananas</password>
 </user>
 <user>
  <username>JohnDoe</username>
  <password>apples</password>
 </user>
</users>
Edited by Ch0cu3r

 

if ($customer->username=$username)

 

For completeness: When doing comparisons you use two equal-signs (or three for strict comparisons). A single equal-sign does an assignment. So the statement above is assigning $username to the $customer->username property and testing its value. It will always be true unless $username is empty or otherwise evaluates to false (zero, null, false).

 

if ($customer->username == $username)

 

You can use xpath. Example usage

<?php 

$xml = simplexml_load_file('users.xml'); 

if(isset($_POST['submit']))
{
   $username = $_POST['username'];
   $result = $xml->xpath('//users/user/username[. ="'. $username .'"]'); // search for the username in the <username></username> node

   // if xpath returned empty result (array)
   if(empty($result))
   {
      echo 'Username is fine';                         // username not found
   }
   else
   {
      echo 'User exists, please use another username'; // username found
   }
}

?>

<form method="post">
  Username: <input type="text" name="username" /><br />
  <input type="submit" name="submit" value="Search" />
</form>

Example users.xml data

<users>
 <user>
  <username>FooBar</username>
  <password>bananas</password>
 </user>
 <user>
  <username>JohnDoe</username>
  <password>apples</password>
 </user>
</users>

Thanks a shit load man. Appreciate it.

Edited by CatDogAB

Hey got a new question, thought I'd use the same thread.

 

I have this which loads my XML file of "Items" (Javascript)

function Ajax(){
var xmlHttp;
    try{    
        xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari
    }
    catch (e){
        try{
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
        }
        catch (e){
            try{
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e){
                alert("No AJAX!?");
                return false;
            }
        }
    }

xmlHttp.onreadystatechange=function(){
    if(xmlHttp.readyState==4){
    
    
    
    txt="<table border='1'><tr><th>Name</th><th>Price</th><th>Item Code</th><th>Cart</th></tr>";
    x=xmlHttp.responseXML.documentElement.getElementsByTagName("item");
    for (i=0;i<x.length;i++){
    xx=x[i].getElementsByTagName("quantity");
    if ((xx[0].firstChild.nodeValue)>0){
    

      txt=txt + "<tr>";
      xx=x[i].getElementsByTagName("name");
        {
        try
          {
          txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
          }
        catch (er)
          {
          txt=txt + "<td> </td>";
          }
        }
      xx=x[i].getElementsByTagName("price");
        {
        try
          {
          txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
          }
        catch (er)
          {
          txt=txt + "<td> </td>";
         }
        }
      xx=x[i].getElementsByTagName("itemcode");
        {
        try
          {
          txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
          }
        catch (er)
          {
          txt=txt + "<td> </td>";
          }
          
        try
          {
          txt=txt + "<td><button>Add to cart</button></td>";
          }
        catch (er)
          {
          txt=txt + "<td> </td>";
          }
        }
      txt=txt + "</tr>";
      }
      }
    txt=txt + "</table>";
    
        document.getElementById('ReloadThis').innerHTML=txt;
        setTimeout('Ajax()',5000);
    }
    }
xmlHttp.open("GET",'data/items.xml',true);
xmlHttp.send(null);
}



window.onload=function(){
    setTimeout('Ajax()',5000);
}

Which makes this:

 

58qVR.png

 

My question is, how can it so when the button is pressed on a product "Add to cart" it automatically shows up down the bottom? Any point in the right direction would be helpful.

Edited by CatDogAB

When the Add to Cart button is pressed you need to make another ajax request to a php script that gives it the product id. The PHP script will then get the product information from the items.xml file that matches the product id. You'd then save that information (such as id, price and name) to a $_SESSION variable, eg

// if session basket variable doesn't exist, create it
if(isset($_SESSION['basket']))
{
     $_SESSION['basket'] = array();
}
// add product info to the session
$_SESSION['basket'][] = $productData;

Once you have saved the data to the session you can either echo out the contents of the $_SESSION['basket'] array into either xml/htm andl use that as the response for the ajax request. You'd then append what is sent back from the ajax request to where you want to display what is stored in the basket.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.