I just started learning OpenCart, currently using version 2.3.0.2 .
I created a module, everything works fine on the backend.
On the frontend however, when I return the template from the controller it shows up blank.
But if I add a die(); in the template it loads the template.
Controller code:
<?php
class ControllerExtensionModuleHelloworld extends Controller {
public function index() {
$this->load->language('extension/module/helloworld');
$data['heading_title'] = $this->language->get('heading_title');
$data['helloworld_value'] = html_entity_decode($this->config->get('helloworld_text_field'));
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/extension/module/helloworld.tpl')) {
// print_r(__LINE__);
return $this->load->view($this->config->get('config_template') . '/template/extension/module/helloworld.tpl', $data);
} else {
// print_r(__LINE__);
return $this->load->view('extension/module/helloworld.tpl');
}
}
}
Template code:
<div class="panel panel-default">
<div class="panel-heading"> <?php echo $heading_title; ?> </div>
<div class="panel-content" style="text-align: center;"> <?php echo $helloworld_value; ?> </div>
<div style="height:100px;width:100px;background-color:blue;"></div>
</div>
Fixed it by changing:
return $this->load->view('extension/module/helloworld.tpl', $data);
To:
$this->response->setOutput($this->load->view('extension/module/helloworld.tpl', $data));
Related
Controller:
public function latestnews()
{
$data['news'] = $this->New_model->getById($id);
$this->load->view('news',$data);
}
Model:
public function getById($id)
{
return $this->db->get_where($this->_table, ["new_id" => $id])->row();
}
View:
<?php
if (isset($news) and $news) {
foreach($news as $new) {
?>
<div class="col-sm-12">
<div class="section">
<img src="<?php echo site_url('uploads/'.$new->image); ?>" />
</div>
<p><?php echo $new->description;?> </p>
</div>
<?php
}
}
?>
How and where to define the variable id?
While clicking a dynamic image,it should be opened in another page containing details but instead showing Undefined variable: id
In the latestnews() function you have no $id defined.
Try to call the latestnews() function with the right $id parameter.
you can write function like this,
function latestnews($id)
{
Controller:
function single_news($id) { // This $id variable comes from URL Ex: example.com/single_news/15. So $id = 15
$this->data['news_data'] = $this->new_model->getNewsByID($id);
$this->load->view('single-news', $this->data); // Create New View file named single-news.php
}
Model:
function getNewsByID($id) {
$this->db->where('id', $id);
$q = $this->db->get('news');
if($q->num_rows() > 0) {
return $q->row();
}
return false;
}
View: (Single News Page)
<div>
<img src="<?php echo site_url('uploads/'.$news_data->image); ?>" />
</div>
I am trying to call an external function within a class method property. It actually does gets called but at the end of the page and whatever is inside the method property, remains separate.
Since I am a self taught student, it is recent that I have started learning PHP classes so I am not really sure if this can be done or not.
Please guide me how this can be done correctly or if not, then what could be the workaround?
The class I have written is as follows:
It will take the input from user while creating of instance and render a modal box based on the input and options selected.
class modalBox{
private $modal_id, $modal_anim, $header_title, $modal_content,$footer;
private $headerOpt,$titleOpt,$footerOpt,$closeBtn;
public function setID($id){
$this->modal_id = $id;
}
public function getID(){
$modal_id = $this->modal_id;
return $modal_id;
}
public function setTitle($title){
$this->header_title = $title;
}
public function getTitle(){
$title = $this->header_title;
return $title;
}
public function setBodyContent($content){
$this->modal_content = $content;
}
public function getBodyContent(){
$modalContent = $this->modal_content;
return $modalContent;
}
public function setFooterContent($footer){
$this->footer = $footer;
}
public function getFooterContent(){
$footerContent = $this->footer;
return $footerContent;
}
public function initiateModal($modal_anim, $headerOpt, $titleOpt, $closeX, $footerOpt, $footerCloseBtn){ ?>
<div class="modal <?php if($modal_anim != 'false'){echo $modal_anim;} ?>" id="<?php echo $this->getID(); ?>" style="z-index: 2;">
<div class='modal-dialog'>
<div class='modal-content'>
<?php
// display if header option is set to true
if ($headerOpt){
?>
<div class="modal-header">
<h4><?php echo $this->getTitle(); ?></h4>
<?php
// display if close button (X) is set to true
if($closeX){
?> <button type="button" class="close" data-dismiss="modal">×</button> <?php } ?>
</div>
<?php } ?>
<div class="modal-body"><?php echo $this->getBodyContent(); ?></div>
<?php if($footerOpt){ ?>
<div class="modal-footer"><?php echo $this->getFooterContent(); ?>
<?php if($footerCloseBtn){ ?>
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
<?php } ?>
</div>
<?php } ?>
</div>
</div>
</div>
<?php
}
}
?>
The function I am trying to call within property is as follows;
This function is not inside a class. This is present independently in functions.php which I have included in index file.
function getDocNameList() {
global $db;
$getDoc = $db->prepare("SELECT id,doc_name from doctor");
$getDoc->execute();
while($docName = $getDoc->fetch(PDO::FETCH_ASSOC)){
// print the returned rows in options list of <select>
echo "<option value='".$docName['id']."'>".$docName['doc_name']."</option>";
}
}
The initiation of class instance is as follows, Please note where I am calling the function
// create class instance
$rangeModal = new modalBox;
//set the modal id
$rangeModal->setID ("rangeFields");
//set the modal title in header
$rangeModal->setTitle("Select Date Range");
// set the body content
$rangeModal->setBodyContent("
<form method='post' action='expenditure.php'>
<div role='wrapper' class='input-group mb-3'>
<input id='datepicker1' name='exp_date_from' value='From Date' required/>
</div>
<div role='wrapper' class='input-group mb-3'>
<input id='datepicker2' name='exp_date_to' value='To Date' required/>
</div>
<div role='wrapper' class='input-group mb-3'>
<select>" . getDocNameList() . "</select>
</div>
");
//set the footer content
$rangeModal->setFooterContent("
<input type='submit' class='btn btn-success' name='submitRange' />
</form>
");
/*
* #args ---
* modal animation
* modal header (boolean)
* modal title (boolean)
* modal close X (boolean)
* modal footer (boolean)
* modal footer close button (boolean)
*/
// initiate modal
$rangeModal->initiateModal('fade',true,true,true,true,true);
I expect the output of the function to be displayed as .... within the block but instead it gets rendered at the bottom of the page just before tag.
You echo it here, so it will be displayed immediately:
echo "<option value='".$docName['id']."'>".$docName['doc_name']."</option>";
So it is not concatenated here, the function does not return anything:
<select>" . getDocNameList() . "</select>
Build it and return it instead:
$output = '';
while($docName = $getDoc->fetch(PDO::FETCH_ASSOC)){
$output .= "<option value='".$docName['id']."'>".$docName['doc_name']."</option>";
}
return $output;
Or build an array and join the elements:
while($docName = $getDoc->fetch(PDO::FETCH_ASSOC)){
$output[] = "<option value='".$docName['id']."'>".$docName['doc_name']."</option>";
}
return implode($output);
Hi guys i am first time in coding codeigniter and my create CMS upload image and show in slider and my error is backtrace and this is my code View.php
<section class="home-slider owl-carousel">
<?php foreach($data as $row) { ?>
<div class="slider-item" style="background-image: url(); width: auto; background-repeat:no-repeat; background-size: contain; background-position:center;">
<div class="container">
<div class="row slider-text align-items-center">
</div>
</div>
</div>
<?php } ?>
model.php
<?php
class HomeModel extends CI_Model {
function __construct() {
parent::__construct();
}
public function selectAllData() {
$this->db->select("file_name,description");
$this->db->from('tbl_slider');
$query = $this->db->get();
return $query->result();
} }
controller.php
class Home extends CI_Controller {
function __construct() {
parent::__construct();
}
function index(){
$this->load->model('HomeModel');
$data['all_data'] = $this->HomeModel->selectAllData();
$this->templates('home_index', $data);
}
function templates($page) {
$this->load->view('templates/header');
$this->load->view($page);
$this->load->view('templates/navbar');
$this->load->view('templates/footer');
$this->load->view('templates/footer-js');
}
}
thank you for respond
You need to set some height and url of background image as below :
<section class="home-slider owl-carousel">
<?php foreach ($data as $row) { ?>
<div class="slider-item" style="background-image: url(demo.jpg); height: 200px; width: auto; background-repeat:no-repeat; background-size: contain; background-position:center;">
<div class="container">
<div class="row slider-text align-items-center">
</div>
</div>
</div>
<?php } ?>
</section>
I want to create a login with Facebook in my website. I found a code in the internet that made it simple loading the library of Facebook php sdk. I tried the code but it doesn't work in me. Please help me how to do login with facebook in codeigniter.
Here is the code :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once( APPPATH . 'libraries/facebook/src/facebook.php' );
class FacebookApp extends Facebook {
var $ci;
var $facebook;
var $scope;
public function __construct() {
$this->ci =& get_instance();
$this->facebook = new Facebook(array('appId' => $this->ci->config->item('app_id'),'secret' => $this->ci->config->item('app_secret'), 'cookie' => true));
$this->scope = 'public_profile';
}
public function login_url() {
$params = array('scope' => $this->scope);
return $this->facebook->getLoginUrl($params);
}
public function logout_url() {
return $this->facebook->getLogoutUrl(array('next' => base_url() .'logout'));
}
public function getFbObj(){
return $this->facebook;
}
public function get_user() {
$data = array();
$data['fb_user'] = $this->facebook->getUser();
if ($data['fb_user']) {
try {
$data['fb_user_profile'] = $this->facebook->api('/me');
return $data;
} catch (FacebookApiException $e) {
$this->facebook->destroySession();
$fb_login_url = $this->facebook->getLoginUrl(array('scope' => $this->scope));
redirect($fb_login_url, 'refresh');
}
}
}
here is my controller :
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class User_Authentication extends CI_Controller
{
function __construct() {
parent::__construct();
// Load user model
$this->load->model('auth/user_model');
$this->load->library('facebook/FacebookApp');
}
public function index(){
$obj_fb = new FacebookApp();
$fb_user_data = $obj_fb->get_user();
$data['fb_login_url'] = $obj_fb->login_url();
}
}
and here is my view:
<div class="modal fade" id="choose" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header btn-success">
<h3 class="modal-title">Connect with</h3>
</div><!-- modal-header -->
<div class="modal-body">
<div class="connectwith">
<form class="form-horizontal" id="payment">
<button onclick="<?php echo base_url()?>User_authentication" class="btn btn-primary"> Continue with Facebook </button>
</form><!-- form-horizontal -->
</div>
</div><!-- modal-body -->
</div><!-- modal-content -->
</div><!-- modal-dialog -->
</div><!-- choose -->
it shows no error when i check it on my console, i don't know what's happen, i am a beginner in adding libraries. Please help me with this. Thanks
First of all load url helper so you can use base_url().Load helper in controller...
function __construct() {
parent::__construct();
//Load Helper
$this->load->helper('url');
// Load user model
$this->load->model('auth/user_model');
$this->load->library('facebook/FacebookApp');
}
In your view replace
onclick="<?php echo base_url()?>User_authentication"
To
onclick="<?php echo base_url('user_authentication');?>"
Use this github https://github.com/bhawnam193/php-programs/tree/master/facebook-login for using fb login .
Firstly make a facebook app and replace the
$app_id ,$app_secret, $site_url
in file fbaccess.php.
I have create a web application using CodeIgniter making at first a login interface. Here are the controller I used but I think something doesn't work but I don't know what.The home page, where the user is granted, it doesn't show.Unfortunately I didn't have a debugger to check what doesn't work. maybe there is a problem to handle the session but really I don't know what can be. maybe you are smarther than me to find the error
Login Controller
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('User','user'); /* This call the model to retrieve data from db */
}
public function index()
{
if(!file_exists('application/views/_login.php'))
{
show_404();
}
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<h4 style="text-align:center;">','</h4>');
$this->form_validation->set_rules('username','username','trim|required|xss_clean');
$this->form_validation->set_rules('password','password','trim|required|xss_clean|callback_pass_check');
if($this->form_validation->run() == FALSE)
{
/* Data to pass to view */
$data['title'] = "User Access";
$data['author'] = "Salvatore Mazzarino";
$data['year'] = date('Y');
$this->load->view('templates/_header',$data);
$this->load->view('_login',$data);
$this->load->view('templates/_footer',$data);
}
else
{
redirect('home', 'refresh');
}
}
public function pass_check($pass)
{
$result = $this->user->find_user($this->input->post('username'),$pass);
if(!empty($result))
{
foreach ($result as $row)
{
$session_array = array('id'=> $row->id, 'username'=> $row->username); /* Create a session passing user data */
$this->session->set_userdata('logged_in', $session_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('pass_check',"Invalid username or password!</br>Try again, please!");
return FALSE;
}
}
}
/* END OF FILE */
Home Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller
{
public function __construct()
{
parent::__construct();
session_start();
}
public function index()
{
if($this->session->userdata('logged_in'))
{
$data['title'] = "Management Emergency";
$data['author'] = "Salvatore Mazzarino";
$data['year'] = date('Y');
$this->load->view('templates/_header', $data);
$this->load->view('_home',$data);
$this->load->view('templates/_footer',$data);
}
else
{
redirect('login', 'refresh');
}
}
public function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home','refresh');
}
}
/* END OF FILE */
The model in the login controller works very well so It isn't a problem of query. Before adding session everything works but when I added session stopped to worked so I think that can be a problem or redirect() or session
Home View
<div data-role = "page">
<div data-role = "header" data-position = "inline">
<?php echo heading($title,1) ?>
</div>
<div class = "menu-content">
<ul data-role = "listview" data-inset="true">
<li data-role = "list-divider">Emergency Menù</li>
<li class = "menu-item">
<a href="">
<div class = "image-wrapper">
<img src="/assets/images/user.png" class = "ui-li-icons" />
</div>
Add patient
</a>
</li>
<li class = "menu-item">
<a href="#">
<div class = "image-wrapper">
<img src="/assets/images/home.png" class = "ui-li-icons" />
</div>
Show all hospital
</a>
</li>
<li class = "menu-item">
<a href="#">
<div class = "image-wrapper">
<img src="/assets/images/favorite.png" class="ui-li-icons" />
</div>
Find patients
</a>
</li>
<li class="menu-item">
<a href="#">
<div class = "image-wrapper">
<img src="/assets/images/email.png" class="ui-li-icons" />
</div>
Send medical infos
</a>
</li>
</ul>
Login View
<div data-role ="dialog">
<div data-role = "header" data-theme="e">
<?php echo heading($title,1) ?>
</div>
<div data-role ="content">
<?php
$var = validation_errors();
if(!empty($var))
{
echo form_error('username');
echo form_error('password');
}
else
{
echo heading('911 - First Aid',2,'style="text-align:center; color:red;"');
echo form_open('login');
?>
<div data-role ="fieldcontain" class="ui-hide-label">
<label for="username">Username:</label>
<input type="text" name="username" id="name" value="" placeholder="Username"/>
</div>
<div data-role ="fieldcontain" class="ui-hide-label">
<label for="password">Password</label>
<input type="password" name="password" id="password" value="" placeholder="Password"/>
</div>
<div data-role ="fieldcontain">
<input type="submit" value="Login" data-theme ="b"/>
</div>
</form>
<?
}
?>
There is a problem with CodeIgniter retaining it's session data after a redirect. Your if($this->session->userdata('logged_in')) in your home.php controller will evaluate false every time because the session is resetting. You could use the native php sessions to skip over this problem.
See: http://codeigniter.com/wiki/Native_session/. Good luck!
UPDATE
Apparently, this is only true of CodeIgniter 1.7.2 when used with IE6. It doesn't affect most browsers.
open the application/config/autoload.php file and add the 'url' in the helper array;
$autoload['helper'] = array('url');
i hope this will help you to fix the problem.