Here is my model code that gets percentage of all questions in table (questions)
$public function ques()
{
#get all question count
$query= $this->db->query("SELECT * FROM questions");
$result = $query->result_array();
$count = count($result);
$limit = ($count/100)*50;
# select only limited question which set with $limit
$query= $this->db->get("Questions" , $limit);
$result = $query->result_array();
return $result;
}
There are four columns :
1. ques_id
2. question
3. exam_id
4. chapter
What I need that to get no of rows of each chapter and get percentage rows rather than to count all the questions only and returning result as shown in code. Thank you
Related
I need 3 question from 6. but every time randomize show fast 4 question.
/*for random question*/
public function qustionShow($question){
$query = $this->conn->query("select * from question where cat_id='$question'");
$c = mysqli_num_rows($query);
$rand = rand(3, $c)-3;
$show = $this->conn->query("select * from question where cat_id ='$question' and id >'$rand' LIMIT 3");
while ($row=$show->fetch_array(MYSQLI_ASSOC)){
$this->qus[]=$row;
}
return $this->qus;
}
You can reduce and optimize your code, using ORDER BY RAND().
Try something like:
public function qustionShow($question, $limit=3){
$show = $this->conn->query("select * from question where cat_id ='$question' ORDER BY RAND() LIMIT $limit");
while ($row=$show->fetch_array(MYSQLI_ASSOC)){
$this->qus[]=$row;
}
return $this->qus;
}
I am currently using below code to sum up numbers of quantity based on a ID
targeting mysql quantity column, just wondering if this is the best practice using codeigniter? or is there a shorter way to execute this.
My Model.php
public function sum_quantity_of_stacks($id){
$query = $this->db->query("SELECT *, SUM(qty) as total FROM stocks WHERE drug_id = '$id'");
return $query->row_array();
}
public function sum_quantity_of_request($id){
$query = $this->db->query("SELECT *,SUM(quantity) as total_request FROM med_request WHERE med_given = '$id' AND status = 'Approved'");
return $query->row_array();
}
Then my view.php
$id = $medicine['ID'];
$data['get_count'] = $this->Medicine_model->sum_quantity_of_stacks($id);
$data['get_request'] = $this->Medicine_model->sum_quantity_of_request($id);
$total_available = $data['get_count']['total'];
$total_given = $data['get_request']['total_request'];
echo $total_available - $total_given;
Above code gives me the current available stocks of my medicine inventory.
No, there are no such way to do that, similar to what are you are doing now.
SELECT other column with SUM feature not possible
But there are a method name select_sum() available in query builder by which you may get SUM only like
public function sum_quantity_of_stacks($id){
$this->db->select_sum('qty','total');
$this->db->where('drug_id',$id);
$query = $this->db->get('stocks');
$res = $query->row_array();
//now SUM is available in $res['total']
return $res['total'];
// or return $query->row_array();
}
I have a function in php I use it for paging it is like this :
$query = "SELECT id,
FROM table
ORDER BY id ASC
LIMIT $offset,5";
this work fine but what I want is to get the page that contain id number let say 10 and with it the other 4 rows, I want it to return something like this:
7,8,9,10,11,12 -> if I give it id number 10.
25,26,27,28,29 -> if I give it id number 26 and so on.
like it would return the 5 rows but I want to know how to set the offset that will get me
the page that have the 5 rows with the specified id included.
what should I do like adding where clause or something to get what I want!
Notice that the IDs in your table won't be consecutive if you delete some rows. The code below should work in such conditions:
$result = mysql_query('select count(*) from table where id < ' . $someId);
$offset = mysql_result($result, 0, 0);
$result = mysql_query('select * from table order by id limit ' . max($offset - 2, 0) . ',5');
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
}
Try something like this
//but for pagination to work $page should be $page*$limit, so new rows will come to your page
$limit = 5;
$start = ($page*limit) -2; // for normal pagination
$start = $page -2; // for your case, if you want ids around the $page value - in this case for id = 10 you will get 8 9 10 11 12
if ($start < 0) $start = 0; // for first page not to try and get negative values
$query = "SELECT id,
FROM rowa
ORDER BY id ASC
LIMIT $start,$limit";
I apologize if the title is confusing, it's hard to title this question.
I've been wondering how I could retrieve data from MySQL but only retrieve data skipping first 4 outputs where it's ordered by id.
Ex:
function firstFour()
{
$sth = $this->db->prepare("SELECT data FROM table WHERE category = 'Sports'
ORDER BY id LIMIT 4");
$sth->execute();
$row = $sth->fetch();
return $row;
}
then on the second query, skip the first four where it's ordered by id on the firstFour query above and output the rest of the data from the sports category.
You need to put limit like follows
LIMIT 4 , 4
LIMIT 8 , 4
so on
Use this query, your limit 4 is making it start from row 0 and retrieve up to row 4, setting the limit as 4, xxxx will go from 4 to whatever xxxx is. I used a large number in place of xxxx
"SELECT data FROM table WHERE category = 'Sports'
ORDER BY id LIMIT 4, 99999999999"
To expand on Zahidul's answer and to make your life easier, you might want to generalize your function a little bit - using firstFour(), secondFour(), etc. is going to make your life a lot harder. I would advise taking Zahidul's idea and passing in your limit variable:
function anyFour($lowLimit)
{
$sth = $this->db->prepare("SELECT data FROM table WHERE category = 'Sports'
ORDER BY id LIMIT $lowLimit, 4");
$sth->execute();
$row = $sth->fetch();
return $row;
}
Then if you wanted to keep the function firstFour(), it would be:
function firstFour()
{
return anyFour(0);
}
From there, you could create a simple loop that incremented by 4.
for ($x = 0; $x < 999;$x++) // 999 should be the total number of sets you want to return...
{
// on each loop, $myRows returns the next 4 rows:
$lowLimit = $x * 4;
$myRows = anyFour($lowLimit);
}
Try this:
$sth = $this->db->prepare("SELECT data FROM table WHERE category = 'Sports'
ORDER BY id;");
$sth->execute();
$row = $sth->fetch();
$row_num = 0;
while ($row) {
$row_num +=1;
if ($row_num > 4) {
//do whatever
}
}
How to get total number of rows for particular query and also limiting the query results to 10 rows. For example. I've a table called sample. It has 400 rows. On running a query like where name = "%Sam%" it returns me 213 rows. Now I'l be taking only first 10 rows and displaying the result to user but I need the total rows returned. How should I need to do it in code igniter?
SELECT
SQL_CALC_FOUND_ROWS *
FROM
sample
WHERE
name like "%sam%"
like this?
How to retrieve total number counts?
You need to run a second query to get the results of SQL_CALC_FOUND_ROWS:
SELECT FOUND_ROWS()
That will return the value found using SQL_CALC_FOUND_ROWS. You may find using an alias easier for getting the result, though:
SELECT FOUND_ROWS() AS num_results
You can use two query one to get the count and the other return the limited result.
Like in your model create a function that only returns the count variable
and create a second function that generates the paginated results.
eg..
public function count($where){
$query = $this->db->query("select count(id) as count from clients $where");
return $query->row('count');
}
}
public function limit($sidx,$sord,$start,$limit,$where){
$query = $this->db->query("select * from clients $where ORDER BY $sidx $sord LIMIT $start , $limit");
if ($query->num_rows() > 0){
return $query->result_array();
}
}
And here goes the controller code
$where = // calculated
$count = count($this->model->count($where));
if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; }
if ($page > $total_pages) $page = $total_pages;
$start = $limit * $page - $limit;
$users = $this->model->limit($sidx,$sord,$start,$limit,$where);
.................