Jump to content

Need help with this while loop


rondog

Recommended Posts

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

<?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,

Archived

This topic is now archived and is closed to further replies.

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