I tried to make a function in the CodeIgniter framework so users can search products from my database and display (echo) them if a certain product is found.
The problem is if I try to search for a keyword I see a blank page and nothing happens.
Search controller:
<?php
class SearchController extends CI_Controller {
public function index(){
$data = array();
//load view
$this->load->view('result_view',$data);
}
function search_keyword()
{
$this->load->model('Search_model');
$keyword = $this->input->post('keyword');
$data['results'] = $this->Search_model->search($keyword);
}
}
My search model:
<?php
class Search_model extends CI_Model {
function search($keyword) {
$this->db->select('product_naam');
$this->db->like('product_naam',$keyword);
$query = $this->db->get('products');
return $query->result();
}
}
?>
My view file:
<h2>Zoek een cadeau</h2><br>
<form class="navbar-form" role="search" action="<?= base_url('SearchController/search_keyword')?>" method = "post">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name = "keyword" size="30px; ">
<div class="input-group-btn">
<button class="btn btn-default " type="submit" value = "Search"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
</center>
<div class="clearfix"></div>
Change
return $query->result();
to
return $query->result_array();
Also, you are not calling result page in function and passing data in view.
function search_keyword()
{
$this->load->model('Search_model');
$keyword = $this->input->post('keyword');
$data['results'] = $this->Search_model->search($keyword);
$this->load->view('result_view',$data);
}
Related
i have a problem in updating data in codeigniter. There's no error message but The data cannot be updated when i change the data name and click submit the button.
I'm new in CI so i cant really figure out where did i do wrong. can you help me?
thankyou in advance
my view (views/form/form_edit_bank.php)
<form action="<?php echo site_url('bankdatel/updatebank/'.$row_bank['idbank']);?>" method="post">
<div class="box-body">
<div class="form-group">
<label>Nama Bank: </label>
<input type="text" name="namabank" value="<?php echo $row_bank['namabank']?>">
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Ubah</button>
</div>
</form>
controller (controllers/bankdatel.php)
public function editbank(){
$this->load->model('model_bankdatel');
$data['row_bank'] = $this->model_bankdatel->selectbank($this->uri->segment(2));
$this->load->view('template/header');
$this->load->view('form/form_edit_bank', $data);
$this->load->view('template/footer');
}
public function updatebank(){
$arrdata = array(
'namabank' => $this->input->post('namabank')
);
$this->load->model('model_bankdatel');
$this->model_bankdatel->updatebank($arrdata, $this->uri->segment(3));
$this->session->set_flashdata('berkas', "<script>alert('Nama Bank Berhasil Diubah');</script>");
redirect('bankdatel');
}
model (model/model_bankdatel.php)
public function updatebank($data, $id){
$this->db->where('idbank', $id);
$this->db->update('mt_bank', $data);
}
public function selectbank($id)
{
$this->db->where('idbank', $id);
return $this->db->get('mt_bank')->row();
}
Your form URL is site_url('bankdatel/updatebank/'.$row_bank['idbank']). It means 1 argument is passing to function. So get $bankid like this
public function updatebank($bankid){
$arrdata = array(
'namabank' => $this->input->post('namabank')
);
$this->load->model('model_bankdatel');
$this->model_bankdatel->updatebank($arrdata, $bankid);
$this->session->set_flashdata('berkas', "<script>alert('Nama Bank Berhasil Diubah');</script>");
redirect('bankdatel');
}
And use return with update query in the model
return $this->db->update('mt_bank', $data);
I have news details inserted and i need to show it on the edit page but when i try to edit and delete it shows blanks page insert and show is working properly. i have been stucked on this from morning. id is getting from the database but it shows a blank page,Not using any Form helper
1.what's problem,is it on route file
2.is it on Controller file
route.php
Route::get('/', function () {
return view('welcome');
});
Route::resource('books','BookController');
Route::resource('news','NewsController');
Auth::routes();
Route::get('/news','NewsController#index')->name('news');
//Route::get('/news/create','NewsController#create');
//Route::get('/news/edit','NewsController#edit');
Edit.blade.php
#extends('theme.default')
#section('content')
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
NEW BEE NEWS DETAILS
</div>
<div class="panel-body">
<div class="row">
<div class="col-sm-12">
<form method="post" action="{{route('news.update',[$news->id])}}"
enctype="multipart/form-data">
{{csrf_field()}}
<input type="hidden" name="_method" value="put">
<div class="form-group">
<label>NEWS TITLE</label>
<input type="text" name="atitle" id="atitle" class="form-control"
placeholder="PLEASE ADD TITLE OF NEWS" value="{{$news->name}}">
<p class="help-block">Example: SELFY PLAYSHARE </p>
</div>
<div class="form-group">
<label>NEWS</label>
<textarea name="news" id="news" class="form-control" {{$news->news}}></textarea>
<p class="help-block">DETAILED NEWS HERE</p>
</div>
<div class="form-group">
<label>NEWS LINK</label>
<input type="text" name="alink" id="alink" class="form-control"
placeholder="PLEASE ADD LINK OF NEWS" value="{{$news->alink}}">
<p class="help-block">Example: https://play.google.com/store/apps/selfyplusure</p>
</div>
<div class="form-group">
<label>NEWS IMAGE</label>
<input type="file" name="addimage" id="addimage" value="{{$news->imagename}}">
</div>
<button type="submit" class="btn btn-default">ADD NEWS</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
NewsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use App\News;
use Illuminate\Http\Request;
class NewsController extends Controller
{
public function index()
{
$news = News::all();
return view('news.index', ['news' => $news]);
}
public function create()
{
return view('news.create');
}
public function store(Request $request)
{
$news=new News();
if($request->hasFile('addimage')){
$request->file('addimage');
$imagename=$request->addimage->store('public\newsimage');
$news->name = $request->input('atitle');
$news->alink = $request->input('alink');
$news->news = $request->input('news');
$news->imagename = $imagename;
$news->save();
if($news) {
return $this->index();
} }
else{
return back()->withInput()->with('error', 'Error Creating News ');
}
}
public function show(News $news)
{
//
}
public function edit(News $news)
{
$news=News::findOrFail($news->id);
return view('news.edit',['News'=>$news]);
}
public function update(Request $request, $id)
{
$news = News::findOrFail($id);
// update status as 1
$news->status = '1';
$news->save();
if ($news) {
// insert datas as new records
$newss = new News();
//On left field name in DB and on right field name in Form/view
$newss->name = $request->input('atitle');
$newss->alink = $request->input('alink');
$newss->news = $request->input('news');
$newss->imagename = $request->input('addimage');
$newss->save();
if ($newss) {
return $this->index();
}
}
}
public function destroy($id)
{
$news = News::findOrFail($id);
$news->status = '-1';
$news->save();
if ($news) {
return $this->index();
}
else{
return $this->index();
}
}
}
Link to delete and Edit
<td><input type="button" name="edit" value="EDIT">
<td><input type="button" name="delete" value="DELETE"></td>
This is the link to edit
<td><input type="button" name="edit" value="EDIT">
For delete please go through
Delete
In your controller try to use this.
return view('news.edit',compact('news'));
I tried to make a search bar so users will be able to search products by searching on the product name but its not working and I'm not able to search a product.
This is my controller function:
<?php
class SearchController extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('Search_model');
}
public function index(){
$data = array();
}
function search_keyword(){
$keyword = $this->input->post('keyword');
$data['results'] = $this->Search_model->search($keyword);
//laad de view
$this->load->view('result_view',$data);
}
}
?>
This is my model function:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Search_model extends CI_Model {
function __construct(){
parent::__construct();
}
function search($keyword) {
$this->db->select('product_naam');
$this->db->like('product_naam',$keyword);
$query = $this->db->get('products');
return $query->result_array();
}
}
?>
And this is my view file with searchbar:
<?php include_once('templates/header.php'); ?>
<center>
<h2>Zoek een cadeau</h2><br>
<form class="navbar-form" role="search" action="<?php echo base_url(); ?>SearchController/search_keyword" method="POST">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="keyword" size="30px; ">
<div class="input-group-btn">
<button class="btn btn-default " type="submit" value = "Search"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
</center>
<div class="clearfix"></div>
<table>
<?php foreach($results as $row){ ?>
<tr>
<td><?php echo $row->product_naam?></td>
</tr>
<?php } ?>
</table>
<?php include_once('templates/footer.php'); ?>
I hope someone can help me with this problem!,
thanks
I don't think like() will help you much with your particular needs. Your best bet, I guess, would be to bypass the problem and use a where function containing your LIKE clause:
$this->db->select('product_naam')->from('products')
$this->db->where("product_naam LIKE '%$keyword%'")->get();
Or if you want pure ci you have to change your 'like' line :
$this->db->like('product_naam',$keyword);
For %$query you can use
$this->db->like('product_naam',$keyword,'before');
and for $query% you can use
$this->db->like('product_naam',$keyword,'after');
Because you use $query->result_array();, items should be taken by
<td><?php echo $row['product_naam']?></td>
result_array()
This method returns the query result as a pure array, or an empty
array when no result is produced
CI documentation
I want to create simple search function. I follow some example. but I was unable to get results. please help me.
//view page
<form class="navbar-form" role="search" action=" {{ base_url }}search/search_keyword" method = "post">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name = "keyword"size="15px; ">
<div class="input-group-btn">
<button class="btn btn-default " type="submit" value = "Search"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
//controller
function search_keyword()
{
$keyword = $this->input->post('keyword');
$data['results'] = $this->mymodel->search($keyword);
$this->twig->display('result_view.php',$this->data);
//$this->load->view('result_view.php',$data);
}
}
//model
function search($keyword)
{
$this->db->like('item_name',$keyword);
$query = $this->db->get('bracelets');
return $query->result();
}
Change
$this->twig->display('result_view.php',$this->data);
TO
$this->load->view('result_view.php',$data);
Use This Function
function search_keyword()
{
$keyword=$this->input->post('keyword');
$data['results']=$this->mymodel->search($keyword);
$this->twig->display('result_view.php',$data);
}
I have a comment box in my app and its working fine. but i want to display the user who comment in my post how can i achieve this? i have this code so far.
This is my view
<div class="view-post">
<div class="body">
<h3>{{$posts->title}}</h3>
<h6>{{$posts->created_at->toFormattedDateString()}}</h6>
<p><img src="{{ asset('img/' . $posts->image) }}" class="img-rounded"/></p>
<p>{{$posts->content}}</p>
</div>
<hr>
<section>
<h5>LEAVE US A COMMENT</h5>
<form action="{{ URL::route('createComment', array('id' => $posts->id))}}" method="post">
<div class="form-group">
<input class="form-control" type="text" placeholder="Comment..." name="content">
</div>
<input type="submit" class="btn btn-default" />
</form>
</section><br>
<section class="comments">
#foreach($posts->comment as $comments)
<blockquote>{{$comments->content}}</blockquote>
#endforeach
</section>
</div>
My Controller
public function viewPost($id)
{
$post = Post::find($id);
$user = Auth::user();
$this->layout->content = View::make('interface.viewPost')->with('posts', $post )->with('users',$user);
}
public function createComment($id)
{
$post = Post::find($id);
$comment = new Comment();
$comment->content = nl2br(Input::get('content'));
$post->comment()->save($comment);
return Redirect::route('viewPost', array('id' => $post->id));
}
In your model you can set up relations between one and another model.
Like that..
User Model
class User extends Model {
public function comments()
{
return $this->hasMany('App\Comment');
}
}
Comment Model
class Comment extends Model {
public function user()
{
return $this->belongsTo('App\User');
}
}
So you can get the user with
$comment = Comment::find(1);
$user = $comment->user()->get();