Codeigniter with bootstrap x-editable and multiple inputs - codeigniter

i been trying to figure out how to do this but i couldn't find any solution or answer about it.
I'm working with x-editable for bootstrap and codeigniter and the issue that is driving me crazy is that i dont know how to integrate x-editable with CI when i have more than one input related to the same "pk" in the same page.
For example, i have materials that have a single "name" and "comment". When i'm trying to edit a single value (for example, the "name" of a material), the x-editable plugin sends a value that i capture in my backend and then send to a model and into de database.
That works great, but the deal is that when i edit the column "name" in my db the column "comment" is changed with the "name" value at the same time, instead of keeping in the actual value.
Init x-editable plugin
$('.inline-editable').editable({
selector: 'a.editable-click',
type: 'text',
url: 'admin/material/update/',
title: 'Click para editar',
});
This are the x-editable inputs
<tr class="entity-row row-fluid inline-editable" data-section="material" data-pk="1">
<td>
Name of the material
</td>
<td>
This is the comment of the material
</td>
</tr>
Backend controller method
public function update() {
$this->db_data['id'] = $this->input->post('pk');
$this->db_data['name_value'] = $this->input->post('value'); // Should be the name $value
$this->db_data['comment_value'] = $this->input->post('value'); // This should be the comment $value
// I try to capture this values for the db columns name to be edited
// But i know this can't be possible because
// they have the same $_POST variable name.
$this->db_data['db_column_name'] = $this->input->post('name');
$this->db_data['db_column_comment'] = $this->input->post('name');
$this->entity_model->update_entity($this->db_table, $this->db_data);
}
Backend model method
function update_entity($table, $data) {
$this->table = $table;
$query .= $this->db->set($data['db_column_name'], $data['name_value']);
$query .= $this->db->set($data['db_column_comment'], $data['comment_value']);
$query .= $this->db->where($this->table.'.id', $data['id']);
$query .= $this->db->update($this->table);
}
I hope you could understand this because english it's not my native language
Thanks!

Related

Cannot get thumbnail in rss feed in laravel

I am using willvincent feed reader to parse rss feeds, But i cannot seem to get the thumbail of the images,
Here is my code
Route::get('feed', function(Request $request) {
$f = FeedsFacade::make('http://www.cbn.com/cbnnews/us/feed/');
// $results = [
// 'image' => $f->get_image_url(),
// ];
foreach($f->get_items(0, $f->get_item_quantity()) as $item) {
$i['title'] = $item->get_title();
$i['thumbnail'] = $item->get_thumbnail();
$i['description'] = $item->get_description();
$i['content'] = $item->get_content();
$i['link'] = $item->get_link();
$i['date'] = $item->get_date();
$results['items'][] = $i;
}
dd($results);
})->name('feed');
Thumbnail always return null, will appreciate anyone's help
Here is how we call image and other contents from rss feed
NOTE: To get image, it is important that the site you fetching rss feeds, use to provide image too in their feed. Else won't get image in your fetched feed (there might be some trick to still fetch image but I am not aware of it for now). For example, google news feed don't provide image so you won't get image from google news feed.
Here is example of one site who use to provide image.
//In your CONTROLLER file
$feed = Feeds::make('https://globalnews.ca/feed/');
$data = array(
'title' => $feed->get_title(),
'permalink' => $feed->get_permalink(),
'items' => $feed->get_items(),
);
return view('view_file', $data)
//OR in case you are calling array value to show on view then it would be like this
return view('view_file', array(
'name' => $var
), $data) //notice "$data" at end of array.
Now after done on controller part, this is how you will call in view file
#foreach ($items as $item)
//GET IMAGE
#if($enclosure = $item->get_enclosure())
<img src="{{$enclosure->get_thumbnail()}}">
#endif
//GET TITLE
<div class="news-title">{{ $item->get_title() }}</div>
//GET DESCRIPTION
{{$item->get_description()}}
//NOTE: this will bring html. TO show as output instead of html you can either use {!! !!) or "strip_tags" function. And "substr" function to show first 100 characters only as small description
{{ substr(strip_tags($item->get_description()), 0, 100) }}
//GET LINK/URL OF NEWS
Read More
#endforeach
Feed which whose example I shown and also reference
https://packagist.org/packages/willvincent/feeds

