Jump to content

[SOLVED] Exploding distinct array


jaxdevil

Recommended Posts

Ok, I have a field with multiple categories and subcateories, split with a delimeter. I am using an explode to split them up from the delimiter, selecting the first category before the first delimeter, and displaying all the unique ones, below is my code, but something is going wrong now, I copied it to a new page and modifie, td it some, and there is multiple instances of it on the same page. I modified the names so it wouldn't interfere with each other, but something is wrong. On the `cats` query, the second one on the page, it only pulls up three rows. There are 5. Now I have noticed there is an extra blank row that keeps coming up on all of them on the main pages, except on this particular query, the second one, it has no blank row, the query above it using the same code is pulling up the 2 unique first instances, and then the 3rd blank row, now this query is showing three rows. I think that has something to do with it. Three rows on the first one, and then three rows limited on the second, something looks like it is linked some how. Below is my code, I am trying to figure out how to have all the results come up not just those first three.

 

<tr>
<td>
Supplier:
</td>
<td>
<SELECT NAME="supplier">
<OPTION VALUE="NONE">-----Select Product Supplier----
<?php
$sql = "SELECT DISTINCT `supplier` FROM products" or die ( "Query failed due to: ".mysql_error());
$query = mysql_query($sql);
$seen = array();

while($row = mysql_fetch_array($query)) {
    $supplier = explode("|",$row['supplier']);
    $list_suppliers = $supplier[0];
    if(!in_array($list_suppliers,$seen) && !in_array($list_suppliers,$multiple))
    {
        array_push($seen,$list_suppliers);
    }
    else
    {
        array_push($multiple,$list_suppliers);
        unset($seen[$list_suppliers]);
    }
}
$size = sizeof($seen);
for($i=0;$i<=$size;$i++)
{
?>
<OPTION VALUE="<?=$seen[$i]?>"><?=$seen[$i]?>
<?php
}
?>
</SELECT>
</td>
</tr>
<tr>
<td>
Category:
</td>
<td>
<SELECT NAME="cats">
<OPTION VALUE="NONE">-----Select Category----
<?php
$sql2 = "SELECT DISTINCT `cats` FROM products" or die ( "Query failed due to: ".mysql_error());
$query2 = mysql_query($sql2);
$seen2 = array();

while($row2 = mysql_fetch_array($query2)) {
    $cats = explode("|",$row2['cats']);
    $list_cats = $cats[0];
    if(!in_array($list_cats,$seen2) && !in_array($list_cats,$multiple))
    {
        array_push($seen2,$list_cats);
    }
    else
    {
        array_push($multiple,$list_cats);
        unset($seen2[$list_cats]);
    }
}
$size2 = sizeof($seen2);
for($a=0;$a<=$size;$a++)
{
?>
<OPTION VALUE="<?=$seen2[$a]?>"><?=$seen2[$a]?>
<?php
}
?>
</SELECT>
</td>
</tr>

Link to comment
Share on other sites

I would recommend adding some debugging code.  The first to add would be inside each of the branches inside the "cats" loop - something like this:

 

    if(!in_array($list_cats,$seen2) && !in_array($list_cats,$multiple))
    {
        print "$list_cats is a new cat<br>";
        array_push($seen2,$list_cats);
    }
    else
    {
        print "$list_cats is a multiple cat<br>";
        array_push($multiple,$list_cats);
        unset($seen2[$list_cats]);
    }

 

I notice you re-use $multiple without renaming or initializing it - that may or may not be the problem.

Link to comment
Share on other sites

try

<?php
while($row = mysql_fetch_array($query)) {
    $supplier = explode("|",$row['supplier']);
    $tmp_list_suppliers[] = $supplier[0];
//    if(!in_array($list_suppliers,$seen) && !in_array($list_suppliers,$multiple))
//    {
//        array_push($seen,$list_suppliers);
//    }
//    else
//    {
//        array_push($multiple,$list_suppliers);
//        unset($seen[$list_suppliers]);
    }
$tmp =array_count_values($tmp_list_suppliers);
$seen = array_keys($tmp,1);
$multiple = array_unique(array_diff($tmp_list_suppliers, $seen));

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.