Opencart get select value to controller for filter - select

I have page called no.tpl, in this page am displaying customer name in select dropdown
this is the code:
no.tpl
<select name="customer_id" id="customer" style="width: 325px;margin-bottom:10px" class="form-control">
<?php foreach($customerData as $customer){ ?>
<option value=<?php echo $customer['customer_id']?>><?php echo $customer['customer_name']?></option>
<?php }?>
</select>
In controller page i have to filter selected customer list
$queryCustomer = $this->db->query("select customer_id, concat(firstname, ' ',lastname) as name, email from " . DB_PREFIX . "customer where customer_id='6'");
$selectedCustomer = $queryCustomer->row;
$selectedCustomerId = $selectedCustomer['customer_id'];
$selectedCustomerName = $selectedCustomer['name'];
$selectedCustomerEmail = $selectedCustomer['email'];
I want customer_id='6' as selected customer_id. I mean pass the select value to controller page

Try this code in view page
<select name="customer_id" id="input-sales-person" style="width: 325px;margin-bottom:10px" class="form-control">
<?php foreach($customerData as $customer){ ?>
<option id="temp" value=<?php echo $customer['customer_id']?>><?php echo $customer['customer_name']?></option>
<?php }?>
</select>
<input type="submit" id="noOrder" Onclick="return ConfirmDelete();" value="Submit" class="btn btn-primary">
Use this following script
<script type="text/javascript">
$('#noOrder').on('click', function() {
var temp1=$( "#input-sales-person option:selected" ).val();
var temp2=$( "#input-sales-person option:selected" ).text();
document.cookie = "selectedCustomerId=" +temp1;
document.cookie = "selectedCustomerName=" +temp2;
location="index.php?route=sale/no";
});
</script>
In controller pass the customer_id as $selectedCustomerId=$_COOKIE['selectedCustomerId'];
$selectedCustomerId=$_COOKIE['selectedCustomerId']; /*customer_id=6*/
$queryCustomer = $this->db->query("select customer_id, concat(firstname, ' ',lastname) as name, email from " . DB_PREFIX . "customer where customer_id='".$selectedCustomerId."'");

Related

Undefined variable in Option Value CodeIgniter

What I'm I doing wrong?
application/views/pegawai_read.php
<select name="" id="" class="form-control">
<?php
foreach($nm_berkas_jn->result() as $j){
?>
<option value="<?php echo $j->id_berkas_jn;?>"><?php echo $j->nm_berkas_jn;?></option>
<?php } ?>
</select>
application/controllers/pegawai.php
public function read($id) {
$id_berkas_jn=$this->session->userdata('id_berkas_jn');
$text = "SELECT * FROM kpg_berkas_jn";
$d['nm_berkas_jn'] = $this->app_model->manualQuery($text);
$d['level']=$this->session->userdata('level');
}
application/models/app_model.php
function manualQuery($q) {
return $this->db->query($q);
}
Add the following line to the bottom of your controller. This send the data to the view
$this->load->view('pegawai_read', $d);

Passing Data from a Controller to a View (not displaying it) then passing back to different Controller in Codeigniter