Populate form without model in laravel

My application is made in laravel for a competition admin.
I have 'create' and 'edit' forms on Teams and Players. One team has multiple players.
I would like to link from the Team page to the 'create player' page. The Create Player page does not use a model (doesn't bind). How can I still prefill the select box with the team from the team page? Can I bind without saving a record in the database?
What should my routes be like?
Pass the team ID in the URL?
/players/create?team={teamId}
PlayersController#create method:
$teams = Team::all();
return view('players.create', compact('teams'));
players.create view:
<select name="team">
#foreach ($teams as $team) {
<option value="{{ $team->id }}"{{ $request->has('team') && $request->query('team') === $team->id ? ' selected' : '' }}>{{ $team->name }}</option>
#endforeach
</select>
You could make a route for example
// teamId is optional
Route::get('player/create/{teamId?}', ['as' => 'player_create', function ($teamId = null) {
// You can of course better do this logic in a controller!
// just an example :)
// check if $teamId is null here for example
// Or whatever logic you want to grab a team by
$team = Team::find($teamId);
$teams = Team::all();
// Again.. or whatever way you want to pass your data!
return view('player.create', ['teamName' => $team->name, 'teams' => $teams, 'whatever' => 'elseyouneed']);
}]);
And in the form of your view:
{!! Form::select('team', $teams, $teamName) !!}
Edit
since html isn't part of the core anymore, you can't use that out of the box so I suppose Chris' approach is better. You could however install a package for it.

Is there a shorter way than this to check values when using same Add form for Edit

I have the same code for an Add and an Edit form. Therefore in the controller I need a check to check if a) POST vars submitted (for saving), and if not then b) the original values (for editing) and if not then no value (blank for Adding). I put them in a $data array to pass to the view. Then in the form I can put:
value="<?php echo $member_id;?>"
So my question is, in Codeigniter is there a shorter way than the following to check if POST, then if not check if the original data exists, and if not then its nothing.
$data = array(
'member_id' => ( isset($_POST['member_id']) ? $_POST['member_id'] : (isset($member->member_id ) ? $member->member_id : '') )
);
I know about set_value() but looks like that wont add in the current data when editing a form, so have not used that.
You can allways make function for it.
function get_value_or_default($array, $key, $default) {
return isset($array[$key] ? $array[$key] :
isset($default) ? $default : '';
}
Or even better:
function update_from_post($object) {
$data = array();
foreach ($object as $prop_name => value) {
$value = get_value_or_default($_POST, $prop_name, $object->{$prop_name});
$data[$prop_name] = $value;
}
Assuming you have different methods in the controller for create vs edit: (you can use the same view in different methods by specifying it in $this->load->view()):
Your create method would assume it was new, and always read the $_POST variables (if $_POST)
Your edit method would first load the object from the database, and then overwrite with $_POST variables if present.
Finally, CodeIgniter has the input helper:
$this->input->post('field_name');
returns false if that field is not in $_POST.
To use your code above:
create
$data = array(
'member_id' => $this->input->post('member_id') ? $this->input->post('member_id') : '')
);
edit
$data = array(
'member_id' => $this->input->post('member_id') ? $this->input->post('member_id') : $member->member_id )
);

Alternative to uri segment on codeigniter?

