Call to undefined method Uni_Fileuploader_Helper_Data::getProductAttachments() - php

I am having the following error while trying to display product attachment:
Call to undefined method Uni_Fileuploader_Helper_Data::getProductAttachments() in www\app\design\frontend\my_theme\default\template\catalog\product\list.phtml on line 51
The way I'm calling the method is:
<?php $attachments = Mage::helper('fileuploader')->getProductAttachments($_product->getId(), 1);
if (count($attachments) > 0): ?>
<?php foreach ($attachments as $_attachment): ?>
<?php echo $_attachment['file']; ?>
<?php endforeach; ?>
<?php endif; ?>
Please help!
Thanks

It seems that function is not located in the class you're instantiating in your code. Upon further inspection on the module's code, that function is defined in
Uni_Fileuploader_Block_Fileuploader
So what you need to do is declare your block as
<block type="fileuploader/fileuploader" name="product.attachments" template="fileuploader/attachments.phtml"/>
Then you can access that function through $this, like so
$this->getProductAttachments()
As an alternative, you can go to Uni_Fileuploader_Helper_Data and copy the function from Uni_Fileuploader_Block_Fileuploader, since you can't change the template type on a core template without breaking the page.

Related

Problem with the visibility of a variable in the included layout PHP

EDIT:
According to the question: Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors?
should work for me, but it does not. I probably don't understand something, but I can't see where the bug is.
I'm a newbie in PHP so the question is probably simple and stupid. I have 3 files: index.php, View.php and layout.php. In View php I have a View class definition and a render() method. Among others, this method includes an HTML template from the file layout.php.
But: in the PHP file in the <main> tag I have a piece of php code (if statement) which, depending on the value of the $page variable (defined in the PHP file), includes a different file
The problem is that this variable appears as undefined in the layout.php file.
I don't understand why, in the end all code is executed in index.php: View class is included and render function includes layout.php.
Please help me understand this, because I can't do it. Thanks in advance.
Notice: Undefined variable: action in C:\xampp\htdocs\notes\templates\layout.php on line 17
index.php
<?php
declare(strict_types=1);
namespace App;
require_once("src/utils/debug.php");
require_once("src/View.php");
$action = $_GET['action'] ?? null;
$view = new View();
$view -> render($action );
View.php
<?php
declare(strict_types=1);
namespace App;
class View
{
public function render(?string $page): void
{
include_once("templates/layout.php");
}
}
layout.php
<html>
<head>
</head>
<body>
<header>
<h1>Nagłówek</h1>
</header>
<nav>
<ul>
<li><a href="index.php/?action=list">Lista notatek</li>
<li><a href="index.php/?action=create">Nowa notatka</li>
</ul>
</nav>
<main>
<?php
if ($page === 'create') {
include_once("templates/pages/list.php");
} else {
include_once("templates/pages/create.php");
}
?>
</main>
<footer>
</footer>
</body>
</html>

Problem with the visibility of a variable in the included PHP file

