I have a php quiz script that I have been using and am trying to modify it to display both correct and incorrect answers and am having issues trying to accomplish this.
So when a user selects the incorrect answer, I want the script to display the incorrect answer and then the correct answer below it. I have figure out how to display the letter of the correct answer, but I want the actual answer to be displayed and not just the letter.
I am a beginner with php so any help would be appreciated.
Here is the entire code:
<?php get_header();
/*
Template Name: PHP Quiz safety asmnt
*/
$Questions = array(
1 => array(
'Question' => 'The appropriate response during a health threatening emergency such as a fire or toxic spill is to:',
'Answers' => array(
'A' => 'Pull the fire alarm; evacuate immediately to the assemply point; and do not re-enter the cleanroom until instructed to do so.',
'B' => 'Notify lab; evacuate immediately to the assemply point; and do not re-enter the cleanroom until instructed to do so.',
'C' => 'Call EH&S'
),
'CorrectAnswer' => 'A'
),
2 => array(
'Question' => 'With proper use of wet bench and disposal procedures, the laboratory should be free of odors. In general, if you smell something, you should:',
'Answers' => array(
'A' => 'Call EH&S; Clear affected area if necessary; Notify staff and other users',
'B' => 'Do nothing; odd smells are normal when working in the cleanroom.',
'C' => 'Notify staff member; clear affected area; wait for instruction from staff who will investigate'
),
'CorrectAnswer' => 'C'
)
);
?>
<div id="bodyPage" class="clear">
<!------- start body ------->
<section id="contentPage">
<h1 class="title"><?php the_title(); ?></h1>
<?php
if (isset($_POST['answers'])){
$Answers = $_POST['answers']; // Get submitted answers.
echo '<br />';
// Automated question checking!)
foreach ($Questions as $QuestionNo => $Value){
// Echo the question
echo $QuestionNo. '. ' .$Value['Question'].'<blockquote>';
if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
echo 'You entered incorrectly:<br />'.'<span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>';
echo '<br/>'.'The correct answer is:<br />'.'<span style="color: green;">'.$Value['CorrectAnswer'][$Answers[$QuestionNo]].'</span>';
} else {
echo 'You entered correctly:<br />'.'<span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>';
}
echo '</blockquote>';
}
} else {
?>
<form action="<?php the_permalink(); ?>" method="post" id="quiz">
<ol>
<?php foreach ($Questions as $QuestionNo => $Value){ ?>
<li>
<h4><?php echo $Value['Question']; ?></h4>
<?php
foreach ($Value['Answers'] as $Letter => $Answer){
$Label = 'question-'.$QuestionNo.'-answers-'.$Letter;
?>
<div>
<input type="radio" name="answers[<?php echo $QuestionNo; ?>]" id="<?php echo $Label; ?>" value="<?php echo $Letter; ?>" />
<label for="<?php echo $Label; ?>"><!--<?php echo $Letter; ?>)--> <?php echo $Answer; ?> </label>
</div>
<?php } ?>
</li>
<?php } ?>
</ol>
<input type="submit" value="Submit Quiz" />
</form>
<?php
}
?>
<!------- end body ------->
</div>
<?php get_footer(); ?>
A little late but here's one way:
foreach ($Questions as $QuestionNo => $Value){
// Echo the question
echo $Value['Question'].'<br />';
if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
echo '<span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; // Replace style with a class
} else {
echo '<span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; // Replace style with a class
}
echo '<br /><hr>';
}
(taken from: PHP quiz with on screen results )
Try this:
foreach( $Questions as $QuestionNo => $Value ) {
echo $QuestionNo . '. ' . $Value['Question'].'<blockquote>';
if( $Answers[$QuestionNo] != $Value['CorrectAnswer'] ) {
echo 'You entered incorrectly:<br />'.'<span style="color:red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>';
echo '<br/>'.'The correct answer is:<br />'.'<span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>';
} else {
echo 'You entered correctly:<br />'.'<span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>';
}
echo '</blockquote>';
}
Related
I got 3 array's where from i make 2 select box and a checkbox. What i want to do is that when i change CMS the checkbox value needs to change like in the array.
if i choose Joomla in the select box then i want the checkbox that is made with the $aOnderdelen, then in the array $aOnderdelen i have an array with Contact-form Foto-gallery and Carousel and this 3 have an array with the name of each CMS with different values and this are the values that the checkbox needs to get when you choose one of those and the CMS.
Example: i choose Joomla and i choose a Contact-formulier than contact-formulier checbox gets 3 as value.
$aCMS = array('SilverbeeCMS','Joomla','WP','Drupal','Scott');
$prijsPerUur=1;
$basisPrijs=
array(
array('titel' => 'Kopie', 'uur' => '8'),
array('titel' => 'Maatwerk', 'uur' => '10'),
array('titel' => 'Aangekocht', 'uur' => '12'),
array('titel' => 'Custom', 'uur' => '14')
);
$aOnderdelen = array
(
'Contact-formulier' => array
(
'SilverbeeCMS'=>3,
'WP'=>2,
'Joomla'=>3,
'Drupal'=>4,
'Scott'=> 5
),
'Foto-gallery' => array
(
'SilverbeeCMS'=>1,
'WP' => 3,
'Joomla'=> 4,
'Drupal'=> 5,
'Scott'=> 6
),
'Carousel' => array
(
'SilverbeeCMS'=>1,
'WP' => 4,
'Joomla'=> 5,
'Drupal'=> 6,
'Scott'=> 7
)
);
?>
This is the HTML form where the Select and checkbox are
<form action="" method="post">
<select id="cms" class="form-control" name="cms">
<?php foreach($aCMS as $key => $value): ?>
<option value="<?php echo strtolower($aCMS[$key]); ?>">
<?php echo $aCMS[$key]; ?>
</option>
<?php endforeach; ?>
</select>
<label><?php echo $template.$verplicht; ?></label>
<select id="templates" class="form-control" name="templates">
<?php foreach($basisPrijs as $key => $value): ?>
<option value="<?php echo $basisPrijs[$key]["uur"]; ?>">
<?php echo $basisPrijs[$key]["titel"]; ?>
</option>
<?php endforeach; ?>
</select>
<?php echo $oTitel; ?>
<div class="checkbox col-xs-12">
<div class="row">
<?php foreach($aCMS as $cmsKey => $cmsValue) ?>
<?php foreach($aOnderdelen as $key => $value):
foreach($value as $key1 => $value1)
{};
$i++;
echo "<div class='checkbox'>
<label><input class='check".$i."' type='checkbox' value='".strtolower($key)."' name='".$key."'>".$key."</label></div>"
;endforeach;?>
</div>
</div>
</form>
You have to use jquery along with AJAX for changing things dynamically
Simple example as
$(document).ready(function() {
$('#selector').change(function() {
//do here things required about changing
//You can also change DOM elements according to needs
//and have Ajax requests
})
});
I found how to do it
switch($("option:selected").val())
{
case "silverbeecms":
$(".check1").val(<?php echo $aOnderdelen["Contact-formulier"]["SilverbeeCMS"] ?>)
$(".check2").val(<?php echo $aOnderdelen["Foto-gallery"]["SilverbeeCMS"] ?>)
$(".check3").val(<?php echo $aOnderdelen["Carousel"]["SilverbeeCMS"] ?>)
break;
case "joomla":
$(".check1").val(<?php echo $aOnderdelen["Contact-formulier"]["Joomla"] ?>)
$(".check2").val(<?php echo $aOnderdelen["Foto-gallery"]["Joomla"] ?>)
$(".check3").val(<?php echo $aOnderdelen["Carousel"]["Joomla"] ?>)
break;
case "wp":
$(".check1").val(<?php echo $aOnderdelen["Contact-formulier"]["WP"] ?>)
$(".check2").val(<?php echo $aOnderdelen["Foto-gallery"]["WP"] ?>)
$(".check3").val(<?php echo $aOnderdelen["Carousel"]["WP"] ?>)
break;
case "drupal":
$(".check1").val(<?php echo $aOnderdelen["Contact-formulier"]["Drupal"] ?>)
$(".check2").val(<?php echo $aOnderdelen["Foto-gallery"]["Drupal"] ?>)
$(".check3").val(<?php echo $aOnderdelen["Carousel"]["Drupal"] ?>)
break;
case "scott":
$(".check1").val(<?php echo $aOnderdelen["Contact-formulier"]["Scott"] ?>)
$(".check2").val(<?php echo $aOnderdelen["Foto-gallery"]["Scott"] ?>)
$(".check3").val(<?php echo $aOnderdelen["Carousel"]["Scott"] ?>)
break;
}
New Answer more Flexibel
First Loop through the PHP array.
<?PHP
foreach($aCMS as $cmsKey => $cmsValue)
?>
<?php
foreach($aOnderdelen as $key => $value)
:
foreach($value as $key1 => $value1){};
?>
Than use data attr to get the prices.
<input class="<?php echo strtolower($key);?>" type="checkbox" value="<?php echo strtolower($key)?>" name="<?php echo $key;?>" data-silverbeecms="<?php echo $value['SilverbeeCMS']; ?>" data-wp="<?php echo $value['WP'];?>" data-joomla="<?php echo $value['Joomla'];?>" data-drupal="<?php echo $value['Drupal'];?>" data-scott="<?php echo $value['Scott'];?>">
After you get the Price in the data you can call this data from JQUERY and do the Match.
urenOnderdelen = 0;
$("#sCms, #templates, .contact-formulier, .foto-gallery, .carousel").change(function()
{
urenOnderdelen = 0;
urenTemplate = $("#templates").val();
$(".contact-formulier, .foto-gallery, .carousel").each(function()
{
if(this.checked)
{
urenOnderdelen+=parseInt($(this).data($("option:selected").val()));
}
});
$("#output, #output1, #gVoor").hide().fadeIn(300);
urenTemplate = $("#templates").val();
urenTemplate = parseInt(urenTemplate);
urenOnderdelen = parseInt(urenOnderdelen);
totaal = urenOnderdelen + urenTemplate;
$("#output").text(euroTeken + totaal*prijs);
});
I'm trying to update a database column using a checkbox. The idea is that I want to only display certain things on the homepage, so the checkbox should send either 0 or 1 to the DB. If the record has 0 in the first page column, it shouldn't appear on the homepage, if it has 1 it should appear.
I tried setting something up, but it doesn't work as intended (it just refreshes the page with no effect)
The checkbox:
<?php echo form_checkbox(array('name' => 'first_page', 'class'=>'checkbox-inline', 'value' => 1)); ?>
Controller:
if(null != ($this->input->post('first_page'))) {
$first_page = 1;
$book_id= $this->input->post('id'); (the id is sent to the controller via an extra hidden input field)
$this->book_model->first_page($first_page, $book_id);
}
Model:
$this->db->set ( "first_page", $first_page);
$this->db->where ( "book_id", $book_id);
$this->db->update ( "books" );
return;
Full form:
<?php echo form_open('admin/modifica_autor'); ?>
<?php echo form_hidden('id', $item->id_carte); ?>
<?php echo form_hidden('id_autor', $item->id_autor)?>
<label for="titlu_nou">Titlu:</label>
<?php echo form_input(array('name' => 'titlu_nou_carte', 'class'=>'form-control', 'value' => $item->titlu)); ?> <br>
<label for="autor_nou">Autor:</label>
<select name='autor_nous' class="form-control"><?php
foreach ( $autori2 as $row ) {
echo '<option value="' . $row->id_autor . '">' . $row->nume_autor . '</option>'; }
?></select><br>
<label for="domeniu_nou">Domeniu:</label>
<select name='domeniu_nou' class="form-control"><?php
foreach ( $domenii as $row ) {
echo '<option value="' . $row->id_domeniu . '">' . $row->nume_domeniu . '</option>';
}
?></select><br>
<label for="pret_nou">Pret:</label>
<?php echo form_input(array('name' => 'pret_nou', 'class'=>'form-control', 'value' => $item->pret.' LEI')); ?><br>
<label for="descriere_noua">Descriere:</label>
<?php echo form_textarea(array('name' => 'descriere_noua', 'class'=>'form-control textarea', 'value' => $item->descriere)); ?><br>
<label for="prima_pagina">Afisare pe prima pagina:</label>
<?php echo form_checkbox(array('name' => 'prima_pagina', 'class'=>'checkbox-inline', 'value' => 1)); ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<?php echo form_submit(array('name'=>'submit_autor','value' => 'Modifica', 'class'=>'btn btn-default')); ?>
<?php echo form_close(); ?>
Line 42 is the error. I'm not sure as to why it keeps saying it's not an array is one section but fails to find the array on line 42. I have tried to change the line to ($_POST['CINS'] as $cNum => $v) and ($CINS as $cNum => $v). Any insight or help would be appreciated.
<?php
$title = "fTest.php";
$action=$_SERVER['PHP_SELF'];
include("html-head.inc");
echo <<<HEREDOC
<header>
<h1>$title</h1>
</header>
HEREDOC;
if (!isset($_POST['submit']))
{
echo "<form method=\"post\" action=\"$action\">";
$CINS = array('101' => "CINS101",
'108' => "CINS108",
'121' => "CINS121",
'251' => "CINS251",
'254' => "CINS254");
echo "<p>Please pick your CINS classes:</p>";
echo "<ul>\n";
foreach ($CINS as $key => $value)
{
echo "<li>";
echo "<input type=\"checkbox\" name=\"CINSc\" value=\"$value\"/>CINS$key" ;
echo "</li>\n";
}
echo "</ul>\n";
echo "<input type=\"reset\" name=\"reset\" value\"Reset\" />";
echo "<input type=\"submit\" name=\"submit\" value\"Submit\" />";
echo "</p>";
echo is_array($CINS) ? 'Array' : 'Not an array';
echo "\n";
echo "</form>";
} // ends IF PORTION for ISSET
else
{
if (count($_POST['CINS'] > 0 ))
{
echo "<h2> Your picks are: </h2>\n";
echo "<ul>\n";
echo is_array($CINS) ? 'Array' : 'Not an array';
foreach ($_POST['CINS'] as $cNum => $v) //This is the error.
{
echo "\t<li>$v</li>\n";
} // end of FOREACH cins
echo "</ul>\n";
} // end of IF count CINS
} // end of ELSE portion for ISSET
?>
Your checkbox name should like below to consider it as an array
<input type="checkbox" name="CINS[]" value = "1" />
The <input> name is an array, and I've found a spare </p> that shouldn't be there.
Your coding style is going to cause A LOT of HEADACHES. I've cleaned it up a bit, I think this style will be much easier to handle.
<?php
$title = "fTest.php";
$action=$_SERVER['PHP_SELF'];
include("html-head.inc");
?>
<header>
<h1><?=$title?></h1>
</header>
<?php if (!isset($_POST['submit'])): ?>
<?php $CINS = array('101' => "CINS101",
'108' => "CINS108",
'121' => "CINS121",
'251' => "CINS251",
'254' => "CINS254"); ?>
<form method="post" action="<?=$action?>">
<p>Please pick your CINS classes:</p>
<ul>
<?php foreach ($CINS as $key => $value): ?>
<li>
<input type="checkbox" name="CINS[]" value="<?=$value?>" />CINS<?=$key?>
</li>
<?php endforeach; ?>
</ul>
<input type="reset" name="reset" value"Reset" />
<input type="submit" name="submit" value"Submit" />
</p> <!-- THIS TAG IS EXTRA WHERE DID IT COME FROM -->
<?= is_array($CINS) ? 'Array' : 'Not an array' ?>
</form>
<?php else if (count($_POST['CINS'] > 0 )): ?>
<h2>Your picks are: </h2>
<ul>
<?= is_array($CINS) ? 'Array' : 'Not an array' ?>
<?php foreach ($_POST['CINS'] as $cNum => $v): ?>
<li><?=$v?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
public function actionEquipmentLoanRequestCreate()
{
if(isset($_POST['EquipmentRequest']))
{
$ids = $_POST['EquipmentRequest']['equipID'];
$equipment = Equipment::model()->findAllByPk($ids);
$requests = self::instantiateEquipmentRequests($equipment);
//echo "<pre>";
//die(count($requests)." ".CActiveForm::validate($requests));
$booking = new EquipmentBooking;
if(isset($_POST['EquipmentRequest']) && $_POST['EquipmentBooking'])
{
$booking->attributes = $_POST['EquipmentBooking'];
$requestPostedVars = $_POST['EquipmentRequest'];
//have posted collection of equipment
//need to take count
//request below not really useful???
$equipmentRequests = array();
foreach($requestPostedVars as $request)
{
if(isset($request))
{
$equipmentRequest = new EquipmentRequest('loanRequest');
$equipmentRequest->attributes = $request;
array_push($equipmentRequests,$equipmentRequest);
}
}
$models = $equipmentRequests;
$models[]=$booking;
$this->performAjaxValidation($models);
$booking->equipment = $equipmentRequests;
if ($booking->save()){
self::assignBookingIds($equipmentRequests,$booking);
$this->redirect('index');
}
}
//displays view to create loan request
$this->render('equipment-loan-request',array('requests' => $requests,'booking' => $booking));
} else {
Yii::app()->user->setFlash('error', "You need to select equipment first!");
$this->redirect(array('simulation/equipment'), true);
}
}
public function instantiateEquipmentRequests($equipment)
{
$equipmentRequests = array();
foreach($equipment as $item)
{
$request = new EquipmentRequest('loanRequest');
$request->equipment = $item;
$request->qty = 1;
array_push($equipmentRequests,$request);
}
return $equipmentRequests;
}
form view ignore excess button replacing later -- ive directly tried calling the requests as opposed to just the bookings or both below as well to no avail.
<?php $form=$this->beginWidget('CActiveFormExtended', array(
'id' => 'equipment-booking-form',
'enableAjaxValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange'=>true,
),
)); ?>
<div class="content-bg">
<?php
echo $form->errorSummary($requests);
if(isset($requests) && count($requests) > 0) {
foreach($requests as $i => $request) {
$this->renderPartial('_requestedEquipment', array("index" => $i, "request" => $request, "form"=> $form));
}
}?>
</div>
<br />
<hr class="brown"/>
<h2>Request Details</h2>
<div class="content-bg">
<h3>Step 1</h3>
<?php echo $form->label($booking,'organisation'); ?>
<?php echo $form->dropDownList($booking, 'organisation', $booking::$organisations,array('id' => 'organisation', 'class' => "selectfield",'empty' => 'Select')); ?>
</div>
<br />
<div class="content-bg">
<h3>Step 2</h3>
<?php echo $form->label($booking,'name'); ?>
<?php echo $form->textField($booking,'name',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($booking,'name'); ?>
<?php echo $form->label($booking,'email'); ?>
<?php echo $form->textField($booking,'email',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($booking,'email'); ?>
<?php echo $form->label($booking,'phone'); ?>
<?php echo $form->textField($booking,'phone',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($booking,'phone'); ?>
</div>
<br />
<div class="content-bg">
<h3>Step 3</h3>
<?php echo $form->label($booking,'address'); ?>
<?php echo $form->textField($booking,'address',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($booking,'address'); ?>
<?php echo $form->label($booking,'suburb'); ?>
<?php echo $form->textField($booking,'suburb',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($booking,'suburb'); ?>
<?php echo $form->label($booking,'postcode'); ?>
<?php echo $form->textField($booking,'postcode',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($booking,'postcode'); ?>
</div>
<br />
<div class="content-bg">
<h3>Step 4</h3>
<div class="five columns alpha">
<?php echo $form->label($booking,'pickupDate'); ?>
<?php echo $form->dropDownDayList($booking, 'pickupDate',array('class' => 'date-day pickup_select','id' => 'pickup')); ?>
<?php echo $form->dropDownYearList($booking,1, 'pickupDate',array('class' => 'date-monthyear pickup_select','id' => 'pickup-month')); ?>
<?php echo $form->hiddenField($booking, 'pickupDate',array('id' => 'pickupData')); ?>
<?php echo $form->error($booking,'pickupDate'); ?>
</div>
<div class="five columns alpha">
<?php echo $form->label($booking,'returnDate'); ?>
<?php echo $form->dropDownDayList($booking, 'returnDate',array('class' => 'date-day return_select','id' => 'return')); ?>
<?php echo $form->dropDownYearList($booking,1, 'returnDate',array('class' => 'date-monthyear return_select','id' => 'return-month')); ?>
<?php echo $form->hiddenField($booking, 'returnDate',array('id' => 'returnData')); ?>
<?php echo $form->error($booking,'returnDate'); ?>
</div>
<br class="clear"/>
</div>
<br />
<div class="content-bg">
<h2>Terms & Conditions</h2>
<p>By submitting this loan enquiry you agree to and acknowledge our Equipment Loan Terms and Conditions.</p>
</div>
<?php echo CHtml::submitButton('Submit'); ?>
<br />
<input class="orange-btn right" type="submit" value="Submit Loan Request">
<?php $this->endWidget(); ?>
</div>
EquipmentRequest partial
<div class="selected-equipment" id="request_<?php echo $request->equipment->equipName; ?>">
<div class="equipment-selected"><?php echo $request->equipment->equipName; ?></div>
<div class="equipment-qty"><?php echo $form->textField($request, "[$index]qty", array('value' => 1,'class' => 'requestQuantity')); ?></div>
<?php echo $form->error($request,"[$index]qty"); ?>
<div class="equipment-update"><span>Remove</span></div>
<?php echo $form->hiddenField($request, "[$index]equipID",array('value' => $request->equipment->equipID)); ?>
<?php echo $form->error($request,"[$index]equipID"); ?>
<br class="clear">
</div>
EDIT
Please note I have rectified the above issue. Was mainly caused by an incorrect usage of performAjaxValidate, which I have now set to call both the booking and the collection of equipmentRequests.
This is now letting me get 2 validation error message objects back, 1 for both models. however only the booking model is being passed to the error summary div.
#bool.dev appreciate your assistance so far, any ideas with the above, have tried merging the collection of equipment request objects and booking object to the one argument call in errorSummary
I've edited the above to show updated controller actions and views
When you want errorSummary() for more than one model, use:
$form->errorSummary(array($model1,$model2,$model3));
I'm trying to add a layout for a cakephp application but now my validation message is no longer being displayed. When validating a comment on a blog entry, the validation message thats suppose to be at the top is not displayed.
If you changing the layout means that you missed to add
<?php
if ($this->Session->check('Message.flash')){
echo $this->Session->flash();
}
?>
before the
Other possible place is in the current controller.
Search if you have code like:
$this->Session->setFlash('...');
The first code is responsible for displaying the message, while the second one is responsible for setting the message.
But the code definitely will help more :)
Here is my add function in comments_controller.php
function add(){
debug($this->data);
//if the user submitted a comment post
if (!empty($this->data)){
//display the 'add view'
$this->Comment->create();
if ($this->MathCaptcha->validates($this->data['Comment']['security_code'])) {
if ($this->Comment->save($this->data)){
$this->Session->setFlash(__('The Comment has been added.', true));
$this->redirect('/posts/index/'.$this->data['Comment']['post_id']);
}
//failed validation
else{
debug($this->data);
$this->Session->setFlash(__('Comment could not be saved. Please try again.', true));
}
}
else {
$this->Session->setFlash(__('Please enter the correct answer to the math question.', true));
$this->redirect('/posts/index/'.$this->data['Comment']['post_id']);
}
Here is my entry.ctp where my posts and comments reside:
<div id="article">
<h2><?php echo $entry[0]['Post']['title']; ?></h2>
<p class="date"><em>Modified:</em> <?php $date = new DateTime($entry[0]['Post']['modified']);
echo $date->format('Y-m-d');?></p>
<p class="date"><em>Author:</em> <?php echo $entry[0]['User']['username']; ?></p>
<p class="intro"><?php echo $entry[0]['Post']['content']; ?></p>
<h2>Comments:</h2>
<div id="comments_success"></div>
<!-- show the comment -->
<?php
echo $form->create('Comment', array('action' => 'add'));
echo $form->input('name', array('class' => 'validate[required] text-input'));
echo $form->input('email', array('class' => 'validate[required,custom[email]] text-input'));
echo $form->input('text', array('id' => 'commenttext', 'type' => 'textarea', 'label' => 'Comment:', 'rows' => '10', 'class' => 'validate[required] text-input'));
//captcha
echo $form->input('security_code', array('label' => 'Please Enter the Sum of ' . $mathCaptcha));
echo $form->input( 'Comment.post_id', array( 'value' => $entry[0]['Post']['id'] , 'type' => 'hidden') );
echo $form->end('Submit');
?>
<!-- comments -->
<ol>
<?php
foreach ($entry[0]['Comment'] as $comment) :
?>
<li>
<h3><?php echo $comment['name']; ?></h3>
<p class="date"><em>Date:</em> <?php echo $comment['created']; ?></p>
<p class="text"> <?php echo $comment['text']; ?></p>
</li>
<?php
endforeach;
?>
</ol>
</div>
This is the index function in my posts_controller
function index($entry_id = null) {
if (isset($entry_id)){
$entry = $this->Post->findAllById($entry_id);
$comments = $this->Post->Comment->getCommentsFromPostID($entry_id);
$this->set('entry' , $entry);
$this->set('mathCaptcha', $this->MathCaptcha->generateEquation());
$this->render('entry');
}
else{
$posts = $this->Post->find('all');
$this->set(compact('posts'));
}
}
I know this is old, but ust make sure you have the following code somewhere visible in your app/views/layouts/default.ctp (or whatever is your layout for this application)
<?php echo $this->Session->flash(); ?>
It will echo nothing if there is no message to be displayed, but if there is a message, then it will be output accordingly.