CodeIgniter view problem - codeigniter

This is my first day playing with CI and I really like it so far but have a problem I can't solve on my own. The issue is that I need to generate single view with two controller functions. One div should include selected row by ID from table A and other div should loop foreach on array from table B.
public function index()//div A
{
$data['query'] = $this->db->get_where('beer', array('id' => 1));
$this->load->view('corp/corp_view', $data);
}
public function loadList() //div B
{
$data['q'] = $this->db->get_where('list', array('id' => 1));
$this->load->view('corp/mentor_list_view', $data);
}
I tried to solve this for few hours by creating another view for loadList() and then including it in the the main view like "$this->load->view()" but I'm getting the values from the index() function query table 'beer' not 'list' table as intended. Again I'm new to this and would appreciate your help.
Thank you for your help.

Thanks for the extra info, i can help yah out now.
In Codeigniter if you want to make a function that is not able to be called by the user just precede it with a '_'. So in your case:
public function index()//div A
{
$data['query'] = $this->db->get_where('beer', array('id' => 1));
$data['query2'] = $this->_mySecondQuery();
$this->load->view('corp/corp_view', $data);
}
public function _mySecondQuery() //div B
{
return $this->db->get_where('list', array('id' => 1));
}
Now in the index page you have access to both queries. Btw, I would not suggest doing much DB work in the controller. DB work is meant to be done in Models. For more info on those see: Codeigniter Models

Related

Prestashop 1.7.5 module - Update SQL custom table

