how to display record by id in codeigniter - codeigniter

I wanto to display blog detail from database by clicking on specific blog item.I have created a controller and modal for that but no idea hot o display data by id in detail page.
Controller-
function blogContent(){
$data['info'] = $this->auth_model->blogDetails($id);
$popular['popular'] = $this->auth_model->getPopularcourses();
$this->load->view('nulearnFront/blog_details', $data);
$this->load->view('nulearnFront/header', $data);
$this->load->view('nulearnFront/footer', $popular);
}
Modal-
public function blogDetails($id) {
$q = $this->db->select('*')->from('blog')->where('id',$id)->get();
return $q->result();
}
but i've no idea how to display data by id on frontend. please check the above code also.

You can add a route for the controller in the config/routes.php file like this:
$route['blog/(:any)'] = 'controller/blogContent/$1';
and modified controller
function blogContent($id){
$data['info'] = $this->auth_model->blogDetails($id);
$popular['popular'] = $this->auth_model->getPopularcourses();
$this->load->view('nulearnFront/blog_details', $data);
$this->load->view('nulearnFront/header', $data);
$this->load->view('nulearnFront/footer', $popular);
}
after that, you should only create a correct link to a blog, like this:
My Blog
and you should get blog id in your controller

Related

Reopening page on Codeigniter

I am trying to reopen same page on Codeigniter. See code below:
<?php
class Leads extends CI_Controller
{
public $tempName;
public $tempFrom;
public $tempTo;
public function index() {
$this->load->model('Lead');
$leads = $this->Lead->getAllLeads();
$pageNum = count($leads);
$this->load->view('main', array('leads' => $leads, 'pageNum' => $pageNum));
}
public function getLeads(){
$this->load->model('Lead');
$name = $this->input->post('name');
$from = $this->input->post('from');
$to = $this->input->post('to');
$leads = $this->Lead->getLeads($name, $from, $to);
$pageNum = count($leads);
$this->load->view('main', array('leads' => $leads, 'pageNum' => $pageNum));
}
}
?>
As you can see, first I open 'main' from 'index()'. In main, I call 'getLeads()', but my page is not getting updated. Is it because I can't refresh data while on the same page? If yes, how can I work around it?
Thanks a lot!
You cannot call a controller from a view. It sounds like you probably should be using AJAX in your "main" view to get additional data from getLeads().
Option A:
redirect with js to the same function you have, but change the method to get (send params in the URL and get them on the server with $this->input->get()).
Option B:
Since you are posting to getLeads I assume you are using ajax. And since you are using ajax, you should return an url or data to render on your website. Maybe you could use $this->session->set_flashdata('leads',$leads) and $this->session->set_flashdata('pageNum',$pageNum) , send an url to another controller function, extract leads and pageNum $this->session->flashdata('leads') & $this->session->flashdata('pageNum') and then render the view?
Responding with load view will not automatically set the html to what you get.

dont find the view in joomla

Hi everyone I am using joomla 2.5 for a website
I create a component to insert products in the data base in the back-end, and I want to show this product in front-end
I have this link in the view to show a products with type=1;
<div class="col2">Productos</div>
In the front-end of the component controller.php I have this
class HardwareController extends JController
function integrados(){
$model = &$this->getModel(JRequest::getCmd('view'));
$view = &$this->getView(JRequest::getCmd('view'), 'html');
$view->setModel($model, true);
$view->hardwareIntegrado();
}
in my model I have
class HardwareModelHardwares extends JModelList
function getIntegrados(){
$db=& JFactory::getDBO();
$query= "SELECT *
FROM ".$db->nameQuote('#__hardware')."
WHERE ".$db->nameQuote('tipo')."=".$db->quote("1").";";
$db->setQuery( $query);
$restaurantes=$db->loadObjectList();
JRequest::setVar('hard', $restaurantes);
return $restaurantes;
}
and in my view.html.php I have this
public function hardwareIntegrado(){
$this->assignRef('pagination', $this->get('pagination'));
$this->assignRef('hardware', $this->get('integrados'));
$this->assignRef('list', $this->get('list'));
parent::display();
}
When I click to the link I obtain this error
500 - View not found [name, type, prefix]: hardware, html, hardwareView
any idea!
You should change your view class to hardwareViewHardware
You are getting this error-
500 - View not found [name, type, prefix]: hardware, html, hardwareView
because it's looking for hardware view.In the link- index.php/hardware/integrados it seems you are passing view=hardware . Whereas everywhere you have defined it as hardwares like in folder name and class.Either change link or class and folder.
Hope this will help.

View Same user_id

