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
}
}
Related
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>
I have only one table.all category names under one field and parent category field is there. My table structure is as follows
CREATE TABLE IF NOT EXISTS `jil_category` (
`ctg_id` int(11) NOT NULL AUTO_INCREMENT,
`ctg_name` varchar(255) NOT NULL,
`ctg_section` int(11) NOT NULL,
`ctg_details` text NOT NULL,
`ctg_status` int(11) NOT NULL,
`ctg_index` int(11) NOT NULL,
`ctg_parent` int(11) NOT NULL,
`ctg_image` varchar(300) NOT NULL,
`ctg_dated` int(11) NOT NULL,
`ctg_ipadd` varchar(30) NOT NULL,
`ctg_ldated` int(11) NOT NULL,
`ctg_lipadd` varchar(30) NOT NULL,
PRIMARY KEY (`ctg_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=196 ;
If anybody knows the solution,then please help
I am giving you an example
function menu()
{
$menus = $this->db->get_where('jil_category', array('ctg_parent'=>0))->result();
$data = [];
foreach($menus as $menu)
{
$submenu = $this->db->get_where('jil_category',array('ctg_parent'=>$menu->ctg_id));
if($submenu->num_rows()>0)
$menu->submenu = $submenu->result();
else
$menu->submenu = [];
$data[] = $menu;
}
$menudata['menus'] =$data;
$this->load->view(index,$menudata);
}
On your View
foreach($menus as $menu)
{ ?>
<li><?php echo $menu->ctg_name;
if(!empty($menu->submenu)){ echo '<ul>';
foreach($menu->submenu as $submenu){ ?>
<li><?=$submenu->ctg_name?></li>
<?php } echo '</ul>'; }?>
</li>
<?php } ?>
You can do that. First of all select distinct Parent Categories from Table.
Using foreach you can display all Parent Categories.
Inside foreach loop, make a function in helper/controller, pass the parent_cat_id to that function and now get all the sub cats for that specific category. just like: getSubCategories($parent_cat_id);
if there's some sub categories in the table, the function returns you the data else it returns you nothing.
Let me know if you need anything else and if thats the solution, +1 the answer.
I am trying to add comments on a joke thread, but I am having trouble grabbing the joke_id which should be used to amend the comments on the post.
The problem i face is, the thread with no comments on will not add the joke_id to the database, and the thread with comments on works fine. I have no idea why.
This is a working example of what I am talking about:
http://ehustudent.co.uk/cis21732825/cis3122/jokehut_4/read/joke/14
This thread is fully working (i just emphasised the joke_id for test purposes)
and this thread does not work:
http://ehustudent.co.uk/cis21732825/cis3122/jokehut_4/read/joke/4
The comment and name is added to the database, but the joke_id is down as '0' and i have no idea why.
Here is my code in the view:
<?php
//assigning values
foreach($results as $row){
$name = $row->name;
$joke = $row->joke;
$joke_id = $row->joke_id;
$date_added = $row->date_added;
$vote = $row->vote;
?>
}
echo form_open('comments/insertComment');
?>
<div class="new-com-bt">
<span>Write a comment ...</span>
</div>
<div class="new-com-cnt">
<input type="text" id="name-com" name="name-com" value="" placeholder="Name is optional" />
<textarea required="yes" class="the-new-com" id="the-new-com" name="the-new-com" placeholder="Write your comment here..."></textarea>
<input type="hidden" name="joke_id" value="<?= $joke_id; ?>">
<input class="bt-add-com" type="submit" value="Post comment">
<div class="bt-cancel-com">Cancel</div>
</div>
<div class="clear"></div>
<?php
echo form_close(); ?>
Here is my controller:
public function index(){
$this->getComment();
}
public function getComment(){
$data['results'] = $this->comments_m->getComments();
$this->load->view('template/header');
$this->load->view('template/sidebar');
$this->load->view('content/comment_box', $data);
$this->load->view('template/footer');
}
public function insertComment(){
$data = array (
'user' => 'user',
'comment' => 'comment',
'joke_id' => 'joke_id',
'id_post' => 'joke_id'
);
$joke_id = $this->input->post('joke_id');
$this->comments_m->insertComment($data);
redirect(base_url('read/joke/'.$joke_id));
}
}
model:
//gets the comments
function getComments ($joke_id){
$query = $this->db->query("SELECT c.name, j.*, co.* FROM jokes j LEFT JOIN category c ON c.category_id = j.category_id LEFT JOIN comments co ON co.joke_id = j.joke_id WHERE j.joke_id = '$joke_id' ") or die("No results found" );
if ($query->num_rows > 0) {
return $query->result();
}
}
//inserts the comments
function insertComment (){
$data = array (
'user' => $this->input->post('name-com'),
'comment' => $this->input->post('the-new-com'),
'joke_id' => $this->input->post('joke_id'),
'id_post' => $this->input->post('joke_id')
);
if(strlen($data['user']) < 1){
$data['user'] = "Guest";
}
$this->db->insert('comments', $data);
}
comments table:
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` varchar(40) NOT NULL,
`comment` text NOT NULL,
`joke_id` int(11) NOT NULL,
`id_post` int(11) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
jokes table:
CREATE TABLE IF NOT EXISTS `jokes` (
`joke_id` int(11) NOT NULL AUTO_INCREMENT,
`joke` varchar(1024) NOT NULL,
`category_id` int(11) NOT NULL,
`vote` int(255) NOT NULL,
`date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`joke_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Any help as to why the joke_id doesn't insert/show on pages with no jokes would be great.
Thank you
I have a maximum of 100 selections (now 79) for the combo box, and have 79 of them. Each one stands for a question and is recorded to "questions" table in MySQL. The options are recorded in a table called "question_codes" and they stand for the field names associated with some questions. I tried 2 for loops within each other but I think that will overload the server because of 79*79 iterations. I also have a text field depending upon the selection with ajax, working properly. Number 0 field in the table "question_codes" is id and I don't use that. Here is my code.
$code_query="SELECT * FROM questions WHERE id='1'";
$question_query="SELECT * FROM questions WHERE id='2'";
$result_question_query=mysql_query($question_query,$conn);
$result_code_query=mysql_query($code_query,$conn);
$selected_query=mysql_query("SELECT * FROM question_codes");
while($row_selected=mysql_fetch_row($selected_query))
{
$column_count=count($row_selected);
}
while($row=mysql_fetch_array($result_question_query) && $row2=mysql_fetch_array($result_code_query))
for($i=1;$i<$column_count;$i++)
{
$db_q="q$i";
$fill=$row[$db_q];
$fill_code=$row2[$db_q];
print('<tr style="background:navy;color:white"><td width="60px">Question '.$i.'</td>
<td>
<select name="'.$db_q.'_code" id="'.$db_q.'_code" onchange="showQuestion(this)">
<option value="">Select a question</option>
for ($j=1;$j<$column_count;$j++)
{
$name=mysql_field_name($selected_query,$j);
if ($fill_code==$name)
{
$selected="selected";
}
else
{
$selected="";
}
print('<option value="'.$name.'" '.$selected.'>'.$name.'</option>');
}
print('</select>
</td>
<td><input type="text" value="'.$fill.'" name="'.$db_q.'" id="'.$db_q.'"></td></tr>');
There's probably a more elegant way to structure the database and PHP.
Perhaps something like this for SQL:
CREATE TABLE `questions` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`question` varchar(255) NOT NULL,
`answer` int(11) unsigned NOT NULL DEFAULT '0'
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `question_options` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`question_id` int(11) unsigned NOT NULL DEFAULT '0',
`option` varchar(64) NOT NULL
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
... and this for the PHP ...
$q_sql = mysql_query("SELECT * FROM `questions`");
while ($q = mysql_fetch_row($q_sql)) {
echo '
<tr><td>Question '. $q['id'] .'</td>
<td><select name="q'. $q['id'] .'">';
$qo_sql = mysql_query("SELECT * FROM `question_options` WHERE `question_id` = '". $q['id'] ."'");
while ($qo = mysql_fetch_row($qo_sql)) {
$selected = ($q['answer'] == $qo['id']) ? ' selected' : '';
echo '
<option value="'. $qo['id'] .'"'. $selected .'>'. $qo['option'] .'</option>';
}
echo '
</select>
</td></tr>';
}
To tighten this further (this method can generate too many queries) ... make a single query instead, something like:
SELECT q.`question`, qo.*
FROM `question_options` as `qo`
LEFT JOIN `questions` as q
ON (q.`id` = qo.`question_id`)
ORDER BY qo.`question_id`
... then with a single while loop, check for a change in the question.
I have job insert page which is working fine.
I just build an update page in admin panel.
I'm not able to show screen short :(
I need to show the category as selected which is already in the job table.
let me show you the table details.This is the job table
CREATE TABLE IF NOT EXISTS `jobs` (
`job_id` int(11) NOT NULL AUTO_INCREMENT,
`job_title` varchar(99) NOT NULL,
`job_category` int(3) NOT NULL,
`job_location` varchar(33) NOT NULL,
`job_country` varchar(33) NOT NULL,
`job_salary` int(12) NOT NULL,
`job_reference` varchar(9) NOT NULL,
`job_contact_name` varchar(9) NOT NULL,
`job_description` text NOT NULL,
`job_requirments` text NOT NULL,
`job_companydetails` text NOT NULL,
`status` int(2) NOT NULL,
`date` date NOT NULL,
`featured` int(1) NOT NULL,
PRIMARY KEY (`job_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1091 ;
and this is the category table
CREATE TABLE IF NOT EXISTS `job_category` (
`category_id` int(11) NOT NULL AUTO_INCREMENT,
`category_name` varchar(25) NOT NULL,
PRIMARY KEY (`category_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;
I'm just showing data from job table for update.
but the thing is that I need to show JOB CATEGORY in the Combobox.
I think it is easy through like this
<select name="job_category">
<?php
$result = mysql_query("SELECT * FROM $category_tbl");
while($row = mysql_fetch_array($result))
{
echo "<option value=$row[category_id]> $row[category_name] </option>";
}
?>
</select>
but the thing is that I need to show selected category name which is in the job table
In the while loop you can have something like:
while($row = mysql_fetch_array($result)) {
echo "<option value='$row[category_id]'";
if($row['category_id'] === $rows['job_category']){
echo "selected='selected'";
}
echo "> $row[category_name] </option>";
}
job['job_category'] would contain the category id of the job you are currently displaying.
Hope this helps.
It is has some bug
I just fixed it
look the changes
$result = mysql_query("SELECT * FROM $category_tbl");
while($row = mysql_fetch_array($result))
{
echo "<option value='$row[category_id]'";
if($row['category_id'] === $rows['job_category'])
{
echo "selected='selected'";
}
echo "> $row[category_name] </option>";
}
thanks for your clue