I think this is probably a simple thing to do, except I don't have good PHP chops yet.
I want to return a list of names from a MySQL db grouped by section, with the section being a header.
Example:
Violin I
Name One
Name Two
Name Three
Violin II
Name One
Name Two
Name Three
Viola
Name One
etc...
It would be great if I could get a starting point with the code. My fields are section, firstName, lastName. My table is members.
Thanks!
Shawn
Loop results from MySQL DB
Moderator: General Moderators
Code: Select all
SELECT * FROM members ORDER BY section ASC, lastName ASC, firstName ASC";Code: Select all
<?php
$section = '';
while ($row = mysql_fetch_assoc($result)) {
if ($section != $row['section'])
{
echo "<b>Section</b></br>";
$section = $row['section'];
}
echo $row['lastName'] . ' ' . $row['firstName'] . '</br>';
}
?>is your query is saved in $result ?
like that
Code: Select all
$sql="SELECT * FROM members ORDER BY section ASC, lastName ASC, firstName ASC";
$result = mysql_query($sql) or die(MySQL_error());I had something like that. I inserted yours and still got the error. Here is my code.
Code: Select all
<?php
$connection = mysql_connect("$server","$username","$password");
$sql="SELECT * FROM members ORDER BY section ASC, lastName ASC, firstName ASC";
$result = mysql_query($sql) or die(MySQL_error());
$section = '';
while ($row = mysql_fetch_assoc($result)) {
if ($section != $rowї'section'])
{
echo "<b>Section</b></br>";
$section = $rowї'section'];
}
echo $rowї'lastName'] . ' ' . $rowї'firstName'] . '</br>';
}
?>