Check if the variable is empty - php

What I want is to check if the columns $campo01 and $campo02 are empty or not, if not empty it displays on my page, the problem is that if a variable is empty it does not display the result of the variable that is not empty
class Test extends URLDynamic
{
public function Testando( $campo01, $campo02, $linguagem, $audio )
{
$CRUD = new CRUD;
$this->SetParametro();
$VerificaAudio = $this->SelectDados(
"campo01, campo02",
"table_01",
"WHERE mCat = ? AND mSlug = ? AND mStatus = ?",
array ( $this->SepURL[0], $this->SepURL[1], 1 )
);
foreach ( $VerificaAudio as $VerificouAudio ) {
$campo01 = $VerificouAudio['campo01'];
$campo02 = $VerificouAudio['campo02'];
if ( empty ( $campo01 ) ) {
echo 'Empty Campo 01';
} elseif ( empty ( $campo02 ) ) {
echo 'Empty Campo 02';
} else {
// Returns the value of each column.
echo $linguagem;
$Temporadas = $this->SelectDados(
"*",
"table_02",
"WHERE mCat = ? AND mSlug = ? AND tAudio = ? AND tStatus = ?",
array ( $this->SepURL[0], $this->SepURL[1], $audio, 1 )
);
} // end else
} // end foreach
}
}
$Test = new Test;
$Test->Testando( "Legendado", "Legendado" ); // campo01
$Test->Testando( "Dublado", "Dublado" ); // campo02

class Test
{
public $Teste01;
public $Teste02;
public function Testando($Param01, $Param02)
{
$this->Teste01 = '';
$this->Teste02 = 'Has value';
echo $this->checkIfEmpty($this->$Param01);
echo $this->checkIfEmpty($this->$Param02);
}
private function checkIfEmpty($var)
{
if (empty($var)) {
return 'Empty';
} else {
return $var;
}
}
}

Related

How to pass values from a assigned variable to view?

I wanted to pass some values to the view.
The $result may be 0 or 1 or '' and I tried to use this below code:
public function whatwedo($result='')
{
$result = array();
$result['status'] = $result;
$this->load->view('admin/whatwedo',$result);
}
public function add_whatwedo()
{
//$this->load->library('form_validation');
$this->form_validation->set_rules('text-input','Title','required');
if($this->form_validation->run() != true)
{
$result = 0;
$this->whatwedo($result);
}
else
{
$this->load->model('admin_model');
$result = $this->admin_model->ins_whatwedo($this->input->post());
//print_r($result);exit();
$this->whatwedo($result);
}
}
And in the view:
<?php
print_r($status);
?>
But, the $status is Array ( )
The problem is this line:
$result = array();
Because now the $result variable is an empty array so when you create the index status on result you assign it an empty array.
To fix you can do something like:
public function whatwedo($input = '')
{
$result['status'] = $input;
$this->load->view('admin/whatwedo', $result);
}
or even...
public function whatwedo($input = '')
{
$this->load->view('admin/whatwedo', ["status" => $input]);
}

instantiate get form data in a class php

