i've wrote this code,
<?php
class fizzbuzz{
function mod3($angka)
{
$a = $angka % 3;
if($a==0) return true;
else return false;
}
function mod5($angka)
{
$b = $angka % 5;
if($b==0) return true;
else return false;
}
function index(){
for ($i=1; $i < 101; $i++) {
if(mod3($i) == true && mod5($i) == true){
echo "fizzbuzz, ";
}else if(mod3($i) == true){
echo "fizz, ";
}else if(mod5($i) == true){
echo "buzz, ";
}else echo $i.", ";
}
}
}
$show = new fizzbuzz;
$show->index();
?>
and then it came up with this error
Fatal error: Call to undefined function mod3() in C:\xampp\htdocs\tes-bimasakti\fizzbuzz.php on line 19
please help me with this error..
You forgot $this->:
if($this->mod3($i) == true && $this->mod5($i) == true){
^^^^^^^--- here ^^^^^^^---here
without $this->, php is looking for a top-level global function. It will NOT look for a method in your object.
use this keyword
function index(){
for ($i=1; $i < 101; $i++) {
if($this->mod3($i) == true && $this->mod5($i) == true){
echo "fizzbuzz, ";
}else if($this->mod3($i) == true){
echo "fizz, ";
}else if($this->mod5($i) == true){
echo "buzz, ";
}else echo $i.", ";
}
}
Related
This is my Controller code
if($university == "")
{
$university = "-";
}else if($university[0] == ""){
$university = "-";
}else if(isset($university[0])){
$university = implode(",",$university);
}else{
$university = $university;
}
How to pass argument in implode() ?
You passed implode argument is ok but your second argument is not an array.
if($university == "")
{
$university = "-";
}else if($university[0] == ""){
$university = "-";
}else if(isset($university[0])){
$university = implode(",",$university); //<-- This argument is not an array
}else{
$university = $university;
}
I wrote the code snippet inside my Laravel controller and I want to sort the products by product number first and then sort the previously sorted products by inventory.
My problem is that the first sort is executed inside the written command but the second sort does not happen.
My code snippet is as follows:
DB::statement("SET SQL_MODE=''");//this is for fix groupby error!
$productdetail = Sa_product::leftJoin('sa_product_allproperties', 'sa_products.productid', 'sa_product_allproperties.product_id')->where('sa_products.product_status', '1');
if (isset($data['static']) && $data['static']['search_products'] != '') {
$search_word = $data['static']['search_products'];
DB::statement("SET SQL_MODE=''");//this is for fix groupby error!
$exploded_word = explode(',', $search_word);
$counter = 0;
$productdetail = $productdetail->where(function ($q) use ($exploded_word, $counter) {
$counter = 0;
foreach ($exploded_word as $word) {
if ($counter == 0) {
$q->where('sa_products.title', 'LIKE', "%{$word}%");
} else {
$q->orwhere('sa_products.title', 'LIKE', "%{$word}%");
}
$counter++;
}
});
}
$data['staticcats'] = explode(',', $data['static']['product_cats']);
if (isset($data['static']) && $data['static'] != '') {
$explodecats = explode(',', $data['static']['product_cats']);
unset($explodecats[0]);
array_pop($explodecats);
} else {
$explodecats = [];
}
if (isset($explodecats) && $explodecats != []) {
$counter = 0;
$productdetail = $productdetail->where(function ($q1) use ($explodecats, $counter) {
$counter = 0;
foreach ($explodecats as $cats) {
if ($counter == 0) {
$q1->where('sa_products.catid', 'LIKE', "%,{$cats},%");
} else {
$q1->orWhere('sa_products.catid', 'LIKE', "%,{$cats},%");
}
$counter++;
}
});
$productdetail = $productdetail->orderBy('visited_counter', 'DESC');
}
$data['staticcats'] = explode(',', $data['static']['product_brands']);
if (isset($data['static']) && $data['static'] != '') {
$explodebrands = explode(',', $data['static']['product_brands']);
unset($explodebrands[0]);
array_pop($explodebrands);
} else {
$explodebrands = [];
}
if (isset($explodebrands) && $explodebrands != []) {
$counter = 0;
$productdetail = $productdetail->where(function ($q2) use ($explodebrands, $counter) {
$counter = 0;
foreach ($explodebrands as $brands) {
if ($counter == 0) {
//$q1->where('brand_id','LIKE', "%,{$brands},%");
$q2->where('sa_products.brand_id', "{$brands}");
} else {
//$q1->orWhere('brand_id','LIKE', "%,{$brands},%");
$q2->orWhere('sa_products.brand_id', "{$brands}");
}
$counter++;
}
});
}
$productdetail = $productdetail->orderBy('sa_products.productid','DESC')->orderBy('sa_product_allproperties.stock_count','DESC')->groupBy('sa_products.productid')->paginate(20);
if (isset($productdetail) && $productdetail->count()>0)
{
$data['searched_products'] = $productdetail;
}
After executing the above code, the received products are sorted by product number, ie the orderBy('sa_products.productid', 'DESC') occurs, but the second sorting is done with the orderBy('sa_product_allproperties.stock_count', 'DESC') Which is not run for inventory sorting.
Can anyone help me solve this problem?
Hello guys i have gone through all 25 questions relating to the title of my question and i am made to believe my scenario is totally different.
Here is the error
Fatal error: Uncaught Error: Call to a member function set_type() on null in /path_to_file/login.php:55 Stack trace: #0 /path_to_call_page/login.php(24): login->authenticate() #1 {main} thrown in /path_to_file/login.php on line 55
Here is my code
<?php
class login
{
public $username;
public $password;
public $err;
public $table_name;
public $session_data;
public $md5 = true;
public $username_column = "username";
public $password_column = "password";
public $builder;
function _construct($username,$password)
{
$this->username = $username;
$this->password = $password;
$this->builder = new queryBuilder();
}
function set_table($tablename)
{
$this->table_name = $tablename;
}
/*
* Tells the login class where to find the username and password in database table
*/
function set_columns($username_col,$password_col)
{
$this->username_column = $username_col;
$this->password_column = $password_col;
}
function md5_on()
{
$this->md5 = true;
}
function md5_off()
{
$this->md5 = false;
}
function authenticate()
{
$db = new mySQLConnection();
$db->select();
// if md5 is turned on
if($this->md5)
$this->password = md5($this->password);
$this->builder->set_type("SELECT");
$this->builder->set_table_name($this->table_name);
$this->builder->set_where("WHERE $this->username_column = '$this->username' and $this->password_column = '$this->password'");
$query = $this->builder->build_query();
if($db->execute_query($query))
$data = $db->fetch($db->result);
else
{
die($db->error);
}
if($db->rows_affected() == 1)
{
$this->session_data = $data[0];
return true;
}
else
{
$this->err = "Invalid username or password. Please try again";
return false;
}
}
function get_error()
{
return $this->err;
}
}
?>
The error occurs everywhere i have
$this->builder
And i have defined it in the _construct method.
This is the queryBuilder class
class queryBuilder
{
var $data;
var $field;
var $tableName;
var $databaseName;
var $where;
var $order;
var $group;
var $limit;
var $queryString;
var $error;
private function put_quotes1($field)
{
$field = trim($field);
$field = "`".$field."`";
return $field;
}
private function put_quotes2($field)
{
$field = trim($field);
$field = "'".$field."'";
return $field;
}
function set_type($type)
{
$this->type = $type;
}
function set_data($data)
{
$this->data = $data;
}
function set_field($field = null)
{
$this->field = $field;
}
function set_where($where)
{
$this->where = $where;
}
function set_limit($limit)
{
$this->limit = $limit;
}
function set_order($order)
{
$this->order = $order;
}
function set_table_name($name)
{
$this->tableName = $name;
}
function prepare_data($data)
{
if(is_array($data))
{
foreach($data as $k => $v)
{
$this->field[] = $k; //setting the column names
$this->data[] = $v; // setting the values
}
}
}
function build_query()
{
switch($this->type)
{
case 'SHOW':
$database_name = $this->put_quotes1($this->databaseName);
$this->queryString = "SHOW ";
if(!isset($this->field) || is_null($this->field))
$this->queryString .= "DATABASES LIKE 'thirdeye%'; ";
else{
$noFields = count($this->field); //no of fields in table
for($i = 0; $i < $noFields; $i++)
{
if($i == ($noFields- 1)) // if on the last field
$this->queryString .= $this->put_quotes1($this->field[$i]).' ';
else
$this->queryString .= $this->put_quotes1($this->field[$i]).',';
}
}
break;
case 'INSERT':
$table_name = $this->put_quotes1($this->tableName);
$this->queryString = "INSERT INTO ".$table_name." (";
$noFields = count($this->field);
$noData = count($this->data);
if($noFields > 0 && $noData > 0)
{
for($i = 0; $i < $noFields; $i++)
{
if($i == ($noFields- 1))
$this->queryString .= $this->put_quotes1($this->field[$i]).')';
else
$this->queryString .= $this->put_quotes1($this->field[$i]).',';
}
$this->queryString.= " VALUES (";
for($i = 0; $i < $noData; $i++)
{
if($i == ($noData -1))
$this->queryString .= $this->put_quotes2($this->data[$i]).');';
else
$this->queryString .= $this->put_quotes2($this->data[$i]).',';
}
}
else
{
$this->error = "No column name or data was supplied";
}
break;
case 'SELECT':
$table_name = $this->put_quotes1($this->tableName);
$this->queryString = "SELECT ";
if(!isset($this->field) || is_null($this->field))
$this->queryString .= "* ";
else{
$noFields = count($this->field); //no of fields in table
for($i = 0; $i < $noFields; $i++)
{
if($i == ($noFields- 1)) // if on the last field
$this->queryString .= $this->put_quotes1($this->field[$i]).' ';
else
$this->queryString .= $this->put_quotes1($this->field[$i]).',';
}
}
$this->queryString .= "FROM ".$table_name;
if(isset($this->where))
$this->queryString .= " ".$this->where;
if(isset($this->order))
$this->queryString .= " ".$this->order;
if(isset($this->limit))
$this->queryString .= " ".$this->limit;
else
$this->queryString .= ";";
break;
case 'UPDATE':
$table_name = $this->put_quotes1($this->tableName);
$this->queryString = "UPDATE ". $table_name. " SET ";
$noFields = count($this->field); //no of fields in table
if(is_array($this->field) && is_array($this->data) && isset($this->where))
{
for($i = 0; $i < $noFields; $i++)
{
if($i == ($noFields -1))
$this->queryString .= $this->put_quotes1($this->field[$i])." = ". $this->put_quotes2($this->data[$i]).' ';
else
$this->queryString .= $this->put_quotes1($this->field[$i])." = ". $this->put_quotes2($this->data[$i]).',';
}
$this->queryString .= " ".$this->where.";";
}
else
{
$this->error = "Cannot build query. One of the following was not set";
}
break;
case 'DELETE':
$table_name = $this->put_quotes1($this->tableName);
$this->queryString = "DELETE FROM ".$table_name;
if(isset($this->where))
{
$this->queryString .= " ".$this->where.";";
}
else
{
$this->error = "Connot build. No condition was set";
}
break;
}
return $this->queryString;
}
}
Any pointers would help. Remember i have been through previous questions so a suggested edit or code answer would be great.
here is the validation code :
public function registration () {
//
//
// -- code
$this->form_validation->set_rules('password', 'Password', 'required|callback__check_length[6,10]');
}
function _check_length($input, $min, $max)
{
$length = strlen($input);
if ($length <= $max && $length >= $min)
{
return TRUE;
}
elseif ($length < $min)
{
$this->form_validation->set_message('_check_length', 'Minimum number of characters is ' . $min);
return FALSE;
}
elseif ($length > $max)
{
$this->form_validation->set_message('_check_length', 'Maximum number of characters is ' . $max);
return FALSE;
}
}
it is giving me error :
Message: Missing argument 3 for Person::_check_length(), called in C:\wamp64\www\abc\system\libraries\Form_validation.php on line 744 and defined
You can do so by exploding the 2nd param. CI doesn't seem to support 3 params in function args for form validation rules:
public function _check_length($input, $minmax) {
$minmax = explode(',', $minmax);
$min = $minmax[0];
$max = $minmax[1];
$length = strlen($input);
if ($length <= $max && $length >= $min) {
return TRUE;
} elseif ($length < $min) {
$this->form_validation->set_message('_check_length', 'Minimum number of characters is ' . $min);
return FALSE;
} elseif ($length > $max) {
$this->form_validation->set_message('_check_length', 'Maximum number of characters is ' . $max);
return FALSE;
}
}
You also don't have to have your own function to do this. You can simply use min_length[x] and max_length[x] rules.
https://www.codeigniter.com/userguide3/libraries/form_validation.html#rule-reference
So I am using session to hold username and then check if its there. if yes, then use it to update related to current transaction. The following is my code:
public function update_edit()
{
/* echo " //// INSIDE UPDATE EDIT "; */
$this->form_validation->set_rules('fullname', 'الاسم الكامل', 'isset|required|min_length[6]|max_length[100]');
//echo 'Username: ';
//check that there are no form validation errors
if($this->form_validation->run() == FALSE)
{
echo " //// INSIDE FORM VALIDATION";
if(($this->session->userdata('username')!=""))
{
echo " //// INSIDE SESSION VALIDATION";
$data = array();
$session_user = $this->session->userdata('username');
if(isset($session_user) && $session_user != '' && $session_user != null)
{
$data = $this->profileModel->load_user_editable_data($session_user);
$this->load->view('layout/header');
$this->load->view('profile_edit', $data);
$this->load->view('layout/footer');
}else{
$this->error_page();
}
}else{
//$this->load->view('login');
$this->login();
}
}else{
echo "dasdaskhdkjashjdas";
$session_user = $this->session->userdata('username');
echo "Session User: " . $session_user;
if(isset($session_user) && $session_user != '' && $session_user != null)
{
echo "going to update;";
$complete = $this->profileModel->update_profile($session_user);
echo "<br>finished updating...";
if($complete == 1)
{
echo "Complete = " . $complete;
$this->load->view('layout/header');
$this->load->view('update_complete');
$this->load->view('layout/footer');
}elseif($complete == 2){
echo "Complete = " . $complete;
$this->error_page();
}
}
}
}
The problem here is that after echo dasdaskhdkjashjdas, nothing the session check doesn't work. I printed the value and it is empty. How can this be when in the function before it worked well and loaded the data? This function is supposed to check the loaded data changed or not, then submit that new data.
Please help :)
Change this:
if(isset($session_user) && $session_user != '' && $session_user != null)
to this:
if(!empty($session_user))