//Anyone can help to create a view data with same id? it is a multiple viewing.
this is my Controller. i dont khow apply in Model and View
function Get_Pitch($id){
$this->load->model('users_model');
$data['query'] = $id;
$this->load->view('view_pitch', $data);
}
Example this is my url "http://localhost/SMS_System/home/sample/102"
in my database is
id=1 name=erwin user_id=102
id=2 name=flores user_id=102
id=3 name=sample user_id=202
how to view the same user_id?
First of all with what you've supplied your URL won't work, you aren't following the normal conventions for CI so it won't know where to look. I am assuming your controller is called sample then you need to tell the application which function you're calling in that controller, finally URL names should be lower case so I changed that, so your URL should read:
"http://localhost/SMS_System/home/sample/get_pitch/102"
Also you need to get your data from a model, you loaded the model then didn't use it. The line after loading the model calls a function from that model and passes it the id you got from your url. Notice the if not isset on the id, this ensures that if someone goes to that page without the id segment there are no errors thrown from the model having a missing parameter, it will just return nothing, that is handled in the view.
Controller:
function get_pitch($id){
//the following line gets the id based on the segment it's in in the URL
$id=$this->uri_segment(3);
if(!isset($id))
{
$id = 0;
}
$this->load->model('users_model');
$data['query'] = $this->users_model->getUserData($id);
$this->load->view('view_pitch', $data);
}
Your model takes the id passed from the controller and uses that to retrieve the data from the database. I normally create the array I am going to return as an empty array and handle that in the view, this makes sure you get no errors if the query fails. The data then returns to the controller in the last line and is passed to the view in your load view call.
Model:
function getUserData($id)
{
$this->db->where('id',$id);
$result = $this->db->get('users') //assuming the table is named users
$data = array(); //create empty array so we aren't returning nothing if the query fails
if ($result->num_rows()==1) //only return data if we get only one result
{
$data = $result->result_array();
}
return $data;
}
Your view then takes the data it received from the model via the controller and displays it if present, if the data is not present it displays an error stating the user does not exist.
View:
if(isset($query['id']))
{
echo $query['id']; //the variable is the array we created inside the $data variable in the controller.
echo $query['name'];
echo $query['user_id'];
} else {
echo 'That user does not exist';
}

Magento: How to Print backend invoices like frontend as HTML?

I'm on Magento 1.7.0.2. How can I print invoices from backend with the same manner that frontend uses? I want it to be on HTML format not PDF.
Assuming that you want to print one invoice at a time from the admin order detail page
Create a custom admin module
Add a controller with the method below
public function printInvoiceAction()
{
$invoiceId = (int) $this->getRequest()->getParam('invoice_id');
if ($invoiceId) {
$invoice = Mage::getModel('sales/order_invoice')->load($invoiceId);
$order = $invoice->getOrder();
} else {
$order = Mage::registry('current_order');
}
if (isset($invoice)) {
Mage::register('current_invoice', $invoice);
}
$this->loadLayout('print');
$this->renderLayout();
}
Reference printInvoiceAction() in app/code/core/Mage/Sales/controllers/GuestController.php
Then in your custom layout.xml use <sales_guest_printinvoice> in /app/design/frontend/base/default/layout/sales.xml as your template
Then add a button with link to the following url (need to get invoice id from order) /customModule/controller/printInvoice/invoice_id/xxx
(Not tested, so let me know if you run into any issues)
You should create your custom css file for printing print.css. And you should add "Print Button", that will call window.print()

CodeIgniter - showing original URL of index function?

I'm not sure if I'm approaching this fundamentally wrong or if I'm just missing something.
I have a controller and within it an index function that is, obviously, the default loaded when that controller is called:
function index($showMessage = false) {
$currentEmployee = $this->getCurrentEmployee();
$data['currentEmp'] = $currentEmployee;
$data['callList'] = $currentEmployee->getDirectReports();
$data['showMessage'] = $showMessage;
$this->load->view('main', $data);
}
I have another function within that controller that does a bulk update. After the updates are complete, I want the original page to show again with the message showing, so I tried this:
/**
* Will save all employee information and return to the call sheet page
*/
function bulkSave() {
//update each employee
for ($x = 0; $x < sizeof($_POST['id']); $x++) {
$success = Employee::updateEmployeeManualData($_POST['id'][$x], $_POST['ext'][$x], $_POST['pager'][$x], $_POST['cell'][$x], $_POST['other'][$x], $_POST['notes'][$x]);
}
$this->index($success);
}
What is happening is that the original page is accessed using:
localhost/myApp/myController
after the bulk update it is showing as:
localhost/myApp/myController/bulkSave
when I really want it to show the url as the index page again, meaning that the user never really sees the /bulkSave portion of the URL. This would also mean that if the user were to refresh the page it would call the index() function in the controller and not the bulkSave() function.
Thanks in advance.
Is this possible?
You are calling your index() funciton directly, within bulkUpdate() hence the uri does not change back to index because you are not making a new server request, you are just navigating within your controller class.
I usually use the same Controller function for tasks like this, directing traffic based on whether or not $_POST data has been passed or not like this...
function index() {
if($_POST) {
//process posted data
for ($x = 0; $x < sizeof($_POST['id']); $x++) {
$data['showMessage'] = Employee::updateEmployeeManualData($_POST['id'][$x], $_POST['ext'][$x], $_POST['pager'][$x], $_POST['cell'][$x], $_POST['other'][$x], $_POST['notes'][$x]);
}
}
else {
//show page normally
$data['showMessage'] = FALSE;
}
//continue to load page
$currentEmployee = $this->getCurrentEmployee();
$data['currentEmp'] = $currentEmployee;
$data['callList'] = $currentEmployee->getDirectReports();
$this->load->view('main', $data);
}
Then if it is a form that you are submitting, just point the form at itself in your view like this...
<?= form_open($this->uri->uri_string()) ?>
This points the form back at index, and because you are posting form data via $_POST it will process the data.
I usually do a redirect to the previous page as it prevent users to refresh (and submit twice) their data.
You can use the redirect() helper function of CI.
http://codeigniter.com/user_guide/helpers/url_helper.html (at the bottom)

Resources