<?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 '';
}
Related
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 am trying to make a small PHP function which can check if a constant is defined, and if so, echo it, and if not, echo space or nothing.
Right now, the if(defined() part is not working, because the constant is being transferred to a variable inside the function.
function getConstant($constant) {
if(defined($constant)) {
echo constant($constant);
} else {
echo '';
}
}
The echo constant($constant) part is working fine, but I cannot check if the constant is actually defined because it is a variable now.
I cannot seem to find a solution for it
public static function isConstants($constant) {
$oClass = new ReflectionClass(__CLASS__);
$allConstants = $oClass->getConstants();
if (isset($allConstants[$constant])) {
echo $allConstants[$constant];
} else {
echo '';
}
}
so im making homepage with three languages.
I am using switch method, here is code -
public function languages()
{
if (isset($_GET['lang']) && $_GET['lang'] != '')
{
$_SESSION['lang'] = $_GET['lang'];
}
else
{
$_SESSION['lang'] = 'en_EN';
}
switch($_SESSION['lang'])
{
case 'en_EN': require_once('language/lang.eng.php');break;
case 'lv_LV': require_once('language/lang.lv.php');break;
case 'ru_RU': require_once('language/lang.ru.php');break;
default: require_once('language/lang.eng.php');
}
}
public function translate($txt)
{
global $text;
return $text[$txt];
}
and it should display in index.php file like this -
<?php $index->translate('search'); ?>
but the problem is that it shows no errors, no notices, no warnings and no translated or default text.
I included function languages() , maybe you can help me with this problem?
EDIT:
im calling $language at start of index.php file - <?php require_once('class.index.php'); $index = new index; $index->languages(); ?> and $text is defined in lang.eng.php; lang.lv.php and lang.ru.php file.
Since you're using a class, I think it's better to use properties instead of globals, it will be easier in future mantainance. Create a class variable holding $text and use that
class Index {
var $text;
public function languages()
{
require(".....");
$this->text = $text;
}
public function translate($txt)
{
if(isset($this->text[$txt]))
{
return $this->text[$txt];
}
else
{
return "no translation";
}
}
}
$index = new Index;
$index->languages();
echo $index->translate('search');
type
<?php echo $index->translate('search'); ?>
Check first whether the session is initialized or not and also place the languages() function call before the translate so that it loads the language prior to translation and also put error_reporting(E_ALL) at top so that any error suppresion will be be removed and also put echo the returned result of translate statement
Why PHP class does not return <h1>404</h1><p>Not found</p>?
class checkid{
public $title;
public $content;
function status(){
$this->title='404';
$this->content='Not found';
}
}
$checkid=new checkid($url);
//$id=0;
((isset($id)) ? '' : $checkid->status().$title=$checkid->title.$content=$checkid->content);
//Why it does not return `<h1>404</h1><p>Not found</p>`?
echo "<h1>$title</h1><p>$content</p>";
Update:
I know well to do it trough
if(isset($id)){}else{
$checkid->status();
$title=$checkid->title;
$content=$checkid->content);
}
but I was worndering if it is possible to make it trough using
((isset($id)) ? '' : $checkid->status().$title=$checkid->title/*here $content break it down*//*.$content=$checkid->content*/);
Change isset('id') to isset($id).
The reason why you were getting <h1>404Not found</h1><p>Not found</p> was because you were concatenating the value of $content to the value of $title.
Also, you're code is quite a mess. I took the liberty of cleaning it up a bit:
class checkid
{
public $title;
public $content;
function status()
{
$this->title='404';
$this->content='Not found';
}
}
$checkid=new checkid($url);
//$id=0;
if(!isset($id))
{
$checkid->status();
$title=$checkid->title;
$content=$checkid->content;
}
echo "<h1>$title</h1><p>$content</p>";
You don't assign $title and $content
You probably want something like this
$checkid->status();
echo "<h1>$checkid->title</h1><p>$checkid->content</p>";
Because you are trying to (ab)use a conditional expression as a if statement.
if(!isset($id))
{
$checkid->status();
$title = $checkid->title;
$content = $checkid->content;
}
What isset('id') means? do you mean isset($id) or isset($_GET['id']) ?
And, btw,
$checkid->status().$title=$checkid->title.$content=$checkid->content);
is nonsens: you attach the return of status() to $checkid AND YOU ATTACH $title which is made equal to $checkid->title CONCATENATED to $content, and everythin is assigned $checkid->content
A bit confusing there
I'm working on creating a callback function in codeigniter to see if a certain record exists in the database, and if it does it'd like it to return a failure.
In the controller the relevent code is:
function firstname_check($str)
{
if($this->home_model->find_username($str)) return false;
true;
}
Then in the model I check the database using the find_username() function.
function find_username($str)
{
if($this->db->get_where('MasterDB', array('firstname' => $str)))
{
return TRUE;
}
return FALSE;
}
I've used the firstname_check function in testing and it works. I did something like
function firstname_check($str)
{
if($str == 'test') return false;
true;
}
And in that case it worked. Not really sure why my model function isn't doing what it should. And guidance would be appreciated.
if($this->home_model->find_username($str)) return false;
true;
Given that code snippet above, you are not returning it true. If that is your code and not a typo it should be:
if($this->home_model->find_username($str)) return false;
return true;
That should fix it, giving that you did not have a typo.
EDIT:
You could also just do this since the function returns true/false there is no need for the if statement:
function firstname_check($str)
{
return $this->home_model->find_username($str);
}
So the solution involved taking the query statement out of if statement, placing it into a var then counting the rows and if the rows was > 0, invalidate.
Although this is a more convoluted than I'd like.
I find your naming kind of confusing. Your model function is called 'find_username' but it searches for a first name. Your table name is called 'MasterDB'. This sounds more like a database name. Shouldn't it be called 'users' or something similar? I'd write it like this :
Model function :
function user_exists_with_firstname($firstname)
{
$sql = 'select count(*) as user_count
from users
where firstname=?';
$result = $this->db->query($sql, array($firstname))->result();
return ((int) $result->user_count) > 0;
}
Validation callback function :
function firstname_check($firstname)
{
return !$this->user_model->user_exists_with_firstname($firstname);
}