I'd like to get the current url in a .tpl file in OpenCart.
I've seen this answer but it works only in a php file.
I have to get this way:
_my_array.push(['_productName',"<?php echo $heading_title; ?>"]);
**_my_array.push(['_productUrl', ["how can I get url ?"]]);**
Thanks
To get full url
<?php
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
?>
and btw those answers work on tpl files also because tpl files are sort of php files only
The best practice would be to get it in the controller then use it in the view file.
In your controller
$data['current'] = $this->url->link($this->request->get['route'], '', 'SSL');
Then in the view file
echo $current;
I also needed current url for schema.org.
Generally you can create current url via link function
public function link($route, $args = '', $secure = false)
So https link for product page that would be
$data['share'] = $this->url->link('product/product', 'product_id=' . (int)$this->request->get['product_id'], true);
Then in the view file
echo $share
Related
I have a website in codeigniter and I need to give an option to user to download multiple images.
Below is simplified code. Now when I run this code it generates only the first iframe and downloads only first file. What correction is required, so that it downloads both images?
downloadimages.php contains following code:
$addtest = base_url().'admin/test_multiple_downfile/img1.jpg';
echo '<iframe src="'.$addtest.'">/iframe>';
$addtester = base_url().'admin/test_multiple_downfile/img2.jpg';
echo '<iframe src="'.$addtester.'">/iframe>';
admin.php under controller folder contains following code:
public function test_multiple_downfile()
{
$this->include_all_helpers_database_session();
$id['id'] = $this->uri->segment('3');
$this->load->view("multiple_downfile",$id);
}
multiple_downfile.php contains following code:
<?php
$this->load->helper('download');
$file_data = file_get_contents(base_url()."attachments/ur136/".$id);
$file_name = $id;
force_download($file_name, $file_data);
?>
I am new to php and codeigniter, so I might be missing something obvious here.
I am trying to pass URL parameters to a Wordpress template page:
www.mysite.com/my_wp_page_template/?city=Anytown&state=ST
Based on some forum examples I have added the following code to the functions.php:
add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
$qvars[] = 'City';
$qvars[] .= 'State';
return $qvars;
}
And, I am trying to extract the variables in a PHP file that is included in a WP page using:
report_for_city.php
<?php
$strCity = $_GET["City"];
$strState = $_GET["State"];
$strCityState = $strCity . ' ' . $strState;
?>
<p>This page contains the report for <?php $strCityState ?>.</p>
But, the variable is not getting inserted in the output HTML. The HTML is being inserted into the WP page, so the PHP template is getting invoked from the WP page using the insert-php plugin:
[insert_php]include('wp-content/php/report_for_city.php');[/insert_php]
What am I doing wrong? How can this be fixed?
The PHP code was missing echo in:
php $strCityState
The following code corrected the problem:
php echo $strCityState
Is there an easier way to output the value than this?
<script src='<?php base_url();?>assets/js/jquery.min.js'></script>
the script above is code that my friend use, that works just for him, but if i change it to <?php echo base_url();?> and it's work for me, not for him. It's become a problem when we transfer files each other, What i am supposed to do ?
thank you.
I've solution script for my case, change this from config.php :
$http = 'http' . ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 's' : '') . '://';
$urlbaru = str_replace("index.php","", $_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$http" . $_SERVER['SERVER_NAME'] . "" . $urlbaru;
Please show more about your problem detail like paste your error code in here that we could use those information to help you.
Btw, base_url() isn't a php embedded function, one of the potential problem is that your friend custom a function named base_url() that supposed to return it's root path.
So, you have to either include or build the same function with the same name to your script.
Ex.
function base_url()
{
return dirname($_SERVER['SCRIPT_FILENAME']);
}
And the 'src' inside your tag should point to your jQuery source file.
Looks like you are using codeigniter framwork, and you have not autoloaded URL helper, that's why you getting error. if I am not wrong you have to load URL helper.
how call .php outside the folder
my folder structurer is
I'm in cart.php
my code is
$tpl_file ='../mail.php';//problem is here LINE 279
$msg_tmpl = file_get_contents($tpl_file);
if (!file_exists($tpl_file))
{
?>
<script>
alert('no');
</script>
<?php
}
else
{
?>
<script>
alert('yes');
</script>
<?php
}
So my question is I want to get all data from the mail.php and assign it $tpl_file.
In above try I'm getting always NO with
A PHP Error was encountered
Severity: Warning
Message: file_get_contents(../mail.php): failed to open stream: No such file or directory
Filename: pages/cart.php
Line Number: 279
any Ideas??
Change
$tpl_file ='../mail.php';
To
$tpl_file ='application/views/pages/mail.php';
NOTE:
From your screenshot,mail.php is located in application/views/pages/mail.php
Try to use full relative path to the file
$tpl_file = APPPATH.'views/pages/mail.php';
OR Try to Use
$tpl_file = $this->load->view('pages/mail',true);
try changing
$tpl_file ='../mail.php';
to
$tpl_file =__FILE__.'mail.php';
as both files are in the same directory then why relate the path to parent directory
Codeigniter refers file and directories with referance to index.php located at root folder of your project.
So, You will get it by
$tpl_file = 'application/views/pages/mail.php';
or, if both are in same directory,
$tpl_file = __DIR__.'/mail.php';
But that will be bad idea, instead assign it the Codeigniter way by,
$tpl_file = $this->load->view('pages/mail', '', true);
So, your final code will be
$tpl_file = $this->load->view('pages/mail', '', true);
if ($tpl_file)
{
// do something
}
More info here.
Hope the following code will work for you.
$this->view('mail');
Try this
$msg_tmpl = $this->load->view('mail', '', true);
Since you are using CI, I would recommend using following code:
<?php
$tmp_file = $this->load->view('pages/mail', array(), TRUE);
?>
Use APPPATH for this
$msg_tmpl = file_get_contents(APPPATH.'views/pages/mail.php');
You can do it several way.
way 1#
$tpl_file =VIEWPATH.'pages/mail.php'
$msg_tmpl = file_get_contents($tpl_file);
mail.php is inside your view path so you can get value like this
way 2#
$msg_tmpl = $this->load->view('pages/mail',null,true);
//this will return the content instead of displaying it output.
Note
Your all php code runs through main index.php.So if you want to use relative path it should be relative with index.php not with cart.php
I want pass result of a function to an included file in that function. let me explain it with an example .
index.php
$siteurl = $_POST['url'];
function validator($siteurl){
global $lang, $siteurl;
......
......
......
return $output;
}
en.php
$lang = array();
$lang['site_url'] = 'Site url is' .$output. '';
$lang['site_url_delete'] = 'delete' .$output. '';
$lang['site_url_edit'] = 'edit ' .$output. '';
well, now i want pass $output from validator function from index.php to fa.php ($lang).
I put this code in fa.php:
$siteurl = $_POST['url'];
$output = validator($siteurl);
It works but it's dirty work because I don't want to call a function (Validator) to each $lang. is there better way to do this?
All files you include in php share the enclosing scope. Think about this like you copy all code from the included file and insert it to the "main" file instead of the include statement.
So, you can write $output = validator($siteurl) in your index.php file, include your lang.php file and directly use the $output variable.
But, however, it is kinda bad practice to use global variables. It may turn your code to a mess after some time. So, be careful and consider to rethink an architecture of your application.
I've been in a wrong way! found the best solution!
solved my problem with function.sprintf
sprintf($lang['site_url'], $output)
$lang['site_url'] = 'Site url is %s';