view data from codeigniter join table - php

I want to view data from database.there are three table to join.in two table have same field name. when i try to view data as follows.but there was an error called
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$project_name1
Filename: views/boq_doc.php
Line Number: 12
Controller
class Project_list extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('project_list_model');
}
function show_project_id() {
$id = $this->uri->segment(3);
$data['projects'] = $this->project_list_model->show_projects();
$data['single_project'] = $this->project_list_model->show_project_id($id);
$this->load->view('boq_doc', $data);
}
}
Model
class Project_list_model extends CI_Model {
// Function To Fetch All Students Record
function show_projects(){
$this->db->select("project.project_name AS project_name1 , project.id AS id, client.firstname AS firstname1, client.lastname AS lastname1,staff.firstname AS firstname2, staff.lastname AS lastname2, project.location,project.category, project.start_date, project.end_date");
$this->db->from('project');
$this->db->join('client', 'project.client_id = client.client_id');
$this->db->join('staff', 'staff.id = project.staff_id');
$query = $this->db->get();
return $query->result();
}
// Function To Fetch Selected Student Record
function show_project_id($data){
$this->db->select('*');
$this->db->from('project');
$this->db->where('id', $data);
$query = $this->db->get();
$result = $query->result();
return $result;
}
}
View
<?php foreach ($projects as $project): ?>
<li><?php echo $project->project_name1; ?></li>
<?php endforeach; ?>
</ol>
</div>
<div id="detail">
<!-- Fetching All Details of Selected Student From Database And Showing In a Form -->
<?php foreach ($single_project as $project): ?>
<form method="post" action="<?php echo base_url() . "index.php/update_ctrl/update_project_id1"?>">
<label id="hide">Id :</label>
<?php echo $project->project_name1; ?>
<label>Name :</label>
<input type="text" name="dname" value="<?php echo $project->location; ?>">
<label>Email :</label>
<input type="text" name="demail" value="<?php echo $project->start_date; ?>">
<label>Mobile :</label>
<input type="text" name="dmobile" value="<?php echo $project->end_date; ?>">
<label>Address :</label>
<input type="text" name="dmobile" value="<?php // echo $project->firstname1; ?>">
<input type="text" name="daddress" value="<?php // echo $project->project_address; ?>">
<input type="submit" id="submit" name="dsubmit" value="Update">
</form>
<?php endforeach; ?>
</div>

Your foreach loop syntax seems wrong try this instead
<?
foreach ($projects as $project)
{
?>
<li><?php echo $project->project_name1; ?></li>
<?
}
?>
</ol>
</div>
<div id="detail">
<!-- Fetching All Details of Selected Student From Database And Showing In a Form -->
<?php foreach ($single_project as $project){ ?>
<form method="post" action="<?php echo base_url() . "index.php/update_ctrl/update_project_id1"?>">
<label id="hide">Id :</label>
<?php echo $project->project_name1; ?>
<label>Name :</label>
<input type="text" name="dname" value="<?php echo $project->location; ?>">
<label>Email :</label>
<input type="text" name="demail" value="<?php echo $project->start_date; ?>">
<label>Mobile :</label>
<input type="text" name="dmobile" value="<?php echo $project->end_date; ?>">
<label>Address :</label>
<input type="text" name="dmobile" value="<?php // echo $project->firstname1; ?>">
<input type="text" name="daddress" value="<?php // echo $project->project_address; ?>">
<input type="submit" id="submit" name="dsubmit" value="Update">
</form>
<?
}
?>
</div>

Related

undeclared php variable in contact form

