Jump to content

PHP+MySQL: New Table for every State pulled


wastedthelight

Recommended Posts

<?php
$sql = "SELECT state, company
        FROM tablename
        ORDER BY state, company";
        
$res = mysql_query($sql) or die(mysql_error());

$lastState = '';
while (list($state, $company) = mysql_fetch_row($res))
{
    if ($lastState != $state)                            // new state ?
    {
        if ($lastState != '')
        {
            echo '</table><br/>';                        // if there was a previous table, close it
        }
        echo '<table>';                                  // start new table
        echo '<tr><th>$state</th></tr>';                 // output new state header
        $lastState = $state;                             // reset last state value
    }
    echo '<tr><td>$company</td></tr>';                     // output company name
}
echo '</table><br/>';                                    // cloase final table
?>

Link to comment
Share on other sites

<?php
$sql = "SELECT state, company
        FROM tablename
        ORDER BY state, company";
        
$res = mysql_query($sql) or die(mysql_error());

$lastState = '';
while (list($state, $company) = mysql_fetch_row($res))
{
    if ($lastState != $state)                            // new state ?
    {
        if ($lastState != '')
        {
            echo '</table><br/>';                        // if there was a previous table, close it
        }
        echo '<table>';                                  // start new table
        echo '<tr><th>$state</th></tr>';                 // output new state header
        $lastState = $state;                             // reset last state value
    }
    echo '<tr><td>$company</td></tr>';                     // output company name
}
echo '</table><br/>';                                    // cloase final table
?>

 

Thanks! Just had to do a little editing because it was actually showing $state instead of the value state is. but yeah, works perfect thanks for your help Barand!

Link to comment
Share on other sites

i spoke to soon.  When adding the rest of the fields (address, zip, phone...) it screwed everything up.  Even taking when it works to changing Select company FROM to Select * From screws it up.

 

This is what I have. It's not out putting anything except autoID and country (id for state adn country for company).

 

<?php
$sql = "SELECT * FROM locations ORDER BY state, company";
        
$res = mysql_query($sql) or die(mysql_error());

$lastState = '';
while (list($state, $company) = mysql_fetch_row($res))
{
    if ($lastState != $state)                                                      // new state ?
    {
        if ($lastState != '')
        {
            echo '</table><br/>';                                // if there was a previous table, close it
        }
        echo '<table width="100%" align="left">';                                  // start new table
        echo '<tr bgcolor="#c0c0c0"><td><b>';echo $state; echo '</b></td></tr>';   // output new state header
        $lastState = $state;                                                       // reset last state value
    }
   		echo '
		<tr><td>';echo $company; echo '</td>
		<td>';echo $address1; echo '</td>
		<td>';echo $address2; echo '</td>
		<td>';echo $city; echo '</td>
		<td>';echo $zip; echo '</td>
		<td>';echo $phone; echo '</td>
		<td>';echo $cell; echo '</td>
		<td>';echo $fax; echo '</td>
		<td>';echo $website; echo '</td></tr>';                     // output company name
}
echo '</table><br/>';                                    // cloase final table
?>

Link to comment
Share on other sites

try

<?php
$sql = "SELECT state, company, address1, 
        address2, city, zip, phone,
        cell, fax, website 
        FROM locations 
        ORDER BY state, company";
        
$res = mysql_query($sql) or die(mysql_error());

$lastState = '';
while (list($state, $company, $address1, $address2, $city, $zip, $phone, $cell, $fax, $website) = mysql_fetch_row($res))
{
    if ($lastState != $state)                            // new state ?
    {
        if ($lastState != '')
        {
            echo '</table><br/>';                        // if there was a previous table, close it
        }
        echo '<table>';                                  // start new table
        echo "<tr><th colspan='9'>$state</th></tr>";                 // output new state header
        $lastState = $state;                             // reset last state value
    }
    echo "<tr><td>$company</td>
              <td>$address1</td>
              <td>$address2</td>
              <td>$city</td>
              <td>$zip</td>
              <td>$phone</td>
              <td>$cell</td>
              <td>$fax</td>
              <td>$website</td>
            </tr>";                                      // output company data
}
echo '</table><br/>';                                    // close final table
?>

Link to comment
Share on other sites

list($state, $company, $address1, $address2, $city, $zip, $phone, $cell, $fax, $website)

needs to be in the same order as the columns in the rows returned.

 

SELECT * selects all columns. and for all I knew there could be 100 of them, in any order. Specifying the fields guarantees the content of the row.

 

Unless you really do need ALL columns, don't use *. It obscures what the query is doing and results in redundant data being fetched, which adds to the inefficiency of the query

Link to comment
Share on other sites

Wow, okay didn't know that. Thanks for all your help!

 

list($state, $company, $address1, $address2, $city, $zip, $phone, $cell, $fax, $website)

needs to be in the same order as the columns in the rows returned.

 

SELECT * selects all columns. and for all I knew there could be 100 of them, in any order. Specifying the fields guarantees the content of the row.

 

Unless you really do need ALL columns, don't use *. It obscures what the query is doing and results in redundant data being fetched, which adds to the inefficiency of the query

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.