i want to join the data of 2 tables in 1 table and display the data of 2 tables. I already have function that load the one table. can someone modify this function code to get the 2 tables ? im just a beginner in php.
Model_users.php
public function getUserGroup($userId = null)
{
if($userId) {
$sql = "SELECT * FROM user_group WHERE user_id = ?";
$query = $this->db->query($sql, array($userId));
$result = $query->row_array();
$group_id = $result['group_id'];
$g_sql = "SELECT * FROM groups WHERE id = ?";
$g_query = $this->db->query($g_sql, array($group_id));
$q_result = $g_query->row_array();
return $q_result;
}
}
Controller User.php
public function index()
{
if(!in_array('viewUser', $this->permission)) {
redirect('dashboard', 'refresh');
}
$user_data = $this->model_users->getUserData();
$result = array();
foreach ($user_data as $k => $v) {
$result[$k]['user_info'] = $v;
$group = $this->model_users->getUserGroup($v['id']);
$result[$k]['user_group'] = $group;
}
$this->data['user_data'] = $result;
$this->render_template('users/index', $this->data);
}
Here you go
$this->db->select("ug.*,g.*");
$this->db->from("user_group ug");
$this->db->join('groups g', 'ug.user_id = g.group_id');
$this->db->where("ug.user_id",1);
$query = $this->db->get();
if ($query->num_rows() > 0) {
$result = $query->result();
} else {
$result = null;
}
return $result;
I have a problem with the database.
I have 3 tables.
a) notebook
b) category_notes
c) domains
I have domain.id in the 'domains' table.
In the 'notes' table, I have domain_id and notes_category_id.
In the 'notes_category' table, I have id, notes_category_name
Script action: Saves notes for a given domain.
Everything works correctly - the data is saved and read.
Now I would like to add a note type, I have already done this functionality - but I'm showing the ID of the note type and not its name.
Of course, I have a relationship that's dear.
Controller domains.php
public function notes($id)
{
$this->load->model('admin/notes_model');
$result = $this->notes_model->get_by_domain_id($id);
echo '{"records":' . json_encode( $result ) . '}';
}
Model - domains_category_model.php
public function get($id = false)
{
if ( $id == false) {
$q = $this->db->get('notes_category');
$q = $q->result();
}
else{
$this->db->where('id', $id);
$q = $this->db->get('notes_category');
$q = $q->row();
}
return $q;
}
Controller - notes_category.php
public function get($id = false)
{
$result = $this->notes_category_model->get($id);
echo '{"records":' . json_encode( $result ) . '}';
}
Controller - notes.php
public function get($id = false)
{
$result = $this->notes_model->get($id);
echo '{"records":' . json_encode( $result ) . '}';
}
Model - Notes_model.php
public function get( $id = false)
{
if ( $id == false ) {
$q = $this->db->get('notes');
$q = $q->result();
}
else{
$this->db->where('id', $id);
$q = $this->db->get('notes');
$q = $q->row();
}
return $q;
}
public function get_by_domain_id($id)
{
$this->db->where('id_domain_rel', $id);
$q = $this->db->get('notes');
$q = $q->result();
return $q;
}
If you want to receive the name without conflict , use alias in select.
$this->db->select('n.* , n.id id_noted')->from('notes n')->get()->result();
$this->db->select('*, id id_noted')->from('notes')->get()->result();
I hope I understand what you want to do.
my problem is i whenever i input fname and mname i got an error from the database. i just want to search both of fname and mname in search field.
my search controller function
public function search()
{
$li = $this->session->userdata('logged_in');
$id = $this->session->userdata('idnumber');
if($li == TRUE)
{
$this->load->model('users_model');
$this->load->helper('smiley');
$this->load->library('table');
$image_array = get_clickable_smileys('http://localhost/efg/images/smileys', 'status');
$col_array = $this->table->make_columns($image_array, 8);
$image_array2 = get_clickable_smileys('http://localhost/efg/images/smileys', 'status');
$col_array2 = $this->table->make_columns($image_array2, 8);
$this->data['smiley_table1'] = $this->table->generate($col_array);
$this->data['smiley_tables'] = $this->table->generate($col_array2);
if($this->input->post())
{
$search = $this->input->post('search');
$memb = $this->users_model->search($search);
$usersearch = $this->users_model->search($search);
$grpsearch = $this->users_model->searchgrp($search);
redirect ('home/profile/'.$memb);
}
}
}
My users_model model
public function search($search)
{
$this->db->select('*');
$this->db->from('users');
$this->db->like('username',$search);
$this->db->or_like('fname',$search);
$this->db->or_like('lname',$search);
$this->db->or_like('mname',$search);
$query = $this->db->get();
foreach ($query->result_array() as $row)
{
$memb = $row['idnumber'];
}
$error = 'There is no record for the one you searched. Please go Back.';
$query1 = $this->db->query("SELECT * FROM users WHERE idnumber ='$memb'");
$hehe = $query1->result_array();
if($hehe==NULL)
{
echo $error; exit;
}
else
{
return $memb;
}
}
Always check the varibles using in condition are declared before the condition.
If it doesnt pass the condition what would be the output
public function search($search)
{
$this->db->select('*');
$this->db->from('users');
$this->db->like('username',$search);
$this->db->or_like('fname',$search);
$this->db->or_like('lname',$search);
$this->db->or_like('mname',$search);
$query = $this->db->get();
$memb = null;
foreach ($query->result_array() as $row)
{
$memb = $row['idnumber'];
}
$error = 'There is no record for the one you searched. Please go Back.';
$query1 = $this->db->query("SELECT * FROM users WHERE idnumber =$memb");
$hehe = $query1->result_array();
if($hehe==NULL)
{
return $error;
}
else
{
return $memb;
}
}
Make sure the variables that youll be using in queries are set. Trap before executing queries.
Also, use Query Builder (Active Record) when you can.
public function search($search)
{
$this->db->select('*');
$this->db->from('users');
$this->db->like('username',$search);
$this->db->or_like('fname',$search);
$this->db->or_like('lname',$search);
$this->db->or_like('mname',$search);
$query = $this->db->get();
$memb = '';
if ($query->result_array() > 0) {
foreach ($query->result_array() as $row) {
$memb = $row['idnumber'];
}
if ($memb) {
$this->db->select("*");
$this->db->where("idnumber", $memb);
$query1 = $this->db->get("users");
//$query1 = $this->db->query("SELECT * FROM users WHERE idnumber ='$memb'");
$hehe = $query1->result_array();
return ($hehe) ? $memb : false;
}
return false;
}
$error = 'There is no record for the one you searched. Please go Back.';
return $error;
}
OK, I am new to PHP so I plead you to have understanding.
I have made my new application, however I can't figure out why is it complaining about. I get very odd and strange error
db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
foreach ($row as $key => $val) {
$this->$key = $val;
}
return $row;
}
return null;
}
function getUser($info=NULL, $active=NULL) {
$sql = "SELECT * FROM tv_user WHERE 1 = 1 ";
if (!is_null($info)) {
if (empty($info->username) || empty($info->password)) {
return null;
}
if (!empty($info->username)) {
$sql .= " AND (username = '{$info->username}' OR email = '{$info->username}' OR id = '{$info->username}') ";
}
if (!empty($info->password)) {
$sql .= " AND password = '".md5($info->password)."' ";
}
}
if (!empty($active)) {
$sql .= " AND active = '{$active}' ";
}
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
foreach ($row as $key => $val) {
$this->$key = $val;
}
return $row;
}
return null;
}
function getUsers($active=NULL) {
$sql = "SELECT * FROM tv_user WHERE 1 = 1";
if (!empty($active)) {
$sql .= " AND active = '{$active}'";
}
$query = $this->db->query($sql);
return $query->result();
}
function isUserExist($username=NULL, $active=NULL) {
$sql = "SELECT * FROM tv_user WHERE 1 = 1 ";
if (!empty($username)) {
$sql .= " AND username = '{$username}' ";
}
if (!empty($active)) {
$sql .= " AND active = '{$active}' ";
}
$query = $this->db->query($sql);
return ($query->num_rows() > 0);
}
function getUserByEmail($email, $active=NULL) {
if (empty($email)) { return null; }
$sql = "SELECT * FROM tv_user WHERE email = '{$email}' ";
if (!empty($active)) {
$sql .= " AND active = '{$active}' ";
}
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
foreach ($row as $key => $val) {
$this->$key = $val;
}
return $row;
}
return null;
}
function getSocialUser($uid=NULL, $provider,$email=NULL, $active=NULL) {
if (empty($provider)) { return null; }
$sql = "SELECT * FROM tv_user WHERE 1 = 1 ";
if (!empty($uid)) { $sql .= " AND uid = '{$uid}' "; }
if (!empty($email)) { $sql .= " AND email = '{$email}' "; }
$sql .= " AND provider='{$provider}'";
if (!empty($active)) { $sql .= " AND active = '{$active}' "; }
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
foreach ($row as $key => $val) {
$this->$key = $val;
}
return $row;
}
return null;
}
function uuid() {
return strtoupper(substr(dechex(time()).dechex(mt_rand(1,65535)), 0, 6));
}
function add($data) {
return $this->db->insert("tv_user", $data);
}
function update($data, $where) {
return $this->db->update("tv_user", $data, $where);
}
function delete($where) {
return $this->db->delete("tv_user", $where);
}
function login($username, $password) {
if (empty($username) || empty($password)) {
return FALSE;
}
$password = md5($password);
$sql = "SELECT * FROM tv_user WHERE username='{$username}' AND password='{$password}' AND active=1";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$user = $query->row();
$user_data = array();
foreach($user as $key => $val){
$user_data[$key] = $val;
}
$user_data["login"] = TRUE;
$this->session->set_userdata('user', $user_data);
if ($user->user_type == USR_ROOT) {
$this->session->set_userdata('root_addmin_logged_in', TRUE);
$this->session->set_userdata('addmin_logged_in', TRUE);
} else
if (in_array($user->user_type,array(USR_ADMIN,
USR_MODERATOR,
USR_TOUR,
USR_HOTEL,
USR_FLIGHT,
USR_VISA)
))
{
$this->session->set_userdata('root_addmin_logged_in', FALSE);
$this->session->set_userdata('addmin_logged_in', TRUE);
} else {
$this->session->set_userdata('root_addmin_logged_in', FALSE);
$this->session->set_userdata('addmin_logged_in', FALSE);
}
$this->session->set_userdata('logged_in', TRUE);
$this->session->set_userdata('logged_user', $user);
return TRUE;
}
return FALSE;
}
function logout() { $this->session->sess_destroy(); }
function verify_reset_password_code($email, $code) {
if (empty($email) || empty($code)) {
return FALSE;
}
$sql = "SELECT * FROM tv_user WHERE email = '{$email}' ";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
return ($code == md5(SITE_NAME.$row->fullname))? TRUE : FALSE;
} else {
return FALSE;
}
}
}
?>
which is interpreted as
PHP Fatal error: Class 'M_user' not found in C:\wamp\www\system\core\Loader.php on line 303
I am not very good in this since I am new. I have to say that I am running my application under WAMP.It may be something trivial, but I don't get it.
I have even included all modules in my autoload.php file as following:
$autoload['model'] = array(
'm_user',
'm_user_online',
'm_content_category',
'm_content',
'm_nation',
'm_mail',
'm_message',
'm_feedback',
'm_photo',
'm_tour',
'm_tour_rate',
'm_tour_departure',
'm_tour_departure_rate',
'm_tour_itinerary',
'm_tour_category',
'm_tour_activity',
'm_tour_destination',
'm_tour_tripnote',
'm_tour_request',
'm_visa',
'm_visa_tips',
'm_question',
'm_hotel',
'm_room',
'm_room_rate',
'm_album_category',
'm_album',
'm_tour_option_category'
);
Part of the code where it gets mentioned:
// --------------------------------------------------------------------
/**
* Model Loader
*
* This function lets users load and instantiate models.
*
* #param string the name of the class
* #param string name for the model
* #param bool database connection
* #return void
*/
public function model($model, $name = '', $db_conn = FALSE)
{
if (is_array($model))
{
foreach ($model as $babe)
{
$this->model($babe);
}
return;
}
if ($model == '')
{
return;
}
$path = '';
// Is the model in a sub-folder? If so, parse out the filename and path.
if (($last_slash = strrpos($model, '/')) !== FALSE)
{
// The path is in front of the last slash
$path = substr($model, 0, $last_slash + 1);
// And the model name behind it
$model = substr($model, $last_slash + 1);
}
if ($name == '')
{
$name = $model;
}
if (in_array($name, $this->_ci_models, TRUE))
{
return;
}
$CI =& get_instance();
if (isset($CI->$name))
{
show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
}
$model = strtolower($model);
foreach ($this->_ci_model_paths as $mod_path)
{
if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
{
continue;
}
if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
{
if ($db_conn === TRUE)
{
$db_conn = '';
}
$CI->load->database($db_conn, FALSE, TRUE);
}
if ( ! class_exists('CI_Model'))
{
load_class('Model', 'core');
}
require_once($mod_path.'models/'.$path.$model.'.php');
$model = ucfirst($model);
$CI->$name = new $model();
$this->_ci_models[] = $name;
return;
}
// couldn't find the model
show_error('Unable to locate the model you have specified: '.$model);
}
This is my m_user.php class:
<?
class M_User extends CI_Model
{
function load($id)
{
$sql = "SELECT * FROM tv_user WHERE id = '{$id}' ";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
foreach ($row as $key => $val) {
$this->$key = $val;
}
return $row;
}
return null;
}
function getUser($info=NULL, $active=NULL)
{
$sql = "SELECT * FROM tv_user WHERE 1 = 1 ";
if (!is_null($info)) {
if (empty($info->username) || empty($info->password)) {
return null;
}
if (!empty($info->username)) {
$sql .= " AND (username = '{$info->username}' OR email = '{$info->username}' OR id = '{$info->username}') ";
}
if (!empty($info->password)) {
$sql .= " AND password = '".md5($info->password)."' ";
}
}
if (!empty($active)) {
$sql .= " AND active = '{$active}' ";
}
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
foreach ($row as $key => $val) {
$this->$key = $val;
}
return $row;
}
return null;
}
function getUsers($active=NULL)
{
$sql = "SELECT * FROM tv_user WHERE 1 = 1";
if (!empty($active)) {
$sql .= " AND active = '{$active}'";
}
$query = $this->db->query($sql);
return $query->result();
}
function isUserExist($username=NULL, $active=NULL)
{
$sql = "SELECT * FROM tv_user WHERE 1 = 1 ";
if (!empty($username)) {
$sql .= " AND username = '{$username}' ";
}
if (!empty($active)) {
$sql .= " AND active = '{$active}' ";
}
$query = $this->db->query($sql);
return ($query->num_rows() > 0);
}
function getUserByEmail($email, $active=NULL)
{
if (empty($email)) {
return null;
}
$sql = "SELECT * FROM tv_user WHERE email = '{$email}' ";
if (!empty($active)) {
$sql .= " AND active = '{$active}' ";
}
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
foreach ($row as $key => $val) {
$this->$key = $val;
}
return $row;
}
return null;
}
function getSocialUser($uid=NULL, $provider,$email=NULL, $active=NULL)
{
if (empty($provider)) {
return null;
}
$sql = "SELECT * FROM tv_user WHERE 1 = 1 ";
if (!empty($uid)) {
$sql .= " AND uid = '{$uid}' ";
}
if (!empty($email)) {
$sql .= " AND email = '{$email}' ";
}
$sql .= " AND provider='{$provider}'";
if (!empty($active)) {
$sql .= " AND active = '{$active}' ";
}
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
foreach ($row as $key => $val) {
$this->$key = $val;
}
return $row;
}
return null;
}
function uuid()
{
return strtoupper(substr(dechex(time()).dechex(mt_rand(1,65535)), 0, 6));
}
function add($data)
{
return $this->db->insert("tv_user", $data);
}
function update($data, $where)
{
return $this->db->update("tv_user", $data, $where);
}
function delete($where)
{
return $this->db->delete("tv_user", $where);
}
function login($username, $password)
{
if (empty($username) || empty($password)) {
return FALSE;
}
$password = md5($password);
$sql = "SELECT * FROM tv_user WHERE username='{$username}' AND password='{$password}' AND active=1";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$user = $query->row();
$user_data = array();
foreach($user as $key => $val){
$user_data[$key] = $val;
}
$user_data["login"] = TRUE;
$this->session->set_userdata('user', $user_data);
if ($user->user_type == USR_ROOT) {
$this->session->set_userdata('root_addmin_logged_in', TRUE);
$this->session->set_userdata('addmin_logged_in', TRUE);
}
else if (in_array($user->user_type, array(USR_ADMIN,USR_MODERATOR,USR_TOUR,USR_HOTEL,USR_FLIGHT,USR_VISA))) {
$this->session->set_userdata('root_addmin_logged_in', FALSE);
$this->session->set_userdata('addmin_logged_in', TRUE);
}
else {
$this->session->set_userdata('root_addmin_logged_in', FALSE);
$this->session->set_userdata('addmin_logged_in', FALSE);
}
$this->session->set_userdata('logged_in', TRUE);
$this->session->set_userdata('logged_user', $user);
return TRUE;
}
return FALSE;
}
function logout()
{
$this->session->sess_destroy();
}
function verify_reset_password_code($email, $code)
{
if (empty($email) || empty($code)) {
return FALSE;
}
$sql = "SELECT * FROM tv_user WHERE email = '{$email}' ";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
return ($code == md5(SITE_NAME.$row->fullname))? TRUE : FALSE;
} else {
return FALSE;
}
}
}
?>
Set
short_open_tag=On
in php.ini
And restart your Apache server.
As per the comments:
Your first chunk of PHP is appearing in your HTML view - good idea to include a screenshot! That would indicate that it is not being parsed as PHP, and instead is being parse as HTML, so I would check to see if a <?php appears at the start.
I am passing a number of values to a function and then want to create a SQL query to search for these values in a database.
The input for this is drop down boxes which means that the input could be ALL or * which I want to create as a wildcard.
The problem is that you cannot do:
$result = mysql_query("SELECT * FROM table WHERE something1='$something1' AND something2='*'") or die(mysql_error());
I have made a start but cannot figure out the logic loop to make it work. This is what I have so far:
public function search($something1, $something2, $something3, $something4, $something5) {
//create query
$query = "SELECT * FROM users";
if ($something1== null and $something2== null and $something3== null and $something4== null and $something5== null) {
//search all users
break
} else {
//append where
$query = $query . " WHERE ";
if ($something1!= null) {
$query = $query . "something1='$something1'"
}
if ($something2!= null) {
$query = $query . "something2='$something2'"
}
if ($something3!= null) {
$query = $query . "something3='$something3'"
}
if ($something4!= null) {
$query = $query . "something4='$something4'"
}
if ($something5!= null) {
$query = $query . "something5='$something5'"
}
$uuid = uniqid('', true);
$result = mysql_query($query) or die(mysql_error());
}
The problem with this is that it only works in sequence. If someone enters for example something3 first then it wont add the AND in the correct place.
Any help greatly appreciated.
I would do something like this
criteria = null
if ($something1!= null) {
if($criteria != null)
{
$criteria = $criteria . " AND something1='$something1'"
}
else
{
$criteria = $criteria . " something1='$something1'"
}
}
... other criteria
$query = $query . $criteria
try with array.
function search($somethings){
$query = "SELECT * FROM users";
$filters = '';
if(is_array($somethings)){
$i = 0;
foreach($somethings as $key => $value){
$filters .= ($i > 0) ? " AND $key = '$value' " : " $key = '$value'";
$i++;
}
}
$uuid = uniqid('', true);
$query .= $filters;
$result = mysql_query($query) or die(mysql_error());
}
// demo
$som = array(
"something1" => "value1",
"something2" => "value2"
);
search( $som );
Here's an example of dynamically building a WHERE clause. I'm also showing using PDO and query parameters. You should stop using the deprecated mysql API and start using PDO.
public function search($something1, $something2, $something3, $something4, $something5)
{
$terms = array();
$values = array();
if (isset($something1)) {
$terms[] = "something1 = ?";
$values[] = $something1;
}
if (isset($something2)) {
$terms[] = "something2 = ?";
$values[] = $something2;
}
if (isset($something3)) {
$terms[] = "something3 = ?";
$values[] = $something3;
}
if (isset($something4)) {
$terms[] = "something4 = ?";
$values[] = $something4;
}
if (isset($something5)) {
$terms[] = "something5 = ?";
$values[] = $something5;
}
$query = "SELECT * FROM users ";
if ($terms) {
$query .= " WHERE " . join(" AND ", $terms);
}
if (defined('DEBUG') && DEBUG==1) {
print $query . "\n";
print_r($values);
exit();
}
$stmt = $pdo->prepare($query);
if ($stmt === false) { die(print_r($pdo->errorInfo(), true)); }
$status = $stmt->execute($values);
if ($status === false) { die(print_r($stmt->errorInfo(), true)); }
}
I've tested the above and it works. If I pass any non-null value for any of the five function arguments, it creates a WHERE clause for only the terms that are non-null.
Test with:
define('DEBUG', 1);
search('one', 'two', null, null, 'five');
Output of this test is:
SELECT * FROM users WHERE something1 = ? AND something2 = ? AND something5 = ?
Array
(
[0] => one
[1] => two
[2] => five
)
If you need this to be more dynamic, pass an array to the function instead of individual arguments.