I'm trying to accomplish the following situation:
$mysql_query = "
SELECT *
FROM st_users
WHERE
`user_comp_supervisor_id` = '$team_supervisor' AND
`user_exempt_from_goals` = '0'
ORDER BY 'calculate_progress_percent()' ASC
";
I know that I can't accomplish ordering by a function in a MySQL statement, but I'm trying to figure out how to take all the returned records, and then order them in order of highest to lowest from a php function result. Any ideas would be greatly appreciated; I've been trying to wrap my head around this for a few hours now... :-(
function diy_calc_progress_percent($user_id,$period_id,$period_week_number)
{
$this->user_id = $user_id;
$this->period_id = $period_id;
$this->period_week_number = $period_week_number;
if ($this->period_week_number == 1)
{
$this->week_id = mysql_result( mysql_query(" SELECT `period_week_one` FROM `st_comp_periods` WHERE `period_id` = '$this->period_id' "),0 );
}
else if ($this->period_week_number == 2)
{
$this->week_id = mysql_result( mysql_query(" SELECT `period_week_two` FROM `st_comp_periods` WHERE `period_id` = '$this->period_id' "),0 );
}
else
{
echo "Week number not valid.";
exit();
}
$this->week_start_date = mysql_result( mysql_query(" SELECT `week_start_date` FROM `st_comp_weeks` WHERE `week_id` = '$this->week_id' "),0 );
$this->week_end_date = mysql_result( mysql_query(" SELECT `week_end_date` FROM `st_comp_weeks` WHERE `week_id` = '$this->week_id' "),0 );
$this->user_department = $this->user_info($this->user_id,"user_comp_department_id");
$this->user_week_diy_goal = mysql_result( mysql_query(" SELECT `goal_diy_department` FROM `st_comp_department_goals` WHERE `goal_department_id` = '$this->user_department' AND `goal_week_id` = '$this->week_id' "),0 );
$this->calc_totals_result = mysql_query("SELECT SUM(record_total_diy_revenue) AS user_week_total FROM `st_entered_records` WHERE `record_user_id` = '$this->user_id' AND `record_date` BETWEEN '$this->week_start_date' AND '$this->week_end_date'");
$this->calc_totals_row = mysql_fetch_assoc($this->calc_totals_result);
$this->user_week_total = $this->calc_totals_row['user_week_total'];
$this->user_week_one_percent = ($this->user_week_total / $this->user_week_diy_goal) * 100;
$this->user_week_one_percent = number_format( (float)$this->user_week_one_percent, 2, '.', '' );
return $this->user_week_one_percent;
}
You probably will have to do some array juggling.
First get all your entries FROM st_users into a first array (mysql_query)
Then you could run through that array, and for each entry you do the calculate_progress_percent() and build up a second array in which you could add the additional info ("user_progress_percent").
After this you can sort the new array ba your new info ("user_progress_percent").
And here is some quick and dirty code-suggestions – code is however not tested… of course…:)
First:
$mysql_query = "SELECT * FROM st_users
WHERE `user_comp_supervisor_id`='$team_supervisor' AND
`user_exempt_from_goals` = '0'";
Then something like this:
$i = 0;
while($tmp = mysql_fetch_array($mysql_query)) {
$my_second_array[$i]['user_id'] = $tmp['user_id'];
$user_id = $my_second_array[$i]['user_id'];
diy_calc_progress_percent($user_id,$period_id,$period_week_number);
$my_second_array[$i]['user_result'] = $diy_calc_progress_percent_result;
$i++;
}
And then sorting that second array should be possible as described here:
Sort Multi-dimensional Array by Value
…hope this helps at some point…
Related
I have a PHP/SQL app that processes invoices. Recently, I had an invoice number come in that is not being processed as text, rather as a large exponential number when I do an insert/update on associated SQL tables. For example, take an invoice number that looks like this: 123E456. PHP will try to convert this to an extremely large number due to the 'E' being bookended by numbers.
I am leaning towards this being a PHP issue because when I look at the SQL being sent to the server, it is being scripted without quotes, 123E456 rather than '123E456'.
I have tried multiple ways to try and force it to be text, but nothing seems to work.
If I put single quotes around the string, I get double single quotes in the SQL.
strval() also does not work
the issue might be in the SQL interpreter, but not entirely sure
Right now, I am instructing my clerks to put a space between the E and the numbers, which works for now. But, I am hoping to address this specific issue in the code rather than have the clerk remember to manage it on their end.
Can anyone help with how to force this as being text in the SQL clause?
OK, the code is rather my own style and is based on retrieving a dummy record (the table has 178 columns) and then populating the values into the elements that need updated. It then creates the SQL from the array and does the update. Most of this is just pre-processing to get the values needed. The database being used is Oracle.
function processF0411Z1($id, $user){
include_once $_SERVER['DOCUMENT_ROOT'].'/truck/inc/base.inc.php';
$b = '\' \'';
$z = 0;
$co = get_route_company($id);
$usrsql='SELECT `userID` from `user` where `id` = ' . $user;
$usr = openRecordset_Fetch_Assoc($usrsql);
if($usr[0]==1)$userid = $usr[1]['userID'];
else $userid = $_SESSION['username'];
$jul = date2jul(getdate());
$tjul= getJulTime(getdate());
$sql = "SELECT a.`id`, a.`carrierInvoice`, a.`carrierNbr`, a.`ivd`, a.`dgl`, b.`bol`, b.`obj_acct`, b.`allocation` FROM `route13` a inner join `route131` b on(a.`id` = b.`id`)WHERE a.`id`=".$id;
$myArr = openRecordset_Fetch_Assoc($sql);
if(isset($myArr) && $myArr[0]>0){
$carr = $myArr[1]['carrierNbr'];
$carrsql = 'select `CarrierName` from `Carriers` where `CarrierNbr` = '. $carr;
$carr_res = openRecordset_Fetch_Assoc($carrsql);
if($carr_res[0]==1)$carrName = $carr_res[1]['CarrierName'];
else $carrName = $carr;
// get the next number in the EDI Batch sequence
$nn = getJDEZFileNN();
// get the base associated array of the F0411Z1 table
$msSQL = 'SELECT * FROM PRODDTA.F59411Z1 WHERE VLEDUS=\'TRUCK\' AND VLEDBT=1';
$F0411Z1 = oracle_fetch_array($msSQL);
for($i=1;$i<=$myArr[0];$i++){
// test to see if this record exists
$tsql = "select * from PRODDTA.F0411Z1 where VLEDUS = '".strtoupper($user)."' and VLEDBT = ".$nn[1]['NNN006']." and VLEDLN = " .$i*1000;
$tres = oracle_fetch_array($tsql);
if($tres[0]>0){
$dsql = "delete from PRODDTA.F0411Z1 where VLEDUS = '".strtoupper($user)."' and VLEDBT = ".$nn[1]['NNN006']." and VLEDLN = " .$i*1000;
$count = oracle_update($dsql);
if($count === $tres[0]){
$count = $count;
}
}
$an8_sql = 'SELECT aban85 FROM PRODDTA.F0101 WHERE aban8='.$myArr[$i]['carrierNbr'];
$aban85 = oracle_fetch_array($an8_sql);
$dp = date_parse($myArr[$i]['ivd']);
$dp1 = getDate(mktime(0,0,0,$dp['month'],$dp['day'],$dp['year']));
$ivd = date2jul($dp1);//date('Y-M-d',mktime(0,0,0,$dp['month'],$dp['day'],$dp['year'])));
$dp = date_parse($myArr[$i]['dgl']);
$dp1 = getDate(mktime(0,0,0,$dp['month'],$dp['day'],$dp['year']));
$inv_no = strval($myArr[$i]['carrierInvoice']);
// index: ("VLEDUS", "VLEDBT", "VLEDTN", "VLEDLN")
$gld = date2jul($dp1);//date('Y-M-d',mktime(0,0,0,$dp['month'],$dp['day'],$dp['year'])));
$F0411Z1[1]['VLEDUS'] = '\''.strtoupper($user).'\'';//$_SESSION['userid'];
$F0411Z1[1]['VLEDLN'] = $i*1000;
$F0411Z1[1]['VLEDBT'] = $nn[1]['NNN006'];
$F0411Z1[1]['VLAN8'] = $myArr[$i]['carrierNbr'];
$F0411Z1[1]['VLPYE'] = $aban85[1]['ABAN85'];//$myArr[$i]['carrierNbr'];
$F0411Z1[1]['VLDIVJ'] = $ivd;//$myArr[$i]['ivd'];
//$F0411Z1[1]['VLDSVJ'] = $jul;
$F0411Z1[1]['VLDGJ'] = $gld;
$F0411Z1[1]['VLCO'] = $co;
$F0411Z1[1]['VLKCO'] = $co;
$F0411Z1[1]['VLAG'] = round(($myArr[$i]['allocation']*100),0);
$F0411Z1[1]['VLAAP'] = round(($myArr[$i]['allocation']*100),0);
$F0411Z1[1]['VLVINV'] = $inv_no;// <-- This element is the issue
$F0411Z1[1]['VLRMK'] = (strlen($carrName)>30?substr($carrName,0,29):$carrName);
$F0411Z1[1]['VLGLBA'] = '00573714';
$F0411Z1[1]['VLMCU'] = '1';
$F0411Z1[1]['VLTORG'] = $userid;//$_SESSION['userid'];
$F0411Z1[1]['VLUSER'] = $userid;//$_SESSION['userid'];
$F0411Z1[1]['VLPID'] = 'TRUCK';
$F0411Z1[1]['VLUPMJ'] = $jul;
$F0411Z1[1]['VLUPMT'] = $tjul;
$F0411Z1[1]['VLJOBN'] = 'TRUCK';
$F0411Z1[1]['VLURAB'] = $id;
$F0411Z1[1]['VLURRF'] = $myArr[$i]['bol'];
$z=1;
for($x=1;$x<=$F0411Z1[0];$x++){
$val1 = $F0411Z1[$x];
// first element of array is the counter, skip it
if($val1 != 1){
foreach($F0411Z1[1] as $val){
if($z==1){
$stmt = 'VALUES('.$val;
$z=99;
}
else{
if(!is_numeric($val))$val = '\''.$val.'\'';
$stmt .= ','.$val;
}
}
$stmt .= ')';
//$msSQL = 'INSERT INTO PS_PRODUCTION.PRODDTA.F0411Z1 '.$stmt;
$msSQL = 'INSERT INTO PRODDTA.F0411Z1 '.$stmt;
$count = oracle_update($msSQL);
if($count != 1) return 36;
}
}
}
}
else return 36;
return 0;
}
You can use the strval() method to cast the number as a string.
$number = 123E456;
$string = strval($number);
Or just force it to cast as a string
$string = (string) $number;
My issue is to display the data. It repeats the same id in it several times.
<?php
$check_shared_section = mysqli_query($MYSQLi,"select * from `vp_wall_post` where `type` = 'section' and `username` = '".mysqli_real_escape_string($MYSQLi,$poster_username)."' ");
$get_section = mysqli_fetch_array($check_shared_section);
$section_post = trim(strip_tags($get_section["post"]));
$section_page = trim(strip_tags($get_section["page_id"]));
$section_id = trim(strip_tags($get_section["pid"]));
echo $section_id;
?>
These are the issues that I am facing. Please help me to solve this problem.
It shows the output id
e4zDFOL3jBgcH8YRfkzJ
e4zDFOL3jBgcH8YRfkzJ
e4zDFOL3jBgcH8YRfkzJ
I want to show different id
e4zDFOL3jBgcH8YRfkzJ
er556gdfg4asffgfgfgg
So2cLYtCTTMYD0fCNFjq
JGH63vAqIAnt5jNCH6OL
<?php
$check_shared_section = mysqli_query($MYSQLi,"select * from `vp_wall_post` where `type` = 'section' and `username` = '".mysqli_real_escape_string($MYSQLi,$poster_username)."' ");
$get_section = mysqli_fetch_array($check_shared_section);
$section_post = trim(strip_tags($get_section["post"]));
$section_page = trim(strip_tags($get_section["page_id"]));
$section_id = trim(strip_tags($get_section["pid"]));
echo $section_id;
?>
I'm not sure why you are seeing the same pid value multiple times as you don't have any looping in your code at all. However the reason you are not getting all the different pid values is that you're not looping the results of the query. You need to use a while loop on the result of mysqli_fetch_array e.g.
while ($get_section = mysqli_fetch_array($check_shared_section)) {
$section_post = trim(strip_tags($get_section["post"]));
$section_page = trim(strip_tags($get_section["page_id"]));
$section_id = trim(strip_tags($get_section["pid"]));
echo $section_id;
// do other stuff with the values
}
Note that if you want to further process the results outside the while loop, you will need to save the values into arrays e.g.
$section_posts = array();
$section_pages = array();
$section_ids = array();
while ($get_section = mysqli_fetch_array($check_shared_section)) {
$section_posts[] = $section_post = trim(strip_tags($get_section["post"]));
$section_pages[] = $section_page = trim(strip_tags($get_section["page_id"]));
$section_ids[] = $section_id = trim(strip_tags($get_section["pid"]));
echo $section_id;
// do other stuff with the values
}
// you can use other loops to further process the values here
// e.g. foreach ($section_posts as $index => $section_post) {
// you can use $index to access the corresponding values from
// the $section_pages and $section_ids arrays e.g.
// $section_page = $section_pages[$index]
I have a huge table with some items. I want to retrive specific items from that table according with the players level of gaming . After the code extracts all the specific items i want to add them into an array ( in this case "$cart") . I want to update a mysql row into a varchar field the array separated by a comma ",";
I tried different methods of adding them into the database but something isn't working.
Heres my code :
$sql = mysql_query("select * from iteme where tip != 'Minereu' AND tip != 'Reteta' AND tip != 'Cooking' AND tip != 'Altele' AND tip != 'Potiuni' AND tip != 'Fragment' AND tip != 'Tools' AND tip != 'Medicina' ");
$numaratoare = 0;
$cart = array();
$nivel_player = mysql_query("select * from membri where id !=0 ");
while($informatie_player = mysql_fetch_array($nivel_player))
{
$nivel_actual_player = lvl($informatie_player['experienta']);
$shop_arme_plus_5 = $nivel_actual_player + 5 ;
if ($nivel_actual_player - 5 == 0 )
$shop_arme_minus_5 = $nivel_actual_player - 5 ;
else
$shop_arme_minus_5 = 0 ;
$shop_arme = mysql_query("select * from iteme where tip = 'Arme' AND level_minim < ".$shop_arme_plus_5." AND level_minim > ".$shop_arme_minus_5." AND vandabil = 1 ");
while($informatie = mysql_fetch_array($shop_arme)) {
$numaratoare ++;
//echo " ".$informatie['nume_ro']." , ";
array_push($cart, $informatie['obiect']);
}
mysql_query("INSERT INTO membri_shop_iteme set iteme_arme = ".$informatie["obiect"]." where id_jucator = ".$informatie_player['id']." "); // Here is the problem.
}
Where's the problem? I tried adding implode(', ', $cart); but still i can't add them into database.
Thanks for helping.
may be its string problem $informatie["obiect"] to $informatie['obiect']
mysql_query("INSERT INTO membri_shop_iteme set iteme_arme = ".$informatie['obiect']." where id_jucator = ".$informatie_player['id']." "); // Here is the problem.
to
$newObject=join(",",$cart);
mysql_query("UPDATE membri_shop_iteme SET iteme_arme = '".$newObject."' where id_jucator = ".$informatie_player['id']." "); // Here is the problem.
You use $informatie["obiect"] in your query, but you have to use $cart.
while($informatie = mysql_fetch_array($shop_arme)) {
$numaratoare ++;
array_push($cart, $informatie['obiect']);
}
mysql_query("UPDATE membri_shop_iteme
SET iteme_arme = '".implode("','", $cart)."'
WHERE id_jucator = '".$informatie_player['id']."'");
I am trying to determine the percentage of number of student passing a test either on 1st attempt or 2nd attempt. I believe I have the code correct but when I echo the results I get no output.
<?php
$NREMT1 = "SELECT nremtcognitive FROM course_students WHERE nremtcognitive = '1' ";
$NREMT2 = "SELECT nremtcognitive FROM course_students WHERE nremtcognitive >= '2' ";
$completed = "SELECT studentstatus FROM course_students WHERE studentstatus >='4'";
$getNREMT1 = mysql_query($NREMT1);
$fetchNREMT1= mysql_num_rows($getNREMT1);
$getNREMT2 = mysql_query($NREMT2);
$fetchNREMT2= mysql_num_rows($getNREMT2);
$getcompleted = mysql_query($completed);
$fetchcompleted= mysql_num_rows($getcompleted);
function percent($fetchNREMT1, $fetchNREMT2, $fetchcompleted) {
$NREMT1count= $fetchNREMT1/$fetchcompleted;
$NREMT1percent= $NREMT1count * 100;
$NREMT1result = number_format($NREMT1percent,0);
$NREMT2count= $fetchNREMT2/$fetchcompleted;
$NREMT2percent= $NREMT2count * 100;
$NREMT2result = number_format($NREMT2percent,0);
}
echo $NREMT1result;
echo $NREMT2result;
?>
You are not calling your function percent anywhere.
You should call it and make it return something or otherwise, just can avoid creating it and put the same code in the same place:
$NREMT1count= $fetchNREMT1/$fetchcompleted;
$NREMT1percent= $NREMT1count * 100;
$NREMT1result = number_format($NREMT1percent,0);
$NREMT2count= $fetchNREMT2/$fetchcompleted;
$NREMT2percent= $NREMT2count * 100;
$NREMT2result = number_format($NREMT2percent,0);
echo $NREMT1result;
echo $NREMT2result;
It seems you don't have it very clear the concept of function. You can take a look at the documentation.
Your problem is that the variables declared inside percent() are never initialized because the function is never called. Try this:
<?php
$NREMT1 = "SELECT nremtcognitive FROM course_students WHERE nremtcognitive = '1' ";
$NREMT2 = "SELECT nremtcognitive FROM course_students WHERE nremtcognitive >= '2' ";
$completed = "SELECT studentstatus FROM course_students WHERE studentstatus >='4'";
$getNREMT1 = mysql_query($NREMT1);
$fetchNREMT1= mysql_num_rows($getNREMT1);
$getNREMT2 = mysql_query($NREMT2);
$fetchNREMT2= mysql_num_rows($getNREMT2);
$getcompleted = mysql_query($completed);
$fetchcompleted= mysql_num_rows($getcompleted);
percent($fetchNREMT1, $fetchNREMT2, $fetchcompleted);
function percent($fetchNREMT1, $fetchNREMT2, $fetchcompleted) {
$NREMT1count= $fetchNREMT1/$fetchcompleted;
$NREMT1percent= $NREMT1count * 100;
$NREMT1result = number_format($NREMT1percent,0);
$NREMT2count= $fetchNREMT2/$fetchcompleted;
$NREMT2percent= $NREMT2count * 100;
$NREMT2result = number_format($NREMT2percent,0);
echo $NREMT1result;
echo $NREMT2result;
}
?>
It is also worth noting that the mysql_* family of functions are now deprecated, and you should really consider using MySQLi or PDO.
Just change your query. Untested, but this should get you started:
SELECT
SUM(IF(CONVERT(nremtcognitive, UNSIGNED INTEGER) >= 2)) / COUNT(*)
FROM course_students;
this is the code i use to generate single json object
$SQL = mysql_query("SELECT * FROM `receipts` WHERE DATE(date) = '2011-08-03'");
if(mysql_num_rows($SQL ) > 0){
$i=0;
$responce->success = true;
while($SQL_RESULT = mysql_fetch_object($SQL)){
$responce->data[$i]['reciept_no'] = $SQL_RESULT->reciept_no;
$responce->data[$i]['time'] = $SQL_RESULT->date;
$responce->data[$i]['user'] = $SQL_RESULT->user;
$i++;
}
}
else{
$responce->success = false;
$responce->data = '';
$responce->reason = "No Activity...";
}
echo json_encode($responce);
result is like
{"success":true,"data":[{"reciept_no":"2411","time":"09:33:56 AM","user":"test"},
{"reciept_no":"2412","time":"11:29:01 AM","user":" test "}]}
so there is another query which similar to this and generate exact same kind of output but from a another mysql table
i want to do is combine two results and send to javascript then decode it in javascript
like wrap first result with like table1 second result with table 2 or something
how to do that?
Sorry for the bad English
Regards
You could run both queries, one outputs to $response1 and the other to $response2, then you can use:
echo json_encode(array('table1'=>$response1,'table2'=>$response2));
JSON is just a text representation of a data structure. If you want to store two separate results in a single structure, then do
data['response #1'] = 'blah blah blah';
data['response #2'] = 'other other other';
You could store the two queries' data into a single sub-array, but then you'd need some extra data to be able to differentiate betweeen the two data sources. "did this record come from query #1? or from query #2?"
$SQL = mysql_query("SELECT * FROM `receipts` WHERE DATE(date) = '2011-08-03'");
if(mysql_num_rows($SQL ) > 0){
$i=0;
while($SQL_RESULT = mysql_fetch_object($SQL)){
$responce->data[$i]['reciept_no'] = $SQL_RESULT->reciept_no;
$responce->data[$i]['time'] = $SQL_RESULT->date;
$responce->data[$i]['user'] = $SQL_RESULT->user;
$i++;
}
}
$SQL = mysql_query("SELECT * FROM `receipts2` WHERE DATE(date) = '2011-08-03'");
if(mysql_num_rows($SQL ) > 0){
$i=0;
$responce->success = true;
while($SQL_RESULT = mysql_fetch_object($SQL)){
$responce->data[$i]['reciept_no'] = $SQL_RESULT->reciept_no;
$responce->data[$i]['time'] = $SQL_RESULT->date;
$responce->data[$i]['user'] = $SQL_RESULT->user;
$i++;
}
}
else{
$responce->success = false;
$responce->data = '';
$responce->reason = "No Activity...";
}
echo json_encode($responce);
Is that your question?