Trying to pass a value to a model using Codeigniter - php

Trying to learn Coedigniter here and struggling a little bit from the start. I basically want to create a query in my model using a value in the url.
Controller (Customer.php)
public function customer_edit($id) {
$this->load->model( 'Customers_model' ); //loads model
$data[ 'results' ] = $this->Customers_model->edit_customers($id);
$this->load->view( 'customers/customer_edit', $data );
}
Model (customers_model.php)
public function edit_customers($id)//This function returns an array
{
$this->load->database('default', TRUE); //connect to database
$this->db->where(['idcustomers'=>$id]); //creates where statement from url
$query = $this->db->get('customers');//create query
return $query->result_array(); //creates array from query
}
View (customer_edit.php)
<div class="form-group">
<label class="control-label col-sm-2">First Name:</label>
<div class="col-sm-3"><input type="text" class="form-control" name="txtfname" value="<?php echo $results['fname'];?>">
</div>
<label class="control-label col-sm-2">Last Name:</label>
<div class="col-sm-3"><input type="text" class="form-control" name="txtlname" value="">
</div>
</div>
When I go to http://blahblah.com/customers/customer_edit/454 I get the following error:
Message: Undefined index: fname
Filename: customers/customer_edit.php
Line Number: 10
This is what I know:
On other pages where I am just displaying all the records in a table it works fine so I am not having a database connection problem. fname and idcustomers are the correct column names in the database.
Thanks in advance for your help.

Here is how I solved the problem. I added a foreach at the beginning of the form like this:
<div class="form-group">
<label class="control-label col-sm-2">First Name:</label>
<div class="col-sm-3"><input type="text" class="form-control" name="txtfname" value="<?php echo ucwords(strtolower($object['fname']));?>">
</div>
Thanks Ghost for helping answer this question.

Related

How to display the value in a readonly input field in laravel

So, in my project, a user needs to register first before logging-in. I am trying to display their first, middle and last name. But, I am having a problem since multiple data are displayed on my input field. What is the correct query? Here is my controller code
public function get_first_name()
{
$first_names = UserModel::all();
$input="<input></input>";
foreach($first_names as $first_name)
{
$input.="<input value={$first_name->seq_id}>{$first_name->first_name}</input>";
}
return $input;
}
public function step1()
{
$users = UserModel::all();
$data['optStatus']=$this->get_civil_status();
$data['displayFirstName']=$this->get_first_name();
return view ("enrollment-steps.step1", $data);
}
Here is the blade file
<div class="col-sm-12 col-lg-4">
<div class="form-group row">
<label for="firstName" class="col-sm-3 text-right control-label col-form-label">First
Name</label>
<div class="col-sm-9">
<input class="form-control" type="text" id='firstName' readonly value="{!! $displayFirstName !!}" >
</div>
</div>
</div>
this is the data that it displays
There is some confusing stuff going on there, but you probably want
<input class="form-control" type="text" id='firstName' readonly value="{{ $data['displayFirstName'] }}" >
which will display the value with the key 'displayFirstName' in the $data array.
dd is your friend, use this in your blade file to see what you have in your $data variable.
{{dd($data)}}
In you controller bind the data's in a single variable, then show it in the blade
public function get_first_name()
{
$first_names = UserModel::all();
$input="<input></input>";
foreach($first_names as $first_name)
{
$input.="<input value={$first_name->seq_id}>{$first_name->first_name}</input>";
}
return $input;
}
Instead of this, do something like this:
public function get_first_name()
{
$first_names = UserModel::all();
$input="<input></input>";
foreach($first_names as $first_name)
{
$displayname = $first_name->seq_id . $first_name->first_name;
$input.="<input value="{$displayname}"</input>";
}
return $input;
}

Getting all the data in the database that was inserted by a single person

