rondog Posted March 9, 2008 Share Posted March 9, 2008 Basically I have a table with: customer, street1, street2,city,state,zip fields. I am going to be outputting XML with it separated by state. Right now I have: <?php include 'connect.php'; $sql = mysql_query("SELECT * FROM ssp_dealers") or die(mysql_error()); $build = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; $build .= "<dealers>\n"; while ($row = mysql_fetch_array($sql)) { } ?> In that while loop I need to build nodes that look like this: <state id="1" name="Alabama" > <dealer customer="company 1" street1="street1" street2="street2" city="san diego" zip="92026" /> <dealer customer="company 2" street1="street1" street2="street2" city="san diego" zip="92026" /> <dealer customer="company 3" street1="street1" street2="street2" city="san diego" zip="92026" /> </state> <state id="2" name="Alaska" > <dealer customer="company 1" street1="street1" street2="street2" city="san diego" zip="92026" /> <dealer customer="company 2" street1="street1" street2="street2" city="san diego" zip="92026" /> <dealer customer="company 3" street1="street1" street2="street2" city="san diego" zip="92026" /> </state> etc etc... I can do that however, I dont know how to make it do that per state. Any tips? Link to comment https://forums.phpfreaks.com/topic/95152-need-help-with-this-while-loop/ Share on other sites More sharing options...
uniflare Posted March 9, 2008 Share Posted March 9, 2008 <?php include 'connect.php'; $sql = mysql_query("SELECT * FROM `ssp_dealers` ORDER BY `state`") or die(mysql_error()); $build = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; $build .= "<dealers>\n"; $laststate = "nostate"; while ($row = mysql_fetch_array($sql)) { // check if different state if($laststate != $row['state']){ // if it is then add these lines // if this is the first state dont add </state> first $build .= ($laststate == "nostate")? "" : "</state>\n"; // Add the new state line $build .= "<state id='1' name='$row['state']' >\n"; // set $laststate for the new loops $laststate = $row['state']; } $build .= "<dealer ....>\n"; } ?> hope this helps, Link to comment https://forums.phpfreaks.com/topic/95152-need-help-with-this-while-loop/#findComment-487401 Share on other sites More sharing options...
rondog Posted March 9, 2008 Author Share Posted March 9, 2008 hey awesome man that worked perfectly..I was getting a T STRING error because of $row['state']..shoulda been $row[state] but this is great Link to comment https://forums.phpfreaks.com/topic/95152-need-help-with-this-while-loop/#findComment-487417 Share on other sites More sharing options...
uniflare Posted March 9, 2008 Share Posted March 9, 2008 im glad it helped im sorry i forgot the code tags and i cant edit fsr :/ T STRING error?? wierd that would make a difference but if it works then it works Link to comment https://forums.phpfreaks.com/topic/95152-need-help-with-this-while-loop/#findComment-487423 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.