Jump to content

comparing arrays


InfinityRogue

Recommended Posts

Hi,
I have a situation where i have a customer that first selects which products he wants and then it stores it in a database. He can then choose to edit his order and i would like to display what he has already selected previously instead of him having to redo the entire order.

So, i now have these two arrays: one which is the list of products, and one that is the selection he has already made.

To make the example easy, lets say that i have an array for products called p={1,2,3,4,5} where the numbers are the products. Then i have an array for the selection called s={1,2,4}.

Anything that the two arrays have in common will be displayed as a number, while the remaining products just are displayed with the text SELECT in the dropdown box i have in html. I dont have a problem writing the stuff out in Html. I have a problem comparing the two arrays and getting the right result.

The end result should be: 1 2 SELECT 4 SELECT

I have this so far:

[code]
$query  = "SELECT * FROM dprices WHERE companyname='$companyname'";  //Get the list of products
$result = mysql_query ($query);

while ($query_data = @mysql_fetch_array ($result))
{
    $productsku  = $query_data["productsku"];  //Here i take the first product in the productlist

    $querytemporder    = "SELECT * FROM temporder WHERE companyname = '$companyname'";
    $resulttemporder  = mysql_query ($querytemporder);    //Get the selected products

    while ($query_temporder = @mysql_fetch_array ($resulttemporder))
    {
            $orderproduct  = $query_temporder['productsku'];  //Here i get the selected products to
                                                                                  compare to the productlist

    if($orderproduct == $productsku)
            {
                ......  //Here is what it writes out in html ...
[/code]

How can i do this? (Let me know if i haven't explained it well enough)

I appreciate any help i can get.


Link to comment
Share on other sites

well, based on your simple example, you could do something like this:

[code=php:0]
do {
   if(in_array(current($s),$p)) echo "SELECT";
   else echo current($s);
}while(next($s));
[/code]

in_array() just returns true/false. If you need the corresponding key, you might want to use array_search() instead. Just keep in mind that the first key is generally zero.
Link to comment
Share on other sites

Sorry, i guess i was wrong about having two arrays. Apparently i only have 2 rows at a time.

[code]
$query  = "SELECT * FROM dprices WHERE companyname='$companyname'";
$result = mysql_query ($query);
           
$query_data = @mysql_fetch_array ($result);
     
$querytemporder    = "SELECT * FROM temporder WHERE companyname = '$companyname'";
$resulttemporder  = mysql_query ($querytemporder);

$query_temporder = @mysql_fetch_array ($resulttemporder);
[/code]

So when i tried your suggestion, nothing was written. I'm guessing since i can only compare 2 items at a time. Any suggestions as to how i can do it then?
Link to comment
Share on other sites

No, i'm getting my elements from a database, so i'm calling the database first for both arrays. But as they come in it's getting it from mysql_fetch_array which i believe only takes in one row at a time. Is there a way that i can get all the rows from the database and create an array from that? Sorry, a bit new at this.
Link to comment
Share on other sites

perhaps i should paste everything i have right now cause i'm not sure i'm getting the right items.

[code]
$query  = "SELECT * FROM dprices WHERE companyname='$companyname'";
$result = mysql_query ($query);

//$query_data = @mysql_fetch_array ($result);
while($newresult[$i++]=mysql_fetch_array($result));
           
$querytemporder    = "SELECT * FROM temporder WHERE companyname = '$companyname'";
$resulttemporder  = mysql_query ($querytemporder);

//$query_temporder = @mysql_fetch_array ($resulttemporder);
while($newresulttemporder[$i++]=mysql_fetch_array($resulttemporder));

do
{
    if(in_array(current($newresulttemporder),$newresult))
    {
          $price = $query_data["price"];

  if($price != 0)
  {
$orderproduct  = $query_temporder["productsku"];
$orderquantity = $query_temporder["value"];

$queryprod    = "SELECT * FROM products WHERE productsku='$orderproduct'";
$resultprod    = mysql_query($queryprod);

$query_prod    = @mysql_fetch_array($resultprod);

$frequency    = $query_prod["frequency"];
$description  = $query_prod["description"];
$color        = $query_prod["color"];

echo "<tr>\n";
echo "<td width=\"110\" height=\"20\" align=\"center\">$orderproduct</td>\n";
echo "<td width=\"80\" height=\"20\" align=\"center\">$frequency</td>\n";
echo "<td width=\"210\" height=\"20\" align=\"center\">$description</td>\n";
echo "<td width=\"80\" height=\"20\" align=\"center\">$color</td>\n";
echo "<td width=\"110\" height=\"20\" align=\"center\">
                <select name=\"prod_$orderproduct\">
                <option value=\"$orderquantity\">$orderquantity</option>
                <option value=\"select\">SELECT</option>";

for($i=1; $i <= 50; $i++)
{
$quantity = $query_prod["quantity$i"];

if (($quantity != 0) && ($quantity != $orderquantity))
{
echo "<option value=\"$quantity\">$quantity</option>";
}
}

echo "</select></td>\n";
echo "<td width=\"110\" height=\"20\" align=\"center\">$price</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td colspan=\"6\" height=\"20\">
                <hr size=\"2\" width=\"680\" color=\"black\" align=\"center\"></td>\n";
echo "</tr>\n";
}
    }
    else
    {
$price        = $query_data["price"];

        if($price != 0)
{
$productsku    = $query_data["productsku"];
   
$orderquantity = $query_temporder["value"];

$queryprod  = "SELECT * FROM products WHERE productsku='$productsku'";
$resultprod  = mysql_query($queryprod);

$query_prod  = @mysql_fetch_array($resultprod);

$frequency  = $query_prod["frequency"];
$description = $query_prod["description"];
$color      = $query_prod["color"];

echo "<tr>\n";
echo "<td width=\"110\" height=\"20\" align=\"center\">$productsku</td>\n";
echo "<td width=\"80\" height=\"20\" align=\"center\">$frequency</td>\n";
echo "<td width=\"210\" height=\"20\" align=\"center\">$description</td>\n";
echo "<td width=\"80\" height=\"20\" align=\"center\">$color</td>\n";
echo "<td width=\"110\" height=\"20\" align=\"center\">
                <select name=\"prod_$productsku\">
                <option value=\"select\">SELECT</option>";

for($i=1; $i <= 50; $i++)
{
$quantity = $query_prod["quantity$i"];

if ($quantity != 0)
{
echo "<option value=\"$quantity\">$quantity</option>";
}
}

echo "</select></td>\n";
echo "<td width=\"110\" height=\"20\" align=\"center\">$price</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td colspan=\"6\" height=\"20\">
                <hr size=\"2\" width=\"680\" color=\"black\" align=\"center\"></td>\n";
echo "</tr>\n";
}
    }
}
while(next($newresulttemporder));

[/code]

Sorry for the mess, but like i said i'm new to this hehe. Even if i added the line that you gave me last, i'm no longer sure how i would get the individual elements from the row in the database now.

As you can see the only difference in how it is written out in html is what is shown in <select>
Link to comment
Share on other sites

okay... try this... remove the last while() line of code, but leave the closing curly bracket.

remove the $i++ from the second while line. remove the semi-colon and remove the do statement.

I didn't mess with any of your inner variables (like the $query_data["price"] thing) because I really don't know what array that is supposed to be associated with. Once you've got that polished up, holler back.

PS> since you're refering to the arrays by their key names (and not their key numbers), I'd encourage you to use mysql_fetch_assoc instead of mysql_fetch_array (which will return both the key name and number -- which can confuse your while loops)

here's the completed code:[code]<?
$query  = "SELECT * FROM dprices WHERE companyname='$companyname'";
$result = mysql_query ($query);
//$query_data = @mysql_fetch_array ($result);
while($newresult[$i++]=mysql_fetch_array($result));
$querytemporder    = "SELECT * FROM temporder WHERE companyname = '$companyname'";
$resulttemporder   = mysql_query ($querytemporder);
//$query_temporder = @mysql_fetch_array ($resulttemporder);
while($newresulttemporder=mysql_fetch_array($resulttemporder)) {
if(in_array(current($newresulttemporder),$newresult)) {
$price = $query_data["price"];
if($price != 0) {
$orderproduct  = $query_temporder["productsku"];
$orderquantity = $query_temporder["value"];
$queryprod     = "SELECT * FROM products WHERE productsku='$orderproduct'";
$resultprod    = mysql_query($queryprod);
$query_prod    = @mysql_fetch_array($resultprod);
$frequency     = $query_prod["frequency"];
$description   = $query_prod["description"];
$color         = $query_prod["color"];
echo "<tr>\n";
echo "<td width=\"110\" height=\"20\" align=\"center\">$orderproduct</td>\n";
echo "<td width=\"80\" height=\"20\" align=\"center\">$frequency</td>\n";
echo "<td width=\"210\" height=\"20\" align=\"center\">$description</td>\n";
echo "<td width=\"80\" height=\"20\" align=\"center\">$color</td>\n";
echo "<td width=\"110\" height=\"20\" align=\"center\">
                <select name=\"prod_$orderproduct\">
                <option value=\"$orderquantity\">$orderquantity</option>
                <option value=\"select\">SELECT</option>";

for($i=1; $i <= 50; $i++) {
$quantity = $query_prod["quantity$i"];
if (($quantity != 0) && ($quantity != $orderquantity)) {
echo "<option value=\"$quantity\">$quantity</option>";
}
}
echo "</select></td>\n";
echo "<td width=\"110\" height=\"20\" align=\"center\">$price</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td colspan=\"6\" height=\"20\">
                <hr size=\"2\" width=\"680\" color=\"black\" align=\"center\"></td>\n";
echo "</tr>\n";
}
    } else {
$price = $query_data["price"];
        if($price != 0) {
$productsku    = $query_data["productsku"];
$orderquantity = $query_temporder["value"];
$queryprod   = "SELECT * FROM products WHERE productsku='$productsku'";
$resultprod  = mysql_query($queryprod);
$query_prod  = @mysql_fetch_array($resultprod);
$frequency   = $query_prod["frequency"];
$description = $query_prod["description"];
$color       = $query_prod["color"];
echo "<tr>\n";
echo "<td width=\"110\" height=\"20\" align=\"center\">$productsku</td>\n";
echo "<td width=\"80\" height=\"20\" align=\"center\">$frequency</td>\n";
echo "<td width=\"210\" height=\"20\" align=\"center\">$description</td>\n";
echo "<td width=\"80\" height=\"20\" align=\"center\">$color</td>\n";
echo "<td width=\"110\" height=\"20\" align=\"center\">
                <select name=\"prod_$productsku\">
                <option value=\"select\">SELECT</option>";

for($i=1; $i <= 50; $i++) {
$quantity = $query_prod["quantity$i"];
if ($quantity != 0) {
echo "<option value=\"$quantity\">$quantity</option>";
}
}
echo "</select></td>\n";
echo "<td width=\"110\" height=\"20\" align=\"center\">$price</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td colspan=\"6\" height=\"20\">
                <hr size=\"2\" width=\"680\" color=\"black\" align=\"center\"></td>\n";
echo "</tr>\n";
}
}
}
?>[/code]
Link to comment
Share on other sites

grrr it's not doing it. I think the problem is in the arrays. The first array which selects from dprices, i should probably just get productsku and price. From the second array i should probably just get productsku and quantity.

So im trying to compare the two productskus. Problem is that now it is comparing them but just writing out the one that they dont have in common... twice.

i have p={1,2,3,4,5} and s={1,2,4} (numbers being the productskus from each table in the database)

i think the problem is that i cant get to the productsku and price in one, and productsku and quantity in the other. (price and quantity will be used later for beauty purposes).

Link to comment
Share on other sites

It's important to note that the $i++ part is making your array multi-dimensional. That means you can no longer refer to the array without specifying the original result's line number.

for example, if you captured $query_data[$i++]=mysql_fetch... then to get the price, you'd have to specify the line number, too: $query_data[12]["price"] -- does that make sense?
Link to comment
Share on other sites

There may be a way to tweak your database and have it do most of the work for you. If it can be done, it would dramatically reduce the amount of PHP coding you're doing. Unfortunately, it's a stretch of my abilities, but one of the mysql experts here might be able to help. Perhaps post your mysql table structure with the same question under the mysql forum:

http://www.phpfreaks.com/forums/index.php/board,3.0.html
Link to comment
Share on other sites

i found out that i wasnt creating any arrays from what i got from the database so now i changed it to this but somehow for p={1,2,3,4,5} and s={1,2,4} for example it keeps writing out 1, 3 times.

[code]
$query  = "SELECT productsku,price FROM dprices WHERE companyname='$companyname'";
$result = mysql_query ($query);
           
while ($query_data = @mysql_fetch_array ($result))
{
$productsku = $query_data['productsku'];
$price      = $query_data['price'];
           
if($price != 0)
{
$productArray[] = $productsku;
           
$querytemp  = "SELECT productsku FROM temporder WHERE companyname='$companyname'";
$resulttemp = mysql_query ($querytemp);

while ($query_temp = @mysql_fetch_array ($resulttemp))
{
$selectproductsku = $query_temp['productsku'];
        $selectArray[$productsku] = $selectproductsku;

//print $selectArray['productsku'];
}
}
}
           
foreach($productArray as $index => $valueArray)
{
if ( isset($selectArray[$valueArray]))
{
// then do something because it is found
echo $selectArray[$valueArray];
}
else
{
// use the product values
echo "select";
}
}
[/code]
Link to comment
Share on other sites

it's still doing it wrong. When i'm creating $productArray i would get a value like:

$productArray[0] = "1"; (where 1 is the productnumber or productsku)

and for $selectArray i would have:

$selectArray[1] = "1"; (where 1 is the productsku of the product and 1 is the selectedproductsku)

I think it might go wrong in the foreach loop where it says isset($selectArray[$valueArray]. It's somehow not comparing it right. Maybe i'm not understanding this whole multidimensional array stuff.
Link to comment
Share on other sites

Hi ~
Iam Noobie ^^

but check this:

if you want show only the list of selected items ~
[code]
$query = "SELECT * FROM dprices p, temporder t
  WHERE p.companyname = t.companyname
  AND p.companyname = '" . $companyname . "'
  AND p.productsku = t.productsku
  AND p.price > 0";

$result = mysql_query ($query);
if (!$result)
{
echo "Query ERROR: " . mysql_error ();
exit ();
}
while ($query_data = mysql_fetch_array ($result))
{
echo "this item is selected: " . $query_data['productsku'] . " Price: " . $query_data['price'] . "<br />";
}
[/code]

and if you want show the list of selected items and no selected items
[code]
$query = "SELECT *, t.productsku as productskuSelected
  FROM dprices p LEFT JOIN temporder t
ON ( p.productsku = t.productsku AND p.companyname = t.companyname)
  WHERE
  AND p.companyname = '" . $companyname . "'
  AND p.price > 0";

$result = mysql_query ($query);
if (!$result)
{
echo "Query ERROR: " . mysql_error ();
exit ();
}
while ($query_data = mysql_fetch_array ($result))
{
if ($query_data['productskuSelected'] != NULL)
{
echo "this item is selected: " . $query_data['productsku'] . " Price: " . $query_data['price'] . "<br />";
}
else
{
echo "this item is not selected: " . $query_data['productsku'] . " Price: " . $query_data['price'] . "<br />";
}
}
[/code]

I hope this code and help you ~
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.