why this code not work?
my model
public function get_question_by_testid($id) {
$this->db->select('question, description, name');
$this->db->from('question');
$this->db->join('test', 'test.id = question.testid');
$this->db->where('testid', $id);
$query = $this->db->get();
$result = $query->result_array();
$count = count($result);
if (empty($count) || $count > 1) {
$question = null;
return $question;
} else {
return $result;
}
}
why this code not work?
//controller function test($id = NULL) {
$data['tests'] = $this->student_model->get_test_by_id($id);
$data['questions'] = $this->student_model->get_question_by_testid($id);
$this->load->view('studentExam',$data);
}
why does this code not work?
//view
foreach ($tests as $test)
{
echo $test['name'];
echo '</br>';
echo $test['description'];
}
foreach ($questions as $question) {
echo $question['test'];
echo $question['question'];
echo '</br>';
}
You have to change this line:
$this->db->where('testid', $id);
To this:
$this->db->where('question.testid', $id);
Related
in view I can fetch all categories:
<?php foreach ($this->parent_categories as $category): ?>
<?php endforeach ?>
Now controller:
$data['parent_categories'] = $this->category_model->get_parent_categories_tree($category);
and finally model category:
//get parent categories tree
public function get_parent_categories_tree($category, $only_visible = true)
{
if (empty($category)) {
return array();
}
//get cached data
$key = "parent_categories_tree_all";
if ($only_visible == true) {
$key = "parent_categories_tree";
}
$key = $key . "_lang_" . $this->selected_lang->id;
$result_cache = get_cached_data($this, $key, "st");
if (!empty($result_cache)) {
if (!empty($result_cache[$category->id])) {
return $result_cache[$category->id];
}
} else {
$result_cache = array();
}
$parent_tree = $category->parent_tree;
$ids = array();
$str_sort = "";
if (!empty($parent_tree)) {
$array = explode(',', $parent_tree);
if (!empty($array)) {
foreach ($array as $item) {
array_push($ids, intval($item));
if ($str_sort == "") {
$str_sort = intval($item);
} else {
$str_sort .= "," . intval($item);
}
}
}
}
if (!in_array($category->id, $ids)) {
array_push($ids, $category->id);
if ($str_sort == "") {
$str_sort = $category->id;
} else {
$str_sort .= "," . $category->id;
}
}
$this->build_query($this->selected_lang->id, true);
$this->db->where_in('categories.id', $ids, FALSE);
if ($only_visible == true) {
$this->db->where('categories.visibility', 1);
}
$this->db->order_by('FIELD(id, ' . $str_sort . ')');
$result = $this->db->get('categories')->result();
$result_cache[$category->id] = $result;
set_cache_data($this, $key, $result_cache, "st");
return $result;
}
but the problem is currently I fetch with this code all categories without limit. How to get first 10 records?
I try:
$result = $this->db->get('categories', 10)->result();
But this not work and still fetch all categories. Can anyone help me resolve this issue? Maybe I do something wrong. Thanks for check.
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 5 years ago.
I have an array like below,
[{
"name":"Daniel",
"connection_status":"1"
},
{
"name":"Danny",
"connection_status":"3"
},
{
"name":"Moris",
"connection_status":"2"
},
{
"name":"Manny",
"connection_status":"1"
}]
I want to sort my array by status like 1,2,3 in this order.
This is my code,
public function getProfileDataForMySociety($user_id)
{
$this->db->select('*');
$this->db->from('profile');
$this->db->where('profile_id!=', $user_id);
$query = $this->db->get();
$list = $query->result();
$friends = $this->checkFriends($list, $user_id);
return $friends;
}
public function checkFriends($list, $user_id)
{
$array = [];
foreach ($list as $k => $v) {
// print_r(json_encode($list));
$friends = $this->checkStatus($v->profile_id);
//print_r($friends);
$relationship = '';
$relation_id = '';
foreach ($friends as $kk => $vv) {
if ($user_id == $vv->sent_id) {
if ($vv->status == 1) {
$relationship = 1;
}
if ($vv->status == 2) {
$relationship = 2;
}
} else if ($user_id == $vv->recieved_id) {
// pending
if ($vv->status == 1) {
$relationship = 3;
$relation_id = $vv->sent_id;
}
if ($vv->status == 2) {
$relationship = 4;
}
}
}
$list[$k]->connection_status = $relationship;
$list[$k]->relation_id = $relation_id;
}
return $list;
}
public function checkStatus($id)
{
$this->db->select('*');
$this->db->from('requests');
$this->db->where('sent_id', $id);
$this->db->or_where('recieved_id', $id);
$query = $this->db->get();
$list = $query->result();
return $list;
}
connection status is not a db field.
Where $list is my o/p array.Can anyone help me.Thanks in advance.
I want to sort my array based on my connection_status.
if i understand you correctly this may help you
public function getProfileDataForMySociety($user_id)
{
$this->db->select('*');
$this->db->from('profile');
$this->db->where('profile_id!=', $user_id);
$this->db->order_by('status', 'asc');
$query = $this->db->get();
$list = $query->result();
return $list;
}
You can apply order by in query
public function getProfileDataForMySociety($user_id)
{
$this->db->select('*');
$this->db->from('profile');
$this->db->where('profile_id!=', $user_id);
$this->db->order_by('status'); // USE ORDER BY
$query = $this->db->get();
$list = $query->result();
return $list;
}
public function getProfileDataForMySociety($user_id)
{
$this->db->select('*');
$this->db->from('profile');
$this->db->where('profile_id!=', $user_id);
$query = $this->db->order('status asc')->get();
$list = $query->result();
return $list;
}
Here's a new file:
//Products.php
<h1>Products</h1>
<?php
$res = IA::printAll();
if (!empty($res))
{
$i = 0;
foreach ($res as $item)
{
echo $item[$i];
$i++;
}
}
if (User::isLogged())
{
$ids = IA::getIDs();
echo "<form name='productpf' method='POST' action='index.php'>
<select name='id'>";
if (!empty($ids))
{
$a = 0;
foreach ($ids as $item)
{
echo "<option value = $item[$a]>$item[$a]</option>";
$a++;
}
}
echo"</select>
<input type='submit' name='addcart' value='Add To Cart' size='30'>
</form>";
}
?>
OK, so the problem now, is that a Fatal error: Call to a member function query() on null in C:\xampp\htdocs\InternetApplications\Classes\IA.php on line 41 is happening. So here are the two functions called on IA.php:
//IA.php
public function get()
{
$db = MySQL::getDB();
$sql = "SELECT * FROM ia";
$res = $db->query($sql); //------------->LINE 41<--------------
return $res;
}
public static function printAll()
{
$p = new IA();
$res = $p->get();
$nrows = $res->num_rows;
$array = array();
for($i = 0;$i < $nrows; $i++)
{
$pdb = $res->fetch_object();
$array[] = array($i => "<div style='border-style:groove' style='border:5px solid gray'>
<p>id: '$pdb->id' </p>
<p>name: '$pdb->name' </p>
<p>type: '$pdb->type' </p>
<p>description: '$pdb->description' </p>
<p>price: '$pdb->price'</p>
</div>");
}
$res->free();
return $array;
}
public static function getIDs()
{
$p = new IA();
$res = $p->get();
$nrows = $res->num_rows;
$array = array();
for($i = 0;$i < $nrows; $i++)
{
$pdb = $res->fetch_object();
$array[] = array($i => $pdb->id);
}
$res->free();
return $array;
}
Now the weird thing, as you can see, both functions above call get() method, and only the second one being called gives the error. In this case the second one being called is getIDs(), so that is the function that doesn't send any output. IF I switch the order and put getIDs() in first, it works but the second one, printAll() does not, therefore, no output. Here are the last two methods from the MySQL class, both of them called by function get():
//MySQL.php
public static function getDB()
{
if (self::$db == NULL)
{
return self::$db = new self();
}
}
public function query($sql)
{
$res = $this->conn->query($sql);
if (!$res)
{
echo $this->conn->error;
}
return($res);
}
there is a huge problem with your getDB method : it returns nothing if the DB exists !
it should be :
public static function getDB()
{
if (self::$db == NULL)
{
self::$db = new self();
}
return self::$db;
}
Hello guys i am trying to create a blog. The first home page is ok.. i am getting the shrot description and the categories from the database but...i have problems with the links:
this are my controller functions:
public function index()
{
$this->load->model('Model_cats');
$data['posts'] = $this->Model_cats->getLivePosts(10);
$data['cats'] = $this->Model_cats->getTopCategories();
$data['title'] = 'Welcome to Paul Harbuz Blog Spot!';
$data['main'] = 'public_home';
$this->load->vars($data);
$this->load->view('template', $data);
}
public function category($id)
{
$data['category'] = $this->Model_cats->getCategory($id);
$data['posts'] = $this->Model_cats->getAllPostsByCategory($id);
$data['cats'] = $this->Model_cats->getTopCategories();
$data['title'] = $data['category']['name'];
$data['main'] = 'public_home';
$this->load->vars($data);
$this->load->view('template', $data);
}
public function post($id)
{
$data['post'] = $this->Model_cats->getPost($id);
$data['comments'] = $this->Model_cats->getComments($id);
$data['cats'] = $this->Model_cats->getTopCategories();
$data['title'] = $data['post']['title'];
$data['main'] = 'public_post';
$this->load->vars($data);
$this->load->view('template');
}
this are my model function:
function getTopCategories()
{
$this->db->where('parentid',0);
$query = $this->db->get('categories');
$data = array();
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$data[$row['id']] = $row['name'];
}
}
$query->free_result();
return $data;
}
function getLivePosts($limit)
{
$data = array();
$this->db->limit($limit);
$this->db->where('status', 'published');
$this->db->order_by('pubdate', 'desc');
$query = $this->db->get('posts');
if($query->num_rows() > 0)
{
foreach($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
}
function getCategory($id)
{
$data = array();
$this->db->where('id',$id);
$this->db->limit(1);
$query = $this->db->get('categories');
if($query->num_rows() > 0)
{
$data = $query->row_array();
}
$query->free_result();
return $data;
}
function getAllPostsByCategory($catid)
{
$data = array();
$this->db->where('category_id', $catid);
$this->db->where('status', 'published');
$query = $this->db->get('posts');
if($query->num_rows() > 0)
{
foreach($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
function getPost($id)
{
$data = array();
$this->db->where('id',$id);
$this->db->limit(1);
$query = $this->db->get('posts');
if ($query->num_rows() > 0)
{
$data = $query->row_array();
}
$query->free_result();
return $data;
}
and in the view page i have something like this:
if ( count($posts) )
{
foreach ($posts as $key => $list)
{
echo '<h2>'.$list['title'].'</h2>';
echo auto_typography(word_limiter($list['body'], 200));
echo anchor('post/'.$list['id'],'read more >>');
}
echo '<br/><br/>';
}
I'm getting the post id in the url but.. i don't know why the page is not found.
You have to add the controller name to the anchor uri segments.
echo anchor('CONTROLLER/post/'.$list['id'],'read more >>');
More on this topic in the CodeIgniter URLs documentation.
If you want a URL like http://example.com/post/123 then you have to add the following to your application/config/routes.php file:
$route['post/(:num)'] = "CONTROLLER/post/$1";
More on routing is also available in the documentation.
I have the following model functions with PHP.
I know I am repeating myself.
Is there anyway I can simplify this code?
function getTopMenus(){
$data[0] = 'root';
$this->db->where('parentid',0);
$Q = $this->db->get('menus');
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data[$row['id']] = $row['name'];
}
}
$Q->free_result();
return $data;
}
function getheadMenus(){
$this->db->where('parentid',0);
$Q = $this->db->get('menus');
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
function getrootMenus(){
$this->db->where('parentid',0);
$Q = $this->db->get('menus');
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data[$row['id']] = $row['name'];
}
}
$Q->free_result();
return $data;
}
I can see one simplification you might try, using pass-by-reference to factor things out into a function:
function prepareMenu(&$data) {
$this->db->where('parentid',0);
$Q = $this->db->get('menus');
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data[$row['id']] = $row['name'];
}
}
$Q->free_result();
}
function getTopMenus() {
$data[0] = 'root';
prepareMenus($data);
return $data;
}
function getRootMenus() {
prepareMenus($data);
return $data;
}
There's also the possibility of using pass-by-reference and variable functions to factor out the part in the middle. May reduce duplication, but may or may not be considered 'simplifying'.
EDIT Here's what I mean. This code is untested.
function getMenus(&$data, $appendFunc) {
$this->db->where('parentid',0);
$Q = $this->db->get('menus');
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$appendFunc(&$data, $row);
}
}
$Q->free_result();
}
function appendTopMenu(&$data, $row) {
$data[$row['id']] = $row['name'];
}
function appendHeadMenu(&$data, $row) {
$data[] = $row;
}
function getTopMenus() {
$data[0] = 'root';
getMenus($data, "appendTopMenu");
return $data;
}
function getheadMenus() {
getMenus($data, "appendHeadMenu");
return $data;
}
function getrootMenus() {
getMenus($data, "appendTopMenu");
return $data;
}
Why not pass parameters into your function and place them in the 'where' and 'get' methods?
I don't know what your db and query classes look like, but i'd start improving these in the first place. Add "array fetch" and "hash fetch" functions to the query class:
class Query ...
function as_array() {
$data = array();
if($this->num_rows() > 0)
foreach ($this->result_array() as $row)
$data[] = $row;
$this->free_result();
return $data;
}
function as_hash($key = 'id') {
$data = array();
if($this->num_rows() > 0)
foreach ($this->result_array() as $row)
$data[$row[$key]] = $row;
$this->free_result();
return $data;
}
Make 'db->where()' return itself
class DB
function where(...) {
stuff
return $this;
Once you have this, your client functions become trivial:
function getTopMenus() {
$data = $this->db->where('parentid',0)->get('menus')->as_hash();
$data[0] = 'root';
return $data;
}
function getheadMenus() {
return $this->db->where('parentid',0)->get('menus')->as_array();
}
function getrootMenus() {
return $this->db->where('parentid',0)->get('menus')->as_hash();
}