I am havin trouble with mySQL reports.
What I have:
My report now
What I need:My future report
I need to get total price of group for example screen size with 12.5', then total price of screens with their size 13.3' and so on...
I tried this:
public function getGroupPrice($priceFrom, $priceTo) {
$whereClauseString = "";
if(!empty($priceFrom)) {
$whereClauseString .= " WHERE `{$this->pc_table}`.`price`>='{$priceFrom}'";
if(!empty($priceTo)) {
$whereClauseString .= " AND `{$this->pc_table}`.`price`<='{$priceTo}'";
}
} else {
if(!empty($priceTo)) {
$whereClauseString .= " WHERE `{$this->pc_table}`.`price`<='{$priceTo}'";
}
}
$query = " SELECT `{$this->screen_table}`.`size`,
sum(`{$this->pc_table}`.`price`) AS `total_price`
FROM `{$this->pc_table}`
INNER JOIN `{$this->screen_table}`
ON `{$this->pc_table}`.`fk_Screenid_Screen`=`{$this->screen_table}`.`id_Screen`
GROUP BY `{$this->screen_table}`.`size`";
$data = mysql::select($query);
return $data;
}
And I get some nonsense data. What could I be doing wrong?
Related
i am working on a school room booking system but i am having some trouble outputting the bookings.
Public function displayRoomByDay() {
$query = DB::GetInstance()->query("SELECT B.roomid, B.period, R.roomname, B.bookingdate, B.bookingid FROM booking B INNER JOIN room R on b.roomid = r.roomid WHERE bookingdate = '2016-11-03' and b.roomid=1 ORDER BY b.period"); //Inner join
$count = $query->count();
$freeCount = 0;
for($periods=1;$periods<=6;$periods++) {
for($x=0;$x<$count-1;$x++) {
$outputted=false;
$freeCount=0;
do {
if($query->results()[$x]->period == $periods) {
echo $query->results()[$x]->period . '<br>';
$outputted=true;
} else {
echo 'FREE' . '<br>';
$freeCount = 1;
}
} while($outputted = false and $freeCount=0);
}
}
}
This is the function that i use to output my data. My SQL query returns two items, a booking in period 5 and a booking in period 1 (i have tested this through PHPMyAdmin). I am trying to use nested for loops and a do while loop to loop through the periods available in a day (6). From there i loop through the two bookings that my sql query returns which is this code:
for($x=0;$x<$count-1;$x++) {
$outputted=false;
$freeCount=0;
do {
if($query->results()[$x]->period == $periods) {
echo $query->results()[$x]->period . '<br>';
$outputted=true;
} else {
echo 'FREE' . '<br>';
$freeCount = 1;
}
} while($outputted = false and $freeCount=0);
}
However when i run my page, i get 1 FREE FREE FREE FREE FREE, when i am trying to get 1 FREE FREE FREE FREE 5 as they are when the bookings are.
Your logic is very complicated for this task. You can simplify it like:
public function displayRoomByDay()
{
$query = DB::GetInstance()->query("
SELECT B.roomid,
B.period,
R.roomname,
B.bookingdate,
B.bookingid
FROM booking B
INNER JOIN room R
ON b.roomid = r.roomid
WHERE bookingdate = '2016-11-03'
AND b.roomid=1 ORDER BY b.period"); //Inner join
$results = array_reduce($query->results(), function ($carry, $item) {
$carry[$item->period] = $item;
return $carry;
}, []);
$periods = range(1, 6);
foreach ($periods as $period) {
if (isset($results[$period]) {
echo $period . '<br />';
} else {
echo 'FREE' . '<br />';
}
}
}
I think that's all you need.
Afternoon all,
I am working through a tutorial from MASTERING EXT JS and am stuck on retrieving data from db.
The book has been using examples using PHP and MYSQL... which I do not know. I use a .net web server and SQL, so I'm trying to convert this example from the tutorial, to how I would do it on my .net webserver.
the result in JSON format should be something like this
{
"data"[
{
"id":1",
"text" : "menu1",
"items": [
{"id": 2",
"text: "submenu2
},
{
"id":"3",
"text":"submenu3"
}
the php code they give me is this
php file 1
$permissions = retrievePermissions($userName); $modules =
retrieveModules($permissions); $result = retrieveMenuOptions($modules,
$permissions);
php file 2
function retrievePermissions($userName){
require('../db/db.php');
$sqlQuery = "SELECT p.menu_id menuId FROM User u ";
$sqlQuery .= "INNER JOIN permissions p ON u.groups_id = p.groups_id ";
$sqlQuery .= "INNER JOIN menu m ON p.menu_id = m.id ";
$sqlQuery .= "WHERE u.username = '$userName' ";
$permissions = [];
if ($resultDb = $mysqli->query($sqlQuery)) {
while($user = $resultDb->fetch_assoc()) {
$permissions[] = $user['menuId'];
}
}
$resultDb->free();
$mysqli->close();
return $permissions; }
function retrieveModules($permissions){
require('../db/db.php');
$inClause = '(' . join(',',$permissions) . ')';
$sqlQuery = "SELECT id, text, iconCls FROM menu WHERE menu_id IS NULL AND id in $inClause";
$modules = [];
if ($resultDb = $mysqli->query($sqlQuery)) {
while($module = $resultDb->fetch_assoc()) {
$modules[] = $module;
}
}
$resultDb->free();
$mysqli->close();
return $modules; }
function retrieveMenuOptions($modules, $permissions){
require('../db/db.php');
$inClause = '(' . join(',',$permissions) . ')';
$result = [];
foreach ($modules as $module) {
$sqlQuery = "SELECT * FROM menu WHERE menu_id = '";
$sqlQuery .= $module['id'] ."' AND id in $inClause";
// check if have a child node
if ($resultDb = $mysqli->query($sqlQuery)) {
// determine number of rows result set
$count = $resultDb->num_rows;
if ($count > 0){
$module['items'] = array();
while ($item = $resultDb->fetch_assoc()) {
$module['items'][] = $item;
}
}
$result[] = $module;
}
}
$resultDb->close();
$mysqli->close();
return $result;
I'm trying to figure out how to return the same json format using my .net webservice/SQL instead of php/MySQL.
It seems like it does 3 separate functions. And the result array is used as a parameter for the next query.
The basics seem easy... like for retreivePermissions... it is a simple SELECT WHERE statement.
retrieveModules seems to be an INNER JOIN with the first results.
But the last one... retrieveMenuOptions, it pulls in both results as parameters, and It returns results.
That is what I don't understand... how can I pull the results from SQL in the same JSON result format.
Am I making sense?
I have an example that uses a .NET Web API controller. Not exactly a web service, but you'll get the idea. Check it out here: http://jorgeramon.me/2015/ext-js-grid-search-with-net-and-mysql-backend/
I have successfully pulled data from mySql into the message of the html email. But after several attempts, I'm unable to add a two break lines after the 10th record. Any assistance would be wonderful.
$sorc325sql = "Select * ".$sorc325sql2;
echo "<br>325sql=".$sorc325sql."<br><br>";
$sorc325res = mysqli_query($connectXXX, $sorc325sql) or die(mysqli_error($connectXXX));
if (mysqli_num_rows($sorc325res) < 1) {
$display_block = "<p><em>No topics exist.</em></p>";
} else {
while ($sorc325row = mysqli_fetch_array($sorc325res)) {
$sorc325arycustctr++;
$message .= $sorc325row['progName'].', ';
}
}
Using Mark B's idea, you need to add it to the current $sorc325arycustctr++; line
$sorc325sql = "Select * ".$sorc325sql2;
echo "<br>325sql=".$sorc325sql."<br><br>";
$sorc325res = mysqli_query($connectXXX, $sorc325sql)
or die(mysqli_error($connectXXX));
if (mysqli_num_rows($sorc325res) < 1) {
$display_block = "<p><em>No topics exist.</em></p>";
} else {
$sorc325arycustctr = 0;
while ($sorc325row = mysqli_fetch_array($sorc325res)) {
$sorc325arycustctr++;
if (! ($sorc325arycustctr % 10)) {
$message .= '<br>';
}
$message .= $sorc325row['progName'].', ';
}
}
I have this coding :
foreach ($users_id_array[REGION_NORTH_REFID][813] as $key) {
$query_course = "SELECT ut_lp_marks.obj_id, object_data.title, read_event.spent_seconds, " .
"read_event.read_count, ut_lp_marks.status, ut_lp_marks.percentage, ut_lp_marks.u_comment FROM ut_lp_marks ".
"LEFT JOIN object_data ON (object_data.obj_id = ut_lp_marks.obj_id) ".
"LEFT JOIN read_event ON (read_event.obj_id = object_data.obj_id AND read_event.usr_id = ut_lp_marks.usr_id) ".
"WHERE ut_lp_marks.usr_id=$key AND object_data.type = 'crs'";
$set_course = mysql_query($query_course);
while($rec_course = mysql_fetch_assoc($set_course))
{
if ($rec_course['status'] == 0) {
$total_regna++;
}
if ($rec_course['status'] == 1) {
$total_reginprogress++;
}
if ($rec_course['status'] == 2) {
$total_regpassed++;
}
if ($rec_course['status'] == 3) {
$total_regfailed++;
}
// $total_attempt = $total_attempt + $rec_course['read_count'];
// $total_spent = $total_spent + $rec_course['spent_seconds'];
}
$no_test++;
}
the variable that i used is for each :
813, 945, 835, 777
My problems is my coding right now is only for 1 variable.
How can i used this same code for different variable,do i need to make array or a function? How can i access .. different $total_regna++; $total_reginprogress++; $total_regpassed++; $total_regfailed++; if i am using 1 code for 4 variable?..
You can make an array of these values such as
$values = array('813','945','835','777');
and instead of using for each, you can implode this array values in query.
$query_course = "SELECT ut_lp_marks.obj_id, object_data.title, read_event.spent_seconds, " .
"read_event.read_count, ut_lp_marks.status, ut_lp_marks.percentage, ut_lp_marks.u_comment FROM ut_lp_marks ".
"LEFT JOIN object_data ON (object_data.obj_id = ut_lp_marks.obj_id) ".
"LEFT JOIN read_event ON (read_event.obj_id = object_data.obj_id AND read_event.usr_id = ut_lp_marks.usr_id) ".
"WHERE ut_lp_marks.usr_id in (".implode(',',$values).") AND object_data.type = 'crs'";
Now while loop will be same.
I think it will be good in your case.
$ids=array(813, 945, 835, 777);
for ($i=0;$i<4;$i++){
foreach ($users_id_array[REGION_NORTH_REFID][$ids[$i]] as $key) {
...
I have two queries sent to a database bring back posts (op_ideas 16 cols) followed by another which holds the votes per post (op_idea_vote cols 4) with matching idea_id's
Example of Data:
Query: op_ideas:
[{"idea_id":"2211","author_id":"100000", "date":"2012-09-06
10:02:28","idea_title":"Another test","4" etc etc
Query: op_idea_votes:
idea_id = 2211, agree=3, disagree=1, abstain=0
The code below ought to look at op_ideas, and then cycle over op_ideas_vote until it finds a match under 'idea_id'. Then it goes to the next record under op_ideas, and again using that idea_id search for it within the op_idea_vote list, find a match, and add it to the array.
This works for only the first record, not for the other three. I am testing, so I have 3 rows in each that match idea_id with different results in the op_idea_vote.
$votes = mysql_query($commentVotes);
$result = mysql_query($gl_query);
while ($gce_result = mysql_fetch_array($result)) {
$voteid = $gce_result['idea_id'];
while($allvotes= mysql_fetch_array($votes)) {
if($voteid = $allvotes['idea_id'])
{
//echo $voteid . " main idea and the votes: " . $allvotes;
$gce_result["agree"] = $allvotes['agree'];
$gce_result["disagree"] = $allvotes['disagree'];
$gce_result["abstain"] = $allvotes['obstain'];
}
else
{
$gce_result["agree"] = 0;
$gce_result["disagree"] = 0;
$gce_result["abstain"] = 0;
}
//print_r($gce_result);
}
$data_result[] = $gce_result;
}
echo json_encode($data_result);
If I use print_f(&gce_result) it works fine in phpfiddle. But when i use the code above, it works for the first record, but it's complete missing the second two. It seems to be missing the second while, as it does not even give me the 0 0 0 results.
Query for op_ideas:
$gl_query = "SELECT DISTINCT * FROM heroku_056eb661631f253.op_ideas INNER JOIN op_organs ORDER BY date ASC;";
if (!mysql_query($gl_query)) {
die('Error: ' . $gl_query . " " . mysql_error());
}
$result = mysql_query($gl_query);
Query For op_idea_vote :
$commentVotes = "SELECT v.idea_id, COUNT(v.agree = 1 or null) as agree, COUNT(v.disagree = 1 or null) as disagree, COUNT(v.obstain = 1 or null) as obstain FROM op_idea_vote v GROUP BY v.idea_id";
if (!mysql_query($commentVotes)) {
die('Error: ' . $commentVotes . " " . mysql_error());
}
$votes = mysql_query($commentVotes);
You can scan a resource only once.
So the inner while will be run only one time.
use == instead of = for checking condition of if & while
in the while loop ,you have to assign the value of $allvotes ,but you never assigned,
while ($gce_result == mysql_fetch_array($result)) {
$voteid = $gce_result['idea_id'];
while($allvotes== mysql_fetch_array($votes)) {
if($voteid == $allvotes['idea_id'])
{
//echo $voteid . " main idea and the votes: " . $allvotes;
$gce_result["agree"] = $allvotes['agree'];
$gce_result["disagree"] = $allvotes['disagree'];
$gce_result["abstain"] = $allvotes['obstain'];
}
else
{
$gce_result["agree"] = 0;
$gce_result["disagree"] = 0;
$gce_result["abstain"] = 0;
}
$data_result[] = $gce_result;
}
}
Your problem is trying to scan over the $votes result more than once.
You should store the result of that query first.
Eg.
while ($vote = mysql_fetch_array($votes)) {
$allvotes['idea_id'] = $vote;
}
while ($gce_result = mysql_fetch_array($result)) {
$voteid = $gce_result['idea_id'];
if (array_key_exists[$voteid, $allvotes]) {
//assign results
} else {
//default
}
}
Another option would be to do the query with a join, so you can do everything in one query. Then just loop over that result.