I'm having problems with a simple contact form.
The error I'm getting is:
Notice: Undefined index: terms in contact.php on line 29
Attention! You have to check the Privacy Policy box to accept our terms.
Code:
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$terms = $_POST['terms']; // <---- line 29
if(trim($terms) == '') {
echo '<div class="alert error"><div class="msg">Attention! You have to check
the Privacy Policy box to accept our terms.</div></div>';
exit();
}
if(trim($comments) == '') {
echo '<div class="alert error"><div class="msg">Attention! Please enter your
message.</div></div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
The template file is below:
<form action="<?php echo $this->config->get('config_url').'ajax/contact.php' ?>" method="post" id="contact_form" enctype="multipart/form-data">
<table>
<tr>
<td>
<label for="name"><?php echo $entry_name; ?></label>
<input type="text" name="name" id="name" style="margin-right:20px" value="<?php echo $name; ?>" />
<?php if ($error_name) { ?>
<span class="error"><?php echo $error_name; ?></span>
<?php } ?>
</td>
<td>
<label for="email"><?php echo $entry_email; ?></label>
<input type="text" name="email" id="email" value="<?php echo $email; ?>" />
<?php if ($error_email) { ?>
<span class="error"><?php echo $error_email; ?></span>
<?php } ?>
</td>
</tr>
</table>
<label for="enquiry"><?php echo $entry_enquiry; ?></label>
<textarea name="enquiry" id="enquiry" cols="30" rows="5"><?php echo $enquiry; ?></textarea>
<?php if ($error_enquiry) { ?>
<span class="error"><?php echo $error_enquiry; ?></span>
<?php } ?>
<input type="checkbox" name="terms" value="<?php echo $terms; ?>" />
<label for="terms">Tick this box to confirm you comply with our Privacy Terms</label>
<input type="submit" id="submit" value="<?php echo $button_continue; ?>" class="button dark-bt" />
</form>
Try this:
$terms = isset($_POST['terms']) ? $_POST['terms'] : '';
You will get the checkbox field only if it is checked by the user in the form.
So, always check like this for checkbox and radio buttons.

How to add/change fields dynamicaly in form from if else condition?

Here i have numerous input fields in a form, which are working perfectly. And now, i want to add new input boxes and their labels according to property_type , Which is already exist on this page.
Now, Kindly Suggest me How can i add new fields in that form.
Here is my Form Code
<div class="dashboard_price_left">
<h3>
<?php
if ($this->lang->line('BasePrice') != '') {
echo stripslashes($this->lang->line('BasePrice'));
} else
echo "Base Price";
?>
</h3>
<p>
<?php
if ($this->lang->line('Atitleandsummary') != '') {
echo stripslashes($this->lang->line('Atitleandsummary'));
} else
echo "Set weekly or monthly price guests will see for your listing.";
?>
</p>
</div>
<?php
if ($listDetail->row()->home_type == 'Office') {
} else if ($listDetail->row()->home_type == 'House') {
}
?>
<form id="pricelist" name="pricelist" action="site/product/savePriceList" method="post">
<div class="dashboard_price_right">
<label><?php
if ($this->lang->line('Pernight') != '') {
echo stripslashes($this->lang->line('Pernight'));
} else
echo "Per night";
?>
</label>
<div class="amoutnt-container">
<?php if ($currentCurrency == '') { ?>
<span class="WebRupee"><?php echo $currencyDetail->row()->currency_symbols; ?></span>
<?php } else { ?>
<span class="WebRupee"><?php echo $currentCurrency; ?></span>
<?php } ?>
<input type="text" id="price" value="<?php
if ($listDetail->row()->price != '0.00') {
echo intval($listDetail->row()->price);
}
?>" class="per_amount_scroll" name="price" onkeypress="return onlyNumbersWithDot(event);" onchange="javascript:Detailview(this,<?php echo $listDetail->row()->id; ?>, 'price');" />
<input type="hidden" id="id" name="id" value="<?php echo $listDetail->row()->id; ?>" />
</div>
<div class="dashboard_currency">
<label><?php
if ($this->lang->line('Currency') != '') {
echo stripslashes($this->lang->line('Currency'));
} else
echo "Currency";
?>
</label>
<div class="select select-large select-block">
<select name="currency" id="currency" onchange="javascript:Detailview(this,<?php echo $listDetail->row()->id; ?>, 'currency');get_currency_symbol(this)" >
<!--<option value="">select</option>-->
<?php foreach ($currencyDetail->result() as $currency) { ?>
<option value="<?php echo $currency->currency_type; ?>" <?php if ($listDetail->row()->currency == $currency->currency_type) echo 'selected="selected"'; ?>><?php echo $currency->currency_type; ?></option>
<?php } ?>
</select>
</div>
</div>
</div>
</form>
Here is a pic of my Property prices form
Note: There are two types of property that are following:
1. Office
2. House
on the basis of these property, I want to add more fields in my form.
Please suggest me, Thanks in Advance :)
you can change it base on conditon and pass hidden field which indicates you office or house.
<?php
if ($listDetail->row()->home_type == 'Office')
{
<input type="hidden" name="home_type" value="Office" />
<input type="text" name="per_day" value="" />
<input type="text" name="half_day" value="" />
<input type="text" name="per_hour" value="" />
}
else if ($listDetail->row()->home_type == 'House')
{
<input type="hidden" name="home_type" value="House" />
<input type="text" name="per_night" value="" />
<input type="text" name="per_day" value="" />
<input type="text" name="per_hour" value="" />
}
?>

