My user inputs different categories that have rates in the database, based on the categories selected. The code I have works, but if a user selects a category twice my code drops the duplicate value and repeats the values of the post from the top or places different rates for the wrong category. Sometimes the user will need to select duplicate categories. Any help would be appreciated thanks
$qty=$_POST['qty'];
$pcategories=$_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 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];
?>
For example the posted array values for $pcategories from user could be: Auto Parts, Bicycles, Biscuits, Ceiling Fans, Bicycles, Blenders, Biscuits, Artwork
select rate from database based on these post:
But after the implode the values look like this: Auto Parts, Bicycles, Biscuits, Ceiling Fans, Blenders, Artwork, Auto Parts, Bicycles.
So I am now getting the rate for these values and not in the order that the user posted, and in some cases it drops the duplicate all together, this only happens if there are duplicate values from the users input but the user may sometimes need to input a duplicate value.
I’ve searched trying to find if an array can be imploded with a duplicate value and keep the order of the posted array but i keep finding how to get rid of the duplicate value, but that’s not what i need. Any suggestions or help would be greatly appreciated.