PHP + MySQL mysql_fetch dizideki satır olarak birden fazla değer düzenlemek bildirimde katılmak

2 Cevap php

I am using a MySQL INNER JOIN statement which returns various results for one distinct record in the main table from the joins, similar to http://stackoverflow.com/questions/1038653/how-to-join-multiple-joins-yet-return-distinct-values My query is

SELECT  cl.*, dep.dept_name  
FROM epapers.clientelle  cl 
INNER JOIN  epapers.dept_staff_users depu 
ON depu.user_id=cl.user_id 
INNER JOIN epapers.dept dep 
ON dep.dept_id=depu.dept_id 
group by cl.user_id 
ORDER BY cl.user_name ASC, 

Ben aşağıda gösterilen bir tablo üzerinde göstermek istiyorum

echo "<tr bgcolor='#CCCCCC'>
<td >$num</td><td >".$row[user_id]."</td><td>".$row[user_name]."</td>
<td >".$row[fname]."</td><td >".$row[lname]."</td>
 <td >".$row[dept_name]."</td>
 <td >".$row[dept_name]."</td>
 <td >".$row[dept_name]."</td>
 <td >".$row[email]."</td>";
 $TrID = "$row[user_id]";
 echo "<td >";
 echo '<form name="activate" action="activate_acc.php" method="POST" ;">';
 echo "<input type='hidden' name='TrID' value='$TrID'>";    
 echo "<input type='checkbox' name='active' value='$row[active]'>"; 
 echo '<input type="submit" name="Submit" value="Delete">';
 echo '</form>';
 echo "</td >";
 ...

Departmanları 3 bölüm kadar olabilir unutmayın. Nasıl bölümler için tek bir satırda mysql sorgu sonuçları dönmek?

2 Cevap

Eğer MySQL kullanıyorsanız GROUP_CONCAT kullanabilirsiniz fakat bazı düzenlemeler yapmak zorunda olacak ve biraz çirkin.

SELECT  cl.*, GROUP_CONCAT( DISTINCT dep.dept_name SEPARATOR '|' ) as dept_name 
FROM epapers.clientelle  cl 
INNER JOIN  epapers.dept_staff_users depu 
ON depu.user_id=cl.user_id 
INNER JOIN epapers.dept dep 
ON dep.dept_id=depu.dept_id 
group by cl.user_id 
ORDER BY cl.user_name ASC



foreach( ....
    $departments = explode( '|', $row['dept_name'] );
...
    if( isset( $departments[0] ) ) {
        echo "<td>{$departments[0]}</td>";
    } else {
        echo "<td></td>";
    }
    // same check
    echo "<td>{$departments[1]}</td>";
    // same check
    echo "<td>{$departments[2]}</td>";

Zamanında biraz denormalisation bulunuyor ...

iyi bir mysql sorgusu ile yapabilirsiniz yolu gibi bu üç bölüm, her biri için tablo katılmak için: dept_2 vb dept_1 b.dept_id olarak a.dept_id seçin ve her bir borç için farklı id ile aynı masaya katılmak . bu şekilde olsa da yoğun bir çok işlemcisi. Bu veri, sorgu yazdım yol almak için çok daha hızlı olacak ve daha sonra Kullanmadan önce bir fonksiyon verileri işlemek olur. burada ben bunu nasıl basit bir örnek:

function prep_data($mysql_result)
{    
    $results = array();
    $work = array();
    while ($row = mysql_fetch_assoc($mysql_result))
    {
        if ($work['user_id'] == $row['user_id'])
        {
            // just add dept data to dept array
            $dept[] = $row['dept_id'];
        }
        else
        {
            // data changed            
            if (isset($work['user_id']))
            {
                // assign dept array to data array
                $work['dept_id'] = $dept;
                // add work array to results collection
                array_push($results, $work);                
                // start working with the new user_id
                $work = $row;
                $dept = array($row['dept_id']);
                continue;                
            } 
            else
            {
                // this is first pass.  grab the row data and start the dept[] array;
                $work = $row;
                $dept = array($row['dept_id']);
                continue;
            }
        }
    }
    $work['dept_id'] = $dept;
    array_push($results, $work);
    return($results);
}

/ / Iyi şanslar