I have a simple php form submitting a query into a few classes. The idea of app is to pass in a band name and check a venue to see if that band is playing. If that band is playing it the app will echo out the band and the date that band is playing. I have managed to get the app to work when its all one php file but i am having trouble spitting the single php file into separate classes.
html form :
<form action = "Ass1.php" method = "GET">
<b>Enter band name : </b>
<input type="text" name="bandName" />
<input name="submit" type="submit" value="Click" />
</form>
The entire php file that works:
<?php
class Concert {
public $Artist = '';
public $Date = '';
function SetConcert( $NewArtist = '', $NewDate = '' ) {
$this->Artist = $NewArtist;
$this->Date = $NewDate;
}
function GetArtist() {
return $this->Artist;
}
function GetDate() {
return $this->Date;
}
}
class Venue {
public $VenueName = '';
public $ConcertsInVenue = array();
function SetVenueName( $NewVenue = '' ) {
$this->VenueName = $NewVenue;
}
function GetVenueName() {
return $this->VenueName;
}
function AddConcert( $NewArtist = '', $NewDate = '' ) {
$NewConcert = new Concert;
$NewConcert->SetConcert( $NewArtist, $NewDate );
$this->ConcertsInVenue[] = $NewConcert;
}
function ListConcertsInVenue( $SearchQuery = '' ) {
//Assume Results Will Be Filtered
$ListAllConcerts = FALSE;
//If Search Query Is Blank, List All Concerts
if( $SearchQuery == '' ){
$ListAllConcerts = TRUE;
}
echo 'Concerts In '.$this->VenueName.':';
//Loop Through All Concerts In This Venue
foreach( $this->ConcertsInVenue as $tmpConcert ){
if( $SearchQuery == $tmpConcert->GetArtist() || $ListAllConcerts == TRUE ){
echo '<p>';
echo $tmpConcert->GetArtist().', ';
echo $tmpConcert->GetDate();
echo '</p>';
}
}
}
}
//Create New Venue Object
$tmpVenue = new Venue;
//Set Venue Name
$tmpVenue->SetVenueName( 'Wembley' );
//Add Concerts To Venue
$tmpVenue->AddConcert( 'Rollin\' Empire', '05-02-2018' );
$tmpVenue->AddConcert( 'Metallica', '06-02-2018' );
$tmpVenue->AddConcert( 'Led Zeppelin', '07-02-2018' );
$tmpVenue->AddConcert( 'Rollin\' Empire', '19-02-2018' );
//Search For Artist In Venue ( Leave Blank To List All )
$tmpVenue->ListConcertsInVenue($_GET['bandName']);
?>
Ive tries creating 3 classes Venue.class.php Concert.class.php and app.class.php and its not working
concert.class.php
<?php
include ("Venue.class.php");
class Concert {
public $Artist = '';
public $Date = '';
function SetConcert( $NewArtist = '', $NewDate = '' ) {
$this->Artist = $NewArtist;
$this->Date = $NewDate;
}
function GetArtist() {
return $this->Artist;
}
function GetDate() {
return $this->Date;
}
}
?>
Venue.class.php
<?php
include ("Concert.class.php");
class Venue {
public $VenueName = '';
public $ConcertsInVenue = array();
function SetVenueName( $NewVenue = '' ) {
$this->VenueName = $NewVenue;
}
function GetVenueName() {
return $this->VenueName;
}
function AddConcert( $NewArtist = '', $NewDate = '' ) {
$NewConcert = new Concert;
$NewConcert->SetConcert( $NewArtist, $NewDate );
$this->ConcertsInVenue[] = $NewConcert;
}
function ListConcertsInVenue( $SearchQuery = '' ) {
//Assume Results Will Be Filtered
$ListAllConcerts = FALSE;
//If Search Query Is Blank, List All Concerts
if( $SearchQuery == '' ){
$ListAllConcerts = TRUE;
}
echo 'Concerts In '.$this->VenueName.':';
//Loop Through All Concerts In This Venue
foreach( $this->ConcertsInVenue as $tmpConcert ){
if( $SearchQuery == $tmpConcert->GetArtist() || $ListAllConcerts == TRUE ){
echo '<p>';
echo $tmpConcert->GetArtist().', ';
echo $tmpConcert->GetDate();
echo '</p>';
}
}
}
}
?>
app.php
<?php
include ("Venue.class.php");
include ("Concert.class.php");
//Create New Venue Object
$tmpVenue = new Venue;
//Set Venue Name
$tmpVenue->SetVenueName( 'Wembley' );
//Add Concerts To Venue
$tmpVenue->AddConcert( 'Rollin\' Empire', '05-02-2018' );
$tmpVenue->AddConcert( 'Metallica', '06-02-2018' );
$tmpVenue->AddConcert( 'Led Zeppelin', '07-02-2018' );
$tmpVenue->AddConcert( 'Rollin\' Empire', '19-02-2018' );
//Search For Artist In Venue ( Leave Blank To List All )
$tmpVenue->ListConcertsInVenue($_GET['bandName']);
?>
Would I need to create a new class in app.php to handle the form input and pass it to the other classes?

count the rows based on array object

I want to count the rows based on like and unlike value in an array.I want to count the rows based on like and unlike value.I need to display like:3 and unlike:1.Now it display like:0,unlike:0 '$content' value is either {"userid":"1","like":"1"} or {"userid":"1","unlike":"1"}
$like_count = 0;
$unlike_count = 0;
while($like_fet = mysql_fetch_assoc($query))
{
$content = $like_fet['CONTENT_VALUE'];
$datas = json_decode($content);
foreach($datas as $item)
{
$like = $item['like'];
$unlike = $item['unlike'];
if($like != '' && $unlike == '')
{
echo "like";
$like_count++;
}
if($unlike != '' && $like == '')
{
echo "unlike";
$unlike_count++;
}
}
}
$like_count=0;
$unlike_count=0;
while($like_fet=mysql_fetch_assoc($query)) {
$json = json_decode($like_fet['CONTENT_VALUE'], true);
if ( isset($json['like']) ) {
$like_count += $json['like'];
}
if ( isset($json['unlike']) ) {
$unlike_count += $json['unlike'];
}
}
depending on how CONTENT_VALUE actually works this can probably simplified to
$like_count=0;
$unlike_count=0;
while($like_fet=mysql_fetch_assoc($query)) {
$json = json_decode($like_fet['CONTENT_VALUE'], true);
if ( isset($json['like']) ) {
$like_count++;
}
else if ( isset($json['unlike']) ) {
$unlike_count++;
}
else {
trigger_error('CONTENT_VALUE without like/unlike element', E_USER_NOTICE);
}
}

like clause in codeigniter

