PHP: how can I add a specific title to my flash message? - php

I'm working on a flash message with PHP, but I would like to have a specific title for each of my flash message. My container for the flash message is set in my header.php so I can use this system everywhere on my site:
<?php if(isset($_SESSION["flash-message"])): ?>
<?php foreach($_SESSION["flash-message"] as $type => $message): ?>
<div class="alert alert-<?= $type; ?>">
<p><?= $message; ?></p>
</div>
<?php endforeach; ?>
<?php endif; ?>
To execute it, I use this:
$_SESSION["flash-message"]["success"] = "Test";
So, I have that:
Now, I would like to have a title, you know? Something like that:
$_SESSION["flash-message"]["title" = "The title"]["success"] = "The message"
Just something like that. I want the title above the message.

Short answer: use an array or object to store your message with corresponding title.
$_SESSION["flash-message"]["success"] = array("Title", "The Message");
And your code will be like following:
<?php if(isset($_SESSION["flash-message"])): ?>
<?php foreach($_SESSION["flash-message"] as $type => $message): ?>
<div class="alert alert-<?= $type; ?>">
<h2><?= $message[0]; ?></h2>
<p><?= $message[1]; ?></p>
</div>
<?php endforeach; ?>
<?php endif; ?>
EDIT:
And what happens if you have more than one success message? Your current code doesn't support multiple "same type" messages. It should be:
$_SESSION["flash-message"]["success"][] = array("Title", "The Message");
to store multiple "same-type" messages.
EDIT-2:
You can also use object approach - more readable. Also contains multiple message ability I mentioned above.
<?php
$message = new stdClass();
$message->title = "Title";
$message->message = "The Message";
$_SESSION["flash-message"]["success"][] = $message;
?>
<?php if(isset($_SESSION["flash-message"])): ?>
<?php foreach($_SESSION["flash-message"] as $type): ?>
<?php foreach ($type as $message): ?>
<div class="alert alert-<?= $type; ?>">
<h2><?= $message->title; ?></h2>
<p><?= $message->message; ?></p>
</div>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endif; ?>

$_SESSION["flash-message"] should be an array of messages, and a message should have an event_type, a title and a content.
You can model messages in many forms, with classes or associative arrays. See this example message modeled with an associative array:
$a_message = [
"event_type" => "success",
"title" => "The title"
"content" => "The message"
];
This way, you could implement your foreach loop like this:
<?php if(isset($_SESSION["flash-message"])): ?>
<?php foreach($_SESSION["flash-message"] as $message): ?>
<div class="alert alert-<?= $message["event_type"]; ?>">
<h1><?= $message["title"]; ?></p>
<p><?= $message["content"]; ?></p>
</div>
<?php endforeach; ?>
<?php endif; ?>

Related

PHP - unset function unsets my var before my code

I would like to clear my session vqriqble "flash' using the unset function but now I can't print my flash message because the unset function is called before my previous code.
It is called in my header template : header.php wich is called in every page at the beginning of the code.
This is the code :
<?php if(isset($_SESSION['flash'])): ?>
<?php foreach($_SESSION['flash'] as $type => $message): ?>
<div class="alert alert-<?= $type; ?> bk-fullwidth-alert">
<p><?= $message; ?></p>
</div>
<?php endforeach; ?>
<?php unset($_SESSION['flash']); ?>
<?php endif; ?>
Thank you guys for your help !
Try this. The problem is that unset() was inside the loop, so the variable was being unset-ed on the first loop execution.
<?php if(isset($_SESSION['flash'])): ?>
<?php foreach($_SESSION['flash'] as $type => $message): ?>
<div class="alert alert-<?= $type; ?> bk-fullwidth-alert">
<p><?= $message; ?></p>
</div>
<?php endforeach; ?>
<?php unset($_SESSION['flash']); ?>
<?php endif; ?>
Edit: try this one
<?php
if(isset($_SESSION['flash'])) {
$flash = $_SESSION['flash'];
unset($_SESSION['flash']);
foreach($flash as $type => $message) {
?>
<div class="alert alert-<?=$type?> bk-fullwidth-alert">
<p><?=$message?></p>
</div>
<?php
}
}
?>

php multiple if's but not related

I have a WP site but this is a PHP question, I have the following code:
f(!empty($c_dates1_new)) { ?>
<p class="text">Dates: From <?php echo $c_dates1_new; ?> to <?php echo $c_datee1_new; ?></p>
<?php } if(!empty($c_dates2_new)) { ?>
<p class="text">Dates 2: From <?php echo $c_dates2_new; ?> to <?php echo $c_datee2_new; ?></p>
<?php } if(!empty($c_dates3_new)) { ?>
<p class="text">Dates 3: From <?php echo $c_dates3_new; ?> to <?php echo $c_datee3_new; ?></p>
<?php } if(!empty($c_dates4_new)) { ?>
<p class="text">Dates 4: From <?php echo $c_dates4_new; ?> to <?php echo $c_datee4_new; ?></p>
<?php }
Now this only shows the content from $c_dates_1 where it should display the content from all of them.
I cannot workout what I need to put between them e.g. it's not an else, it's not an OR and it's not an AND as they are all separate checks, not related to each other.
any input is appreciated