Store data obtained using foreach loop from view into the database using CodeIgniter

I am trying to store the values obtained from the form into the database. I am stuck where I do not know how to obtain the values individually from foreach($questions as $row) and store them into the database.
My database table has the following columns:
variantid | stepid | questionid | newquestion | radiovalues | description
When I click on save, I am trying to save all the details obtained from the view into the database. every value obtained with each loop of foreach($questions as $row) needs to get stored in a new row. I think I should be using insert_batch, but I do not know how I can put all the data into the array.
How can I proceed with this? The following is the code that I have written. I have not written anything in model as I am stuck.
<form id="theform">
<label for="variants">Variants:</label><br>
<?php
foreach($variants as $row)
{?>
<div class="form-group">
<input type="radio" name="variants[]" checked="true" id="variants" value="<?php echo $row->variantid ?>"/><?php echo $row->variantname ?><br/>
<?php }
?>
<div id='steplist'>
<?php
foreach($substeps as $row){
?>
<div id='img_div'>
<input type="radio" name="stepid[]" checked="true" id="stepid<?php echo $row->stepid; ?>" value="<?php echo $row->stepid; ?>"/><?php echo $row->stepname ?><br/>
</div>
<?php
foreach($questions as $each){
?><br><br>
<input type="text" name="questions[]" id="questions<?php echo $each->questionid; ?>" value="<?php echo $each->questionname; ?>" readonly/><br/>
<input type="hidden" name="questionid[]" id="questions<?php echo $each->questionid; ?>" value="<?php echo $each->questionid; ?>"/>
<a id="addq">Add question</a>
<input type="text" class="form-control" name="newquestion[]" style="font-weight:bold;width:100%;height:5%" id="new_questions<?php echo $each->questionid; ?>" /><br>
<div class="radio" id="answer">
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="no" required>no</label>
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="probably no" required>probably no </label>
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="unknown" required>unknown</label>
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="probably yes" required>probably yes</label>
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="yes">yes</label>
</div>
<textarea name="question1_text[]" id="description<?php echo $each->questionid; ?>" rows="3" cols="73"></textarea><br>
<?php
}
?>
<?php
}
?>
</div>
</form>
<input type='button' value='Save' class='save' id='save' />
<script>
$(document).ready(function(){
$("#save").click(function(e){
e.preventDefault();
$.ajax({
type:"POST",
url:"<?= base_url() ?>index.php/task/perform",
data:$('#theform').serialize(),
success:function(response){
alert(response);
}
});
});
});
This is my controller:
public function perform(){
var_dump($_POST);
$variantid = $this->input->post('variants');
$stepid = $this->input->post('stepid');
$questionid = $this->input->post('questions');
$newquestion = $this->input->post('newquestion');
$radioquestions = $this->input->post('radioquestions');
$question1_text = $this->input->post('question1_text');
$this->load->model('task_model');
$result= $this->task_model->performdata();
if($result){
echo "Data saved successfully";
}
}
Update
Thank you for the quick reply. I changed the code according to the suggestion. How can I get these values into the controller and send them into the database?
The HTML inside the foreach is invalid as it is missing a closing div tag and your input does not have the correct name attribute, for multiple inputs you should use variants[] and not the same ID. Do it in this manner:
<?php foreach ($variants as $row) { ?>
<div class="form-group">
<input type="radio" name="variants[]" checked="true" class="variants" value="<?php echo $row->variantid ?>"/><?php echo $row->variantname ?><br/>
</div>
<?php } ?>

