i have 2 functions in a controller,
function feed()
{
$xml = simplexml_load_file('http://localhost/feed.php');
// pass to other function
$this->purchased($xml->prices);
foreach ($xml->prices as $price) {
echo ' <tr id="'.$price->sign.'"><td class="price">'.$price->wholesale.'</td>';
}
}
in the above function i take some values from a feed and append it to a html in the front end using jquery
in the below function what i do is list down all the products purchased by a particular user. this function also refreshed every 5 seconds.
function purchased($price)
{
foreach ($price as $x)
{
$retail = $x->retail;
}
}
what i need to do is get the values returned form the function feed() to the purchased function to do some calculations .. but when i use the above method i get the bellow error
Message: Undefined variable: price
Message: Missing argument 1 for Actions::purchased()
can someone tell me how can i get the prices from the feed function and use it with the purchased function?
Not sure if I understood what are you doing and what are you trying to achieve, but..
Passing variables works only when you call function. So, when you execute feed() function, then you call purchased() function and pass variable. purchased() works, ends, and then script goes back to the feed() function.
Calling purchased() from anywhere else doesn't give you the values from feed() function.
Try to change function to:
function purchased($price = '')
{
if (!isset ($price) || empty($price)) {
$xml = simplexml_load_file('http://localhost/feed.php');
$price = $xml->prices;
}
foreach ($price as $x) {
$retail = $x->retail;
}
}
Related
So I am calling a function to get the balance of an ethereum account.
I am using the php web 3 found here.
I wrapped the web3 class inside my own function.
So I call my function -> my function executes the web3 eth command.
My function that calls the eth command:
public function getAccountBalance($account) {
$newBalance = '';
$this->web3->eth->getBalance($account, function ($err, $balance) use($newBalance) {
if ($err !== null) {
echo 'Error: ' . $err->getMessage();
return;
}
$newBalance = $balance->toString();
echo $newBalance; // this echos the balance fine
});
echo $newBalance; // this returns empty, as like we defined at the top
return $newBalance;
}
I am trying to return the balance from the eth function to return it within my getAccountBalance() function but whenever I try to return it, it gives empty, as if it didn't update the $newBalance value inside the $this->web3->eth->getBalance($account, function ($err, $balance) use($newBalance) { callback.
If I echo it inside the $this->web3->eth->getBalance($account, function ($err, $balance) use($newBalance) { callback, it outputs the correct balance fine.
If I try and echo it outside of the $this->web3->eth->getBalance($account, function ($err, $balance) use($newBalance) {, it gives me the value at the top of my function, where I define it: $newBalance = '';, so it's giving me an empty response.
I am not sure why this command does not let me and some others do...
I have tried adding global $newBalance instead of use($newBalance) too but still no luck.
Okay, so that was quick. It turns out my code is completely fine, except from 1 & i missed out.
I need to use:
$this->web3->eth->getBalance($account, function ($err, $balance) use(&$newBalance) {
Without this &, it could not pull the variable into the function.
With this added, It now returns the balance as I wish.
I am trying to use a php function to get the prices of a tola (11.664 grams) at an order status page. The function uses a php page 'priceApi4CurCtrl.php' that fetches the price data from a website using an external API. My function is as follows:
function tolaPrice($cur_pick) {
require('priceApi4CurCtrl.php');
if($cur_pick == 'pkr') {
$tola_price = $bitprice_pkr*10*11.664;
return $tola_price;
} elseif($cur_pick == 'usd') {
$tola_price = $bitprice_usd*10*11.64;
return $tola_price;
} elseif($cur_pick == 'aed') {
$tola_price = $bitprice_aed*10*11.64;
return $tola_price;
}
}
// Succeeds for the first call as under
$cur_pick = 'pkr';
echo tolaPrice($cur_pick);
// Fails for the second call as under
$cur_pick = 'aed';
echo tolaPrice($cur_pick);
The function works fine for the first call using echo tolaPrice($cur_pick). However, it fails all subsequent calls and hence I am unable to complete the order status of second and subsequent orders.
I am not sure how to work around this.
Instead of trying to wrap an if else loop in a function, I simply calculated the prices in a separate file named tola_price.php as follows:
include('priceApi4CurCtrl.php');
$tola_price_pkr = $bitprice_pkr*10*11.664;
$tola_price_usd = $bitprice_usd*10*11.64;
$tola_price_aed = $bitprice_aed*10*11.64;
And then called the tola_price.php within my script with if else loop as follows:
require_one('tola_price.php');
if($cur_pick == 'pkr') {
$tola_price = $tola_price_pkr;
} elseif($cur_pick == 'usd') {
$tola_price = $tola_price_usd;
} elseif($cur_pick == 'aed') {
$tola_price = $tola_price_aed;
}
And then used the prices to build further script.
Thanks to those who offered help
I have one function call remove_certificate_packages($certificate_id, array_keys($package_id)) this will invoke the below function
function remove_certificate_packages($certificate_id, $package_id)
{
if (is_numeric($package_id)) // so this is list of package id:s
$package_id = array($package_id);
if (!$package_id) return true;
**notify_package_unlinked($certificate_id,array_keys($package_id));**//one more func call
return true;
}
in this function, I have one more function call "notify_package_unlinked" I need to pass the "$package_id". It will call the appropriate function but the problem is, in the "notify_package_unlinked" function the value is showing "Array". What is the problem? Could you please help
function notify_package_unlinked($certificate_id,$package_id)
{
$query="select id,filename,version from packages where id =$package_id";
$res = db_query($query);
$package= db_fetch_object($res);
$packid=$package->id;
$packname=$package->filename;
$packversion=$package->version;
print "$packid"; // here it is printing the value"Array"
}
I got my output using foreach loop .
foreach($package_id as $id){$pack=$id;}
I do not want to be asking a question that has already been answered, however I have done a ton of research and I am stuck. Any help would be appreciated.
I am trying to query and add database results to OpenCart's addproduct.tpl
In the MODEL file I have:
public function units() { //function that gets database info and returns it
$unit_variables = $this->db->query("SELECT unit FROM ". DB_PREFIX ."
weight_class_description");
if (!$unit_variables) {
die(mysql_error());
}
else {
foreach ($unit_variables->rows as $temp_var) {
print_r($temp_var['unit'], true);
}
}
}
In the CONTROLLER file I have:
$this->load->model('catalog/product'); //where my function is located
$this->model_catalog_product->units();
$weight_variables = units();
if (isset($this->request->post['weight_variables'])){
$this->data['weight_variables'] = $this->request->post['weight_variables'];
}
In the VIEW I have:
<?php echo $weight_variables ?>
I get the following error:
Call to undefined function units() in /path/to/controller/file on line etc.
Note: When I print_r($temp_var); instead of returning print_R($temp_var, true) and delete these lines of code $weight_variables = units(); if (isset($this->request->post['weight_variables'])){ $this->data['weight_variables'] = $this->request->post['weight_variables'] } in the controller file my model file will display the query results on the addproduct.tpl
units() is a METHOD of your object, yet you're calling it as a standalone regular function:
$weight_variables = units();
^^^^^^^
Shouldn't it be:
$weight_variables = $this->model_catalog_product->units();
instead? And note that as-written, your method doesn't actually return anything, so $weight_variables will simply get assigned null..
I have a function that takes an input variable and outputs a template with the following call:
outputhtml($blue_widget);
outputhtml($red_widget);
outputhtml($green_widget);
And a simplified version of the function:
function outputhtml($type)
{
static $current;
if (isset($current))
{
$current++;
}
else
{
$current = 0;
}
//some logic here to determine template to output
return $widget_template;
}
Now here is my problem. If I call the function in a script three times or more, I want the output to be one way, but if I only call the function twice, then I have some html changes that need to be reflected in the templates that are returned.
So how can I modify this function to determine if there are only two calls for it. I can't go back after the fact and ask "hey function did you only run twice???"
Having trouble getting my head around how I tell a function that it is not going to be used after the second time and the necessary html modifications can be used. How would I go about accomplishing this?
function outputhtml($type)
{
static $current = 0;
$current++;
//some logic here to determine template to output
if ($current === 2) {
// called twice
}
if ($current > 2) {
// called more than twice
}
return $widget_template;
}
That would not be practical using a static $current inside the function; I would suggest using an object to maintain the state instead, like so:
class Something
{
private $current = 0;
function outputhtml($type)
{
// ... whatever
++$this->current;
return $template;
}
function didRunTwice()
{
return $this->current == 2;
}
}
The didRunTwice() method is asking "did you run twice?".
$s = new Something;
$tpl = $s->outputhtml(1);
// some other code here
$tpl2 = $s->outputhtml(2);
// some other code here
if ($s->didRunTwice()) {
// do stuff with $tpl and $tpl2
}
The only way you can find out if a function was only called twice is by putting the test at the end of your code; but perhaps by then the templates are no longer accessible? Can't tell much without seeing more code.