Select query not working with post array with duplicate valu
Posted: Sun May 15, 2016 6:35 pm
My user post an array of categories, then I need to select rates from a table based on the array of categories that the user selected. So far I have been able to get this to work fine.
Example:
Array Categories: Auto Parts, Bicycles, Biscuits, Ceiling Fans, Blenders, Artwork
Returned Array Rates: 60, 35, 25, 20,15, 5
Then I do calculations based on the rates selected in the rate array. The code i have below works fine if there is no duplicate values in array.
The problem I am having is if the user inputs a category twice the returned array is only returning the rate for one instance of the inputted category
Example:
Array Categories: Auto Parts, Bicycles, Biscuits, Ceiling Fans, Bicycles, Blenders, Biscuits, Artwork
Returned Array Rates: 60, 35, 25, 20, 15, 5
what it should be-> Array Rates: 60, 35, 25, 20, 35, 15, 25, 5
The array being returned is dropping the duplicate rate values but i need to do the calculations based on the rate in each row of the the returned array.
I don't now how to make that returned value stay in the returned array. any help would be great or any suggestion of another way I can do this.
Example:
Array Categories: Auto Parts, Bicycles, Biscuits, Ceiling Fans, Blenders, Artwork
Returned Array Rates: 60, 35, 25, 20,15, 5
Then I do calculations based on the rates selected in the rate array. The code i have below works fine if there is no duplicate values in array.
The problem I am having is if the user inputs a category twice the returned array is only returning the rate for one instance of the inputted category
Example:
Array Categories: Auto Parts, Bicycles, Biscuits, Ceiling Fans, Bicycles, Blenders, Biscuits, Artwork
Returned Array Rates: 60, 35, 25, 20, 15, 5
what it should be-> Array Rates: 60, 35, 25, 20, 35, 15, 25, 5
The array being returned is dropping the duplicate rate values but i need to do the calculations based on the rate in each row of the the returned array.
I don't now how to make that returned value stay in the returned array. any help would be great or any suggestion of another way I can do this.
Code: Select all
$qty=mysql_real_escape_string($_POST['qty'];
$pcategories=mysql_real_escape_string($_POST['pcategories'];
<?php foreach($qty as $a => $b){ ?>
<?php // Get the duty rate based on the product categorie user selected
$connection = mysqli_connect("localhost","root","","customs") or `enter
code here`die("Error " . mysqli_error($connection));
$sql = "
SELECT `categories`, `rate`
FROM `lt_products`
WHERE `categories` IN ('".implode("','",$pcategories)."')
ORDER BY FIELD(categories, '".implode("','",$pcategories)."')";
$result = mysqli_query($connection, $sql)or die(mysql_error());
while($row = mysqli_fetch_assoc($result)) {
$row_rate[] = $row["rate"];
}
$rate_row[] = $row_rate[$a];
?>