Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
compare array items with db records.
Old 08-16-2006, 08:19 AM compare array items with db records.
dahak's Avatar
Skilled Talker

Posts: 60
Location: Romania
Trades: 0
i have a problem.
i have a function that generates some random codes and stores them into an array,after this is inserts them into the database.

i need a function that compares the data from the array with the database and see if there are any matches dont insert,so there wont be any duplicates in the db.
i have made this so far :
PHP Code:
    function verifyCodes$sClientID "" $sCampaignID ""$sCode )
    {
        
        
$arrConds = array();
        if( 
$sClientID != "" )
        {
            
$arrConds[] = "`client_id` = ".(int)$sClientID."";
        }
        if( 
$sCampaignID != "" )
        {
            
$arrConds[] = "`campaign_id` = ".(int)$sCampaignID."";
        }
        
$sQuery "SELECT `code` FROM `codes`";
        if ( 
count$arrConds ) > 
        {
            
$sQuery .= " WHERE ".implode" AND "$arrConds );
        }
        
$sQuery .= " AND ";
        
$arrCodeConds = array();
        
        foreach (
$sCode as $key => $value)
        {
            
$arrCodeConds[] = "code = '" .$value['code'] ."'";
        }
        
$sQuery .= implode(" OR " $arrCodeConds);
        
        
$this->oDB->query$sQuery );

        
    } 
i need this function to return an array with the codes that arent in the database.

thank you .
dahak is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-16-2006, 11:09 AM Re: compare array items with db records.
Ultra Talker

Posts: 483
Trades: 0
Not having tested this, I believe you're looking to replace:
Code:
foreach ($sCode as $key => $value)
{
    $arrCodeConds[] = "code = '" .$value['code'];
}
with something like:
Code:
foreach ($sCode as $key => $value)
{
    $arrCodeConds[] = "code NOT LIKE '" .$value['code'];
}
Also, when you implode your arrCodeConds with " OR ", you should do it with " AND " instead. That way, the query will return any codes that AREN'T in your database, as opposed to returning any codes that ARE in your database, which your query seemed to be heading towards...

Hope that helps.
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 08-17-2006, 02:29 AM Re: compare array items with db records.
dahak's Avatar
Skilled Talker

Posts: 60
Location: Romania
Trades: 0
Thanks,i've done it after all but your post was helpfull.
All the best.
dahak is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to compare array items with db records.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.15062 seconds with 12 queries