Im getting the id from a form using
$id=$this->uri->segment(2)
I need your help to replace that by something else generic without getting the id from the url
im using pagination in several pages too & im using
$this->uri->segment()
too ..
PS:when i want to display a list o programs that belong to a category & when i want to display the list of programs of channel that belong to a category ,the segment change so i wont use the segment i wanna get the id dynamically ..
Best regards
Don't know if this is what you are after, but you can send parameters in a standard codeigniter install with URI and catch them in the method params.
class Welcome extends MY_Controller {
public function index($a = NULL, $b = NULL, $c= NULL)
{
echo 'Testing params: <br>
Param1: ' . $a . '<br>Param2: ' . $b . '<br>Param3: ' . $c;
}
}
Calling http://example.com/index.php/welcome/index/test1/test2/test3 will render
Testing params:
Param1: test1
Param2: test2
Param3: test3
Example
Going by this comment code:
foreach $rows as $row .. .. id; ?>"
href="program/details_program/id; ?>"> program_title; ?> page detail.php $id
= $this->uri->segment(2); $query = $this->db->get_where('programs', array('id' => $id));
it looks like you have a program controller with a details_program method that you are linking to and this is where you want a different solution to uri->segment. If this is the case, something like this would work with a URL like http://example.com/program/program_details/123:
class Program extends CI_Controller{
function program_details($id = NULL){
$query = $this->db->get_where('programs', array('id' => $id));
}
}
If this isn't what you want, please update the question with all of the relevant (properly formatted) code

Code Igniter - form_dropdown selecting correct value from the database

Im having a few problems with the form_dropdown function in CodeIgniter .... My application is in 2 parts, a user goes in, enters a form and submits it .... once its submitted a admin can go in and edit that persons form and then save it to database.
So, to display the dropdown in the initial form, im using the following ( all the options in the dropdown are coming from the database )
Model:
function get_salaries_dropdown()
{
$this->db->from($this->table_name);
$this->db->order_by('id');
$result = $this->db->get();
$return = array();
if($result->num_rows() > 0){
$return[''] = 'please select';
foreach($result->result_array() as $row){
$return[$row['id']] = $row['salaryrange'];
}
}
return $return;
}
Then in the Controller:
$data['salaries'] = $this->salary_expectation->get_salaries_dropdown();
Then finally the View:
<?php echo form_dropdown('salaries', $salaries, set_value('salaries', $salaries)); ?>
That bit works perfect in displaying the dropdown filled with values for the user to select.
So, when the user selects a value, then hits save, its saved to the database.
On the Edit page which the admin see's, im using the same code to display the dropdown filled with options, but how do i get it to automatically choose the one thats been selected by the user in the initial stage?
Cheers,
According to Codeigniter documentation
The first parameter will contain the
name of the field, the second
parameter will contain an associative
array of options, and the third
parameter will contain the value you
wish to be selected. You can also pass
an array of multiple items through the
third parameter, and CodeIgniter will
create a multiple select for you.
Your admin controller should have something like
$data['selected'] = $this->salary_expectation->get_salary_selected();
According to this, the admin view should be like this
<?php echo form_dropdown('salaries', $salaries, $selected_value); ?>
one nasty solution to select the <option> element of <select> generated by form_dropdown() function of the form_helper is using the post input sended.
I made this because any solutions I found doesn't display the value that the user select in the form neither set_selected nor set_vaule.
Well, in my controller I have:
$countries = $this->country_model->get_dropdown_array(); // The array have something like $countries[COUNTRY_ID] = COUNTRY_NAME
$data['countries']=$countries;
In my view:
$selected_country = $this->input->post('country');
echo form_dropdown('country',$countries,$selected_country);
And works fine !!! :)
The form_dropdown function has a third parameter for the selected option. Use it like this:
<?php echo form_dropdown('piece_type',
array(
'type1' => 'Firts type',
'type2' => 'Second option'
$selected_value,
'id = "piece_type"') ?>
I had the same problem but i have overcome on this problem using code igniter syntex.
Here is the solution.
Fisrt step
Before the loop initialize two arrays
$options = array();
$select = array();
Then in the loop write this instruction
foreach($result->result_array() as $row)
{
/////////Your Condition ////////////
if($row['id'] == $myarray['mycolumn'])
{
$options [$row['id']] = $row['salaryrange'];
$select = $row['id'] ;
}else{
$options [$row['id']] = $row['salaryrange'];
}
}
Now
echo form_dropdown('dropdown_name' , $options , $select);
It is working ok
For update case, you have pass corresponding value to view, if passing variable is like $ind_post(from controller ) then write this code like:
<?php echo form_dropdown('salaries', $salaries, $ind_post->salaries,''); ?>

Resources