How can i get all the rows in a database table that was inserted by a logged in person? I'll provide the codes and snippets below
Controller.php file
public function index(){
$data['user1'] = UserModel::where('seq_id','=',Session::get('loginId'))->first();
return view ("enrollment-steps.step2", $data,
[
'data'=>$data['user1'],
]);
}
Blade.php file
<label><strong> Honors if any(Secondary level): </strong></label>
<div class="col-sm-12 col-lg-4">
<div class="form-group row">
<label for="step2_honor" class="col-sm-3 text-right control-label col-form-label">Honors</label>
<div class="col-sm-9">
<input class="form-control" type="text" id='step2_honor' placeholder="Secondary" value="{!! $user1?->honors !!}" >
</div>
</div>
</div>
What are the other methods I can use to get all the data inserted by the same person? first() method will only get the last inserted row, the get() method throws a Property [honors] does not exist on this collection instance.. For example, in the snippet below app_id 2708 inserted 3 rows, How can i get all the data of in the database that was connected to that person?
Controller
public function index()
{
$userData = UserModel::where('app_id', 2708)->get();
return view('enrollment', compact('userData'));
}
Blade
#foreach($userData as $data)
{{$data->schoolname}}
#endforeach

Why can't I show all the posted newsfeeds of the user? [duplicate]

