用户发布排序/分组

时间:2011-04-24 17:58:32

标签: php mysql sql web-applications

我正在处理库存php / sql脚本,我正在尝试将我的数据显示为

Location 1 - User 1
item
item
item

Location 1 - User 2
item 
item...

我有几个不同的位置会有多个用户,但是我的sql语句让我无法正确显示它。

我用于该页面的代码是

<?php
include_once('inc/header.php');
$handle = db_connect();

//passed in URL
if(isset($_GET['campus']) and $_REQUEST['campus'] != "" and isset($_GET['room']) and $_REQUEST['room'] != "") 
{
    $searchCampus = $_REQUEST['campus'];
    $searchCampusName = $_REQUEST['campusName'];
    $searchRoom = $_REQUEST['room'];
    $searchRoomName = $_REQUEST['room'];
} 
else if(isset($_GET['getResults']) and $_REQUEST['getResults']  != "" ) //embeded into a page with include
{
    $searchCampus = $_REQUEST['report_cr_campus'];
    $searchCampusName = $_REQUEST['report_cr_campus_name'];
    $searchRoom = $_REQUEST['report_cr_room'];
    $searchRoomName = $_REQUEST['report_cr_room_name'];
}   

$reportQuery = "SELECT main_equip_id, loc_campus, loc_room, equip_serial, 
equip_PO,     equip_id, equip_used_as_name,
equip_type, inv_equip_type.equip_type_name,
equip_model, inv_equip_model.equip_model_name, main_user_id, user_full_name
FROM inv_main
LEFT JOIN inv_equip ON inv_main.main_equip_id = inv_equip.equip_id 
LEFT JOIN inv_location ON inv_main.main_location_id = inv_location.loc_id
LEFT JOIN inv_equip_type ON inv_equip.equip_type = inv_equip_type.equip_type_id
LEFT JOIN inv_equip_model ON inv_equip.equip_model = inv_equip_model.equip_model_id
LEFT JOIN inv_equip_used_as ON inv_equip.equip_used_as = inv_equip_used_as.equip_used_as_id
LEFT JOIN inv_user ON inv_main.main_user_id = inv_user.user_id              
WHERE loc_campus = '$searchCampus' and equip_dirty_bit = 1 and loc_room = '$searchRoom'";
$reportQuery .= "ORDER BY loc_room ASC, equip_type_name ASC, equip_model_name ASC ";    
$resultLocation = $handle->query($reportQuery);
$insideCounter = 0; //used to determine if on first row.

    if ($resultLocation->num_rows) 
    {
        //Display campus name
        if(isset($_GET['getResults']) OR isset($_GET['campusName']))
            echo "<h2>".$searchCampusName."</h2>";

        //display data from query
        while ($Row = $resultLocation->fetch_assoc()) 
            {
                //First row of output, setup URLs to drill down user or room and setup titles for table.
                if($insideCounter == 0 )//OR $lastUserID != $Row['main_user_id'])
                    {
                    //echo "Counter:".$insideCounter."  LastId:".$lastUserID."      RowUserID:".$Row['main_user_id'];
                        $insideCounter = 9998;
                        echo "<h2><a href=\"reports_location.php?campus=$searchCampus&campusName=$searchCampusName&room=".$Row['loc_room']."\" target=\"_blank\">".$Row['loc_room']."</a> <a href=\"reports_user.php?user=".$Row['main_user_id']."\" target=\"_blank\">".$Row['user_full_name']."</a> - ".$resultLocation->num_rows." Results</h2>";
                                echo "<table class=\"output\">
                                        <tr class=\"rowheader\">";
                                echo "<td width=\"50px\"></td>";
                                echo "<td width=\"200px\">Type</td>";
                                echo "<td width=\"200px\">Model</td>";          
                                echo "<td width=\"200px\">Used As</td>";
                        echo "</tr>";
                    }   
                //end if first row  

                    echo '<tr onmouseover="this.className=\'normalActive\'" onmouseout="this.className=\'normal\'" class="normal">';
                        echo "<td></td>";
                        echo "<td>".$Row['equip_type_name']."</td>";
                        echo "<td>".$Row['equip_model_name']."</td>";
                        echo "<td>".$Row['equip_used_as_name']."</td>";     
                    echo "</tr>";   

                //holds ID of current users data to check if its the same user on next pass to dump titles or not.
                $lastUserID = $Row['main_user_id']; 
            }
                $insideCounter = 0;
                echo "</table><hr>";

}

