function givemeposts($user, $pos){
$sql = "select * from posts where user ='" . $user . "' and pos = '" . $pos . "' order by inde";
...
givemeposts('public', 'home01'); // this works as usual
Now I nedd to call the above function, but without $pos param, i.e. whatever $pos value is.
Something like:
givemeposts('public', whatever);
Any way to do this?
You can use default parameter :
function givemepost( $user , $pos=NULL ){
if($pos==NULL){
//some code
}lese{
$sql = "select * from posts where user ='" . $user . "' and pos = '" . $pos . "' order by inde";
}
}
This way you can call the function with one paramter, and set what the function have to do, inside the if branch.
In PHP you can set default values for arguments
The default value must be a constant expression, not (for example) a variable, a class member or a function call.
Note that when using default arguments, any defaults should be on the right side of any non-default arguments; otherwise, things will not work as expected.
If you do this, the default value for $pos will be '' (an empty string):
function givemeposts($user, $pos = ''){
Or you can something else than an empty string, whatever value you want:
function givemeposts($user, $pos = 'SomeValue'){
Then you can simply call your function like this:
givemeposts($user);
You can try this:
function givemeposts($user, $pos = '')
{
if(!empty($pos)) {
$pos = "AND pos = '" . $pos . "'";
}
$sql = "SELECT * FROM posts WHERE user ='" . $user . "' $pos ORDER BY inde";
}
givemeposts('public');
Related
I am trying to work on someones code and ran into this.
private function getAttImages($limit, $forumIds = 0, $fidsReverse = false, $topicIds = 0, $membersIds = 0, $order = 'attach_date', $sort = 'desc', $group = null)
{
$fids = '';
if ($forumIds)
{
$r = '';
if ($fidsReverse)
{
$r = ' NOT ';
}
if (is_array($forumIds))
{
$forumIds = implode(',', $forumIds);
}
$fids = ' AND forums_topics.forum_id ' . $r . ' IN (' . $forumIds . ')';
}
function continues to other things. However, the question is that first if statement if($forumIds) wouldn't it be useless if every time this function is called $forumIds is set to 0 ?
This is the default value for this function if nothing else is specified. It means that if nothing is entered when calling the function, it will default be 0 and the function will essentially do nothing.
Examples:
getAttImages(5, 1)
Will essentially set $limit to 1 and $forumids to 1. The rest of the parameters will be set to their default values as nothing is entered ( $fidsReverse = false, $topicIds = 0, $membersIds = 0, $order = 'attach_date', $sort = 'desc', $group = null )
The only required parameter is limit as it has no default value in it. So, at minimum the function can be called like so:
getAttImages(0); and the rest will just default to the values defined in the function. However, this code won't do anything as $forumIds will be 0.
No. $forumIds is set to zero in the function parameters, but that zero is only applied to $forumIds if someone calls the function but does not explicitly set a value for that parameter.
The code
..., $forumIds = 0, ...
is setting up the default for that variable. It can be overridden with any value when called, but will default to 0 if no value for $forumIds is provided.
See the PHP documentation: http://php.net/manual/en/functions.arguments.php#functions.arguments.default.
public function test_passing_string() {
$this - > load - > model(array('registration/Registration_model', 'Jawaban_lab_model'));
$registration = new Registration_model();
$jawaban_lab = new Jawaban_lab_model();
$id = "kuda4";
$jawaban_lab - > load($id); //load jawaban_lab from id
$manualy_written_registration_number = "REG/FM/130102-0001";
echo "registration number from jawaban_lab->registration_number : ".$jawaban_lab - > registration_number
.
"<br> registration number from manualy_written_registration_number : ".$manualy_written_registration_number;
//$registration->load($jawaban_lab->registration_number);
$registration - > load($manualy_written_registration_number);
echo "<br> patient id : ".json_encode($registration - > PatientID);
}
Before go to the question, I will explain my code.
On test_passing_string() function, I call 2 model, and create object for each model there are $registration and $jawaban_lab.
To load data from model I create a load() function. load() has two parameters: column_value and column_name. The default value for column_name is that model's Primary Key.
BUT
The problem comes from
$registration->load($jawaban_lab->registration_number);
I can't retrieve any $registration object data, then I test it by passing the value manually by write this:
$manualy_written_registration_number = "REG/FM/130102-0001";
$registration - > load($manualy_written_registration_number);
And the result appear, doesn't that mean my load() function is fine?
Then I check value inside $jawaban_lab->registration_number by echoing it, surprisingly it display same value as my $manualy_written_registration_number variable.
This is screenshoot in my browser when I run test_passing_string() function:
Using $manualy_written_registration_number value
Using $jawaban_lab->registration_number value
Why can't I use the value from
$jawaban_lab->registration_number even though it has the same value as
my manually writen registraiton number?
public function load($column_value, $column_name = NULL) {
$query = NULL;
if ($column_name != NULL) {
// using custom column.
$query = $this->dbs->get_where($this::DB_TABLE, array(
$column_name => $column_value
));
} else {
// using column primary key .
$query = $this->dbs->get_where($this::DB_TABLE, array(
$this::DB_TABLE_PK => $column_value
));
}
if ($query->row()) {
$this->populate($query->row());
}
}
I use multiple database using CodeIgniter 3, registration_model from SQL server and jawaban_lab from MySQL, jawaban lab have column registration_number to store registration_model primary key
var_dump
First of all thanks to rlanvin and Nirajan N Raju
from rlanvin's comment, i find out the problem is come from codeigniter's query helper, because when i enable codeigniter profiling sql server query return "SELECT CASE WHEN (##OPTIONS | 256) = ##OPTIONS THEN 1 ELSE 0 END AS qi"
so i think codeigniter might be cannot generate query so i create the query manually
i change
public function load($column_value, $column_name = NULL) {
$query = NULL;
if ($column_name != NULL) {
// using custom column.
$query = $this->dbs->get_where($this::DB_TABLE, array(
$column_name => $column_value
));
} else {
// using column primary key .
$query = $this->dbs->get_where($this::DB_TABLE, array(
$this::DB_TABLE_PK => $column_value
));
}
if ($query->row()) {
$this->populate($query->row());
}
}
to this
public function load($column_value, $column_name = NULL) {
$query = NULL;
if ($column_name != NULL) {
$query = $this->dbs->query("SELECT * FROM " . $this::DB_TABLE . " WHERE " . $column_name . " LIKE '" . trim($column_value) . "'");
} else {
$query = $this->dbs->query("SELECT * FROM " . $this::DB_TABLE . " WHERE " . $this::DB_TABLE_PK . " LIKE '" . trim($column_value) . "'");
}
if ($query->row()) {
$this->populate($query->row());
}
}
I a, trying to implement a modification to the registration process in opencart. I have modified the form in the view and the values are posted to the account/customer.php model in which I have added some code to process my new fields as shown below:
/* add children and links to product categories */
$this->load->model('account/children');
//loop childrens names to use key for remaining info
foreach($data['children-name'] as $key=>$name){
//re-assign data and purify
$child['name'] = mysql_real_escape_string($name);
$child['surname'] = mysql_real_escape_string($data['children-surname'][$key]);
$child['gender'] = mysql_real_escape_string($data['children-gender'][$key]);
$child['dob'] = mysql_real_escape_string($data['children-dob'][$key]);
$child['school'] = mysql_real_escape_string($data['children-school'][$key]);
$child['unit'] = mysql_real_escape_string($data['children-unit'][$key]);
$child['height'] = mysql_real_escape_string($data['children-height'][$key]);
$child['chest'] = mysql_real_escape_string($data['children-chest'][$key]);
$child['waist'] = mysql_real_escape_string($data['children-waist'][$key]);
$child['waistToKnee'] = mysql_real_escape_string($data['children-waist-to-knee'][$key]);
$child['insideLeg'] = mysql_real_escape_string($data['children-inside-leg'][$key]);
$child['shoeSize'] = mysql_real_escape_string($data['children-shoe-size'][$key]);
$child['customerId'] = $customer_id;
//update/create child record
$this->model_account_children->addChild($child);
}
/* end add children and links to product categories */
I then created the relative model file in account/children.php in which has the following code:
class ModelAccountChildren extends Model {
public function addChild($data) {
//create other fields not in $data
$lastUpdated = date('Y-m-d h:i:s');
$childCat = getChildCategory($data['school']);
$dob = date('Y-m-d h:i:s', $date['dob']);
if($data['gender'] == 'm'){
$data['gender'] = 'Male';
}else{
$data['gender'] = 'Female';
}
echo "INSERT INTO " . DB_PREFIX . "customer_children (customer_id, child_name, child_surname, child_gender, child_dob, category_id, child_school, child_unit, child_height, child_chest, child_waist, child_waist_to_knee, child_inside_leg, child_shoe_size, child_last_updated) VALUES (".$data['customerId'].", '".$data['name']."', '".$data['surname']."', '".$data['gender']."', '".$dob."', ".$childCat['category_id'].", '".$data['school']."', '".$data['unit']."', '".$data['unit']."', '".$data['height']."', '".$data['chest']."', '".$data['waist']."', '".$data['waistToKnee']."', '".$data['insideLeg']."', '".$data['shoeSize']."', '".$lastUpdated."')";
//perform insert
$this->db->query("INSERT INTO " . DB_PREFIX . "customer_children (customer_id, child_name, child_surname, child_gender, child_dob, category_id, child_school, child_unit, child_height, child_chest, child_waist, child_waist_to_knee, child_inside_leg, child_shoe_size, child_last_updated) VALUES (".$data['customerId'].", '".$data['name']."', '".$data['surname']."', '".$data['gender']."', '".$dob."', ".$childCat['category_id'].", '".$data['school']."', '".$data['unit']."', '".$data['unit']."', '".$data['height']."', '".$data['chest']."', '".$data['waist']."', '".$data['waistToKnee']."', '".$data['insideLeg']."', '".$data['shoeSize']."', '".$lastUpdated."')");
}
public function updateChild($data) {
}
public function getChildCategory($childSchoolStr){
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category_description WHERE name LIKE '" . $childSchoolStr . "'");
return $query->row;
}
}
For some reason the script is not getting access to the new model at the point I call it in model/account/customer.php on the line:
$this->load->model('account/children');
Am I doing something wrong?
Worked it out chaps.
$childCat = getChildCategory($data['school']);
Bit of a brain lapse, line above should be:
$childCat = $this->getChildCategory($data['school']);
It seems correct to me. You can use it within model. Do you use vqmod? Try deleting cache files.
Maybe add a test function to your model and see you can call it.
What is the error you get?
Have you definitely put the file in catalog/model/account/children.php ? If so, do you get any error messages at all that would help debug this? Also, when you run the code, are you certain that $data['children-name'] contains data?
I am trying to work this function in wordpress theme the way where I can get value only what I want and not all to gather. I am not much familiar with using array in function so I need you expert help to make it works and I would really appreciate that.
Here is my code
function gallery_artist($arg=null, $arg2=null, $arg3=null){
global $png_gallery_meta;
$png_gallery_meta->the_meta();
$gallery = get_post_meta(get_the_ID(), $png_gallery_meta->get_the_id(), TRUE);
$gallery_value = $gallery['image_artist'];
$artist_info = $gallery_value;
$string = $artist_info;
$artist = substr($string, 0, stripos($string, "/") );
// getting first name and last name from user id
$author_first_name = get_userdata(basename($string))->first_name;
$author_last_name = get_userdata(basename($string))->last_name;
$author_full_name = $author_first_name . ' ' . $author_last_name;
$user_id = '<a href=" ' . get_author_posts_url(basename($string)) . ' " title="more submissions of ' . $author_first_name .' '. $author_last_name . ' " >' . $artist . '</a>';
$arg = $author_first_name;
echo $arg;
$arg2 = $author_last_name;
echo $arg2;
$arg3 = $user_id;
echo $arg3;
}
After than I want to use this function to get any value I want and not unnecessary to render all three to gather. Means if I want only first name than I pass value only for that and it will render only first name so on..
Here how I want to use something. but you can suggest any code and type of function I have no issue.
<?php call_user_func_array('gallery_artist', array('first_name','last_name','artist_id') ) ?>
Big Big thanks to you all..
Try replacing the bottom section with this:
if(!is_null($arg))
{
$arg = $author_first_name;
echo $arg;
}
if(!is_null($arg2))
{
$arg2 = $author_last_name;
echo $arg2;
}
if(!is_null($arg3))
{
$arg3 = $user_id;
echo $arg3;
}
So I have these:
echo ('timeframe1:');echo ($timeframe);
function filter_where($where = '') {
echo ('timeframe2:');echo ($timeframe);
$where .= " AND post_date > '" . date('Y-m-d', strtotime("-$timeframe days")) . "'";
return $where;
}
echo ('timeframe3:');echo ($timeframe);
Result from those codes above are:
timeframe1: 5
timeframe2:
timeframe3: 5
Question is, how to get my $timeframe value inside the function? As you can see, the result from echoing the $timeframe inside the function is null. How get the predefined $timeframe value of 5 inside the function?
use GLOBAL to pass variable that is outside of a function
function filter_where($where = '') {
global $timeframe; // <---- pass it as global
echo ('timeframe2:');echo ($timeframe);
$where .= " AND post_date > '" . date('Y-m-d', strtotime("-$timeframe days")) . "'";
return $where;
}
OR You call pass $timeframe as a parameter to your function
function filter_where($where = '', $timeframe ) /* here we pass a variable into function */ {
echo ('timeframe2:');echo ($timeframe);
$where .= " AND post_date > '" . date('Y-m-d', strtotime("-$timeframe days")) . "'";
return $where;
}
call function:
filter_where('', $timeframe);
it's not that clean as variant with GLOBAL, but still work though.