I want to include like clause in the case of last_name.This is my controller code used for search box.I want to compare form value last_name with db value family_name.
public function filtered_volunteer_details()
{
$user_data = $this->session->userdata("logged_user");
$tab_result = array();
if (is_array($user_data) && $user_data["user_role"] == "staff")
{
if ($this->input->method() == "post")
{
$condition = array();
$data = $this->input->post();
if(isset($data["membership_number"]) && $data["membership_number"]!= "")
{
$condition["membership_number"]=trim($data["membership_number"]);
}
if(isset($data["last_name"]) && $data["last_name"]!= "")
{
$condition["family_name "]=trim($data["last_name"]);
}
$this->load->model(array(
"Users"));
$result = $this->Users->get_volunteers("*",$condition);
$volunteer_tbl_list=array();
foreach($result as $volunteer_results)
{
$volunteer_tbl_list[] = $this->load->view("staff/manage_volunteer/volunteer_list_tbl_row", $volunteer_results, TRUE);
}
echo json_encode(array(
"status" => "success",
"volunteer_tbl_list" => $volunteer_tbl_list));
return;
}
}
echo json_encode(array(
"error" => "true",
"msg" => "Invalid Access."));
return;
}
This is my model code
public function get_volunteers($fields = "*",$condition=array())
{
$condition["staff"]="N";
$this->db->select($fields);
$query = $this->db->get_where(self::$tbl_name, $condition);
//var_dump($this->db->last_query());
return( $query->result() );
}
Try adding a $this->db->like() to the query in get_volunteers(). documentation
public function get_volunteers($fields = "*",$condition=array())
{
$condition["staff"]="N";
$this->db->select($fields);
$this->db->like($condition);
$this->db->from(self::$tbl_name);
return( $query->result() );
}

add icon to error message using php session

I have this class for show error with session method using PHP.
class Messages {
//-----------------------------------------------------------------------------------------------
// Class Variables
//-----------------------------------------------------------------------------------------------
var $msgId;
var $msgTypes = array( 'help', 'info', 'warning', 'success', 'danger' );
var $msgClass = 'alert';
var $msgWrapper = " <div class='alert %s-%s flashit'>
<button class='close' aria-hidden='true' data-dismiss='alert' type='button'>×</button>
<p><i style='vertical-align: middle;' class='%s icon-2x'></i> %s</p>
</div>";
var $msgBefore = '';
var $msgAfter = "";
public function __construct() {
// Generate a unique ID for this user and session
$this->msgId = md5(uniqid());
// Create the session array if it doesnt already exist
if( !array_key_exists('flash_messages', $_SESSION) ) $_SESSION['flash_messages'] = array();
}
public function add($type, $message, $redirect_to=null) {
if( !isset($_SESSION['flash_messages']) ) return false;
if( !isset($type) || !isset($message[0]) ) return false;
// Replace any shorthand codes with their full version
if( strlen(trim($type)) == 1 ) {
$type = str_replace( array('h', 'i', 'w', 'e', 's'), array('help', 'info', 'warning', 'danger', 'success'), $type );
$icon = str_replace( array('h', 'i', 'w', 'e', 's'), array('fa-help', 'fa-info', 'fa-warning', 'fa-danger', 'fa-success'), $type );
// Backwards compatibility...
} elseif( $type == 'information' ) {
$type = 'info';
$icon = 'fa-info';
}
// Make sure it's a valid message type
if( !in_array($type, $this->msgTypes) ) die('"' . strip_tags($type) . '" is not a valid message type!' );
// If the session array doesn't exist, create it
if( !array_key_exists( $type, $_SESSION['flash_messages'] ) ) $_SESSION['flash_messages'][$type] = array();
$_SESSION['flash_messages'][$type][] = $message;
if( !is_null($redirect_to) ) {
header("Location: $redirect_to");
exit();
}
return true;
}
//-----------------------------------------------------------------------------------------------
// display()
// print queued messages to the screen
//-----------------------------------------------------------------------------------------------
public function display($type='all', $print=true) {
$messages = '';
$data = '';
if( !isset($_SESSION['flash_messages']) ) return false;
if( $type == 'g' || $type == 'growl' ) {
$this->displayGrowlMessages();
return true;
}
// Print a certain type of message?
if( in_array($type, $this->msgTypes) ) {
foreach( $_SESSION['flash_messages'][$type] as $msg ) {
$messages .= $this->msgBefore . $msg . $this->msgAfter;
}
$data .= sprintf($this->msgWrapper, $this->msgClass, $type,$icon,$messages);
// Clear the viewed messages
$this->clear($type);
// Print ALL queued messages
} elseif( $type == 'all' ) {
foreach( $_SESSION['flash_messages'] as $type => $msgArray ) {
$messages = '';
foreach( $msgArray as $msg ) {
$messages .= $this->msgBefore . $msg . $this->msgAfter;
}
$data .= sprintf($this->msgWrapper, $this->msgClass, $type,$icon,$messages);
}
// Clear ALL of the messages
$this->clear();
// Invalid Message Type?
} else {
return false;
}
// Print everything to the screen or return the data
if( $print ) {
echo $data;
} else {
return $data;
}
}
//..... more
}
Call:
$msg = new Messages();
$msg->add('i', 'This is a Information message!');
echo $msg->display();
Now in Output:
<i style="vertical-align: middle;" class=" icon-2x"></i>
Icon class not printed and empty: class=" icon-2x". how do can i fix this ?
EDit: Indeed i need to print for each type One class name.

Categories