EDIT:
According to the question: Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors?
should work for me, but not. I probably don't understand something, but I can't see where the bug is. So, in my opinion, it`s not duplicate.
I'm a newbie in php so the question is probably simple and stupid. I have 3 files: index.php, View.php and layout.php. In View php I have a View class definition and a render () method in it. Among others, this method include html template from the file layout.php.
But: in the php file in the tag I have a piece of php code (if statement) which, depending on the value of the $ page variable (defined in the php file), includes a different file.
The problem is that this variable appears as undefined in the layout.php file.
I don't understand why, in the end all code is executed in index.php: View class is included and render function includes layout.php.
Please help me understand this, because I can't do it. Thanks in advance
Notice: Undefined variable: action in C:\xampp\htdocs\notes\templates\layout.php on line 17
index.php
<?php
declare(strict_types=1);
namespace App;
require_once("src/utils/debug.php");
require_once("src/View.php");
$action = $_GET['action'] ?? null;
$view = new View();
$view -> render($action );
View.php
<?php
declare(strict_types=1);
namespace App;
class View
{
public function render(?string $page): void
{
$apction = $page
include_once("templates/layout.php");
}
}
layot.php
<html>
<head>
</head>
<body>
<header>
<h1>Nagłówek</h1>
</header>
<nav>
<ul>
<li><a href="index.php/?action=list">Lista notatek</li>
<li><a href="index.php/?action=create">Nowa notatka</li>
</ul>
</nav>
<main>
<?php
if ($action === 'create') {
include_once("templates/pages/list.php");
} else {
include_once("templates/pages/create.php");
}
?>
</main>
<footer>
</footer>
</body>
</html>

How do you call a helper function in the category.tpl file OpenCart 2

This is my previous question: How to convert a decimal $attribute['text']; into a fraction in opencart
I have a helper function that is declared in startup.php and defined in helper/dec2frac.php
I am trying to call the helper function from a category.tpl file with this code:
<?php if ($product['attribute_groups']) { ?>
<?php foreach ($product['attribute_groups'] as $attribute_group) { ?>
<?php foreach ($attribute_group['attribute'] as $attribute) { ?>
<?php /*var_dump($attribute);*/
if($attribute['name'] == "Adjuster Position")
{
//echo("<h1>HELLLO</h1>");
dec2frac($attribute['text']);
}
?>
...but I am getting this error message:
Fatal error: Call to undefined function dec2frac() in startup.php
helper/dec2frac.php
How can I call my helper function in the category.tpl file?
Do I need to reference the helper function in my category.php file?
I am trying to achieve same but in a different way. This following link will help, it will work with opencart v2.3x
Not able to find my custom object in Registry - OpenCart-v2.3.0.2
Try the above link (it is my posted question). Create a object and save it in registry and get it from registry and call the required function.
in your category.php file, get your object from registry. For eg:
$kt = $registry->get('ktLibrary'); //Object
$value = $kt->getSomeValue(); //function
object $kt will be available throughout the category.tpl file.

Include and class PHP

Guys why doesn't php read my class file? I have this structure:
home.php
stamp.php
class/class.stamp.php
class/class.text.php
home.php
<?php
echo "hello i'm the home page";
include 'class/class.stamp.php'
$stamp = new Stamp();
include 'class/class.text.php';
?>
class.text.php
<?php
$stamp->something('hello yes it is');
?>
class.stamp.php
<?php
class Stamp {
public function something($text);
echo $text;
}
}
?>
The result? Nothing! It says:
Fatal error: Call to a member function something on a non-object in /bla/bla/bla/
But if i do something like that, it works!
home.php
<?php
echo "hello i'm the home page";
include 'class/class.text.php';
?>
class.text.php
<?php
include 'class.stamp.php'
$stamp = new Stamp();
$stamp->something('hello yes it is');
?>
class.stamp.php
<?php
class Stamp {
public function something($text) {
echo $text;
}
}
?>
But I don't need that, please guys help me :(
Check if the issue is with the directory.
You said it works with class.stamp.php, but shouldn't that be class/class.stamp.php? Here's the thing, if your homepage is located in a certain folder, then in that same folder location you should have the folder class, adjacent to your Home.php file.
I've also had the issue of uninstantiated object, so make sure you are including it in every scope. To help you catch the error, use
require "class/class.stamp.php";//Throws an error if it can't find the file.
require_once "class/class.stamp.php";//Bad practice
If require doesn't help you solve it, post everything please.

Undefined variable after wrapped include in PHP

I'm using PHP for web development. I'm using the following function to wrap the include of a view:
<?php
function render($templateFile) {
$templateDir = 'views/';
if (file_exists($templateDir . $templateFile)) {
include $templateDir . $templateFile;
} else {
throw new Exception("Template '{$templateFile}' couldn't be found " .
"in '{$templateDir}'");
}
}
?>
Although this seems right to me, there is a really unexpected behavior: when I define a variable to something (e.g. an array) and use render for including a view that uses that variable, I get an undefined variable error. But when I explicitely use include there is no error at all and things are just fine.
This is the script that calls render:
<?php
include 'lib/render.php'; // Includes the function above.
$names = array('Trevor', 'Michael', 'Franklin');
render('names.html'); // Error, but "include 'views/names.html'" works fine.
?>
And this is the file that uses the $names variable:
<html>
<head>
<title>Names</title>
</head>
<body>
<ol>
<?php foreach ($names as $name): ?>
<li><?php echo $name; ?></li>
<?php endforeach; ?>
</ol>
</body>
</html>
Help will be very much appreciated.
This is from the PHP documentation on the include function (c.f. http://us1.php.net/manual/en/function.include.php):
When a file is included, the code it contains inherits the variable
scope of the line on which the include occurs. Any variables available
at that line in the calling file will be available within the called
file, from that point forward. However, all functions and classes
defined in the included file have the global scope.
And also:
If the include occurs inside a function within the calling file, then
all of the code contained in the called file will behave as though it
had been defined inside that function. So, it will follow the variable
scope of that function.
So, if your render function can't access $names, then neither can your included file.
A possible solution would be to pass the parameters you want to be able to access in your view template, to your render function. So, something like this:
function render($templateFile, $params=array()) {
$templateDir = 'views/';
if (file_exists($templateDir . $templateFile)) {
include $templateDir . $templateFile;
} else {
throw new Exception("Template '{$templateFile}' couldn't be found " .
"in '{$templateDir}'");
}
}
Then, pass them like this:
$names = array('Trevor', 'Michael', 'Franklin');
render('names.html', array("names" => $names));
And use them in your view template like this:
<html>
<head>
<title>Names</title>
</head>
<body>
<ol>
<?php foreach ($params['names'] as $name): ?>
<li><?php echo $name; ?></li>
<?php endforeach; ?>
</ol>
</body>
</html>
There are probably better solutions to this, like putting your render function into a View class. Then you can call the View class function from inside your template file, and access parameters that way instead of just assuming there will be a $params variable in the view templates scope. But, this is the simplest solution.
The problem is, when you include the file directly using include 'views/names.html' the variable $name remains in the same files scope. Hence, it works. But when the include is done through the function, the varibale $name remains out of scope inside the function. So it doesn't work. For example, declare $names as global inside the function and it will work.
If you update the function like below you will see $names variable works.
function render($templateFile) {
global $names; // declares the global $names variable to use in the included files
$templateDir = 'views/';
if (file_exists($templateDir . $templateFile)) {
include $templateDir . $templateFile;
} else {
throw new Exception("Template '{$templateFile}' couldn't be found " .
"in '{$templateDir}'");
}
}

Categories