codeigniter - get the value of checked checkboxes in the form in other way

Is there other way how to get the value of all checked checkboxes in my form and display it on the other view?
here's my input view
<div class="col-md-6">
<input type="checkbox" name="sen_id[]" class="Form-label-radio" value="<?php echo $sen['id']; ?>" id="<?php echo $sen['id']; ?>" />
<input type="checkbox" name="sen_id[]" class="Form-label-radio" value="<?php echo $sen['id']; ?>" id="<?php echo $sen['id']; ?>" />
<input type="checkbox" name="sen_id[]" class="Form-label-radio" value="<?php echo $sen['id']; ?>" id="<?php echo $sen['id']; ?>" />
</div>
my model
public function view_sen($sen_id){
$sen_id[] = $this->input->post('sen_id');
foreach ($sen_id as $id) {
$this->db->select('*')->from('party_candidates')->where('id', $id);
$query[] = $this->db->get()->result_array();
}
return $query;
}
my controller
public function balot_form(){
$sen_id[] = $this->input->post('sen_id');
$view_sen_votes = $this->vote->view_sen($sen_id);
$data = array("view_sen_votes" => $view_sen_votes);
$this->load->view("admin_dashboard/votation_page/balot_form_page", $data);
}
display results view
<div class="col-md-6">
<?php foreach($view_sen_votes as $sen => $id) { ?>
<div class="row">
<div class="form-group">
<label>Senators</label>
<input type="text" class="form-control" value="<?php echo $sen[$id]; ?>" name="sen_id" />
</div>
</div>
<?php } ?>
</div>
how to get the value and display it on my result view? thanks, regards..
Try like below to check key value.
print_r( $view_sen_votes );

Post data not being sent by a code igniter form

