Removing Duplicate Name from Drop Down List [closed] - php

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.. :)

Related

Codeigniter - How to inner join?

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>

how to fetch the a record from a table and display them in another view

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;
}
?>

get checked checkboxes from database

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
}
}

Populating Multiple Combo Boxes with PHP from MySQL database

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.

update with combo box

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

Categories