I have a view, in which I want to, on the push of the button, send data to a specific function in a controller, which would then manipulate it a little and pass it forward to another view. However, whenever the button is pushed, the screen is blank. The URL changes to the correct URL, but in Chrome Dev Tools it gives the:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
error. I am not sure what is failing to load, or what is going on. Any help is greatly appreciated!
My view(the relevant part):
<script>
$('#begin_practice').on('click', function(){
location.href= "<?php echo base_url() . 'test/setupTest/' . $test_id . '/' . $lang . '/' . $practice; ?>";
});
</script>
My controller (the relevant function):
public function setupTest($test_id, $lang, $practice){
$this->load->model('Test_model');
$test_key= ($practice) ? $this->Test_model->getKey($test_id, $lang, $practice) : $this->Test_model->getKey($test_id, $lang, $practice);
$data['test_key'] = $test_key;
$data['test_id'] = $test_id;
$data['lang'] = $lang;
$data['practice'] = $practice;
if($practice){
$this->load->view('tests/test_sample/' . $lang . 'practice_test_view', $data);
}else{
$this->load->view($lang . '/test_view', $data);
}
}
My model:
public function getKey($test_id, $lang, $practice){
if($test_id == 7){
$image_array = ($practice) ? $image_array = file_get_contents('<?=base_url();?>files/json/task_' . $test_id . '_practice_' . $lang . '.json') : $image_array = file_get_contents('<?=base_url();?>files/json/task_' . $test_id . '_' . $lang . '.json');
}else{
$image_array = ($practice) ? $image_array = file_get_contents('<?=base_url();?>files/json/task_' . $test_id . '_practice.json') : $image_array = file_get_contents('<?=base_url();?>files/json/task_' . $test_id . '.json');
}
$image_array = json_decode($image_array, true);
if($test_id !=8){
shuffle($image_array);
}
return $image_array;
}
If anything else is needed, please let me know. Thanks in advance!
Related
I am trying to understand the MVC method with the use of OOP. However it seems like I've hit the wall here.
I am trying to pass multiple objects to the view. But all I can do so far is pass just one object. The ideal result would be passing multiple objects, while keeping the names that are assigned to them in the controller.
The render, start and end functions in the View class go something like this:
public function render($viewName, $data){
$viewAry = explode('/', $viewName);
$viewString = implode(DS, $viewAry);
if(file_exists(ROOT . DS . 'app' . DS . 'views' . DS . $viewString . '.php')){
include(ROOT . DS . 'app' . DS . 'views' . DS . $viewString . '.php');
include(ROOT . DS . 'app' . DS . 'views' . DS . 'layouts' . DS . $this->_layout . '.php');
}else{
die('The view \"' . $viewName . '\" does not exist.');
}
}
public function content($type){
if($type == 'head'){
return $this->_head;
}elseif ($type == 'body'){
return $this->_body;
}
return false;
}
public function start($type){
$this->_outputBuffer = $type;
ob_start();
}
public function end(){
if($this->_outputBuffer == 'head'){
$this->_head = ob_get_clean();
}elseif($this->_outputBuffer == 'body'){
$this->_body = ob_get_clean();
}else{
die('You must first run the start method.');
}
}
And this is how would the controller look like:
public function indexAction(){
$items = $this->PortalModel->getItems();
$collections = $this->PortalModel->getCollections();
$this->view->render('home/index', $items);
}
So this is how I get the one $data object to the view and loop trough it.
But how could I store multiple results from the database to the view?
You should pass an array of variables into view instead of one variable.
public function indexAction(){
$variables = [
'items' => $this->PortalModel->getItems(),
'collections' => $this->PortalModel->getCollections()
];
$this->view->render('home/index', $variables);
}
I have a website which already works on 2 languages ,russian and english(everything runs well in both languages), now i have added armenian language.
The Problem --- when i switch on the website into armenain language , i see ,for example,in breadcrumbs
text_home button_continue button_login ....
i have checked \catalog\language\armen\armenian.php file and noticed that values of this varables exist.
By the way ,when i add from armenian.php into ,for example, language/armen/common/header .php this code
$_['text_home'] = 'arm_home';
it works , but thit means that i should add by hand in every single page this general variable...
i would like to have more optimal solution ...
from admin panel i set armenain as default language
Maybe ,i should edit system\library\language.php ???
Here is the structure
<?php
class Language {
private $default = 'en-gb';
private $directory;
private $data = array();
public function __construct($directory = '') {
$this->directory = $directory;
}
public function get($key) {
return (isset($this->data[$key]) ? $this->data[$key] : $key);
}
public function set($key, $value) {
$this->data[$key] = $value;
}
// Please dont use the below function i'm thinking getting rid of it.
public function all() {
return $this->data;
}
// Please dont use the below function i'm thinking getting rid of it.
public function merge(&$data) {
array_merge($this->data, $data);
}
public function load($filename, &$data = array()) {
$_ = array();
$file = DIR_LANGUAGE . 'english/' . $filename . '.php';
// Compatibility code for old extension folders
$old_file = DIR_LANGUAGE . 'english/' . str_replace('extension/', '', $filename) . '.php';
if (is_file($file)) {
require($file);
} elseif (is_file($old_file)) {
require($old_file);
}
$file = DIR_LANGUAGE . $this->default . '/' . $filename . '.php';
// Compatibility code for old extension folders
$old_file = DIR_LANGUAGE . $this->default . '/' . str_replace('extension/', '', $filename) . '.php';
if (is_file($file)) {
require($file);
} elseif (is_file($old_file)) {
require($old_file);
}
$file = DIR_LANGUAGE . $this->directory . '/' . $filename . '.php';
// Compatibility code for old extension folders
$old_file = DIR_LANGUAGE . $this->directory . '/' . str_replace('extension/', '', $filename) . '.php';
if (is_file($file)) {
require($file);
} elseif (is_file($old_file)) {
require($old_file);
}
$this->data = array_merge($this->data, $_);
return $this->data;
}
}
Thank you in advance
I used an old package of armenain language ,which wasn't compatible with oc 2.3,
solution https://crowdin.com/project/opencart-translation-v2/hy-AM#
I have created a fuction to redirect my users, but it's redirecting to the wrong page with the addon URLerror
Here's my code:
class Redirect
{
/**
* To the homepage
*/
public static function home()
{
header("location: " . Config::get('URL'));
}
/**
* To the defined page
*
* #param $path
*/
public static function to($path)
{
header("location: " . Config::get('url') . $path);
}
}
Which I am using like so:
Redirect::to('dashboard/following?success=5');
But it is redirecting to:
http://localhost/user/dashboard/URLerror
My config url is: http://localhost/
It doesn't make sense why it should be doing that as the code looks fine I think
I have tried it with a forward slash before dashboard too, but that is the same result
First off:
public static function home()
{
header("location: " . Config::get('URL'));
}
public static function to($path)
{
header("location: " . Config::get('url') . $path);
}
You're using two different styles in the get() string. Unless you are using strtolower on the argument within the get() method, you should use only one. URL or url.
Also, you should think about sanitizing your URL path. try this:
header("Location: " . Config::get('URL') . urlencode($path));
See how that works.
Another thing, urlencode is going to sanitize / as well, so you should add another parameter.
public function to($path, $data = array())
{
$urldata = "";
if(!empty($data))
{
$count = 0;
$urldata = "?";
foreach($data as $item => $value)
{
$count++;
if($count < count($data))
{
$urldata .= urlencode($item . "=" . $value . "&");
}
else
{
$urldata .= urlencode($item . "=" . $value);
}
}
}
header("Location: " . Config::get('url') . $path . $urldata);
}
Keep in mind that function is untested, so you may have to do some work to get it working. But that'll make passing data to a URL easier, especially if you're using a GET request.
I am using codeigniter. I need to get all variables from a language file to an array.Is it possible?
Is there any method available like as follows?
$a = $this->load->language('editor');
print_r($a);
I was tried $this->lang->language; But,This will return labels from another language files loaded.
$CI = & get_instance();
$arr = $CI->lang->language;
Or Use following library
Class My_language {
var $language = array();
/**
* List of loaded language files
*
* #var array
*/
var $is_loaded = array();
function __construct() {
log_message('debug', "Language Class Initialized");
}
function load($langfile = '', $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '') {
$langfile = str_replace('.php', '', $langfile);
if ($add_suffix == TRUE) {
$langfile = str_replace('_lang.', '', $langfile) . '_lang';
}
$langfile .= '.php';
if (in_array($langfile, $this->is_loaded, TRUE)) {
return;
}
$config = & get_config();
if ($idiom == '') {
$deft_lang = (!isset($config['language'])) ? 'english' : $config['language'];
$idiom = ($deft_lang == '') ? 'english' : $deft_lang;
}
// Determine where the language file is and load it
if ($alt_path != '' && file_exists($alt_path . 'language/' . $idiom . '/' . $langfile)) {
include($alt_path . 'language/' . $idiom . '/' . $langfile);
} else {
$found = FALSE;
foreach (get_instance()->load->get_package_paths(TRUE) as $package_path) {
if (file_exists($package_path . 'language/' . $idiom . '/' . $langfile)) {
include($package_path . 'language/' . $idiom . '/' . $langfile);
$found = TRUE;
break;
}
}
if ($found !== TRUE) {
show_error('Unable to load the requested language file: language/' . $idiom . '/' . $langfile);
}
}
if (!isset($lang)) {
log_message('error', 'Language file contains no data: language/' . $idiom . '/' . $langfile);
return;
}
if ($return == TRUE) {
return $lang;
}
$this->is_loaded[] = $langfile;
$this->language = array();
$this->language = $lang;
return $this->language;
unset($lang);
log_message('debug', 'Language file loaded: language/' . $idiom . '/' . $langfile);
return TRUE;
}
}
Call like this
$this->load->library('my_language');
$arr = $this->my_language->load('demo');
print_r($arr);
I know this is quite an old question, but I just want to give my solution for this problem since no answers has done the trick for this problem. (tested on codeigniter 3)
$this->load->helper('language');
$foo = $this->lang->load('lang_file', 'english', true);
print_r($foo);
notice that the third parameter for load method determines whether to return the loaded array of translations. source: codeigniter 3 docs.
hope this helps
Yeah ofcourse its possible. You can do like this :
//load helper for language
$this->load->helper('language');
//test is the language file in english folder
$this->lang->load('test','english');
//fetch all the data in $var variable
$var=$this->lang->language;
//print $var
print_r($var);
$var will return the array. :)
If you want to return language file data in Array than you need to pass the third parameter in load function.
$this->lang->load('header','hindi',true) // filename,language,true
Hi I am trying to get my gravatar api working with my open cart admin/controller/common/header.php and my admin/view/template/common/header.tpl
Still not working gave it ago before that some one gave me advice on but now not working? So thought give it ago another way but nothing.
admin / controller/ header. php
This is just trimmed down version
<?php
class ControllerCommonHeader extends Controller {
protected function index($get_gravatar) {
}
function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
$url = 'http://www.gravatar.com/avatar/';
$url .= md5( strtolower( trim( $email ) ) );
$url .= "?s=$s&d=$d&r=$r";
if ( $img ) {
$url = '<img src="' . $url . '"';
foreach ( $atts as $key => $val )
$url .= ' ' . $key . '="' . $val . '"';
$url .= ' />';
}
return $url;
}
admin / view / template / common / header.tpl
<?php
$email = $user_info['email']; // Not Working "Need it to pick up who ever logins"
$email = "your#rmail.com"; // Works
$default = "http://www.somewhere.com/homestar.jpg";
$size = 150;
?>
<li>
<a href="" class="text-center">
<img src="<?php echo $grav_url = "http://www.gravatar.com/avatar/" . md5( strtolower( trim( $email ) ) ) . "?d=" . urlencode( $default ) . "&s=" . $size;; ?>" alt="" />
</a>
</li>
Changes for getting gravatar image in header.tpl
Update system/library/user.php as below:
After: $this->username = $user_query->row['username'];
Add: $this->email = $user_query->row['email'];
Before: public function getUserName() {
Add:
public function getUserEmail() {
return $this->email;
}
Update admin/controller/common/header.php as below:
After: $this->data['logged'] = sprintf($this->language->get('text_logged'), $this->user->getUserName());
Add: $this->data['email'] = $this->user->getUserEmail();
Update admin/view/template/common/header.tpl as below:
<div class="img-circle">
<img src="http://www.gravatar.com/avatar/<?php echo md5(strtolower(trim($email))); ?>">
</div>
Please let me know the result of these changes.
Note: In opencart, you need to assign values to variables like: $this->data['variable_name'] in controller files and use them template files like: $variable_name.
Have you tried adding an extension to your url => www.gravatar.com/avatar/far512q3tgfqwe*.jpg* for example, a quick google search and i came up with this url, check it for further info:
http://en.gravatar.com/site/implement/images/
Try this piece of code in your header.php to get the email of the current logged in user:
$this->load->model('user/user');
$email_data = $this->model_user_user->getUser($this->user->getId());
$email = $email_data['email'];
if you want to get emails for all users it need to be handled differently.