I've got a slight problem with my CI2.0.2 application.
From what I can tell, it is not getting any post data from a form and so is failing to run an update function, though most pages work fine.
The form is as follows:
<?php echo form_open('job/update/', array('id' => 'update', 'name' => 'update')); ?>
<div class="images">
<?php echo img('application/images/updateJob.png');?>
</div>
<h1>Update Job</h1>
<br><br><br><br>
<div class="fieldset">
<?php
if (isset($error))
{
echo '<div id ="error">';
echo '<h2>Error:</h2>';
echo '<p>'.$error.'</p>';
echo '</div>';
}
?>
<input type="hidden" name="c" id='c' value="<?php if(!empty($view[0])) { echo $view[0]; } else { echo "0"; } ?>">
<div class="left">
<h4>Job Details:</h4>
<div>
<label for="job_number">Job No:</label>
<input type="text" name="job_number" id="job_number" value="<?php echo $job['jobno']; ?>" disabled="disabled">
</div>
<div>
<label for="date">Date:</label>
<input type="text" name="date" id="date" value="<?php echo $job['datetime']->format('d-M-Y'); ?>">
</div>
<div>
<label for="council">Council:</label>
<input type="text" name="council" id="council" value="<?php echo htmlspecialchars($job['council']); ?>"> *
</div>
<div>
<label for="dano">DA No:</label>
<input type="text" name="dano" id="dano" value="<?php echo htmlspecialchars($job['dano']); ?>">
</div>
<div>
<label for="client">Client:</label>
<input type="text" name="client" id="client" value="<?php echo $job['clientid']; ?>" disabled="disabled">
</div>
</div>
<div class="right" style="margin-left: 390px;">
<h4>Location:</h4>
<label for="street_no">Street No:</label>
<input type="text" name="street_no" id="street_no" value="<?php echo $address['streetno']; ?>"> *
<br>
<label for="street_name">Street Name:</label>
<input type="text" name="street_name" id="street_name" value="<?php echo $address['streetname']; ?>"> *
<br>
<label for="street_type">Street Type:</label>
<select name="street_type" id="street_type">
<?php
foreach ($street_types as $type)
{
echo '<option';
if ($type == $address['streettype']) echo ' selected="selected"';
echo '>'.$type.'</option>';
}
?>
</select> *
<br>
<label for="suburb">Suburb:</label>
<input type="text" name="suburb" id="suburb" value="<?php echo $address['suburb']; ?>"> *
<br>
<label for="postcode">Postcode:</label>
<input type="text" name="postcode" id="postcode" value="<?php echo $address['postcode']; ?>"> *
<br>
<label for="state">State:</label>
<input type="text" name="state" id="state" value="<?php echo $address['state']; ?>"> *
<br>
<label for="country">Country:</label>
<input type="text" name="country" id="country" value="<?php echo $address['country']; ?>">
<br>
<label for="dp">DP:</label>
<input type="text" name="dp" id="dp" value="<?php echo $address['dp']; ?>">
<br>
<label for="lot_no">Lot No:</label>
<input type="text" name="lot_no" id="lot_no" value="<?php echo $address['lotno']; ?>">
<br>
<label for="po_box">PO Box:</label>
<input type="text" name="po_box" id="po_box" value="<?php echo $address['pobox']; ?>">
</div>
<div>
<input type="submit" id="submit" name="submit" value="Submit" class="button">
<p>* = Required fields</p>
</div>
</div>
<?php echo form_close();?>
And the update section of the controller is as follows:
/**
* Update an existing job.
* #param int $job_number The number of the job to update.
*/
public function update($job_number=0)
{
$this->load->model('Job_model');
$job = array();
$address = array();
// Get post data.
$job['joblocation'] = '';
$job['jobno'] = $this->input->post('job_number');
$job['datetime'] = new DateTime($this->input->post('date'));
$job['dano'] = $this->input->post('dano');
$job['council'] = $this->input->post('council');
echo $job['jobno'];
echo $job['dano'];
echo $job['council'];
$address['streetno'] = $this->input->post('street_no');
$address['streetname'] = $this->input->post('street_name');
$address['suburb'] = $this->input->post('suburb');
$address['country'] = $this->input->post('country');
$address['postcode'] = $this->input->post('postcode');
$address['state'] = $this->input->post('state');
$address['dp'] = $this->input->post('dp');
$address['lotno'] = $this->input->post('lot_no');
$address['pobox'] = $this->input->post('po_box');
$address['streettype'] = $this->input->post('street_type');
echo "here2";
if (isset($_POST['submit']))
{
$this->Job_model->update($job);
echo "here";
redirect('job/');
}
// Otherwise, get the data from the database.
else
{
$job = $this->Job_model->search($job_number);
$job = $job[0];
$job['datetime'] = new DateTime($job['datetime']);
$address = $this->Job_model->get_address($job['joblocation']);
$address = $address[0];
}
// Get the street types.
$street_types = array();
$this->load->model('staff_model');
$streets = $this->staff_model->get_street_types();
foreach ($streets as $street)
{
$street_types[$street['streettype']] = $street['streettype'];
}
// Load the client list.
$clients = array();
$this->load->model('client_model');
$people = $this->client_model->get_client_person_list();
$companies = $this->client_model->get_client_company_list();
// Allocate view data.
$viewdata = array();
$viewdata['job'] = $job;
$viewdata['street_types'] = $street_types;
$viewdata['address'] = $address;
$viewdata['people'] = $people;
$viewdata['companies'] = $companies;
$this->layout->view('job/update', $viewdata);
}
Any ideas why this might be happening?
Thanks in advance,
James
Try changing the quotes you're using on this line:
<input type="hidden" name="c" id='c' value="<?php if(!empty($view[0])) { echo $view[0]; } else { echo '0'; } ?>" />
The way you had it with the double quotes around the 0 you were breaking that php because the first one would be closing the opening value quote.

Categories