Current location: Hot Scripts Forums » Programming Languages » PHP » mysql_query Select * but ignore duplicates


mysql_query Select * but ignore duplicates

Reply
  #1 (permalink)  
Old 04-20-10, 09:17 AM
ben_johnson1991 ben_johnson1991 is offline
Newbie Coder
 
Join Date: Dec 2009
Posts: 11
Thanks: 4
Thanked 0 Times in 0 Posts
Exclamation mysql_query Select * but ignore duplicates

Hey.. I have a table full of links, one of the fields is called page, this is page the link is printed on.
example
link --- Page
index.php --- index
about.php --- index
edit.php ---- account
addnew.php --- forum
contact.php --- index

so i want a script that would print only
index,account,forum
as index has been duplicated.

The reason for this is so then, on each separate page, i want to use a while that will print all the links listed with that 'page'

I've been told to use an array, but i haven't managed to figure it out.


I hope i've made this clear enough..

so basically id end up with


PAGE: index
-index.php
-about.php
-contact.php

PAGE:account
-edit.php

PAGE: forum
-addnew.php


Cheers for any help guys!!!
Reply With Quote
  #2 (permalink)  
Old 04-20-10, 05:11 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Your question is not clear at all.

So,

You want index.php, about.php, contact.php to show up on index.

And,

edit.php to show up on account.

And,

addnew.php to show up on forum.

OR,

You only want index.php, edit.php, addnew.php to show up on any page?

OR,

is:
Quote:
PAGE: index
-index.php
-about.php
-contact.php

PAGE:account
-edit.php

PAGE: forum
-addnew.php
what you want your menu to look like?
Reply With Quote
  #3 (permalink)  
Old 04-20-10, 08:10 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Nah, your question is perfectly clear.
PHP Code:

// The query
$sql 'SELECT `link`, `page` FROM `table` ORDERBY `page`';

// The result
$res mysql_query$sql );

// Initialize our `$pages` array
$pages = array();

// Loop the query result
while ( $row mysql_fetch_assoc$res ) )
{

    
// does the current `$pages` index exist?
    // example: if `$row['page']` is `index`, then $pages['index'] = array();
    
if ( ! isset( $pages$row['page'] ] ) )
    {
        
$pages$row['page'] ] = array();
    }

    
// Add the `link` to it's corresponding `page`
    
$pages$row['page'] ][] = $row['link'];

}

// See what we have
print_r$pages ); 
The print_r( $pages ); should give you something like this:
Code:
Array
(
    [index] => Array
        (
            [0] => index.php
            [1] => about.php
            [2] => contact.php
        )
    [account] => Array
        (
            [0] => edit.php
        )
    [forum] => Array
        (
            [0] => addnew.php
        )
)
So now, let's build out our menu using the $pages array:
PHP Code:

echo '<ul>';

foreach ( 
$pages as $page => $links )
{

    echo
        
'<li>',
        
'PAGE: '$page,
        
'<ul>';

    foreach ( 
$links as $link )
    {
        echo
            
'<li>',
            
'<a href="./'$link'">'$link'</a>',
            
'</li>';
    }

    echo
        
'</ul>',
        
'</li>';

}

echo 
'</ul>'
Which should give you something like this (only this is nested for readability):
Code:
<ul>
	<li>
		PAGE: index
		<ul>
			<li>
				<a href="./index.php">
					index.php</a>
			</li>
			<li>
				<a href="./about.php">
					about.php</a>
			</li>
			<li>
				<a href="./contact.php">
					contact.php</a>
			</li>
		</ul>
	</li>
	<li>
		PAGE: account
		<ul>
			<li>
				<a href="./edit.php">
					edit.php</a>
			</li>
		</ul>
	</li>
	<li>
		PAGE: forum
		<ul>
			<li>
				<a href="./addnew.php">
					addnew.php</a>
			</li>
		</ul>
	</li>
</ul>
__________________
The toxic ZCE

Last edited by Keith; 04-20-10 at 08:19 PM.
Reply With Quote
  #4 (permalink)  
Old 04-20-10, 09:49 PM
adrafa adrafa is offline
New Member
 
Join Date: Apr 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Thumbs up

Nice Code Keith!!!!
Exactly what I was looking for!
How do I add more "Childs" to it?

