I want to do inner join between estate table and estatetype table. I make a edit page but I want to show customer information by cusId. I can draw data from estate table. But estateType in estate table. I hold estateType names in estatetype table. When I call estateType from estate table, just showing number(1,2,3) But I want to show their names from estatetype table. How I can do it?
I want to show customer estate information by Id from estatetype table. But I cannot do this.
Controller:
$viewData = new stdClass();
$this->load->model('join_model');
$viewData->estateList = $this->join_model->estatetypes();
$viewData->customers = $this->db->where("cusId", $cusId)->get("customer")->row();
$viewData->property = $this->db->where("cusId", $cusId)->get("estate")->row();
$viewData->estype = $this->db->get("estatetype")->result();
$viewData->heating = $this->db->get("heating")->result();
$viewData->cities = $this->db->get("city")->result();
$this->load->view('property_edit',$viewData);
Model:
<?php
class Join_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function estatetypes()
{
$this->db->select('estate.CusId,estate.estateType,estatetype.estateTypeId,estatetype.estateTypeAr,estatetype.estateTypeEng');
$this->db->from('estate');
$this->db->join('estatetype', 'estate.estateType = estatetype.estateTypeEng');
$results = $this->db->get()->row();
return $results;
//return $this->db->get_where('users', array('userId' => $id), 1);
}
}
I want to show when I add estateType from estate table in view, show estataTypeEn from estateType. For this, I did the inner join but when I add this to view and controller. There is nothing. How do I do this?
View:
<!-- Basic select -->
<div class="form-group">
<label class="control-label col-lg-3">Estate Type <span class="text-danger">*</span></label>
<div class="col-lg-9">
<select name="estateType" class="form-control">
<option value="<?php echo $property->estateType; ?>" readonly><?php echo $estateList->estateType; ?></option>
<?php
foreach($estype as $etype){ ?>
<option value="<?php echo $etype->estateTypeId; ?>"><?php echo $etype->estateTypeEng; ?></option>
<?php }?>
</select>
</div>
</div>
estatetype table (types names are here)
CREATE TABLE `estatetype` (
`estateTypeId` int(11) NOT NULL AUTO_INCREMENT,
`estateTypeNameEn` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`estateTypeNameAr` varchar(255) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`payTypeId`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
estate table
CREATE TABLE `estate` (
`estateId` int(11) NOT NULL AUTO_INCREMENT,
`CusId` int(11) DEFAULT NULL,
`estateType` int(11) DEFAULT NULL COMMENT '',
`estateCentare` varchar(6) COLLATE utf8_bin DEFAULT NULL,
`estateRoom` varchar(2) COLLATE utf8_bin DEFAULT NULL COMMENT '',
`estateSalon` varchar(2) COLLATE utf8_bin DEFAULT NULL COMMENT '',
`estateBathroom` varchar(2) COLLATE utf8_bin DEFAULT NULL COMMENT '',
`estateHeating` int(11) DEFAULT NULL COMMENT '',
`estateCity` int(11) DEFAULT NULL COMMENT '',
`estateAddress` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`estateCoord` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`estateGarden` tinyint(1) DEFAULT NULL COMMENT '',
`estateBalcony` tinyint(1) DEFAULT NULL COMMENT '',
`estatePackage` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`estatePackageDate` datetime DEFAULT NULL,
`estatePackageUser` int(11) DEFAULT NULL,
`estateCreateDate` datetime DEFAULT NULL,
`estateCreateUser` int(11) DEFAULT NULL,
`estateEditDate` datetime DEFAULT NULL,
`estateEditUser` int(11) DEFAULT NULL,
`estateDue` decimal(5,0) DEFAULT NULL,
`estateImg` varchar(255) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`estateId`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
You don't need to use Join you can create a custom_helper.php
A CodeIgniter helper is a PHP file with multiple functions. It is not a class
Create a file and put the following code into it.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('get_name'))
{
function get_name($id,$value)
{
$CI=& get_instance();
$number_data = $CI->db->get_where('estatetype', array('estateTypeId' => $id))->num_rows();
if($number_data > 0){
$data = $CI->db->get_where('estatetype', array('estateTypeId' => $id))->row()->$value;
}
else{
$data = translate('Not_Found');
}
return $data;
}
}
Save this to application/helpers/ .
Using The Helper
Load this in your controller
$this->load->helper('custom_helper');
If you use this helper in a lot of locations you can have it load automatically by adding it to the autoload configuration file i.e. <your-web-app>\application\config\autoload.php.
$autoload['helper'] = array('custom_helper');
And in your view, you can call this function like this.
<select name="estateType" class="form-control">
<?php
$estype = $this->db->get("estate")->result_array();
foreach($estype as $row){ ?>
<option value="<?php echo $row['estateType']; ?>"><?php echo get_name($row['estateType'],'estateTypeNameEn'); ?></option>
<?php }?>
</select>
if you want to get other table or other columns from a table you can change the function like this.
function get_name($table,$field,$equal,$value)
{
$CI=& get_instance();
$number_data = $CI->db->get_where($table, array($field => $equal))->num_rows();
if($number_data > 0){
$data = $CI->db->get_where($table, array($field => $equal))->row()->$value;
}
else{
$data = translate('Not_Found');
}
return $data;
}
and in view you can add this.
<select name="estateType" class="form-control">
<?php
$estype = $this->db->get("estate")->result_array();
foreach($estype as $row){ ?>
<option value="<?php echo $row['estateType']; ?>"><?php echo get_name('estatetype','estateTypeId',$row['estateType'],'estateTypeNameEn'); ?></option>
<?php }?>
</select>
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 days ago.
Improve this question
I have been working on a site however i can't seem to find the answer to this question. I have a drop down box which is pulling data from table info_weapons_manafacture. Here is the table layout.
--
-- Table structure for table `info_weapons_manafacture`
--
DROP TABLE IF EXISTS info_weapons_manafacture;
CREATE TABLE `info_weapons_manafacture` (
`id` int(11) NOT NULL,
`info_weapons_manafacture_name` varchar(255) NOT NULL,
`info_weapons_manafacture_abbreviation` varchar(255) NOT NULL,
`info_weapons_manafacture_description` varchar(255) NOT NULL,
`info_weapons_manafacture_founded` varchar(255) NOT NULL,
`info_weapons_manafacture_headquarters` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Here is the Table for Manafactures;
--
-- Table structure for table `info_weapons_manafacture`
--
DROP TABLE IF EXISTS info_weapons_manafacture;
CREATE TABLE `info_weapons_manafacture` (
`id` int(11) NOT NULL,
`info_weapons_manafacture_name` varchar(255) NOT NULL,
`info_weapons_manafacture_abbreviation` varchar(255) NOT NULL,
`info_weapons_manafacture_description` varchar(255) NOT NULL,
`info_weapons_manafacture_founded` varchar(255) NOT NULL,
`info_weapons_manafacture_headquarters` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Now the drop down box is populating with the values, but in order to make the drop down box show the value from the database we post this code.
<label for="Characterimage" class="col-lg-2 col-form-label tits">Manafacture:</label>
<select class="form-control form-control-sm col-lg-10" id="js-choose" name="info_weapon_manafactureID" style="height: 40px">
<?php $manafacture_id = $r['info_weapon_manafactureID'];
$q2 = $db->query("SELECT * FROM info_weapons_manafacture WHERE id = '$manafacture_id'");
($r2 = $q2->fetch(PDO::FETCH_ASSOC));
if($r2['image']){?>
<option value="<?= $r2['id']; ?>" data-img="uploads/<?= $r2['image']; ?>"><?= $r2['info_weapons_manafacture_name']; ?></option>
<?php }else{ ?>
<option value="<?= $r2['id']; ?>" data-img="<?= ASSETS ?>img/no-image.png"><?= $r2['info_weapons_manafacture_name']; ?></option>
<?php } } ?>
<?php
$q5 = $db->query("SELECT * FROM info_weapons_manafacture");
while ($r5 = $q5->fetch(PDO::FETCH_ASSOC)) {
if($r5['image']){?>
<option value="<?= $r5['id']; ?>" data-img="uploads/<?= $r2['image']; ?>"><?= $r5['info_weapons_manafacture_name']; ?></option>
<?php }else{ ?>
<option value="<?= $r5['id']; ?>" data-img="<?= ASSETS ?>img/no-image.png"><?= $r5['info_weapons_manafacture_name']; ?></option>
<?php } ?>
<script>
jQuery.fn.uniqueAttrs = function(attr) {
if(!attr) return this;
var that = this;
return this.filter(function (index, node) {
return that.index(that.filter(function() {
return this[attr] === node[attr];
})) === index;
});
};
var $ul = $('#js-choose');
$ul.html($ul.find('option').uniqueAttrs(<?= $r5['info_weapons_manafacture_name']; ?>));
</script>
<?php } ?>
</select>
However this is making it so that the value in the drop down box is repeating the one that is in the database, i was wondering if there is a way to highlight the value that is in the record while also not having a duplicate value in the list...
Thanks in Advance.. :)
I have a table named settings with fields like id,tax1,tax2 etc., and i created a view named service.view i need to get the tax1 value from settings table and display a label in service.view file. how could i do this? can some one help me code...
this is my table structure
CREATE TABLE IF NOT EXISTS `service_settings` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`company_name` varchar(30) NOT NULL,
`company_logo` varchar(50) NOT NULL,
`company_tax11` varchar(20) NOT NULL,
`company_tax12` varchar(20) NOT NULL,
`company_tax13` double NOT NULL,
`company_tax14` double NOT NULL,
`company_tax21` varchar(20) NOT NULL,
`company_tax22` varchar(20) NOT NULL,
`company_tax23` double NOT NULL,
`company_tax24` double NOT NULL,
`company_tax31` varchar(20) NOT NULL,
`company_tax32` varchar(20) NOT NULL,
`company_tax33` double NOT NULL,
`company_tax34` double NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
I am using codeigniter and mysql database
I need to get a single record from this table and display it in a service.view
Alter this code as you want. This display your data in database as well
in model
<?php
public function get_service()
{
$query= $this->db->query("SELECT * FROM user service_settings");
$result= $query->result_array();
return $result;
}
?>
in controller
<?php
public function login()
{
$data['table'] = $this->Model_Name->get_service();
$this->load->view('service');//load your relevant view
}
?>
in view
<table>
<tr>
<th>company_name</th>
<th>company_tax11</th>
<th>company_tax12</th>
<th>company_tax13</th>
<th>company_tax14</th>
</tr>
<?php
foreach ($table as $new_tbale)
{
?>
<tr>
<td>$new_tbale['company_name']</td>
<td>$new_tbale['company_tax11']</td>
<td>$new_tbale['company_tax12']</td>
<td>$new_tbale['company_tax13']</td>
<td>$new_tbale['company_tax14']</td>
</tr>
<?php
}
?>
</table>
Edit 01
<?php
public function get_service()
{
$query= $this->db->query("SELECT company_tax11 FROM user service_settings");
$result= $query->result_array();
return $result;
}
?>
I am looknig to seek a way to get the tick boxes checked if they are assigned to the category in the database.
<?php
try{
// Selecting entire row from cat_list table
$results = $dbh->query("SELECT cat_id, cat_title FROM cat_list");
}catch(Exception $e) {
echo $e->getMessage();
die();
}
$category = $results->fetchAll(PDO::FETCH_ASSOC);
?>
<br>
<label><input type="checkbox" name="" class="selectall"/> Select all</label>
<div id="checkboxlist" >
<?php
foreach($category as $cat){
?>
<input type="checkbox" value="<?php echo $cat["cat_id"]; ?>" <?php echo ($cat['cat_id'] == 1) ? 'checked="checked"' : ''; ?> name="cat_no[]" id="box1"> <?php echo $cat["cat_title"]; ?></a><br>
<?php
}
So when I create the post I select from the available categories which are displayed as an array, the code above is taken from my edit post form so I want it retrieve the categories I assigned to it and tick the boxes.
I have 3 tables:
doc_list (Stores documents)
cat_list (Stores Categories)
cat_doc_link_table (stores the doc_id & cat_id from the previous two tables)
Here are how they are formed:
CREATE TABLE `cat_doc_link_table` (
`id` int(11) NOT NULL,
`link_cat_id` int(11) NOT NULL,
`link_doc_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE `cat_list` (
`cat_id` int(11) NOT NULL,
`cat_title` varchar(32) NOT NULL,
`cat_color` varchar(20) NOT NULL,
`cat_icon` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16 AUTO_INCREMENT=66 ;
CREATE TABLE `doc_list` (
`doc_id` int(11) NOT NULL,
`doc_title` varchar(50) NOT NULL,
`doc_content` text NOT NULL,
`doc_created` datetime NOT NULL,
`user_id` int(11) NOT NULL,
`doc_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf16 AUTO_INCREMENT=295 ;
put the selected cat_id's in an array(). then use in_array() to check.
// query your db to return an array of cat_id's for the specified post.
$cats_array = array('123', '124', '156');
foreach($category as $cat){
// compare
if(in_array($cat['cat_id'], $cats_array)) {
// cat checked
}else{
// not checked
}
}
I have a lead table that contains data which also contains information which user has entered this data. Now I want to create a filter that when selecting a particular user only his data is displayed. How to do this in cakephp. Please help.
Thanks in advance
User database
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`fname` varchar(100) DEFAULT NULL,
`lname` varchar(100) DEFAULT NULL,
`username` varchar(100) NOT NULL,
`password` varchar(50) NOT NULL,
`email` varchar(100) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`role` varchar(100) NOT NULL,
`created` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
lead table
CREATE TABLE IF NOT EXISTS `leads` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`account_id` int(11) NOT NULL,
`name` varchar(200) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`board_number` varchar(11) DEFAULT NULL COMMENT 'board number',
`mobile_number` varchar(11) DEFAULT NULL COMMENT 'mobile number',
`requirements` varchar(1000) DEFAULT NULL,
`total_price_quoted` int(11) DEFAULT NULL COMMENT 'total price quoted',
`our_price` int(11) DEFAULT NULL COMMENT 'our price',
`margin` int(11) DEFAULT NULL,
`closing_month` varchar(100) DEFAULT NULL COMMENT 'closing month',
`probablity` varchar(100) DEFAULT NULL,
`status_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`date_added` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=24 ;
Controller Code LeadController.php
public function user_filter() {
//$this->render('filter');
$this->loadModel('User');
$this->User->recursive = 0;
$userFilter = $this->User->find('all',array(
'fields' => array('User.username')
));
$this->set('userFilter', $userFilter);
return $userFilter;
}
// View/filterMenu.ctp
//here a dropdown menu of listed user
<?php
$userFilters = $this->requestAction(
array(
'controller' => 'leads',
'action' => 'user_filter'
)
);
?>
<ul class="dropdown-menu">
<?php foreach ($userFilters as $userFilter): ?>
<li>
<?php
echo $this->Html->link($userFilter['User']['username'],array(
'controller'=>'leads',
'action'=>'user_filter'
));
echo "<\n>";
?>
</li>
<?php endforeach; ?>
</ul
I want to create view of that particular user.
you cannot provide links through dropdown, you must use javascript to direct to page.
to display all the leads from particular user, pass the user id to the function and fetch data using user id.
public function user_filter($id = null) {
if ($id){
$leads = $this->Lead->find('all', array('conditions' => array('Lead.user_id' => $id)));
}else{
$leads = $this->Lead->find('all');
}
$this->loadModel('User');
$this->User->recursive = 0;
$userFilter = $this->User->find('all',array(
'fields' => array('User.username')
));
$this->set('leads', $leads);
$this->set('userFilter', $userFilter);
return $userFilter;
}
Hope this will help you.
I would like to create a list from my database via PHP (I use CodeIgniter).
Here's my database :
CREATE DATABASE IF NOT EXISTS `flux_demo` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
USE `flux_demo`;
CREATE TABLE IF NOT EXISTS `people` (
`people_id` int(11) NOT NULL AUTO_INCREMENT,
`Active` int(1) DEFAULT NULL,
`Login` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`Password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`Mail` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`Name` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`Bio` text COLLATE utf8_unicode_ci NOT NULL,
`Register Date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Last Activity` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`Thumbnail` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`people_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `projects` (
`project_id` int(11) NOT NULL AUTO_INCREMENT,
`Project` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`Description` text COLLATE utf8_unicode_ci NOT NULL,
`Status` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`Thumbnail` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`Start Date` date NOT NULL,
`End Date` date NOT NULL,
PRIMARY KEY (`project_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `assigned_projects_ppeople` (
`people_id` int(11) NOT NULL,
`project_id` int(11) NOT NULL,
PRIMARY KEY (`people_id`,`project_id`),
FOREIGN KEY (people_id) REFERENCES people (people_id),
FOREIGN KEY (project_id) REFERENCES projects (project_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
As you can see, I use a many-to-many table to make the relationship between projects and people.
In CodeIgniter, I use the session class to store session data ('id', 'Name', 'Login', 'Thumbnail').
My view page is a simple list using ul and li tags.
<?php
$this->load->model('project_model');
$this->project_model->getProjectsForUser();
echo "<ul>";
foreach($result as $r)
{
echo "<li><a href=http://www.domain.com/$r->Project>$r->Project</a></li>";
}
echo "</ul>";
?>
My question is how can I create the list matching with the session user ?
My uri segments will be : http://domain.com/project_name
REEDIT : I created the model :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Project_model extends CI_Model{
public function getProjectsForUser()
{
$query = $this->db->query(
'SELECT r.project_id, r.Project
FROM assigned_projects_ppeople a JOIN people p
ON a.people_id = p.people_id JOIN projects r
ON a.project_id = r.project_id
GROUP BY r.project_id, r.Project'
);
$data = $query->row_array();
return $data;
}
}
?>
Any idea ?
Thank you !
Create a function in model so that returns the al the records of projects from database
$response=$this->yourmodel->model_function();
Then try to loop the resultant data in the ul as
echo "ul";
foreach($response as $projectData)
{
echo "<li><a href=http://www.domain.com/$projectData->Project>$projectData->Project</a></li>";
}
echo "</ul>";
I finally succeed. Once finished, we understand it was not difficult.
Here is my function to query my database :
public function getProjectsForUser()
{
$login_id = $this->session->userdata('User_id');
$this->db->select('Project');
$this->db->from('assigned_projects_ppeople a');
$this->db->where('people_id', $login_id);
$this->db->join('projects r', 'r.project_id = a.project_id');
$query = $this->db->get();
return $query;
}
Then, I definited the variable in my controller page :
$this->load->model('project_model');
$query = $this->project_model->getProjectsForUser();
$args['available_projects'] = $query->result();
$this->load->view('home', $args);
And the final code to insert in my view page was :
<?php
echo "<ul>";
foreach($available_projects as $r)
{
echo '<li>'.$r->Project.'</li>';
}
echo "</ul>";
?>
I have to define how I can use a URI like that :
http://localhost/project/project_id
I think is like blog method, so I'm gonna study it.
Thank you for your help !