Results 1 to 3 of 3

Thread: Removing duplicate entries in while loop

  1. #1
    Join Date
    Nov 2010
    Posts
    115
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Removing duplicate entries in while loop

    Hi All,


    In my program i am running a while loop which will produce one row for every loop, Now if you see the table user(9754) is there multiple times. Now i want to make sure that it is taking only the last row and leaving all the duplicate entries.



    PHP Code:

    $query 
    mysql_query("select * from member where rank = '2'' ");
            while(
    $result mysql_fetch_array($query))
            {

    $user $result['code'];
    $amount $result['amount'];

    echo 
    "<tr><td>    $user </td> <td> $amount </td> </tr>";


    The above code will produce the following output inside a table

    HTML Code:
    <table border="1" width="150" >
    <tr> <td> 
    <td>User  </td>	 <td>Amount</td></tr>
    <td>3206</td>	 <td>606</td> </tr>
    <td>9754</td>	 <td>60</td> </tr>
    <td>9754</td>	 <td>70</td> </tr>
    <td>9754</td>	 <td>140</td> </tr>
    <td>9754</td>	 <td>240</td> </tr>
    <td>2483</td>	 <td>350</td> </tr>
    </table>

    I want this to be like below


    HTML Code:
    <table border="1" width="150" >
    <tr> <td> 
    <td>User  </td>	 <td>Amount</td></tr>
    <td>3206</td>	 <td>606</td> </tr>
    <td>9754</td>	 <td>240</td> </tr>
    <td>2483</td>	 <td>350</td> </tr>
    </table>

    Can anyone help me how to solve this issue.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Pretty much the same way we did it in the other thread. Only instead of echoing from the while loop, use the loop to create the array, then echo from the processed array. Something like:

    PHP Code:
    $query mysql_query("select * from member where rank = '2'' "); 
    while(
    $result mysql_fetch_array($query)){ 
        
    $temparray[] = $result['code']; 
        
    $temparray[] = $result['amount']; 


    foreach( 
    $temparray as $key => $value ){ 
        
    // use the % (modulus (remainder after division)) to determine if the key is odd or even 
        
    if( $key%=== ){ 
        
    // it's even (or 0) --a customer number 
        
    $cust_no[$value] = isset($cust_no[$value])? $cust_no[$value] + $temparray[$key 1] : $temparray[$key 1]; 
        } 


    // echo results: 
    foreach( $cust_no as $k => $v ){ 
        echo 
    "<tr><td>    $k </td> <td> $v </td> </tr>"

    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    hemi519 (08-25-2012)

  4. #3
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Hi,

    Try this query...i think it's helpful

    $query = mysql_query("select max(amount),code from member group by code where rank = '2'");

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •