Get the value of a cell in mysql table - php

I'm using CodeIgniter to write a few lines of simple code to get value from the mysql table.
What I've tried:
$interview_id=45
$category_raw = $this->db->query("select category from interview where interview_id='$interview_id'");
$category = $category_raw->first_row();
But for some reason, the $category is shown as "array()", while I actually want to get the value within.
Any advice on how to proceed?
Thanks so much,

Try
$interview_id=45;
$category_raw = $this->db->query("select category from interview where interview_id=$interview_id");
$category = $category_raw->first_row()->category;
Or with active record
$interview_id=45;
$this->db->select("category");
$this->db->where("interview_id",$interview_id);
$category_raw = $this->db->get("interview");
$category = $category_raw->first_row()->category;

You can take the value like
$category = $category_raw->first_row()->category;
because first_row() alwise return value as object
documentation

Related

Detect when id retrieved from php query is changed

I'm running a query where I'm getting the average score of a student, by getting the average score of an activity type(quiz,exams,etc.) and multiplying it to its assigned grade percentage(ex: 80 *.03).
I've grouped the result by its activity_ID and Student ID.
To get the Grade of the student I need to add the score of each activity type. by simple adding all the result of each activity type.
(ex:grade =10.40+30+12;)
Now my problem is when the student ID is changed the result of the previous student is added to the result of the new one.
(ex:new student grade = previous result, plus his own)
I want to detect when the Student ID is changed inside the
while($row = mysqli_fetch_array($result)) {
$studentID_link = $row['studentID'];
$atName = $row['AT_name'];
$GLOBALS['AR_Score'] = $row['AVG(AR.Score)'];
$AT_gradePercentage =$row['AT_gradePercentage'];
$AT_gradePercentage /=100;
$periodGrades = $AR_Score*$AT_gradePercentage;
$variableName = $variableName + $periodGrades;
}
Use
mysqli_fetch_assoc()
Check for the id after storing the initial student id in a temp variable outside of the loop. Compare each new row's student ID to the previously stored temporary id. Also declare an array outside of the loop and use
array_push()
to push each total onto the array. Use an associative array with the student ID as the key and avoid using array_push if you are accessing student IDs out of order or use an index in mysql to allieviate this issue.
Honestly, you may want to reexamine your sql or show us if you want us to rewrite the code for you.
Update:
If the current mysqli_fetch_array is fetching an associative array, you can leave it, however refer to my above suggestions if you are having issues with that.
$curr_student = NULL;
$array = [];
while($row = mysqli_fetch_array($result)) {
$studentID_link = $row['studentID'];
if($curr_student === NULL || $curr_student !== $studentID_link){
$curr_student = $studentID_link;
if(!array_key_exists($curr_student, $array)) $array[$curr_student] = 0;
}
$atName = $row['AT_name'];
$GLOBALS['AR_Score'] = $row['AVG(AR.Score)'];
$AT_gradePercentage =$row['AT_gradePercentage'];
$AT_gradePercentage /=100;
$periodGrades = $AR_Score*$AT_gradePercentage;
$array[$curr_student] = $array[$curr_student] + $periodGrades;
}
I'm not certain about your global array and how you are using it, but this should work to add your students into each array member regardless of random access of student ID.

IN clause is not working properly in MYSQL php

I have a sql query like this
$statusData = $DB->get_records_sql("SELECT id, element, value FROM {scorm_scoes_track} WHERE scormid = ? AND userid = ? AND element =$DB->get_in_or_equal('cmi.core.lesson_status','cmi.core.total_time')", array($scorm_id, $userid) );
So i am trying to fetch the values from query
<?php
$statusData = reset($statusData);
if($statusData->element == 'cmi.core.lesson_status')
$status = $statusData->value ;
elseif($statusData->element == 'cmi.core.total_time')
$totaltimes = $statusData->value ;
but its just showing only status value..it's not showing total time value..Can any one help me
Thanks in advance..
You do not have any in operator in your code, so there is nothing that could work at all as an in!
Replace the = with in and remove the moodle call as well as shown below:
...AND element IN ('cmi.core.lesson_status','cmi.core.total_time')

Using a set of Mysql results as a comparison in PHP

This question is quite simple but I'm not sure of the terms involved to look up the answer myself. What I have is a MYSQL database containing all of my product information. I would like to make an image appear on the product pages for products that have a certain attribute. I have created this SQL query which outputs the list of product_ids for the products that I would like the image to show up on. However, I do not know how to use this set of results in the page itself.
I currently have:
$cupwinnerids = mysql_query("SELECT product_id FROM `jos_vm_product_type_1` WHERE jos_vm_product_type_1.Cup_Winner ='Cup Winners';");
while($row = mysql_fetch_array($cupwinnerids))
{
echo $row['product_id'] . ",";
}
This outputs the correct ids with a comma in between them. What I would like to do is wrap the whole thing with something like $listofids = (...) and then I can use in the product page PHP file: if $product_id is in $listofids then ... if not then ... I am just having a problem understanding how to use this selection of ids. If I try to output the list directly I just get "array". Any help would be greatly appreciated.
The problem may be that you are trying to display the array using echo, while you should use print_r.
In your case you could do something like this:
$listofids = array();
$cupwinnerids = mysql_query("SELECT product_id FROM `jos_vm_product_type_1` WHERE jos_vm_product_type_1.Cup_Winner ='Cup Winners';");
while($row = mysql_fetch_array($cupwinnerids))
{
array_push($listofids, $row['product_id']);
}
print_r($listofids);
As Ryan commented, if you want to manage the values of the array you should then use a foreach loop, that iterates through the array, something like this
foreach ($listofids as $id) {
echo $id . ", ";
}
// EDIT: you could also use echo implode($listofids,", ") which will do basically the same
If what you want is to compare if $product_id is in $listofids us in_array, that returns true if the value you are looking for is in the array, and false if it isn't:
if (in_array($product_id, $listofids)) {
echo "Product ID is in the List of Product ID's";
}
else {
echo "Product ID isn't in the List of Product ID's";
}
Why not return $cupwinnerids to your products page, and then do something like...
while($row = mysql_fetch_array($cupwinnerids)) {
if ($row['product_id'] == $product_id)
// display your image
}
It sounds like you would be better off modifying your query for returning the products so that it joins to the jos_vm_product_type_1 table -
SELECT `jos_vm_product`.*, IF(`jos_vm_product_type_1`.`product_id` IS NULL, 0, 1) AS `winner`
FROM `jos_vm_product`
LEFT JOIN `jos_vm_product_type_1`
ON `jos_vm_product`.`product_id` = `jos_vm_product_type_1`.`product_id`
AND `jos_vm_product_type_1`.`Cup_Winner` ='Cup Winners'

How to make query to a stdclass objecta to get a value from variable and make a sum with it?

I have a wordpress table called wp_mgm_transactions with two text fields and several rows of records: MODULE and DATA.
In each field DATA i have this values/structure (is this a stdclass object?):
{"id":"3","membership_type":"member","duration":1,"duration_type":"l","cost":"10.00","num_cycles":1,"role":"subscriber","default":"1","description":"Acesso a todos os conte\u00fados do site.","hide_old_content":"0","active":"1","sort":"3","trial_on":"0","trial_duration":"0","trial_duration_type":"d","trial_cost":"0.00"}
I need to make a query to get the sum of 'cost'of all the data in rows WHERE module = 'paypal'
i know how to do it, if the DATA field was a number field, it would like this:
$SumTotalData = $wpdb->get_var("SELECT sum(data) FROM wp_mgm_transactions WHERE module = 'paypal'");
But, with all the above data type field, i'm lost. Any help?
Thank you,
nelson
Would this fit your need?
$find = What your looking for
$resultlength = The length of the supposed result, in this case 10.00 is 5 characters long.
$text = What to search in
$find = 'cost';
$resultlength = 5;
$text = '{"id":"3","membership_type":"member","duration":1,"duration_type":"l","cost":"10.00","num_cycles":1,"role":"subscriber","default":"1","description":"Acesso a todos os conte\u00fados do site.","hide_old_content":"0","active":"1","sort":"3","trial_on":"0","trial_duration":"0","trial_duration_type":"d","trial_cost":"0.00"}';
$result = substr(strstr($text, $find), strlen($find)+3, $resultlength);
echo $result;

How to manipulate parameter according to other parameter in a php function?

I have id names with the second part of table name plus id.
For example
admins_id in omc_admins table,
customers_id in omc_customers table,
products_id in omc_products table,
categories_id in omc_categories table etc.
Now the following function code is supposed to find orphans. For example, when I delete categories, it will check the products and find orphans of that category.
Now I am not sure how to manipulate parameter according to other parameter. For example 'id' in $this->db->select('id,name');.
id will be changing according to the $db_table.
function checkOrphans($segment, $id, $db_table){
$data = array();
$this->db->select('id,name');
$this->db->where($id,id_clean($segment));
$Q = $this->db->get($db_table);
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data[$row['id']] = $row['name'];
}
}
$Q->free_result();
return $data;
}
I think I can use $db_table. For example $db_table is omc_categories, I can use categories part and add to id.
categories_id.
Can anyone tell me how to do it please?
I am using codeigniter, but question is purely php.
You can get the id name in this way:
$id_name = preg_replace('/.*_(.*)/', '${1}_id', $db_table);
then
$this->db->select($id_name . ',name');

Categories