creating output with titles, - while loop and arrays?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

creating output with titles, - while loop and arrays?

Post by michlcamp »

I know I've seen examples of this around here -

I need to create php output from mysql db - table 'distributors' ('name', 'address', 'city', 'state', 'phone', 'email')

List has to be arranged by 'State', using 'state' for titles/headings
Do I do this with a while loop and arrays?
any examples much appreciated in advance...
thanks.mc

Example - (Output I'm looking for) :

Alabama
Distrib 1 Name
Distrib 1 Address
Distrib 1 City
Distrib 1 Email

Distrib 2 Name
Distrib 2 Address
Distrib 2 City
Distrib 2 Email

Arizona
Distrib 1 Name
Distrib 1 Address
Distrib 1 City
Distrib 1 Email

Distrib 2 Name
Distrib 2 Address
Distrib 2 City
Distrib 2 Email

etc.
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: creating output with titles, - while loop and arrays?

Post by kryles »

Code: Select all

 
 
$query = "SELECT state,name,address, city,email FROM distributors ORDER by state";
$result = mysql_query($query);
 
while($rows = mysql_fetch_row($result))
{
echo '<p>'.$row[0].'</p>';
echo "<p>$row[1]<br />$row[2]<br />$row[3]</p>";
}
 
You could also make this into a table, but this is the ugly fast way :)

Edit: This will create the state title for each distributor....so dont use this, but I'll try to figure out the rest soon lol
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: creating output with titles, - while loop and arrays?

Post by kryles »

Code: Select all

 
 
/* Get State names */
$query_State = "SELECT DISTINCT state FROM distributors ORDER BY state";
$result_State = mysql_fetch_array($query_State)
 
while($row = mysql_fetch_row($result_State))
{
  /* For each state */
$query = "SELECT name,address, city,email FROM distributors WHERE state='".$row[0]."'";
$result = mysql_query($query);
echo "<p>$row[0]</p>";
 
while($rows = mysql_fetch_row($result))
{
  /* for each line under that state */
echo "<p>$rows[0]<br />$rows[1]<br />$rows[2]<br />$rows[3]</p>";
}
}
 
 
I think this should be what you are looking for. Or at least get you started.....*crosses fingers*
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

Re: creating output with titles, - while loop and arrays?

Post by michlcamp »

didn't work..but thanks..feels close.

mysql_fetch_array(): supplied argument is not a valid MySQL result resource
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: creating output with titles, - while loop and arrays?

Post by Zoxive »

michlcamp wrote:didn't work..but thanks..feels close.

mysql_fetch_array(): supplied argument is not a valid MySQL result resource
That means you got an error.

Error Reporting is your friend.

Code: Select all

mysql_error();
///

kryles made an minor mistake and never queried the first SQL statement.

Code: Select all

$result_State = mysql_fetch_array($query_State)
// Should be
$result_State = mysql_query($query_State);
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

Re: one more thing...

Post by michlcamp »

yes, I found that one and fixed it. Works.

But now...

how do I show only fields that are filled out?
how do I use something like this with an array?

Code: Select all

 
if ($contact)
  { echo "Contact: $contact<br>";}
  
if ($address)
  { echo "$address<br>";}
  
if ($address2)
  { echo "$address2<br>";}
  
 
Post Reply