storing a variable and looping (1 reply)

Submitted by programmer on Fri, 08/15/2008 - 19:10.

I am very much a newbie to mysql with php and I am having some difficulty figuring out how to do what I will describe below:

I am trying to do the following:

List the availability of a product in different states around the US by showing the state and then listing below it all the rows from the database that are in that state, then moving down to the next state and repeating the same procedure until everything has been exausted.

I can select a unique list of states by doing the following:

<?php
<br />
$result mysql_query("SELECT DISTINCT clients.State FROM clients,stock WHERE clients.StoreNo=stock.StoreNo AND stock.ProductID=2 AND clients.Country <> 'CAN' ORDER BY State"
<
br />
or die(
mysql_error());  
<
br />
$row mysql_fetch_array$result );
<
br />
    
// Print out the contents of each row into a table
<br />
        echo 
"<a name='frubi_"
<
br />
        echo 
$row['State'];
<
br />
        echo 
"'>";
<
br />
        echo 
$row['State'];
<
br />
        echo 
"</a></td>"
<
br />
?>

This gives me the first state value of a row that has a Product ID of 2.

Then *somehow* I have to list all the cities below and move to the next.

Would this involve an "IF" statement? A "do while"?

Somehow I would have to store the value of the string variable and compare the rows in the database, and then I would have to discard that value and repeat it with the next.

Any help with this procedure would be GREATLY appreciated.

Post your Answer

  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options