CatDogAB Posted November 1, 2013 Share Posted November 1, 2013 (edited) 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 November 1, 2013 by CatDogAB Quote Link to comment https://forums.phpfreaks.com/topic/283493-searching-an-xml-file/ Share on other sites More sharing options...
Ch0cu3r Posted November 1, 2013 Share Posted November 1, 2013 (edited) 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 November 1, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/283493-searching-an-xml-file/#findComment-1456474 Share on other sites More sharing options...
DavidAM Posted November 1, 2013 Share Posted November 1, 2013 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) Quote Link to comment https://forums.phpfreaks.com/topic/283493-searching-an-xml-file/#findComment-1456529 Share on other sites More sharing options...
CatDogAB Posted November 3, 2013 Author Share Posted November 3, 2013 (edited) 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 November 3, 2013 by CatDogAB Quote Link to comment https://forums.phpfreaks.com/topic/283493-searching-an-xml-file/#findComment-1456648 Share on other sites More sharing options...
CatDogAB Posted November 4, 2013 Author Share Posted November 4, 2013 (edited) 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: 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 November 4, 2013 by CatDogAB Quote Link to comment https://forums.phpfreaks.com/topic/283493-searching-an-xml-file/#findComment-1456841 Share on other sites More sharing options...
CatDogAB Posted November 7, 2013 Author Share Posted November 7, 2013 Anyone, please? Quote Link to comment https://forums.phpfreaks.com/topic/283493-searching-an-xml-file/#findComment-1457341 Share on other sites More sharing options...
Ch0cu3r Posted November 7, 2013 Share Posted November 7, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/283493-searching-an-xml-file/#findComment-1457354 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.