include_once('inc/footer.php');
?>      

我收到以下输出

    MAG
Front Office User1 - 13 Results
 Type      Model           Used As 
 Computer  Optiplex GX270  Staff 
 Computer  Optiplex GX520  Staff 
 Computer  Optiplex GX520  Staff 
 Computer  Optiplex GX520  Staff 
 Fax       Fax 1010        Staff 
 Monitor   CRT             Staff 
 Monitor   FS 17" - 700p   Staff 
 Monitor   FS 17" - 700p   Staff 
 Monitor   FS 17" - 700p   Staff 
 Monitor   FS 17" - VA702b Staff 
 Printer   450 Turbo       Staff 
 Printer   LaserJet 2100   Staff 
 Printer   LaserJet 3800dn Staff 

我正在寻找更像这样的输出......

  User1 - 3 Results
  Type Model Used As  
  Computer Optiplex GX520 Staff 
  Monitor FS 17" - VA702b Staff  
  Printer LaserJet 2100 Staff 

  User2 - 3 Results
  Type Model Used As  
  Computer Optiplex GX270 Staff 
  Monitor FS 17" - 700p Staff 
  Printer 450 Turbo Staff 

  User3 - 2 Results
  Type Model Used As Location 
  Monitor FS 17" - 700p Staff MAG Front Office 
  Monitor FS 17" - 700p Staff MAG Front Office 

通过更改

if($insideCounter == 0 )//OR $lastUserID != $Row['main_user_id'])

if($insideCounter == 0 OR $lastUserID != $Row['main_user_id'])

我得到像

这样的输出
MAG
Front Office User1 - 13 Results    
Type Model Used As 
 Computer Optiplex GX270 Staff 

Front Office User2 - 13 Results  
Type Model Used As 
 Computer Optiplex GX520 Staff 

Front Office User3 - 13 Results  
Type Model Used As 
 Computer Optiplex GX520 Staff 

Front Office User4 - 13 Results  
Type Model Used As 
 Computer Optiplex GX520 Staff 

Front Office User1 - 13 Results  
Type Model Used As 
 Fax Fax 1010 Staff 
 Monitor CRT Staff 

Front Office User5 - 13 Results  
Type Model Used As 
 Monitor FS 17" - 700p Staff 
 Monitor FS 17" - 700p Staff 

Front Office User1 - 13 Results 
...

现在我改变了

$reportQuery .= "ORDER BY loc_room ASC, equip_type_name ASC, equip_model_name ASC ";    

$reportQuery .= "ORDER BY user_full_name ASC, loc_room ASC, equip_type_name ASC, equip_model_name ASC ";    

输出看起来更好但不包含用户的正确结果

Front Office User1 - 13 Results
 Type Model Used As 
 Computer Optiplex GX520 Staff 
 Monitor FS 17" - VA702b Staff 
 Printer LaserJet 2100 Staff 

Front Office User2 - 13 Results  
Type Model Used As 
 Computer Optiplex GX520 Staff 
 Fax Fax 1010 Staff 
 Monitor CRT Staff 
 Printer LaserJet 3800dn Staff 

Front Office User3 - 13 Results  
Type Model Used As 
 Computer Optiplex GX520 Staff 

Front Office User4 - 13 Results  
Type Model Used As 
 Computer Optiplex GX270 Staff 
 Monitor FS 17" - 700p Staff 
 Printer 450 Turbo Staff 

Front Office User5 - 13 Results  
Type Model Used As 
 Monitor FS 17" - 700p Staff 
 Monitor FS 17" - 700p Staff 

关于我哪里出错的任何建议?

0 个答案:

没有答案