Page 1 of 1

Need help: PHP/MySQL DB to create Query for array list

Posted: Mon Nov 12, 2007 12:53 pm
by rocbar
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a MySQL DB table name category that looks like the table below:
I would like to create a query to output rows from the DB using an array and while statement were the rows are asceding and listed by the view and cat_id order just as the example below.

[syntax="html"]
<table>
         <tr>
            <td>cat_id</td>
            <td>view</td>
            <td>cat_name</td>
        </tr>
        <tr>
            <td>1</td>
            <td>0</td>
            <td>Polices</td>
        </tr>
        <tr>
            <td>2</td>
            <td>1</td>
            <td>Travel_Exp</td>
        </tr>
        <tr>
            <td>3</td>
            <td>2</td>
            <td>MyMoney</td>
        </tr>
        <tr>
            <td>4</td>
            <td>0</td>
            <td>SOP</td>
        </tr>
        <tr>
            <td>5</td>
            <td>2</td>
            <td>Funds</td>
        </tr>
        <tr>
            <td>6</td>
            <td>1</td>
            <td>Sales</td>
        </tr>
        <tr>
            <td>7</td>
            <td>4</td>
            <td>Buying</td>
        </tr>
</table>

Query output list should look like this:

Policies
  travel_Exp
    MyMoney
    Funds
    Tokens
  Sales
SOP
  Buying

feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Nov 19, 2007 3:48 pm
by califdon
I'm afraid that the reason nobody has offered any help is that we can't understand what you're trying to do. We would need you to show us what fields are in your database table. You probably need something like this, but it's hard to tell what you have to start with, or how you want to display it.

Code: Select all

...
// (connect to your database)
...
$sql="SELECT * FROM tablename GROUP BY cat_name, ???";
$result=mysql_query($sql) or die(mysql_error());
$prev_cat
while ($row=mysql_fetch_assoc($result)) {
   extract($row);
   if ($cat_name != $prev_cat) {
      echo "<tr><td>$cat_name</td></tr>";
   }
   echo "<tr><td style="indent:12px;">$account_name</td></tr>";
   ...
}
...