Query the database where used for loops - php

I have a problem calling the database where I used for loops.
database query 1
SELECT id,qtyslot FROM rack WHERE theid = '1'
Eg id = 2
and qtyslot = 12
We create for loops
for ($x = 1; $x <= 12; $x++) {
Here I call the database with the identity of the slot varies
1 or 2,4 (with comma from 2 to 4 slots) and etc.
HOW CAN SELECT
SELECT * FROM books WHERE idrack = '2' AND slot"......." // $x
The database structure as follow.
-----------------------
| id | slot | name |
-----------------------
| 1 | 1 | name |
-----------------------
| 2 | 2,4 | name | --> How to define this with the above loop ($x)
-----------------------
| 3 | 5,6 | name | --> How to define this with the above loop ($x)
-----------------------
| 4 | 7 | name |
-----------------------
And the result like this in table
-----------------------------------------
| loop qtyslot | In slot | remark |
-----------------------------------------
| 1 | name slot 1 | remark |
-----------------------------------------
| 2 | | |
---------------| | |
| 3 | Name slot 2,4 | remark |
---------------| | |
| 4 | | |
-----------------------------------------
| 5 | Name in | |
---------------| slot 5,6 | remark|
| 6 | | |
------------------------------------------
etc up to 12
Can anyone help me.

You can use SUBSTRING_INDEX function in mysql.
Try This :
SELECT * FROM books WHERE idrack = '2' AND $x BETWEEN SUBSTRING_INDEX(slot, ',', 1) and SUBSTRING_INDEX(slot, ',', -1)

Related

Laravel extracting elements from collection, and sort it

i have a Collection like that:
+----------+----------+
| user_id | city_id |
+----------+----------+
| 1 | 1 |
| 2 | 1 |
| ... | ... |
| 901 | 2 |
| 902 | 2 |
| ... | ... |
| 1501 | 3 |
| 1502 | 3 |
| ... | ... |
+----------+----------+
in this example i have
900 users for city_id = 1
600 users for city_id = 2
300 users for city_id = 3
what i want to do:
Sort each user in a fair way, so that I end up with a list like this:
+---------+----------+
| user_id | city_id |
+---------+----------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 901 | 2 |
| 902 | 2 |
| 1501 | 3 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
| 903 | 2 |
| 904 | 2 |
| 1502 | 3 |
+---------+----------+
I divided everything by 300 (for example), so i have by day:
3 users from city 1
then 2 users from city 2
then 1 user from city 3
then 3 users from city 1
then 2 users from city 2
then 1 user from city 3
...
I tried to use the take() function but it doesnt modify the original collection.
And i used the shift() function but it just take one item.
i tried that code:
while($users->isNotEmpty()){
// $user->count() = 1800
for ($i=0; $i<3; $i++){
$final->push($users->where('city_id', 1)->shift());
}
for ($i=0; $i<2; $i++){
$final->push($users->where('city_id', 2)->shift());
}
$final->push($users->where('city_id', 3)->shift());
// $user->count() = 1800; $final->count() = 6
}
it doesn't shift element from $user collection. still have 1800 elements...
Thanks !

How to update one table based on another one?

I have these two tables:
// user
+----+-------+------------+
| id | name | total_rep |
+----+-------+------------+
| 1 | Jack | 100 |
| 2 | Peter | 334 |
| 3 | John | 1 |
| 4 | Ali | 5463 |
+----+-------+------------+
// rep
+----+------------+---------+------+
| id | reputation | id_user | done |
+----+------------+---------+------+
| 1 | 5 | 2 | 1 |
| 2 | 2 | 3 | 1 |
| 3 | 15 | 2 | Null |
| 4 | 10 | 2 | Null |
| 5 | 5 | 4 | 1 |
| 6 | 10 | 3 | Null |
+----+------------+---------+------+
I'm trying to sum the number of reputation column from rep table where done is Null for specific user and then add it to total_rep column from user table. So it is expected output:
// specific user
$id = 2;
// user
+----+-------+------------+
| id | name | total_rep |
+----+-------+------------+
| 1 | Jack | 100 |
| 2 | Peter | 359 | -- this is updated
| 3 | John | 1 |
| 4 | Ali | 5463 |
+----+-------+------------+
Note: Then I will update done column and set all Null values for that user to 1. (this is not my question, I can do that myself)
How can I do that?
One way to do it is to use the result of a subquery as a scalar value in an update statement.
UPDATE `user`
SET total_rep = total_rep + (
SELECT SUM(reputation) AS rep_sum FROM `rep` WHERE done IS NULL AND id_user = 2)
WHERE id = 2;
You can't update two tables in one statement, so you will need to execute a transaction. Or maybe in some code do the next thing I'll make it in PHP
$query_result=DB::select('Select sum(reputation) as reputation, id_user from rep where done is null group by id_user');
foreach($query_result as $result){
//update the data
}
You have to take the data and update firstable the reputation table to clean it and then the user table to sum the rep values

Loop through every value of string

I get form db field (organisations.paths)the following strings:
/1/2/3/4
Each number is the id of organisation's name from this db table:
+----+-------------+----------+----------+----------------------+
| id | frameworkid | path | parentid | fullname |
+----+-------------+----------+----------+----------------------+
| 1 | 1 | /1 | 0 | NYC University |
| 2 | 1 | /2 | 0 | Board of directors |
| 3 | 1 | /1/2/3 | 1 | Math faculty |
| 4 | 1 | /1/2/3/4 | 3 | Statistic department |
| 5 | 1 | /1/2/3/5 | 2 | Linguist department |
+----+-------------+----------+----------+----------------------+
Then I have the description table for each organisation:
+----+----------+---------+----------------+
| id | data | fieldid | organisationid |
+----+----------+---------+----------------+
| 1 | HQ | 1 | 1 |
| 2 | advisory | 1 | 2 |
| 3 | advisory | 1 | 3 |
| 4 | bottom | 1 | 4 |
| 5 | advisory | 1 | 5 |
+----+----------+---------+----------------+
How to join the both description table and main table and loop only through organisations, which have HQ or advisory in their description? So it becomes:
NYC University, Board of directors, Math faculty (Statistic department-won't be shown, as it is with description bottom)
You need to use explode, IN and join Function of PHP and mysql.
$var = "/1/2/3/4";
$in = join(" , ", explode("/", ltrim($var, '/')));
$sql = "SELECT `dt`.`fullname` FROM `db_table` dt
LEFT JOIN `organization` o
ON `o`.`organisationid` = `dt`.`id`
WHERE `o`.`id` IN ($in) AND (`o`.`data` = 'HQ' OR `o`.`data` = 'advisory')";
Make a loop to get the names and show them as you want.

how to display rows with same value in php as a group

DATABASE Structure:
ID | USERID | ENTRYDATE | EXITDATE | PRICE | ROOMID
-----------------------------------------------------
1 | 2 | 2012-10-1 | 2012-10-4| 100 | 1
2 | 2 | 2012-10-1 | 2012-10-4| 100 | 2
3 | 2 | 2012-10-1 | 2012-10-4| 100 | 3
4 | 2 | 2012-10-6 | 2012-10-9| 100 | 4
5 | 2 | 2012-10-6 | 2012-10-9| 100 | 55
i need some help please. I want to display a set of records from this database. i want to display all those with same USERID, but group those with the same ENTRYDATE and EXITDATE in the same DIV. example, the result should be like this...
ID | USERID | ENTRYDATE | EXITDATE | PRICE | ROOMID
------------------------------------------------------
1 | 2 | 2012-10-1 | 2012-10-4| 100 | 1
2 | 2 | 2012-10-1 | 2012-10-4| 100 | 2
3 | 2 | 2012-10-1 | 2012-10-4| 100 | 3
ID | USERID | ENTRYDATE | EXITDATE | PRICE | ROOMID
----------------------------------------------------
4 | 2 | 2012-10-6 | 2012-10-9| 100 | 4
5 | 2 | 2012-10-6 | 2012-10-9| 100 | 5
I am not sure I understand your question correctly. I assume you have a string-indexed array and you want to split it basing on ENTRYDATE and EXITDATE values. Maybe something like this:
$currentEntry = $data[0]['ENTRYDATE'];
$currentExit = $data[0]['EXITDATE'];
$splitted = array();
$i = 0;
foreach ($data as $row) {
if ($row['ENTRYDATE'] == $currentEntry && $row['ENTRYDATE'] == $currentExit) {
$splitted[$i] []= $row;
} else {
$currentEntry = $row['ENTRYDATE'];
$currentExit = $row['EXITDATE'];
$i++;
}
}

PHP&MySQL: Combine more lines and put it in array [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
mysql with comma separated values
I have two tables with more lines, like this:
1. numberstable
---------------------------
| number_id | number_name |
---------------------------
| 1 | one |
| 2 | two |
| 3 | three |
| 4 | four |
.
.
.
---------------------------
2. testtable
-------------------------------
| test_id | numbers | sthelse |
-------------------------------
| 1 | 2.4.5.6 | text1 |
| 2 | 4.8.7.1 | text2 |
| 3 | 2.7.8.5 | text3 |
-------------------------------
First I would like to combine all three "numbers" rows from table "testtable" to get something like this: 1.2.4.5.6.7.8 and then exclude it in next query. This query is "SELECT number_id, number_name FROM numberstable ORDER BY number_name". After excluding I would like to show just numbers which aren't in use in "testtable" (9, 10, 11, ...).
How to do that?
If you are trying to relate the numberstable to the testable. I would think you would be much better served to add another table that would relate the two to where you had a schema like
1. numberstable
---------------------------
| number_id | number_name |
---------------------------
| 1 | one |
| 2 | two |
| 3 | three |
| 4 | four |
.
.
.
---------------------------
2. testtable
---------------------
| test_id | sthelse |
---------------------
| 1 | text1 |
| 2 | text2 |
| 3 | text3 |
--------------------
3. numbersintesttable
-----------------------
| test_id | number_id |
-----------------------
| 1 | 2 |
| 1 | 4 |
| 1 | 5 |
| 1 | 6 |
| 2 | 4 |
| 2 | 8 |
| 2 | 7 |
| 2 | 1 |
-----------------------
So the new table would be a many-to-many join table that you could use to get all your needed data in a single query by utilizing the type of joins you want (INNER, OUTER, etc.)

Categories