I have a table of food where I store category ids as coma separated like in the below image
food table
I made a method in model which takes array (category ids) as argument and fetches the food items which have ids matching to the array from argument.
I made the below query
if(count($categoryIds) > 0 && count($allergyIds) == 0 ){
$tempArr = array();
foreach($categoryIds as $eachCategoryId){
$sql = "Select food.food_id,food_name,food_image,food_thumbnail,description,food_variations.price as price,is_active
from food
join food_variations on food_variations.food_id = food.food_id
where FIND_IN_SET($eachCategoryId,category_id)
and food.restaurant_id = $restaurantId
and food.is_active = 1
and food.is_deleted = 0
and food.food_status = 3
and food_variations.food_variation_id = food.default_food_variation_id";
$result = $this->db->query($sql)->result_array();
array_push($tempArr, $result);
}
echo "<pre>";print_r($tempArr);
}
Below is the result of above query
Array
(
[0] => Array
(
[0] => Array
(
[food_id] => 10
[food_name] => Rama Mckee
[food_image] =>
[food_thumbnail] =>
[description] => asdfs
[price] => 34
[is_active] => 1
)
[1] => Array
(
[food_id] => 6
[food_name] => Rishi
[food_image] =>
[food_thumbnail] =>
[description] => test
[price] => 120
[is_active] => 1
)
[2] => Array
(
[food_id] => 5
[food_name] => test
[food_image] => http://localhost/gastroapp/assets/uploads/food_images/a5918726b920e7cbfc7f90e1afc48091.jpg
[food_thumbnail] => http://localhost/gastroapp/assets/uploads/food_images/thumb/a5918726b920e7cbfc7f90e1afc48091.jpg
[description] => test
[price] => 120
[is_active] => 1
)
)
[1] => Array
(
[0] => Array
(
[food_id] => 10
[food_name] => Rama Mckee
[food_image] =>
[food_thumbnail] =>
[description] => asdfs
[price] => 34
[is_active] => 1
)
[1] => Array
(
[food_id] => 7
[food_name] => ezhva
[food_image] =>
[food_thumbnail] =>
[description] => ddsfsd
[price] => 20
[is_active] => 1
)
[2] => Array
(
[food_id] => 8
[food_name] => test
[food_image] =>
[food_thumbnail] =>
[description] => test
[price] => 22
[is_active] => 1
)
[3] => Array
(
[food_id] => 6
[food_name] => Rishi
[food_image] =>
[food_thumbnail] =>
[description] => test
[price] => 120
[is_active] => 1
)
)
)
I am getting duplicate results and I think this might also cause performance issues.
The below was the query when I had only one category per food which was giving me desired result.
return $this->db->select('food.food_id,food_name,food_image,food_thumbnail,description,food_variations.price as price,is_active')
->from('food')
->join('food_variations', 'food_variations.food_id = food.food_id')
->where_in('category_id',$categoryIds)
->where('food.restaurant_id', $restaurantId)
->where('food.is_active', '1')
->where('food.is_deleted', '0')
->where('food.food_status','3')
->where('food_variations.food_variation_id IN( select food_variation_id from food_variations where food_variation_id = food.default_food_variation_id )')
->get()
->result_array();
Please help.
First You need to insert your list in an array
$list = {ids column}
$list = array_map("intval", explode(",", str_replace(',' , '', $list)));
Now your list is saved in an array $list
Now you can call it
foreach($list as $value) {
$sql = "SELECT * FROM menu WHERE user_id = '$user_id' AND id = '$value' AND deleted = '0' AND active = '1';";
$result = mysqli_query($dBconnection, $sql);
$check = mysqli_num_rows($result);
if ($check>0) {
while($row = mysqli_fetch_assoc($result)) {
cho $row['id'];
}
} else {
echo 'empty list';
}
}
For mysql, string column is recommended for saving static data instead of adding just IDs. Instead of just integer, you will have to store the object with relevant values. If you want it's relational data, its better to use an intermediate table.
like U need an extra table category_products table to store category_id and product_id since You have many-to-Many relationships(a product have many category and a category have many products)
Related
Array:
$ecodesAr = Array (
[0] => 1Z0-060
[1] => 98-375
[2] => 98-368
[3] => 98-367 )
for($k=0; $k<count($ecodesAr); $k++){
$arrayTB[$k] = $this->Functions->exam('title', $ecodesAr[$k]); }
Modal code:
public function exam($q, $d) {
$q = $this->db->where($q, $d)
->get('exams');
return $q->row();
}
Result:
Array (
[0] =>
[1] =>
[2] =>
[3] => stdClass Object ( [id] => 1091 [hot_exam] => 0 [top_exam] => 0 [category] => 114 [subcats] => 288 [slug] => 98-367 [sku] => OI5Ao [title] => 98-367 [name] => MTA Security Fundamentals [update] => 2021-09-16 [regular_price] => 130 [sale_price] => 59 [on_homepage] => 0 [on_request] => 0 [expired] => 0 [path_slug] => 98-367.pdf [questions] => 123 [demo_slug] => 98-367-demo.pdf [prc_price] => 65 [prc_demo] => 98-367-demo [prc_exam] => 98-367 [is_active] => 1 )
)
The First 3 values are skiped in the output and just the last value got, I want to all array data against values please help anyone!
$productsarry = Array (
[0] => Milk
[1] => Cream
[2] => Sugar
[3] => Yougert
);
for($k = 0; $k < count($productsarry); $k++) {
$arrayTB[$k] = query("select slug, qty, name, price from exams where title ='$ecode'")->row();
}
I make a query to the database and I print this. As you can see, the page_id => 2 can be made into one array.
Array
(
[page_id] => 3
[name] => Add Me
[article_category] => Celebrities
[count_article_category] => 1
[average_ctr] => 5.555560111999512
)
Array
(
[page_id] => 2
[name] => I Too Had a Love Story
[article_category] => Celebrities
[count_article_category] => 7
[average_ctr] => 2.525434238570077
)
Array
(
[page_id] => 2
[name] => I Too Had a Love Story
[article_category] => Desi
[count_article_category] => 1
[average_ctr] => 2.892319917678833
)
Array
(
[page_id] => 2
[name] => I Too Had a Love Story
[article_category] => Lifestyle
[count_article_category] => 1
[average_ctr] => 2.3632400035858154
)
Array
(
[page_id] => 2
[name] => I Too Had a Love Story
[article_category] => Sports
[count_article_category] => 1
[average_ctr] => 2.150439977645874
)
Array
(
[page_id] => 1
[name] => Indian Celebrities
[article_category] => Celebrities
[count_article_category] => 2
[average_ctr] => 2.7356200218200684
)
Array
(
[page_id] => 1
[name] => Indian Celebrities
[article_category] => Hilarious
[count_article_category] => 1
[average_ctr] => 0.7684919834136963
)
I want the array to look like this.
[2]
(
[Celebrities] => 7
[Lifestyle] => 1
[Desi] => 1
)
I tried the following code which runs in the foreach loop. I understand that my logic or my coding is wrong, but I am not able to comprehend my mistake.
Please do help.
if (isset($data['page_id']))
{
if (array_key_exists($data['page_id'], $pages_data))
{
echo "<pre>";
var_dump($pages_data);
$pages_data[$data['page_id']] += array(
$data['article_category'] => $data['count_article_category'],
);
} else {
// $pages_data[$data['article_category']] = $data['average_ctr'];
// $pages_dat a[$data['page_id']] = $data['page_id'];
$pages_data[$data['page_id']] = array(
$data['article_category'] => $data['count_article_category'],
);
}
echo "<pre>";
print_r($pages_data);
}
Okay, here we go:
$pages_data = array();
foreach ($datas as $data) {
if (isset($data['page_id'])) {
// create new element for $pages_data[$data['page_id']] if it does not exists
if (!isset($pages_data[$data['page_id']])) {
$pages_data[$data['page_id']] = array();
}
// if it exists - just add certain values:
$pages_data[$data['page_id']][$data['article_category']] = $data['count_article_category'];
}
}
I have a problem with some kind of 'webshop' I'm building
I add some products in the database but I want to only change the quantity when the product already exists.
I only don't know how to achieve this. Can somebody maybe help me with a function to check if the product already exists so I can add a +1 to ['aantal'] everytime I add a product that already exists?
the post is:
if(isset($_POST['submit-bonbon'])){
$postGewicht = $_POST['gewicht'];
$bonbonName = $_POST['product'];
$image = $_POST['afbeelding'];
$quantity = $_POST['aantal'];
$jsonData = unserialize($_SESSION['json_data']);
if(count($jsonData,1) >= 1){
$jsonData['bonbons'][] = array('gewicht' => $postGewicht,'name' => $bonbonName,'afbeelding' => $image, 'aantal' => $quantity);
} else {
$bonbonsArray[] = array('gewicht' => $postGewicht,'name' => $bonbonName,'afbeelding' => $image, 'aantal' => $quantity);
$order = array('userId' => $bonbonUser, 'bonbons' => $bonbonsArray);
$jsonData = $order;
}
$_SESSION['json_data'] = serialize($jsonData);
$orderData = $_SESSION['json_data'];
$query = mysql_query("SELECT * FROM CMS_bonbonOrder WHERE userId='$bonbonUser'");
mysql_query("UPDATE CMS_bonbonOrder SET jsonData='$orderData' WHERE userId='$bonbonUser';");
}
So I put a Json string in the database that looks like this for example:
Array
(
[bonbons] => Array
(
[0] => Array
(
[gewicht] => 16
[name] => test
[afbeelding] => amarena%2020g1.jpg
[aantal] => 1
)
[1] => Array
(
[gewicht] => 16
[name] => test
[afbeelding] => amarena%2020g1.jpg
[aantal] => 1
)
[2] => Array
(
[gewicht] => 16
[name] => test
[afbeelding] => amarena%2020g1.jpg
[aantal] => 1
)
[3] => Array
(
[gewicht] => 18
[name] => Testbonbon 2
[afbeelding] => bresil%2017g1.jpg
[aantal] => 1
)
[4] => Array
(
[gewicht] => 16
[name] => test
[afbeelding] => amarena%2020g1.jpg
[aantal] => 1
)
)
)
I really hope I made myself clear enough!
Thanks
hi im new to api calling and i seem to have a problem with getting an item called price from my array. The following is the array that I am supposed to extract price from.
Array
(
[prodId] => ROC-PRD-2
[prodName] => iphone 6
[projectId] => 8
[categoryIds] => Array
(
[0] => ROC-CAT-1
)
[prodParentSku] => iph6a1
[prodMetaTitle] => iphone 6
[visible] => 1
[prodStatus] => 1
[modifiedDate] => 1443472415
[createDate] => 1443472193
[productImages] => Array
(
[0] => Array
(
[id] => 89
[imageName] => iphone-ipad hi res.png
[imagePath] => http://tos-staging-web-server-s3.s3.amazonaws.com/8/products/ROC-PRD-2/iphone_ipad_hi_res.png
[visible] => 1
[featured] =>
[modifiedDate] => 1443472390
[createDate] => 1443472390
)
[1] => Array
(
[id] => 90
[imageName] => ipad 2.jpg
[imagePath] => http://tos-staging-web-server-s3.s3.amazonaws.com/8/products/ROC-PRD-2/ipad_2.jpg
[visible] => 1
[featured] =>
[modifiedDate] => 1443472397
[createDate] => 1443472397
)
)
[pricing] => Array
(
[price] => 1000
[memberGroupPrices] => Array
(
)
)
)
I am able to get the product images information such as id, imagepath, using the following for loop
foreach ( $product['productImages'] as $key => $data){
foreach ($data as $key => $eachImage){
}
}
However for price my code is as follow:
foreach ( $product['pricing'] as $key => $price){
}
If i echo the $price i would get "1000Array"
If i echo $price['price'], nothing comes out.
You Dont need to look for $pricing as its its single;
$price = $product["pricing"]["price"];
$memberGroupPrices = $product["pricing"]["memberGroupPrices"];
Just print the price with out looping
$price = $array["pricing"]["price"];
Please check with this
foreach ( $product['pricing'] as $key => $price){
if($key=='price')
$price_val=$price;
}
I have started working with Magento, and I'm trying to get all custom options associated with a given product.
I've found a solution to that, however, I ran into issues.
My PHP-code:
foreach ($_product->getOptions() as $optionInfo) :
$values = $optionInfo->getValues();
foreach ($values as $values) :
$valuesArray[$values['option_type_id']] = array("option_type_id" => $values['option_type_id'], "option_id" => $values['option_id'], "title" => $values['title']);
endforeach;
$option = array("id" => $optionInfo->getId(), "type" => $optionInfo->getType(), "title" => $optionInfo->getTitle(), "values" => $valuesArray);
$options[$optionInfo->getId()]= $option;
endforeach;
It sure do return the correct information. Atleast in the first iteration:
[2] => Array
(
[id] => 2
[type] => drop_down
[title] => Custom option 1
[values] => Array
(
[4] => Array
(
[option_type_id] => 4
[option_id] => 2
[title] => Flaphack 1
)
[5] => Array
(
[option_type_id] => 5
[option_id] => 2
[title] => Flaphack 2
)
[6] => Array
(
[option_type_id] => 6
[option_id] => 2
[title] => Flaphack 3
)
)
)
However, during the second iteration (and perhaps even the third and forth and so on), I'm having duplicates of the values. In the second iteration, I'm getting the same values as i got in the first iteration PLUS the correct values for the second iteration:
[1] => Array
(
[id] => 1
[type] => drop_down
[title] => Custom option 2
[values] => Array
(
[4] => Array
(
[option_type_id] => 4
[option_id] => 2
[title] => Flaphack 1
)
[5] => Array
(
[option_type_id] => 5
[option_id] => 2
[title] => Flaphack 2
)
[6] => Array
(
[option_type_id] => 6
[option_id] => 2
[title] => Flaphack 3
)
[1] => Array
(
[option_type_id] => 1
[option_id] => 1
[title] => Flaphack 1.1
)
[2] => Array
(
[option_type_id] => 2
[option_id] => 1
[title] => Flaphack 1.2
)
[3] => Array
(
[option_type_id] => 3
[option_id] => 1
[title] => Flaphack 1.3
)
)
)
Do you guys have any idea what's going on? Would be greatly appriciated.
Best,
Nikolaj
Try this code,
foreach ($_product->getOptions() as $optionInfo) :
$values = $optionInfo->getValues();
$valuesArray = array(); // added line
foreach ($values as $values) :
$valuesArray[$values['option_type_id']] = array("option_type_id" => $values['option_type_id'], "option_id" => $values['option_id'], "title" => $values['title']);
endforeach;
$option = array("id" => $optionInfo->getId(), "type" => $optionInfo->getType(), "title" => $optionInfo->getTitle(), "values" => $valuesArray);
$options[$optionInfo->getId()]= $option;
endforeach;
The $valuesArray is getting values in each iteration and you never cleared it. So when the outer foreach gets into second loop the $valuesArray gets values in incremental fashion. If you clear $valuesArray in each iteration of outer foreach you will get what you wanted.