I want to create automated room divisions.
Assumptions :
I have 4 data.
Each room maximum 2 people.
If room is full / reach max "2", 3th people automated insert to the next room.
Here my code. assumption 4 data.
$kuota=2; //Max 2 data in 1 room
while ($row = mysqli_fetch_array($query)) {
$id = $row["id"];
$x=1;
while($x<=$kuota) {
$sql2 = "UPDATE cds SET room=$y WHERE id='$id'";
$query2 = mysqli_query($con, $sql2) or die (mysqli_error($con));
$x++;
}
$y++
}
but it doesn't work.
My code is so ugly :(
I want to get a result like this
name || Room
Michael || 1
Muller || 1
...
Cyntia || 2
Gina || 2
while ($row = mysqli_fetch_assoc($query)) {
$id = $row['id'];
$sql2 = "UPDATE cds AS c1
CROSS JOIN (SELECT room
FROM cds
GROUP BY room
HAVING COUNT(*) < $kuota
ORDER BY room
LIMIT 1) AS c2
SET c1.room = c2.room
WHERE id = $id";
mysqli_query($con, $sql2) or die (mysqli_error($con));
}
The c2 subquery returns the first room that has fewer than $kuota people assigned to it.
Related
Need your help.
php, mysql
I have the following project.
I have two tables
**table 1**
user_id Plan
1 5
1 7
2 5
2 9
3 7
1 9
**table 2**
Plan Price
5 100
7 200
9 300
I must find the total cost of plans selected by one user
eg user_id = 2 must pay 400
I have already the following code, but this one adds the Price of all Plans in database in the above example total cost = 600
What am I doing wrong? :(
$totalcost = NULL;
$sql = "select SUM(Plan.Cost) as ANSWER FROM Plan";
$result = mysql_query($sql, $link) or die(mysql_error());
$totalcost = mysql_fetch_row($result);
$sql = "select * FROM Plan";
$result = mysql_query($sql, $link) or die(mysql_error());
$rows = mysql_num_rows($result);
$Plan = array();
if (is_resource($result)) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$Plan[] = $row;
}
}
You have to specify the user_id in your query, like:
$user_id = 2; // or get it from $_GET, $_POST...
$sql = "select SUM(Plan.Cost) as ANSWER
FROM Plan, Users
WHERE Users.Plan = Plan.Plan AND Users.user_id = $user_id";
You probably want to use LEFT JOIN in your query, so you can make something like this:
SELECT table1.user_id, table1.plan, SUM(table2.cost) FROM table1 LEFT JOIN table2 ON table1.plan=table2.plan WHERE table1.user_id = $user_id;
this way you can fetch results in 1 query and make database do all the work instead of looping through data in functions etc.
SQL left join tutorial
I am parsing 3 variables from JSON link, each of variable consist of the name of a cinema theater:
$cinema1
$cinema2
$cinema3
After, I need to select an ids of all 3 cinemas from the table, then using that ids I need to look for the table "movietimings" to see if there is movies are playing today, if there is a movie playing in that cinemas today, then I need to go for "movie" table and show all information about that movie.
So, this is what have been accomplished:
$sql2 = "SELECT DISTINCT * FROM cinema WHERE cinemaname = '$cinema1' AND cinemaname='$cinema2' AND cinemaname='$cinema3' ";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
// output data of each row
while($row2 = $result2->fetch_assoc()) {
$cinemaid2 = $row2['id'];// I have received a cinema id
//getting the main id, since the id I just got is a branch of the main cinema, so the main id that is associated with movie timings is down below
$sql5 = "SELECT DISTINCT * FROM cinemas WHERE cinemaname = '$cinemaid2'";
$result5 = $conn->query($sql5);
if ($result5->num_rows > 0) {
// output data of each row
while($row5 = $result5->fetch_assoc()) {
$cinemaid = $row5['id'];//received main id required
$today = date("m/d/Y");//getting todays date
//now trying to find if a movie today playing
$sql3 = "SELECT * FROM moviecinemashowsassociation WHERE cinemaid LIKE '$cinemaid' AND showdate LIKE '$today'";
$result3 = $conn->query($sql3);
if ($result3->num_rows > 0) {
// output data of each row
while($row3 = $result3->fetch_assoc()) {
$movieid = $row3['movieid'];// selected a movie id played today
//selecting information about movie
$sql4 = "SELECT * FROM movie WHERE id='$movieid'";
$result4 = $conn->query($sql4);
if ($result4->num_rows > 0) {
// output data of each row
while($row4 = $result4->fetch_assoc()) {
The problem is that I have 3 different cinemas which should go through one loop.
You are looking for a OR condition instead of AND condition since the cinemaname can't hold multiple value at same point of time and thus it would be either of the values specified
WHERE cinemaname = '$cinema1' OR cinemaname = '$cinema2' OR cinemaname = '$cinema3'
(OR) Use a IN operator
WHERE cinemaname IN ('$cinema1' ,'$cinema2' , '$cinema3')
I have a table of which I must select information using a MySQLi Query and push it to the end of a PHP array.
The table is in this format:
table = friends
id user1 user1_id user2 user2_id datemade accepted
1 name1 1 name2 2 2015-05-27 03:24:32 1
2 name3 3 name2 2 2015-05-27 03:24:32 1
3 name3 3 name1 1 2015-05-27 03:24:32 1
4 name4 4 name2 2 2015-05-27 03:24:32 1
id = an auto_incrementing number to keep track of everything
user1 = the person's name that asks for friendship
user1_id = that person's special unique id
user2 = name of the person that accepts/decline's friendship
user2_id = that person's special unique id
datemade = the date it was made :P
accepted = did he accept? (don't worry about this)
I want to select all users that are friends with $u.
In this example, $u's id is 1 (name is name1).
After running the query it would push it to the end of friend_array.
So if I printed this array the output would be:
2, 3
Since, id=1 is friends with id=2 and id=3
What query should I do and how would I push that to an array (I know about about array_push but I do not know how to implement it)?
Please try this code. It will return the array for all user's friends.
$sql = "SELECT user1 AS user FROM friends UNION SELECT user2 AS user FROM friends";
$data = mysqli_query($sql);
while ($v = mysqli_fetch_assoc($data)) {
$sql = mysqli_query("SELECT * FROM `friends` where (user1 = '" . $v['user'] . "' or user2 = '" . $v['user'] . "')");
$arr = array();
while ($array = mysqli_fetch_assoc($sql)) {
if ($array['user1'] == $v['user']) {
$arr[$v['user']][] = $array['user2_id'];
} else {
$arr[$v['user']][] = $array['user1_id'];
}
}
}
I finally figured it out!
$sql = "SELECT COUNT(id) FROM friends WHERE user1_id='$log_id' AND accepted='1' OR user2_id='$log_id' AND accepted='1'";
$query = mysqli_query($db_conx, $sql);
$query_count = mysqli_fetch_row($query);
$friend_count = $query_count[0];
//echo($friend_count); //how many friends
if($friend_count < 1){
echo($u." has no friends yet");
} else {
$all_friends = array();
$sql = "SELECT user1_id FROM friends WHERE user2_id='$log_id' AND accepted='1' ORDER BY RAND()";
$query = mysqli_query($db_conx, $sql);
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
array_push($all_friends, $row["user1_id"]);
}
$sql = "SELECT user2_id FROM friends WHERE user1_id='$log_id' AND accepted='1' ORDER BY RAND()";
$query = mysqli_query($db_conx, $sql);
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
array_push($all_friends, $row["user2_id"]);
}
$friendArrayCount = count($all_friends);
}
So it first counts how many friends you have to check if you have any, if you do not a, temp. message appearing saying no friends. If you have some friends it will run a query to check what friends you have is both columns - user1_id and user2_id. It will then finally add everything to the $all_friends array and it then counts how many friends you have.
Thank you to everyone that helped!
table 1
two column >> paid - order_num1
table 2
two column >> order_num2
I want get order_num2 value from table 2 and
update paid(insert paid = 1) in table one with same order_num value
If order_num1=order_num2 then paid = 1 in table 1
$q = mysql_query("select order_num2 from table2 where samevalue = samevalue ");
$x = mysql_fetch_row($q);
mysql_query("update table1 set paid=1 where order_num1='$x['order_num2']'");
But it does not work!
First get from one table and update paid from another table if order_num have same value
Try
$table2 = mysqli_query("SELECT * FROM table2");
$row = mysqli_fetch_array($table2);
$num2 = $row['order_num2'];
$table1 = mysqli_query("SELECT * from table1");
$roww = mysqli_fetch_array($table1);
$num1 = $row['order_num1'];
if ($num2 == $num1) {
$updateDB = mysqli_query("UPDATE table1 SET paid = 1 WHERE order_num1 = '$num2'");
} else {
//Not equal so the paid column wont get updated
}
For now the no of vacancy is 3 for that company. I want it to stop subtracting at 0.
This is my code:
$result4 = mysqli_query($con,"SELECT no_of_vacancy FROM job_details WHERE jobscope= 'Information Technology' AND job_title='Oak 3 Films Pte Ltd (Sales Marketing Department)';");
$result5 = mysqli_query($con, "SELECT COUNT(company) FROM student_details WHERE jobscope1 = 'myJobScope' AND company = 'myCompany';");
while ($row5 = mysqli_fetch_assoc($result5))
{
$result6 = mysqli_query($con, "UPDATE `job_details` SET `no_of_vacancy`= `no_of_vacancy` - 2 WHERE `job_title` = 'myCompany';"));
}
I want to subtract a value from a company's no of vacancy once a student is assigned to it.
Company | Vacancy|
ABC | 3
I want it to show 0 once 3 students is assigned to it. For now it goes till -1 every time the code runs.
To stop loop execution:
while ($row5 = mysqli_fetch_assoc($result5))
{
$result6 = mysqli_query([...]);
if(condition)
break;
}
To skip current execution:
while ($row5 = mysqli_fetch_assoc($result5))
{
if(condition)
continue;
$result6 = mysqli_query([...]);
}
You stop While loop with break;
You can stop everything with exit;
What is the point of this code ?
Did you already try using "break"?
You could for example use your code in the following manner:
$result4 = mysqli_query($con,"SELECT no_of_vacancy FROM job_details WHERE jobscope= 'Information Technology' AND job_title='Oak 3 Films Pte Ltd (Sales Marketing Department)';");
$result5 = mysqli_query($con, "SELECT COUNT(company) FROM student_details WHERE jobscope1 = 'Information Technology' AND company = 'Oak 3 Films Pte Ltd (Sales Marketing Department)';");
while ($row5 = mysqli_fetch_assoc($result5))
{
$result6 = mysqli_query($con, "UPDATE `job_details` SET `no_of_vacancy`= `no_of_vacancy` - 2 WHERE `job_title` = 'Oak 3 Films Pte Ltd (Sales Marketing Department)';"));
if ($result6 == 0)
{
break;
}
I hope it could help.