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

Link to comment
Share on other sites

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.