Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Display results from array without duplicates

    This is my code:

    PHP Code:
    $result mysql_query("SELECT * FROM area ORDER BY date") or die("Error: " mysql_error());
    $parent = array();
    while(
    $row mysql_fetch_assoc($result)){
    foreach(
    $row as $value){
        
    $result explode(","$value);


        foreach(
    $result as $newvalue){
        
    $query="SELECT c_name FROM clients where clients.x='$newvalue'";


        
    $res mysql_query($query);
        
    $r mysql_fetch_assoc($res);
        
    $parent[] = $r['c_name'];
        
    $list implode($parent",");
        
        }}
        

        
    echo 
    "<tr>";   
    echo 
    "<td>" . ( $row['name']) . "</td>";
    echo 
    "<td>" $list "</td>";
    echo 
    "<td>" . ( $red['date']) . "</td>";
    echo 
    "</tr>"
    and it shows me area name and date correctly but list of clients contains results that are in database but also the ones that are not selected or same as in other row.
    .

    What i am doing wrong?

    Thank you
    Last edited by africa; 03-04-2013 at 03:17 PM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Your clients are stored in your database individually, or several of them all in one column?

    each client name is a row in the database ...

    ALUCEL
    AGIT
    NATIS


    or the 'clients' column contains several in one column ....

    ALUCEL, AGIT, NATIS


    Which way is it being done?

  • #3
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Its several in one column ALUCEL, AGIT, NATIS but i store id's.
    So it looks like 453,145,1665 and then i explode that and display names of clients from different table based on those id's. I know its not good solution and database should be normalized but i need it like this for now.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    The first thing you are doing wrong is a bad database design. Since it is not normalized you have very little control over data within it and will be guaranteed anomalies throughout every operation you attempt to perform. If you think select is bad, wait until you need to update one of the "child" records since you will now need to select every record in order to do so. You will also end up with exponential growth of the query counts, which isn't a good thing. If you normalize the database and issue a single joined query, than this type of issue will go away.

    As for what you have, that is caused by the $parent not being initialized on each iteration, so all you are doing is appending to it within the inner foreach. Simply initialize the $parent = array() at the start of the while.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    africa (03-04-2013)

  • #5
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    For some reason edit button deleted my first post but thanks Fou-Lu. That solved duplicate data

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yeah, since you only have like 4 posts, if you modify a post that contains a link it will go into moderation queue. It's also a bit of a pain to get it out since we can't use the inline tools to take care of it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

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