I am trying to a create a View which provides a summary table of various site members profiles - then each summary has a button, that once clicked, takes the user to that member's full profile. I can get the summary page to work properly, but I cant get the second part -where the button takes the user to the members full profile to work.
Here is my controller:
public function network_barre()
{
$this->load->model("Profiles_model");
$style ='barre';
$profilesubdata["fetch_data"] = $this->Profiles_model->fetch_data($style);
$this->load->view('templates/header');
$this->load->view('pages/page-network_barre', $profilesubdata);
$this->load->view('templates/footer');
Here is my Model "Profiles Model"
function fetch_data($style)
{
$this->db->select("username, email, style, about");
$this->db->from("profiles");
$this->db->where('style', $style);
$query = $this->db->get();
return $query;
}
Here is the view
<div class="container">
<div class="row d-flex justify-content-center card-top">
<?php
if($fetch_data->num_rows()>0)
{
foreach ($fetch_data->result() as $row)
{
?>
<div class="col-lg-4 col-md-6">
<div class="card card-warning wow zoomInUp mb-4 animation-delay-5">
<div class="withripple zoom-img">
<img src="<?=base_url();?>assets/img/demo/avatar4.jpg" class="img-fluid">
</div>
<div class="card-body">
<span class="badge badge-warning pull-right"><?php echo $row->style; ?></span>
<h3 class="color-warning"><?php echo $row->username; ?></h3>
<p><?php $string=character_limiter($row->about, 255, '&#8230(More in Profile)') ; echo $string?></p>
<div class="row">
<div class="col text-center">
<!-- BUTTON -->
<form method="POST" action="<?php echo base_url(); ?>public_area/gotopublicprofile/<?php echo $row->email; ?>">
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name();?>" value="<?php echo $this->security->get_csrf_hash();?>">
<button type="submit" class="btn btn-raised btn-sm btn-warning" name="submit" value="submit"><i class="zmdi zmdi-account"></i>Profile</button>
</form>
</div>
</div>
</div>
</div>
</div>
<?php //this tag close out php tags above
}
}
?>
</div>
</div>
here is Controller gotopublicprofile
function gotopublicprofile()
{
$this->load->model("profiles_model");
$email = '$this->uri->segment(3)';
$profiledata["fetch_profiledata"] = $this->Profiles_model->fetch_profiledata($email);
$this->load->view('templates/header');
$this->load->view('pages/page-profilepublic', $profiledata);
$this->load->view('templates/footer');
}
And here is the fetch_profiledata function in the Model:
function fetch_profiledata($email)
{
$this->db->where('email', $email);
$query = $this->db->get("profiles");
return $query;
}
I always get these error messages:
Undefined property: Public_area::$Profiles_model
AND
Call to a member function fetch_profiledata() on null
I am sure I am just missing something simple, as lots of websites use this approach.
Thanks in advance!
So I figured it out..it was a combination of little things:
First
$email = '$this->uri->segment(3)';
became
$email = $this->uri->segment(3);
Thanks #kirb! and
function gotopublicprofile()
{
$this->load->model("profiles_model");
became
function gotopublicprofile()
{
$this->load->model("Profiles_model");
Thanks #Atal Prateek!

Form input OR drop menu

Hi I'm trying to make a form where on the name field you can use EITHER text or drop menu (example the drop menu would contain names already in the database whereas the text input would be for new names)
<div class="form-group form-group-sm" style="margin: 1px;">
<label class="col-sm-2 control-label" for="Owner">Owner Name: </label>
<div class="col-sm-4"><input class="form-control" type="text" id="Owner" name="Owner" <?php $q = $opened['Owner']; if(isset($opened['IDNumber'])) { echo "value=$q"; } else { echo "placeholder='Enter Owner Name'";}; ?> required></div>
<div class="col-sm-1">Or Select</div>
<div class="col-sm-4"><select class="form-control" id="Owner" name="Owner" required>
<option>Select</option>
<?php
$sql3="SELECT DISTINCT Owner FROM list Order by Owner asc";
$results = mysqli_query($dbcon,$sql3) or die();
while($row = mysqli_fetch_array($results)) {
echo '<option value="'.$row['Owner'].'">'.htmlspecialchars($row['Owner']).'</option>';
}
?>
</select></div>
</div>
how would i get it to input the data from one OR the other? not both

How to populate values in drop down list in php postgresql

I am getting error while populating the values in drop down list using php postgresql. Error means no output in drop down list.
Below is my code.
<!DOCTYPE html>
<html>
<body>
<form name="form1" id="form1" action="" method="post">
<select name="selectid" Id="select">
<option value="">--- Select ---</option>
<?php
// postgresql database connection
include ('dbconfig.php');
$list = pg_query($db, "select id, name from codevalue ");
while($row_list=pg_fetch_array($list)){
?>
<option value=<?php echo $row_list["id"]; ?>>
<?php echo $row_list["name"]; ?>
</option>
<?php
}
?>
</select>
?>
<button type="submit" name="submit" >Submit</button>
</form>
</body>
</html>
Thanks in advance.
Change
while($row_list=pg_fetch_array($list)){
to
while($row_list=pg_fetch_assoc($list)){
You are retrieving an array and not an associated value. As an array you'd have to reference column and row.
Change your loop to an assoc instead of an array. Try this example:
Check if you have any rows in the database.
if (pg_num_rows($list) == 0)
{
echo "<option>No rows exist</option>";
} else {
while($row_list = pg_fetch_assoc($list)){
?>
<option value=<?php echo $row_list["id"]; ?>>
<?php echo $row_list["name"]; ?>
</option>
<?php
}
}

CodeIgniter passing a variable to a Modal then to a View

I have a list of cities in my database. There is an edit button that brings up a modal with a form where they can change the name of the city and submit. I'm having a hard time getting the city_id to pass from the edit button through the modal and to the controller. I get a "message undefined variable for the city_id on the line that tries to put the city_id in the form open code.
Adding a modal into the mix has made this confusing to me.
View Code
Edit
<!-- Modal for Edit -->
<div class="modal hide" id="editCityDialog">
<div class="modal-header">
<button class="close" data-dismiss="modal">x</button>
<h3>Edit City</h3>
</div>
<div class="modal-body">
<?php $this->load->helper('form'); ?>
<?php echo form_open('/cities/edit_city/'. $city_id); ?>
<label class="control-label" for="name">City</label>
<input type="text" name="city_name" id="city_name" value=""/><br />
<input type="submit" class="btn-small btn-primary" value="Edit City" />
<button class="btn-small" data-dismiss="modal">Cancel</button>
<?php echo form_close(); ?>
</div>
<!-- jQuery for Modal -->
<script>
// scripts for modal windows
$(document).on("click", ".edit-modal", function () {
var city_name = $(this).data('name');
var city_id = $(this).data('id');
$(".modal-body #city_name").val( city_name );
$('#editCityDialog').modal('show');
});
</script>
Controller Code
// Edit City
public function edit_city() {
$id = $this->uri->segment(3);
if ($this->input->city('submit')) {
$city_name = $this->input->xss_clean($this->input->city('city_name'));
$this->cities_model->edit_city($city_id, $city_name);
}
}
Model Code
// Edit City
public function edit_city($city_id, $city_name) {
$data = array(
'city_name' => $city_name
);
$this->db->where('city_id', $city_id);
$this->db->update('cities', $data);
}
You can use the city_id you already have and set the action with that:
$(document).on("click", ".edit-modal", function () {
var city_name = $(this).data('name');
var city_id = $(this).data('id');
$(".modal-body #city_name").val( city_name );
//set the forms action to include the city_id
$(".modal-body form").attr('action','/cities/edit_city/'+city_id);
$('#editCityDialog').modal('show');
});
if echoing <?php echo $c['city_id']; ?> works for the edit button, you should do that for the form_open as well like this
<?php echo form_open('/cities/edit_city/'. $c['city_id']); ?>
I would also recommend that if your edit_city() function is only ever called if there is a segment(3), then it is easier to do this:
// Edit City
public function edit_city($id) {
if ($this->input->city('submit')) {
$city_name = $this->input->xss_clean($this->input->city('city_name'));
$this->cities_model->edit_city($city_id, $city_name);
}
}