Jump to content

Recommended Posts

I need to have a query run inside a while loop that joins data from two tables depending on what the the current item_id is.

 

Table examples:

Cabinets

 

cabinet_id

 

cab_name

 

1

 

investments

 

2

 

documents

 

Folders

 

folder_id

 

fol_name

 

cabinet_id

 

1

 

stocks

 

1

 

2

 

word docs

 

2

 

3

 

spreadsheets

 

2

 

Items

 

item_id

 

item_name

 

folder_id

 

1

 

Stock 1

 

1

 

2

 

letter

 

2

 

3

 

letter 2

 

2

 

4

 

budget

 

3

 

Now here's my function code:

function show_filing() {
  include 'library/config.inc.php';
  $conn=mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
  mysql_select_db($dbnamemain);
            
  $query=mysql_query("SELECT *
        FROM items
        ") or die(mysql_error());
  echo "<p>
        <table width='100%' border='0' cellspacing='1' cellpadding='1'>
        <th width='200'>PATH</th>
        <th width='200'>NAME</th>
        <th>DESCRIPTION</th>
        <th width='70'>ACTION</th>";
            
  while($row=mysql_fetch_array($query)) {
        $folcab_query=mysql_query("SELECT folders.fol_name, cabinets.cab_name
            FROM folders, cabinets
            WHERE folders.cabinet_id=cabinets.cabinet_id
            ") or die(mysql_error());
        $row1=mysql_fetch_array($folcab_query);
        echo "<tr>
            <td>$row1[cab_name] -> $row1[fol_name] -> </td>
            <td>" .substr($row['item_name'],0,100). "</td>
            <td>$row[item_description]</td>
            <td align='center'>Delete | Edit</td>
            </tr>";
  }
  echo "</table>";
            
  mysql_close($conn);
}

 

I need for example if the item the while loop is on is the first item I need it to get

 

item_id: 1

item_name: Stock 1

folder_id: 1

 

And then also with the query inside the while statement to get

 

fol_name: stocks

cab_name: investments

 

The first time it runs it gets the correct fol_name and cab_name, but the fol_name and cab_name variable don't change whith the item_id.  How can I make the second run through result in

 

item_id: 2

item_name: letter

folder_id: 2

fol_name: word docs

cab_name: documents

 

then third, fourth, etc...?

 

Thanks!

It's hard to understand what you want based on your description.  From what it seems like, you want to display a path listing , item name, description:    based off of looking up all the items in your table.

 

IF THIS IS NOT WHAT YOU WANT, explain more clearly and ignore the rest.  Otherwise read on.

 

Otherwise, try this code, and see the results.  (I left out item_description because it was not in your supplied tables)

<?php
/*
Your config stuff here
*/

  $query=mysql_query("SELECT c.cab_name as cabinet, 
f.fol_name as folder, 
i.item_name as item
FROM items i
JOIN folders f ON(i.folder_id=f.folder_id)
JOIN cabinets c ON(c.cabinet_id=f.cabinet_id)
") or die(mysql_error());
  echo "<p>
        <table width='100%' border='0' cellspacing='1' cellpadding='1'>
        <th width='200'>PATH</th>
        <th width='200'>NAME</th>
        <th>DESCRIPTION</th>
        <th width='70'>ACTION</th>";

        while(list($cab,$fol,$item)=mysql_fetch_array($query)){
        echo "<tr>
            <td>$cab -> $fol -> </td>
            <td>" .substr($item,0,100). "</td>
            <td>Descr</td>
            <td align='center'>Delete | Edit</td>
            </tr>";
       }
  echo "</table>";
?>

Outputs: (something similar, but not as nice looking)

[pre]

PATH                            NAME   DESCRIPTION ACTION     

investments -> stocks ->        Stock 1  Descr          Delete | Edit

documents -> word docs ->        letter    Descr          Delete | Edit

documents -> word docs ->        letter 2  Descr          Delete | Edit

documents -> spreadsheets ->    budget    Descr          Delete | Edit

[/pre]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.