Yii: How to update multiple models using only 1 form 1 model and 1 action?

I have a model website and a model url.
Each url data is attached to a website data. A url is related to a website via a website_id.
On my web app, I need to check the data before accepting it.
I want to see on screen the entire data regarding a url data and I managed to do this.
Now, I also want to update the entire data that is listed on screen.
On screen I have the entire url data and website data, but when I try to save the data, the website data is empty.
My logic was to add to the url model a property: public $website;
And within the url model, a method aftersave:
protected function afterSave() {
$w = null;
$w = Website::model()->findByAttributes(array('id' => $this->website_id));
//echo '<pre>';
//print_r($w);
print_r($this->website);
$w->link = $this->website['link'];
$w->domain = $this->website['domain'];
$w->description = $this->website['description'];
$w->save(false);
die;
return parent::afterSave();
}
and here is the important code from the _form file:
<div class="row">
<?php echo $form->labelEx($model,'will_expire'); ?>
<?php echo $form->dropDownList($model,'will_expire',array(0=>'No',1=>'Yes')); ?>
<?php echo $form->error($model,'will_expire'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model_website,'link'); ?>
<?php echo $form->textField($model_website,'link'); ?>
<?php echo $form->error($model_website,'link'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model_website,'domain'); ?>
<?php echo $form->textField($model_website,'domain'); ?>
<?php echo $form->error($model_website,'domain'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model_website,'description'); ?>
<?php echo $form->textField($model_website,'description'); ?>
<?php echo $form->error($model_website,'description'); ?>
</div>
There are few questions:
Is the relation url and website being defined in the model such that $url->website of a existing url gives you valid website record?
You form shows, you are not using any website_id, means that you are creating a new website record for every URL, means $w = Website::model()->findByAttributes(array('id' => $this->website_id)); will always return NULL
I assume you have defined a relation of website within your URL model.
Suggested function from my understanding:
protected function afterSave() {
//data is already assigned by the caller
$w = null;
$w = Website::model()->findByAttributes(array('id' => $this->website_id));
if($w)
{
Yii::log("updating existing website data","info");
$this->website->save();
}
else
{
Yii::log("creating new website data","info");
$this->website->save();
$this->website_id = $this->website->website_id;
$this->update(array('website_id')); //save the relation
Yii::log("created new website {$this->website_id}","info");
}
return parent::afterSave();
}
It turned out to be small mistakes like w instead of W, object property instead of array;
Ok, so let me teach you how to do this;
I have 2 models: Url and Website.
Url model has a foreign key website_id and a relation to model Website;
Within the Url model I have a property called website and i declared it like: public $website = array();;
The data gets added thru a bookmarklet that uses a API so the data will be there when the admin comes to update/check it;
In the Url model i have a method afterSave:
protected function afterSave() {
$w = null;
$w = Website::model()->findByAttributes(array('id' => $this->website_id));
if($w)
{
$w->link = $this->website['link'];
$w->domain = $this->website['domain'];
$w->description = $this->website['description'];
$w->save();
}
return parent::afterSave();
}
where $this->website gets populated in the UrlController on actionUpdate like:
public function actionUpdate($id, $type = 'update') {
$model = $this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if (isset($_POST['Url'])) {
$model->attributes = $_POST['Url'];
$model->website = $_POST['Website'];
if ($model->save())
if ($type == 'update')
$this->redirect(array('view', 'id' => $model->id));
else
$this->redirect(array('/admin/url/approvePublicLink'));
}
$model_website = Website::model()->findByAttributes(array('id'=>$model->website_id));
$this->render('update', array(
'model' => $model,
'model_website' => $model_website,
));
}
and gets passed to the afterSave method later;
this is the _update view:
<div style="padding:20px 20px;">
<h1>Update Url, Website, Keywords ...</h1>
<?php echo $this->renderPartial('_form', array(
'model'=>$model,
'model_website' => $model_website,
)); ?>
</div>
and this is the _form view that gets rendered partial:
<div class="form">
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'url-form',
'enableAjaxValidation'=>false,
));
?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'website_id'); ?>
<?php echo CHtml::link($model->relation_website->domain,$model->relation_website->domain,array('class'=>'avia','target'=>'_blank')); ?>
<?php echo $form->error($model,'website_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'link'); ?>
<?php echo $form->textField($model,'link',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'link'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'title'); ?>
<?php echo $form->textField($model,'title',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'title'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textField($model,'description',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'description'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'important'); ?>
<?php echo $form->dropDownList($model,'important',array(0=>'Normal',1=>'Important')); ?>
<?php echo $form->error($model,'important'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'views'); ?>
<?php echo $form->textField($model,'views'); ?>
<?php echo $form->error($model,'views'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'created'); ?>
<?php echo $model->created; ?>
<?php echo $form->error($model,'created'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'updated'); ?>
<?php echo $model->updated; ?>
<?php echo $form->error($model,'updated'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'will_expire'); ?>
<?php echo $form->dropDownList($model,'will_expire',array(0=>'No',1=>'Yes')); ?>
<?php echo $form->error($model,'will_expire'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model_website,'link'); ?>
<?php echo $form->textField($model_website,'link'); ?>
<?php echo $form->error($model_website,'link'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model_website,'domain'); ?>
<?php echo $form->textField($model_website,'domain'); ?>
<?php echo $form->error($model_website,'domain'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model_website,'description'); ?>
<?php echo $form->textField($model_website,'description'); ?>
<?php echo $form->error($model_website,'description'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'status'); ?>
<?php echo $form->dropDownList($model,'status',array(-1=>'Banned',0=>'Normal',1=>'Active')); ?>
<?php echo $form->error($model,'status'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
I hope it helps.

i have problem with give comment in post view in yii

i want to give the comments in the views form & i follow the function..
<?php if(Yii::app()->user->hasFlash('commentSubmitted')): ?>
<div class="flash-success">
<?php echo Yii::app()->user->getFlash('commentSubmitted'); ?>
</div>
<?php else: ?>
<?php $this->renderPartial('/comment/_form',array(
'model'=>$data,
)); ?>
but now it show an error....
'htmlspecialchars() expects parameter 1 to be string, object given'
my '_form.php' under 'comment' is
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'comment-form',
'enableAjaxValidation'=>true,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'content'); ?>
<?php echo $form->textArea($model,'content',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'content'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'author'); ?>
<?php echo $form->textField($model,'author',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'author'); ?>
</div>
The problem with yii's 'htmlspecialchars() expects parameter 1 to be string, object given' is that we don't know what is causing this error.
I have made this crude change yii/web/helpers/CHTML.php:
public static function encode($text)
{
if (is_object($text)){
echo __FILE__ ." ".__LINE__;
var_dump($text);
die(" This is an object, and it must be string!");
}
else
return htmlspecialchars($text,ENT_QUOTES,Yii::app()->charset);
}
So at least I know what object has been sent to that method.
ouch, dont change core classes.
Yii active record model is glued to your database table. one possible reason for this warning message is that your db table does not contain all columns you are trying to make available in your form.
just guessing :)

Conditional Statement if no value exists in PHP or Jquery

I created an unordered list with the following html:
<ul id="infoBox">
<li class="facebook"><?php the_title(); ?> on Facebook</li>
<li class="twitter">Follow <?php the_title(); ?> on Twitter</li>
<li class="youtube">Watch <?php the_title(); ?> on Youtube</li>
</ul>
<?php echo get_field('value'); ?> is grabbing a string from the backend of my site. Sometimes, I do not have a string to display, so I want to create a conditional statement in jquery and/or php that basically says: If there is no field to get (if the field is left empty on the backend), do not display the list item at all. For instance, if a band doesn't have a youtube page, do not display the list item with a class of 'youtube' at all.
Any idea how I would go about this?
<ul id="infoBox">
<?php $response = get_field('facebook_page');
if(!empty($response)): ?><li class="facebook"><?php the_title(); ?> on Facebook</li><?php endif; ?>
<?php $response = get_field('twitter_page');
if(!empty($response)): ?><li class="twitter">Follow <?php the_title(); ?> on Twitter</li><?php endif; ?>
<?php $response = get_field('youtube_page');
if(!empty($response)): ?><li class="youtube">Watch <?php the_title(); ?> on Youtube</li><?php endif; ?>
</ul>
Would this work for you?
if(get_field('value') == '' || is_null(get_field('value'))
echo 'no value';
else
echo 'there is a value';
I hope understand your question and could help.
<ul id='infoBox'>
<?php
$name = the_title();
$potentialItems = array('facebook' => "$name on facebook",
'youtube' => "Watch $name on youtube",
'twitter' =? "Follow $name on twitter");
foreach($potentialItems as $k=>$v)
{
$gf = get_field($k.'_page');
if($gf)
{
echo "<li class='$k'><a href='$gf'>$v</a></li>";
}
}
?>
</ul>

Categories