Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-04-2013, 02:02 PM   PM User | #1
africa
New to the CF scene

 
Join Date: Mar 2013
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
africa is an unknown quantity at this point
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..
africa is offline   Reply With Quote
Old 03-04-2013, 03:06 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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?
mlseim is offline   Reply With Quote
Old 03-04-2013, 03:21 PM   PM User | #3
africa
New to the CF scene

 
Join Date: Mar 2013
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
africa is an unknown quantity at this point
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.
africa is offline   Reply With Quote
Old 03-04-2013, 03:21 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
africa (03-04-2013)
Old 03-04-2013, 03:27 PM   PM User | #5
africa
New to the CF scene

 
Join Date: Mar 2013
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
africa is an unknown quantity at this point
For some reason edit button deleted my first post but thanks Fou-Lu. That solved duplicate data
africa is offline   Reply With Quote
Old 03-04-2013, 03:30 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
mysql, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:43 AM.


Advertisement
Log in to turn off these ads.