I'm trying to add a new module to my shop to manage the sql table's values that I made. I can't find a proper guide that show me how to do that because all the forms have values contained in ps_configuration and not within a custom table.
How can I show those values in my form and get to update them?
Thank you if you'll take the time to answer that ^^
So, my form fields are still blank and they don't update my table when I submit.
I added this to "_construct" function:
public function __construct() {
$this->id_sell = $id_sell;
$this->country = $country;
$this->p_cod = $p_cod;
and this to "getContent"
public function getContent() {
$sqlV = 'SELECT * FROM `'._DB_PREFIX_.'mytable` WHERE id_sell = 1';
if ($row = Db::getInstance()->getRow($sqlV))
$country = $row[country];
$p_cod = $row[p_cod];
and last this on "getConfigFormValues":
protected function getConfigFormValues()
{
return array(
'country' => Tools::getValue('country'),
'p_cod' => Tools::getValue('p_cod'),
);
}
So, now I know that a need a class ObjectModel {}, too. Working on it.. and hoping for the best :D

Elasticsearch Implementation on Cakephp 3

I was able to incorporate elasticsearch on cakephp 3 but I lost the ability to save on my database. The add method on the controller creates an index instead of doing both (save to table and create an index in elasticsearch). Followed the instructions from here - https://book.cakephp.org/3.0/en/elasticsearch.html.
Any idea on how I can achieve the above?
Thanks.
Adding code from Categories Controller.
public function add()
{
$category = $this->Categories->newEntity();
if ($this->request->is('post')) {
$category = $this->Categories->patchEntity($category, $this->request->getData());
if ($this->Categories->save($category)) {
$this->Flash->success(__('The category has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The category could not be saved. Please, try again.'));
}
$this->set(compact('category'));
$this->set('_serialize', ['category']);
}

Pagination is not working properly in Codeigniter

I am novice in Codeigniter and I am working on pagination. But there is something wrong in my code. What mistake I have been done in the following code?
I have attached my code snippet below.
Route
$route['default_controller'] = "pages";
$route['default_controller/(:num)'] = "pages/index/$1";
Controller
public function index($page='home')
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$config=[
'base_url' => base_url().'/pages/index',
'per_page' => 5,
'total_rows' =>$this->products_model->record_count()
];
$this->pagination->initialize($config);
$data['title'] = ucfirst($page); // Capitalize the first letter
$data['products'] = $this->products_model->get_products($config['per_page'], $this->uri->segment(3));
$data['request'] = 'pages/home';
$this->load->view('templates/content',$data);
}
Model
public function get_products($limit, $offset)
{
$this->db->order_by('id', 'DESC');
$this->db->limit($limit, $offset);
$query=$this->db->get('product');
return $query->result_array();
}
public function record_count($value='')
{
return $this->db->count_all("product");
}
View
<?php echo $this->pagination->create_links() ?>
In the View, I am displaying the records from a table. The first page of records displays, but when I click the second page it shows Error
"**404 Page Not Found**"
Any kind of help regarding this issue will be highly appreciated. Thanks in advance.
From your logic, the line "index($page='home') the index method gets the script to display from the parameter passed to it. On the first load, the default page, 'home.php' is matched, but when you select the second page, the CI router and index function look for maybe '1.php' or '2.php' and so on, depending on your paging, do those files exist for every page you request? Otherwise, you should instead check if the page variable is a valid page (e.g non-negative) then continue execution and load the results to your view

Optimizing code with chunk or cursor in laravel

I'm having Company Model and Contact Model defined in my Laravel 5.4 application, both of them have many to many relationship. So, for example contacts model has:
public function company()
{
return $this
->belongsToMany('App\Company', 'company_contact','contact_id', 'company_id')->withTimestamps();
}
Now I've a data set where I want to pull all contacts data and there company details so I was using:
public function getData()
{
$contacts = Contact::all();
foreach($contacts as $contact)
{
$getCompany = $contact->company()->withPivot('created_at')->orderBy('pivot_created_at', 'desc')->first();
$getCompany->contacts = Company::find($getCompany->id)->contacts;
$contact->company = $getCompany;
$contact->companies_interested = json_decode($contact->companies_interested);
$companies = [];
if($contact->companies_interested)
{
foreach($contact->companies_interested as $companiesInterested)
{
$getCompany = Company::withTrashed()->find($companiesInterested);
$companies[] = array(
'value' => $getCompany->id,
'label' => $getCompany->name
);
}
$contact->companies_interested = json_encode($companies);
}
}
return response()->json([
'model' => $contacts
], 200);
}
This works perfectly fine for small data set, but while using large number of data it fails (approx 10,000 fields), I guess php memory fails to load when it comes to large data set. I was going through Laravel docs to find out the solution and came to know about chunk() and cursor() methods, Can someone guide me what can be done in this problem or what can be the approach to overcome this.
Thanks
I recommend you to test both methods for some quirkiness of your system.
Chunk:
It will "paginate" your query, this way you use less memory.
Uses less memory
It takes longer
`
public function getData() {
Contact::chunk(1000, function ($contacts) {
foreach ($contacts as $contact) {
//rest of your code...
}
});
}
`
Cursor:
You will use PHP Generators to search your query items one by one.
It takes less time
Uses more memory
`
public function getData() {
foreach (Contact::cursor() as $contact) {
//rest of your code...
}
}
`
For a more detailed explanation see this answer: What is the difference between laravel cursor and laravel chunk method?
For performance testing see this post: https://translate.google.com/translate?hl=en&sl=auto&tl=en&u=http%3A%2F%2Fqiita.com%2Fryo511%2Fitems%2Febcd1c1b2ad5addc5c9d

Laravel 4 Framework - Query scope not working

Im a big 'ol newbie at Laravel, and im trying to do a query scope but it doesnt seem to be working, i keep getting this error
Argument 1 passed to Letters::scopeForUser() must be an instance of User
My user IS logged in, but it still doesnt seem to be working.
This is my Letters model
<?php
class Letters extends Eloquent {
protected $table = 'letters';
public function scopeForUser(User $u)
{
return $query->where('userid', '=', $u->id);
}
}
and in my controller i have the following
Route::get('myletters', array(
'before' => 'auth|userdetail',
function()
{
// Grab the letters, if any, for this user
$letters = Letters::forUser(Auth::user())->get();
$data = [
'letters' => $letters
];
return View::make('myletters', $data);
}
));
Any help would be greatly appreciated.
Cheers
You should pass a variable $query as the first argument to your method in the Model. For example:
public function scopeForUser($query, User $u)
{
return $query->where('userid', '=', $u->id);
}
The first argument doesn't necessarily need to be $query, but it should be the same variable that you are using inside the scope method ($query in this case).

Resources