Stopping loop before counter reaches 0 - php

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.

Related

ranking of student position based on their percentage scores

I use this code below to determine the position of each student result based on the percentage score of their subject, the program assigned their position as (1st, 2nd, 2nd, 4th ...)
My challenge now is that whenever the percentage score is 100, the system position the student as last in the class instead of 1st position.
Below is the code:
<?php
session_start();
include("DB/config.php");
ini_set('max_execution_time', 1000000);
$class_arm= $_SESSION['clas'];
$queris = "select * from setup";
$result3 = mysqli_query($con,$queris) or die(mysqli_error($con));
$rws3 = mysqli_fetch_assoc($result3);
$ses3=$rws3['section'];
$term3=$rws3['term'];
$query_sj = "SELECT subj FROM sec_result where class='$class_arm' and year='$ses3' and term='$term3' and tsc<>'' and tsc is not null and tsc <> '0'";
$result_sj = mysqli_query($con,$query_sj) or die(mysqli_error($con));
while($row_sj = mysqli_fetch_array($result_sj)){
$query = "SELECT distinct studid,subj,tsc FROM sec_result where class='$class_arm' and year='$ses3' and term='$term3' and subj='".$row_sj['subj']."' ORDER BY tsc DESC";
$result = mysqli_query($con,$query) or die(mysqli_error($con));
/*$row2 = mysqli_fetch_array( $result );
echo $row2['tsc'];
exit;*/
if( !$result ){
echo 'SQL Query Failed';
}else{
$rank = 0;
$last_score = false;
$rows = 0;
while( $row = mysqli_fetch_array( $result ) ){
$rows++;
if( $last_score!= $row['tsc'] ){
$last_score = $row['tsc'];
$nuval = $row['tsc'];
$rank = $rows;
}
mysqli_query($con,"UPDATE sec_result SET pos = '$rank' where studid='".$row['studid']."' and year='$ses3' and term='$term3' and subj='".$row_sj['subj']."'") or die(mysqli_error($con));
}
}
}
?>
My expectation is that the student that scores 100 percent should be given the first position.
Please, I need your help to resolve this, Thanks.
My expectation is that the student that scores 100 percent should be given the first position.

Sum fields from different tables per userID

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

How to show the result form getting array?

Database :
--> product table
P_id P_name P_uploadKey
1 Cemera 7365
2 Notebook 7222
3 Monitor 7355
4 Printer 7242
--> buy table
B_id P_id B_name date
1 1,3,4 somchai 12/3/2016
2 2,3 kri 12/3/2016
This sql to show the find id on buy table where $_GET['B_id'] = '2' :
$bid = $_GET['B_id'];
$sqlB ="select * from buy where B_id ='$bid' ";
$Recordset2 = mysql_query($sqlB, $connect) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
And this sql code to show the result what is they buy, by get the $row_Recordset2['P_id'] like a 2,3 from code above :
$pid = $row_Recordset2['P_id'];
$sqlp ="select * from buy where P_id ='$pid' ";
$Recordset3 = mysql_query($sqlp, $connect) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
do {
echo $row_Recordset3['P_name']. "<br>";
} while ($row_Recordset3 = mysql_fetch_assoc($Recordset3));
I want the to show like this, how we edit it:
Notebook
Monitor
This is answer i can do it.
$pid = $row_Recordset2['P_id'];
$array = explode(',', $pid);
foreach ($array as $item) {
$sqlp ="select * from buy where P_id ='$item' ";
$Recordset3 = mysql_query($sqlp, $connect) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
do {
echo $row_Recordset3['P_name']. "<br>";
} while ($row_Recordset3 = mysql_fetch_assoc($Recordset3));
}
You can use like below,
$sqlp ="select * from product where P_id IN '($pid)'";
instead of
$sqlp ="select * from buy where P_id ='$pid' ";

Get information from MySql Query and put it in PHP array

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!

PHP/MySQL Update query every 2 column update room change automated

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.

Categories