I have the following table:

region-name-date-count
abcde-aaaaa-02/01-180
abcde-aaaaa-02/02-120
abcde-aaaaa-02/03-100
abcde-bbbbb-02/01-98
abcde-bbbbb-02/02-140
abcde-bbbbb-02/03-100
abcde-ccccc-02/01-98
abcde-ccccc-02/02-140
abcde-ccccc-02/03-100
fghijk-ddddd-02/01-180
fghijk-ddddd-02/02-120
fghijk-ddddd-02/03-100
fghijk-eeeee-02/01-98
fghijk-eeeee-02/02-140
fghijk-eeeee-02/03-100

I'm loking for the results to be:

Region: abcde
Name: aaaaa-02/01-180
-02/02-120
-02/03-100
Name: bbbbb-02/01-98
-02/02-140
-02/03-100
Name: cccccc-02/01-98
-02/02-140
-02/03-100
Region: fghijk
Name: ddddd-02/01-180
-02/02-120
-02/03-100
Name: eeeee-02/01-98
-02/02-140
-02/03-100

Your code works perfect for the region and name part, I just can't figure out how to make name a "child" of region.

I will appreciate any enlightenment on this...
Thanks!
Reply With Quote
  #5 (permalink)  
Old 04-21-10, 09:47 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
This is untested as well, but it's pretty much the same thing with only an extra layer of nesting:
PHP Code:

/* - - - - - - - - - -
 * Get the data
 */

// The query
$sql 'SELECT `region`, `name`, `date`, `count` FROM `table` ORDERBY `region`, `name`';

// The result
$res mysql_query$sql );

// Initialize our `$regions` array
$regions = array();

// Loop the query result
while ( $row mysql_fetch_assoc$res ) )
{

    
// does the current `region` exist?
    // example: if `$row['region']` is `abcde`, then $regions['abcde'] = array();
    
if ( ! isset( $regions$row['region'] ] ) )
    {
        
$regions$row['region'] ] = array();
    }

    
// do the same as above, except for the `name` key
    
if ( ! isset( $regions$row['region'] ][ $row['name'] ] ) )
    {
        
$regions$row['region'] ][ $row['name'] ] = array();
    }

    
// add the date and count data to it's corresponding region/name
    
$regions$row['region'] ][ $row['name'] ][] = array(
        
'date'  => $row['date'],
        
'count' => $row['count'],
    );

}

/* - - - - - - - - - -
 * Build the list
 */

echo '<ul>';

foreach ( 
$regions as $region => $names )
{

    echo
        
'<li>',
        
'Region: '$region,
        
'<ul>';

    foreach ( 
$names as $name => $datas )
    {

        echo
            
'<li>',
            
'Name: '$name,
            
'<ul>';

        foreach ( 
$datas as $data )
        {
            echo
                
'<li>',
                
'-'$data['date'], '-'$data['count'],
                
'</li>';
        }

        echo
            
'</ul>',
            
'</li>';

    }

    echo
        
'</ul>',
        
'</li>';

}

echo 
'</ul>'
__________________
The toxic ZCE

Last edited by Keith; 04-21-10 at 09:53 AM.
Reply With Quote
The Following User Says Thank You to Keith For This Useful Post:
adrafa (04-23-10)
  #6 (permalink)  
Old 04-23-10, 10:03 PM
adrafa adrafa is offline
New Member
 
Join Date: Apr 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Thank you very much Keith, that code really worked. I've been searching all over and others suggestions took me to nesting tables, pivoting, etc., Very confusing. Your code is clean and it does what the OP asked and what I was looking for. Again, thank you
Reply With Quote
Reply

Bookmarks

Tags
array, duplicate, mysql, select


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
AJAX/PHP Chained Select - With Extra Function? xenia_jazz PHP 4 02-24-09 05:16 PM
Sending a DB query on a html link pcinfoman PHP 141 07-25-07 12:41 PM
change options of select based on selection in other select nassau JavaScript 3 08-31-06 08:00 AM
Multiple Select phppick JavaScript 3 04-11-05 12:09 AM
i have 3 select box this is urgent traceMe JavaScript 0 12-02-03 01:24 AM


All times are GMT -5. The time now is 08:14 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.