Jump to content

Duplicates in loop


carlsenjesper

Recommended Posts

Hi All

 

Please help me figure this one out as I am completely new to PHP and defiantly not going to win any trophies in coding efficiency. 

 

I am pulling my CastingCartTalentID from WordPress and then using that ID to look up a category in another table in WordPress.

All CastingCartTalentID's are returned just fine, but since my Categories are being looked up within the nested foreach loop, I end up with duplicated Categories:-(

The Categories are finally assigned to buttons in HTML, but I don't want to have duplicated buttons on the page.

 

 

 

$myTalentID = $wpdb->get_results("SELECT CastingCartTalentID FROM ". table_agency_castingcart ." WHERE CastingJobID = '$mycustomID'");
foreach ($myTalentID as $obj){
$myID = $obj->CastingCartTalentID;
$mySort = $wpdb->get_results("SELECT DISTINCT B1.ProfileCustomValue FROM ". table_agency_customfield_mux ." B1, ". table_agency_customfields ." A1 WHERE B1.ProfileID = '$myID' AND A1.ProfileCustomTitle = 'Category' AND A1.ProfileCustomID = B1.ProfileCustomID");
foreach ($mySort as $obj){

$myCat = $obj->ProfileCustomValue;

echo "<td><button class=\"filter\" data-filter=\".category-$myCat\">$myCat</button></td>";

}}

 

When I print_r my $mySort I get the following:

 

Array ( [0] => stdClass Object ( [ProfileCustomValue] => B ) )

Array ( [0] => stdClass Object ( [ProfileCustomValue] => A ) )

Array ( [0] => stdClass Object ( [ProfileCustomValue] => C ) )

Array ( [0] => stdClass Object ( [ProfileCustomValue] => B ) )

 

I have tried using array_unique but that dosnt help me. Is there another way to solve this? or is there a solution to remove duplicates?

 

Please any help is appreciated

 

 

Link to comment
https://forums.phpfreaks.com/topic/294718-duplicates-in-loop/
Share on other sites

Defiantly??

 

1 - perhaps this would be better posted in a wordpress forum?

 

2 - when posting code here (and in most forums) you should use the appropriate tags to present it better. Here that would be "code" and "/code" wrapped in square brackets.

 

3 - code that is formatted more like code and not a paragraph of text is easier to read, interpret and understand for those who are unfamiliar with it, by far.

Link to comment
https://forums.phpfreaks.com/topic/294718-duplicates-in-loop/#findComment-1506142
Share on other sites

You should take some time to learn how to use a database - specifically JOINs. You should avoid running queries in loops like that guy that hangs around the elementary school. Also, if you are going to give a table an alias, use something that is descriptive rather than "A1" & "B1". The DISTINCT isn't really doing what it could because you are running separate queries. Run ONE query.

 

 

After analyzing the two queries, I think this should work

 

$query = "SELECT DISTINCT cfmux.ProfileCustomValue
          FROM " . table_agency_customfield_mux ." cfmux
          JOIN " . table_agency_castingcart ." ccart
            ON cfmux.ProfileID = ccart.CastingCartTalentID
          JOIN " . table_agency_customfields ." cfields
            ON cfields.ProfileCustomID = cfmux.ProfileCustomID
          WHERE cfmux.ProfileID = '{$myID}'
            AND ccart.CastingJobID = '{$mycustomID}'
            AND cfields.ProfileCustomTitle = 'Category'";
$results = $wpdb->get_results($query);
 
foreach ($results as $obj)
{
    $myCat = $obj->ProfileCustomValue;
    echo "<td><button class=\"filter\" data-filter=\".category-{$myCat}\">{$myCat}</button></td>\n";
}
Link to comment
https://forums.phpfreaks.com/topic/294718-duplicates-in-loop/#findComment-1506148
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.