Page 1 of 1
creating output with titles, - while loop and arrays?
Posted: Mon Mar 03, 2008 8:20 am
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.
Re: creating output with titles, - while loop and arrays?
Posted: Mon Mar 03, 2008 8:51 am
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
Re: creating output with titles, - while loop and arrays?
Posted: Mon Mar 03, 2008 11:00 am
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*
Re: creating output with titles, - while loop and arrays?
Posted: Mon Mar 03, 2008 2:44 pm
by michlcamp
didn't work..but thanks..feels close.
mysql_fetch_array(): supplied argument is not a valid MySQL result resource
Re: creating output with titles, - while loop and arrays?
Posted: Mon Mar 03, 2008 2:55 pm
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.
///
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);
Re: one more thing...
Posted: Mon Mar 03, 2008 3:07 pm
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>";}