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

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
rocbar
Forum Newbie
Posts: 1
Joined: Mon Nov 12, 2007 12:00 pm

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

Post 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]
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post 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>";
   ...
}
...
Post Reply