So I have the next databases:
+------+---------------+-------+
| Id | Title | Price |
+------+---------------+-------+
| 300 | Part1 | 30 |
| 400 | Part2 | 40 |
| 500 | Part3 | 50 |
| 3215 | Promo Package | 130 |
+------+---------------+-------+
And
+-----------------+--------------+
| Promo_prod_main | product_part |
+-----------------+--------------+
| 3215 | 300 |
| 3215 | 400 |
| 3215 | 500 |
+-----------------+--------------+
Those two would create lets say a promo package(the 3 products).
The problem is that sometimes , the prices on the 3 parts change but it won't affect the promo package price.
I decided to create a script that selects each promo package and then gets the products that its created from , sum up the array of the prices and then update the promo package.
All good thinking until I got to code , I managed this far:
$rezultatebumb = mysqli_query($linkul, "SELECT id,title,price FROM product WHERE attr_group_id IS NULL AND is_homepage IS NULL") or die(mysqli_error($linkul)); // this query only selects the promo packages
$array_m = array();
foreach ($rezultatebumb as $valc) {
$array_m[] = $valc['id'];
}
$array_s = array();
foreach ($array_m as $arr) {
$rezprod = mysqli_query($linkul, "SELECT product_part FROM product_promo WHERE Promo_prod_main=" . $arr) or die(mysqli_error($linkul));
$tesat = $rezprod->fetch_assoc();
$array_s[] = $tesat['product_part'];
echo count($array_s);
}
print_r($array_s);
echo '</br></br>';
print_r($array_m);
results next->
One product from each package
Array ( [0] => 146 [1] => 1497 [2] => 565 [3] => 6576 [4] => 1516 )
Each promo package
Array ( [0] => 5732 [1] => 5758 [2] => 6998 [3] => 7032 [4] => 7033 )
Before I get messages like 'You're supposed to write the code yourself' , well I tried, I just can't get anywhere past this point.
I will appreciate any tip or fix to this problem , thank you!.
Sadly the only thing I understand is this part.
I decided to create a script that selects each promo package and then
gets the products that its created from , sum up the array of the
prices and then update the promo package.
So, You can something like joins to get an array of all products which are associated with the promo package.
SELECT p.Price from product p
join promo_table pt
on (p.Id = pt.Promo_prod_main)
where pt.promo_prod_main=3215
Pass it results on array and then do array_sum() to array to get the total of price.
And then update it's total price
you can use inner join to have the package prepared. With query of inner join:
select * from package a
inner join product b
on a.package_id = b.package_id
where a.package_id = 5
Something on these lines, you should have pacakage_id corresponding to each product. Or you can have another mapping table to map a product with various packages.
Related
I have a table named questions in the following form:
Table Name: questions
qn_id | question |
---------------------------------------------------------------
19 | What is your rating on your Team Work?
20 | What is your rating on your Skills?
21 | What is your rating on your Personal Quality?
I have another table named rating as shown below:
Table Name: rating
id | employee_id | question_id | self_score | supervisor_score
-----------------------------------------------------------------
1 | 205 | 19 | 4 | 3
2 | 205 | 20 | 5 | 4
3 | 205 | 21 | 4 | 5
Note: There are two people involved in providing the score. One is supervisee and another is supervisor.
The score provided by the supervisee is kept in the column named self_score. In the table, employee_id is the id of the supervisee. Supervisor also gives the rating to the supervisee for the same question as shown aboove in the table.
I have generated the data from the database as an Array which is shown below:
$params = Array
(
[selfscore_19] => 4
[supervisorscore_19] => 3
[selfscore_20] => 5
[supervisorscore_20] => 4
[selfscore_21] => 4
[supervisorscore_21] => 5
)
Note: In the key named [selfscore_19] , selfscore_ is the string and 19 is the question id.
Questions are same for both - 'self' and 'supervisor'.
So I have appended the strings named 'selfscore' and 'supervisorscore' with the question_id.
In the array shown below, key is: [selfscore_19] which has been appended with the question_id i.e 19 and
the value is score provided by 'self' which is 4.
And other data follows similar pattern.
Attempt:
The code below shows the attempt done by me so far:
Note: I have only shown the main part of the method.
foreach($params as $key => $value){
$exp_key = explode('_', $key);
if($exp_key[0]=='selfscore'){
$arr_result[] = $value;
}
}
foreach($params as $key => $value){
$exp_key = explode('_', $key);
if($exp_key[0]=='supervisorscore'){
$arr_result[] = $value;
}
}
if(isset($arr_result)){
echo '<pre>';
print_r($arr_result);
echo '<pre>';
}
This is the output of the above code:
Current Output:
Array
(
[0] => 4
[1] => 5
[2] => 4
[3] => 3
[4] => 4
[5] => 5
)
Now I want to tranform the Current Output in the form of following Output.
Although I have already researched about this case, but I am finding it a bit tricky.
What modification should I do to achieve the required output?
Suggestions are highly appreciated.
Required Output:
$score['self_score']=array(
array('question_id'=>19,'score'=>4)
array('question_id'=>20,'score'=>5)
array('question_id'=>21,'score'=>4)
);
$score['supervisor_score']=array(
array('question_id'=>19,'score'=>3),
array('question_id'=>20,'score'=>4)
array('question_id'=>21,'score'=>5)
);
Do it with a single loop. Use the first part of $exp_key as the key of the resulting array, and the second part as the value of the question_id key of the sub-array.
foreach ($arr_result as $key => $value) {
$exp_key = explode("_", $key);
$score[$exp_key[0]][] = array("question_id" => $exp_key[1], "score" => $value);
}
I think I'll recommend that you use a UNION to join two simple queries so that your initial resultset is very close to what you want (close enough to simply iterate and/or modify it).
SQL: (Demo)
(SELECT 'group' AS 'self_score', question_id, self_score
FROM rating
WHERE employee_id = 205
ORDER BY id)
UNION
(SELECT 'supervisor_score', question_id, supervisor_score
FROM rating
WHERE employee_id = 205
ORDER BY id)
Resultset:
group | question_id | self_score
------------------|---------------|------------
self_score | 19 | 4
self_score | 20 | 4
self_score | 21 | 5
supervisor_score | 19 | 3
supervisor_score | 20 | 4
supervisor_score | 21 | 5
If you are open to a PDO solution, fetchAll(PDO::FETCH_GROUP) will come in pretty handy.
I searched a lot still could not find satisfactory answer. Please have a look at the sample mysql table below:
id | Name | Country
1 | User1 | India
2 | User2 | India
3 | User3 | India
4 | User4 | Australia
5 | User5 | Australia
I want to display above table in a while loop using PHP, something like below:
Country Name: | Total Members From The Country:
India | 3
Australia | 2
I only want to target and display only one column/field i.e "country". Can anyone please share the query code here so I can proceed with my work and thank you for your time.
Keywords you could search for are GROUP BY and COUNT. The query then should look something like this:
SELECT Country, COUNT(*) FROM Users GROUP BY Country;
This will group all rows by the value of your Country column and count the rows with COUNT which will give you a row for each country with the corresponding count in your table.
You are free to extend the query with a WHERE to filter for specific countries.
Try this query:
SELECT country, count(*) as total_member
FROM your_table
GROUP BY country
You should have something like this as result if you fetch an associative array :
$result = array(
0 => array (
"country" => "India",
"total_member" => 3
),
1 => array (
"country" => "Australia",
"total_member" => 2
)
);
Then just loop throught your result and display what you want :
foreach ($result as $key => $value) {
echo $value['country'] . " | " . $value['total_member'];
}
Adapt so it looks like you want !
Is it what you wanted?
I have a table , similar to this:
| key | value |
|----------|-------|
| limit | 15 |
| viplimit | 25 |
| .. | |
And i have an array :
Array
(
[0] => Array
(
[key] => limit
[value] => 10
)
[1] => Array
(
[key] => viplimit
[value] => 99
)
...
Now , saying we have 100 rows. What would be the best way to update the table corresponding to the array ?
There would be the option of a query for each 100 row, but that is just bad performance.
This should work:
$statement = "UPDATE mytable
SET key = CASE id
WHEN 1 THEN 'key'
WHEN 2 THEN 'another_key'
WHEN 3 THEN 'some_key'
END,
value = CASE id
WHEN 1 THEN 15
WHEN 2 THEN 25
WHEN 3 THEN 45
END
WHERE id IN (1, 2, 3)
");
DB::statement($statement);
Just think how to create correct query. If it's admin panel or something that will be run not very often, I'd just use iteration to keep things simple.
Hello This may looks to be simple task but I am getting struck... In my application i am getting an array which would be like
Array
(
[0] => Array
(
[home_id] => 1
[distance] => 12
)
[1] => Array
(
[home_id] => 3
[distance] => 14
)
[2] => Array
(
[home_id] => 94
[distance] => 1.679713069
)
.
.
.
.
)
And my table looks like
home_id | home_name
1 | My home 1
2 | My home 2
3 | My home 3
From this array i will get the home_id which is in database table. So How can i get the result details which includes the home_name and the distance from the first array which might be like
home_name | distance
___________________________
My home 1 | 0.562620830044
My home 3 | 14
Thank you in advance
Loop through your array and get the home_name from database using codeigniter active record query -
foreach($yourArray as $row)
{
$id = $row['home_id'];
$distance = $row['distance'];
$db_result = $this->db->get_where('yourtable', array('home_id' => $id));
if($db_result && $db_result->num_rows() > 0){
$result = $db_result->row();
$home_name = $result->home_name;
}
}
If you cannot JOIN the two tables in one query and have to use that array then you can do:
foreach($yourArray as $home)
{
$id=$home["home_id"];
$distance=$home["distance"];
$id=intval($id);
$sql="SELECT home_name FROM yourTable WHERE home_id=$id"; // execute this query to get name
}
From this array i will get the home_id which is in database table. So How can i get the result details which includes the home_name and the distance from the first array which might be like
If you want to get home details from table home_details_table using the id you get from the main array, so replace the field home_namein home_details_table by home_id and link both tables as a One to Many relation.
home_table:
home_id | home_name
1 | My home 1
2 | My home 2
3 | My home 3
home_detail_table:
home_id | distance
1 | 0.562620830044
3 | 14
Then with a JOIN, you will be able to do :
foreach($mainArray as $home)
{
$id = $home["home_id"];
$sql="SELECT h.home_id, h.home_name, d.home_distance FROM home_table h JOIN home_details_table d ON h.home_id = d.home_id WHERE h.home_id = ".$id;
// with this query you will have the name and distance of the given home id.
}
With help from others on here, I've got a nested loop on the go that pull a list of months from one sql table and then, for each of those months, it goes through an events table and pulls the respective events.
Table structures are along the lines of:
MonthTable
ID | MonthShort | MonthLong
1 | 2012Oct | October 2012
2 | 2012Sep | September 2012
EventTable
ID | MonthID | Event | Guests | Adults | Children
1 | 1 | Wedding | 200 | 150 | 50
2 | 1 | Bar Mitzvah | 100 | 50 | 50
3 | 1 | Funeral | 100 | 50 | 50
4 | 2 | Birthday | 50 | 30 | 20
5 | 2 | Birthday | 300 | 200 | 100
6 | 2 | Wedding | 200 | 180 | 20
My loop works so that it populates menu A with all available months, then populates menu B with all of the events for that month. You can then click on the event and it displays the relevant information - this is where I'm a bit stuck.
The arrays I've got are similar to the following, the guests array is what I'm trying out atm:
$events = array();
$months = array();
$guests = array();
while ($row = mysql_fetch_array($result)) {
$months[$row["MonthID"]] = $row["MonthLong"];
$events[$row["MonthID"]][] = $row["Event"];
$guests[$row["MonthID"]][] = $row["Guests"];
}
I use a foreach to populate menu B with ($events[$x] as $event). The screen for each event will have an entry similar to the following and this is what I'd like to do (obviously I know this won't work bu it should serve for illustrative purposes):
echo ' Number of guests: ' . print_r($guests[$x])
With guests and events both on the same counter I though it would allow me to print the array entry in the relevant position.
So what I'd like it if you click on "October 2012" and then select "Funeral", the screen would say:
Number of guests: 100
There are actually several dozen records per event but no point going into all of them...
Apologies for the rambling and if this makes no sense! I'm new to PHP and am only really stuck on this bit.
SQL query is built on the following:
$sql = "
SELECT
a.id, b.id AS monthId, a.event, b.monthshort, b.monthlong
FROM
events_table_name AS a
INNER JOIN
month_table_name AS b ON b.id = a.monthId
ORDER BY
b.id, a.id ASC
";
You need make use of the index in the foreach statement. I mean
foreach ($events[$x] as $i => $event) {
...
echo ' Number of guests: ' . print_r($guests[$x][$i]);
}
I would go for a different data structure in PHP. How about this? You might have to change your SQL query to get it, but this is the data structure I'd aim for:
$months = array(
'1' => array(
'long' => 'October 2012',
'events' => array(
'1' => array(
'name' => 'Wedding',
'guests' => '200'
),
'2' => array(
'name' => 'Bar Mitzvah',
'guests' => '100'
),
'3' => array(
'name' => 'Funeral',
'guests' => '100'
)
)
),
'2' => array(
// etc.
)
);
This way, you're able to look up a month; for each month, its events; for each event, its attendance and name.