This question already has an answer here:
PDO Return All Rows [duplicate]
(1 answer)
Closed 3 years ago.
I have a cooperative(user) that can post many newsfeeds, so a one is to many relationship. My goal is to show all the newsfeeds of the cooperative in a list and use that list to link each newsfeeds to an update and delete page. However my code is only showing the first newsfeed and since I put it inside a loop, the first newsfeed is being loop six times. Why is that?
Im using the $loggerUser['coopID'] to get the coopID that is supplied to the where clause of the viewCoopNewsfeed function.
this is my sql command
function viewCoopNewsfeed($coopID)
{
try {
$connection = connect();
$statement = $connection->prepare("SELECT * FROM newsfeed WHERE
coopID = :coopID");
$statement->bindParam(":coopID", $coopID);
$statement->execute();
$statement->setFetchMode(PDO::FETCH_ASSOC);
if ($coopNewsfeed = $statement->fetch()) {
return $coopNewsfeed;
} else {
return NULL;
}
} catch (PDOException $exception) {
die("Error: " . $exception->getMessage());
}
}
and this is the loop in the view
<?php
if(!empty($coopNewsfeeds))
{
foreach($coopNewsfeeds as $coopNewsfeed)
{
?>
<form method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Photo</label>
<img class="profile-pic" src="<?=$coopNewsfeeds['photo']?>"/>
</div>
</div>
<br><br><br><br><br><br><br><br><br><br>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control" placeholder="Title" name="title" value="<?=$coopNewsfeeds['title']?>" required>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Body</label>
<textarea class="form-control" name="Body" value="<?=$coopNewsfeeds['Body']?>" placeholder="Body" id="" cols="30" rows="2"></textarea>
</div>
</div>
</form>
<?php
}
?>
<?php
}
?>
$coopNewsfeed = $statement->fetch() this fetches the next result (the first one in your case, because you executed fetch() only once). Since you are returning $coopNewsfeed immediatly after this statement, you are getting only 1 row.
Use fetchAll() instead.
if ($coopNewsfeed = $statement->fetchAll()) {
return $coopNewsfeed; // this now returns all results in a 2d array
} else {
return NULL;
}
And then, in your view :
foreach($coopNewsfeeds as $coopNewsfeed)
{
// some html
// notice the variable name -----v-----------v
<img class="profile-pic" src="<?=$coopNewsfeed['photo']?>"/>
// more html and stuff
}

dropdown select value in yii

I am currently trying to create a sample shopping site,The problem came when I created a search page.In my search page I Have two fields one for enter the keyword and other for select type of item
Which means search,keyword in the selected item.but the item defined in DB is 0 and 1 and i Trying to get it my code but fails...could any one help me...
The Gfunction code is
public static function item(){
$optionList='';
$CategoryList = OfferEvents::model()->findAllByAttributes(array());
foreach($CategoryList as $catdata)
{ $optionList.="<option value='".$catdata['type']."'>".$catdata['name']."</option>"; }
return $optionList;
}
and view code is
<form action="<?php echo Yii::app()->baseUrl; ?>/offer?>" method="GET" class="form-inline form-section-2 row fadeInDown animated">
<div class="col-sm-5 form-group">
<input type="text" name="search" class="form-control" id="search" value="" placeholder="Enter Your Keyword">
</div>
<div class="col-sm-4 form-group">
<select name="cat" class="form-control selectpicker">
<option value='0'>Select type</option>
<?php echo GFunctions::item(); ?>
</select>
</div>
<div class="col-sm-3 form-group">
<button type="submit" class="btn btn-default btn-new">Search</button>
</div>
</form>
why don't you use Yii built in functions to handle the dropdown?
I am not sure how your Models looks like but you can choose one of the following functions to achieve what you want instead of trying to return a String $optionList with HTML tags.
CHtml::activeDropDownList() // for directly bind to active record model
CHtml::dropDownList()
Try this code....
Function
public static function item(){
$CategoryList = OfferEvents::model()->findAllByAttributes(array());
$optionList = CHtml::listData($countries, 'type', 'name')
return $optionList;
}
In view
<?php echo CHtml::dropDownList('cat', '', GFunctions::item(), array('prompt'=>'Select type'));?>

Cant get values of checkboxes in codeigniter

Im trying to insert multiple checkboxes but cant get their values in codeigniter 2
this is my code in View
<!-- Multiple Checkboxes (inline) -->
<div class="form-group">
<div class="col-md-4">
<label class="checkbox-inline" for="checkboxes-0">
<input type="checkbox" name="checkboxes[]" id="checkboxes-0" value="22">
Пентхаус
</label>
<br>
<!-- Text input-->
<div class="form-group">
<div class="col-md-8">
<input id="cena" name="cena[]" type="text" placeholder="Въведи цена" class="form-control input-md">
</div>
</div>
<label class="checkbox-inline" for="checkboxes-1">
<input type="checkbox" name="checkboxes[]" id="checkboxes-1" value="21">
Гараж/Паркомясто
</label>
<br>
<!-- Text input-->
<div class="form-group">
<div class="col-md-8">
<input id="cena" name="cena[]" type="text" placeholder="Въведи цена" class="form-control input-md">
</div>
</div>
This is my Model:
public function InsertCheckbox() {
$property_typesRequest = $this->input->post('checkboxes');
foreach($property_typesRequest as $value){
$this->db->insert_batch('property_type_details', $property_typesRequest);
}
}
in Controller i just use this:
$this->estate_m->InsertCheckbox();
And this going insert 0 in database, when i var_dump $property_typesRequest theres shows bool(false). I cant get values of checkboxes...
EDIT...
I have try to edit my code but still no result:
public function edit()/*this is controller */
{
$data=array('column_name'=>$this->input->post('checkboxes');
$result=$this->estate_m->InsertCheckbox($data);
if($result==true)
{
echo "Success";
}
else
{
echo "Fail";
}
}
public function InsertCheckbox($data) /*this is Model */
{
$this->db->insert('property_type_details', $data);
return ($this->db->affected_rows() != 1 ) ? false : true;
}
With this edited code always gives me Succes
Form submitted values should be in multi dimension array
To achieve that your form inputs should be in multi dimension.
For insert_batch() function, the array should be in multi dimension.
In array every key must be field names in db table and value must be the form input value.
So change the form structure like the below array.
array(
array(
'checkboxes' => 'checkboxe value' ,
'cena' => 'cena values'
),
array(
'checkboxes' => 'checkboxe value' ,
'cena' => 'cena values'
)
);
Form inputs should be like below:
<input name="data[1][checkbox_columnname]" type="checkbox" value="21">Пентхаус
<input name="data[1][textbox_columnname]" type="text">
<input name="data[2][checkbox_columnname]" type="checkbox" value="22">Гараж/Паркомясто
<input name="data[2][textbox_columnname]" type="text">
And the model should not have foreach loop.
Simply pass the data as below.
$data=$this->input->post('data');
$this->db->insert_batch('mytable', $data);
Please use this code in model.
public function InsertCheckbox() {
$property_typesRequest = $this->input->post('checkboxes');
foreach($property_typesRequest as $value){
$data['columnename'] = $value;
$this->db->insert('tablename', $data);
}
}
Hope it inserts into the database
Thank you

Categories