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;}
Related
I can't put real code here because is very long and will be hard to
explain.
I have users table in database and I have data table in database too.
So, to get the user data I'll pass user_id as parameter. Like this:
public function get_user_data($user_id) {
}
But. I can only get 1 data per "request". (Keep reading)
public function user_data() {
$getUsers = $this->db->get('users');
foreach($getUsers->result_array() as $user)
{
$data = $this->get_user_data($user->ID);
var_dump($data); // Only return 1 data;
}
}
But, I guess that have an way to "bypass" this but I don't know. I'm having trouble thinking.
As I said, I want to "bypass" this, and be able to send multiple user IDs, my real function do not accept that by default and can't be changed.
Thanks in advance!
replace
foreach($getUsers->result_array() as $user)
{
$data = $this->get_user_data($user->ID);
var_dump($data); // Only return 1 data;
}
to this
foreach($getUsers->result_array() as $user)
{
$data[] = $this->get_user_data($user->ID);
}
var_dump($data);
If you are aiming at sending more data to the function, you always need to make signature change of your function as one of the below :
function get_user_data() {
$args = func_get_args();
/** now you can access these as $args[0], $args[1] **/
}
Or
function get_user_data(...$user_ids) {
/** now you can access these as $user_ids[0], $user_ids[1] **/
}
// Only higher version of PHP
But I am not sure how you will handle returning data.
EDIT: Yes, then in the function, you can collect data in array and return an array of data from function.
If you can change in your function from where to where_in I think you will get an easy solution.
public function get_user_data($user_ids)
{
// your db code
$this->db->where_in('ID',$user_ids); //replace where with where_in
}
public function user_data()
{
$getUsers = $this->db->get('users');
foreach($getUsers->result_array() as $user)
{
$user_ids[] = $user->ID;
}
$this->get_user_data($user_ids);
}
<?php if(isset($staff['FirstName'])){echo encodeToUtf8($staff['FirstName'];} ?>
code above is the code source of my costume function, I create a function to make my code more shorter than the previous one.
this is my function:
function ifset($table_name, $field_name){
if(isset($table_name[$field_name])){
encodeToUtf8($table_name[$field_name]);
}
return $table_name;
return $field_name;
}
so that I can use the code must shorter like this <?php echo ifset($staff, 'IDNumber'); ?>
but it's not working, it give error : Undefined variable if field has no value and not what a expected.
have someone any idea about this case.
You can't return two statements in a single function. You could return as an array.
function ifset($table_name, $field_name){
if(isset($table_name[$field_name])){
encodeToUtf8($table_name[$field_name]);
}
return array($table_name,$field_name);
}
You can then access it using a list.
list($table, $field) = ifset($staff, 'IDNumber');
The return statement is wrong. Here is the fix:
function ifset($table_name, $field_name)
{
if(isset($table_name[$field_name]))
{
return encodeToUtf8($table_name[$field_name]);
}
return '';
}
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;
}
}
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 two functions getCompanyDetails and getHostingDetails
The first database getCompanyDetails works fine but the getHostingDetails shows
Trying to get property of non-object
getCompanyDetails:
Controller: $data['companyName'] = $this->quote->getCompanyDetails()->companyName;
Model:
public function getCompanyDetails()
{
$this->db->select('companyName,companySlogan,companyContact,
companyEmail,companyWebsite,companyPhone,
companyFax,companyAddress');
$this->db->from('companyDetails');
$result = $this->db->get();
if($result->num_rows()<1)
{
return FALSE;
}else{
return $result->row();
}
}
getHostingDetails:
Controller:
$data['hostingRequired'] = $this->quote->getHostingDetails()->hostingRequired;
Model:
public function getHostingDetails()
{
$this->db->select('hostingRequired,domainRequired,domainToBeReged,
domaintoBeReged0,domainTransfer,domainToBeTransfered,
domainToBeTransfered0,currentHosting');
$this->db->from('hostingDetails');
$result = $this->db->get();
if($result->num_rows()<1)
{
return FALSE;
}else{
return $result->row();
}
}
Well, one method returns an object from $result->row() and the other false. You can't call a method on false.
false is returned when no record is found. So you need to check the return value before using it.
Well in your get functions chances is your code might return you false if there is no rows returned. You might want to check before retrieving the details. Example:
$details = $this->quote->getHostingDetails();
if($details){
$data['hostingRequired'] = $details->hostingRequired;
}
The problem is probably how you use those functions in your controller. If any of them returns FALSE, then
$this->quote->getHostingDetails()->hostingRequired;
is going to give you errors. Try
if ($row = $this->quote->getHostingDetails()) {
echo $row->hostingRequired;
}