I have an admin panel that I am creating. I have a left panel section and then the right side which shows the div when the panel button is clicked. I created a fiddle to show what it looks like and to help me explain this...
https://jsfiddle.net/jq8c51c9/
In the Fiddle it works just like it should, but I took out all of my php. The problem is around the Announcements div, it shows the div for the League Dues under it. Also once you click on announcements and then click on another panel button, if I click on Announcements again the only thing that will show up is this..
Announcements Current Announcements
League Dues
Again this is NOT doing this in the Fiddle.
Here is the full code for the area that the issue resides in. I have been stuck on this forever and cannot figure out why I am having difficulties with only these two divs.
Does anyone see what it is that I am doing wrong?
Announcements
try {
//Prepare
$con = mysqli_connect("localhost", "", "", "");
if ($user_stmt = $con->prepare("SELECT `id` FROM users")) {
$user_stmt->execute();
$user_stmt->bind_result($user_id);
if (!$user_stmt) {
throw new Exception($con->error);
}
$user_stmt->store_result();
$user_result = array();
//while ($user_row = $user_stmt->fetch()) {
?>
<div class="announcement_success"></div>
<p>Add New Announcement</p>
<form action="" method="POST" id="insert_announcements">
<input type="hidden" value="<?php echo $userid; ?>" id="approved_id" name="user_id" />
<textarea rows="4" cols="50" id="announcement_message" name="message" class="inputbarmessage" placeholder="Message" required></textarea>
<label for="contactButton">
<button type="button" class="contactButton" id="submit_announcement">Add Announcement</button>
</label>
</form>
<?php
if ($announcements_stmt = $con->prepare("SELECT * FROM announcements")) {
$announcements_stmt->execute();
$announcements_stmt->bind_result($announcements_id, $announcements_user_id, $announcements_messages, $announcements_date);
if (!$announcements_stmt) {
throw new Exception($con->error);
}
$announcements_stmt->store_result();
$announcements_result = array();
?>
Current Announcements
<table>
<tr>
<th>ID</th>
<th>Username</th>
<th>Message</th>
<th>Date</th>
</tr>
<?php
while ($row = $stmt->fetch()) {
?>
<tr>
<td><?php echo $announcements_id; ?></td>
<td><?php echo $announcements_username; ?></td>
<td><?php echo $announcements_messages; ?></td>
<td><?php echo $announcements_date; ?></td>
</tr>
</table>
<?php
}
}
}
}
catch (Exception $e)
{
echo "Error: " . $e->getMessage();
}
?>
</div>
<div id='dues'>League Dues</div>
</div>
You have an error when building the announcement-table.
The closing </table> tag is inside the while loop, so the html will be screwed up, making everything after the closed table disappear.
So change the while loop to:
....
<?php
while ($row = $stmt->fetch()) {
?>
<tr>
<td><?php echo $announcements_id; ?></td>
<td><?php echo $announcements_username; ?></td>
<td><?php echo $announcements_messages; ?></td>
<td><?php echo $announcements_date; ?></td>
</tr>
<?php
}
?>
</table>
<?php
....
Anyway, I recommend to build your html first in a variable and echo it out later alltogether. That makes cleaner code and reduces risk of inconsitency of html tags. When using with HEREDOC you even don't have to bother about quotes.
In your case that could for example look like that:
<?php
$table_head = <<<EOT
<tr>
<th>ID</th>
<th>Username</th>
<th>Message</th>
<th>Date</th>
</tr>
EOT;
$table_content = "";
while ($row = $stmt->fetch()) {
$table_content.= <<<EOT
<tr>
<td>$announcements_id</td>
<td>$announcements_username</td>
<td>$announcements_messages</td>
<td>$announcements_date</td>
</tr>
EOT;
}
$announcement_table = "<table>".$table_head.$table_content."</table>";
echo $announcement_table;
I am new to yii. My ActiveRecord doesn't validate when save() is called.
model codes:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('firstName, secondName, lastName, gender, phoneNumber, email, address, education_background, basicSalary, departmentID, roleID, contractID, employed_by, reports_to, date_employed', 'required'),
array('phoneNumber, departmentID, roleID, contractID', 'numerical', 'integerOnly'=>true),
array('firstName, secondName, lastName', 'length', 'max'=>20),
array('firstName, secondName, lastName','type', 'type'=>'char','message'=>'This field accepts characters only'),
array('email','email'),
);
}
controller codes:
public function actionCreate()
{
$model=new Employee;
$message="email already exists";
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Employee']))
{
$model->attributes=$_POST['Employee'];
$model->password=md5(strtolower($model->firstName.'123'));
$to=$model->email;
$mailmessage="Dear ".$model->lastName." ".$model->firstName."<br> Thanks for your interest in working for XXX Company.
<br>Please note your login details are :- Email:".$model->email." Password: ".$model->firstName."123<br>
You can change password to your preference choice when you login.<br>
Kindly Regards<br>
HRMS administrator";
//echo $model->password; exit;
$criteria=new CDbCriteria();
$criteria->select='email';
$criteria->condition='email=:email';
$criteria->params=array(':email'=>$model->email);
if($check=Employee::model()->exists($criteria)){
$this->render('create',array(
'model'=>$model,'message'=>$message));
exit;
}else{
$model->save();
/*if($model->validate()){
echo 'valideted';
exit;
}
else {
echo 'not valid';
print_r($model->getErrors());
}
exit;
//$model->getErrors();*/
/*if(){
echo 'saved';
}
else{
print_r($model->getErrors());
}
exit;*/
$this->mailsend($to,$mailmessage);
// $cmd=Yii::app()->db->createCommand('select max(employee.employeeID),role.roleName,employee.departmentID FROM employee JOIN role ON employee.roleID=role.roleID');
$cmd=Yii::app()->db->createCommand('select employee.employeeID,role.roleName,department.departmentName FROM employee JOIN department ON employee.departmentID=department.departmentID JOIN role ON employee.roleID=role.roleID
ORDER BY employee.employeeID DESC LIMIT 1');
$rstid=$cmd->queryRow(false);
$id=$rstid[0];
$role=$rstid[1];
$dep=$rstid[2];
$history=Yii::app()->db->createCommand();
$history->insert('history',array(
'employeeID'=>$id,
'action_to'=>'as '.$role,
'department'=>$dep,
'reports_to'=>$model->reports_to,
'date'=>$model->date_employed,
));
Yii::app()->session['id']=$model->employeeID;
$this->redirect(array('view','id'=>$model->employeeID));
}
}
$this->render('create',array(
'model'=>$model,'message'=>''
));
}
view codes
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'employee-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<table>
<tr><td><?php echo $form->labelEx($model,'firstName'); ?></td>
<td><?php echo $form->textField($model,'firstName',array('size'=>20,'maxlength'=>20)); ?></td>
<td><?php echo $form->error($model,'firstName'); ?></td></tr>
<tr><td><?php echo $form->labelEx($model,'secondName'); ?></td>
<td><?php echo $form->textField($model,'secondName',array('size'=>20,'maxlength'=>20)); ?></td>
<td><?php echo $form->error($model,'secondName'); ?></td></tr>
<tr><td><?php echo $form->labelEx($model,'lastName'); ?></td>
<td><?php echo $form->textField($model,'lastName',array('size'=>20,'maxlength'=>20)); ?></td>
<td><?php echo $form->error($model,'lastName'); ?></td></tr>
<tr><td><?php echo $form->labelEx($model,'gender'); ?></td>
<td><?php echo $form->dropDownList($model,'gender',array('Male'=>'Male','Female'=>'Female')); ?></td>
<td><?php echo $form->error($model,'gender'); ?></td></tr>
<tr><td><?php echo $form->labelEx($model,'phoneNumber'); ?></td>
<td><?php echo $form->textField($model,'phoneNumber'); ?></td>
<td><?php echo $form->error($model,'phoneNumber'); ?></td></tr>
<tr><td><?php echo $form->labelEx($model,'email'); ?>
</td>
<td><?php echo $form->textField($model,'email',array('size'=>50,'maxlength'=>50)); ?></td>
<td style='color:red;'><?php echo $form->error($model,'email'); ?>
<?php echo $message; ?></td></tr>
<tr><td><?php echo $form->labelEx($model,'address'); ?></td>
<td><?php echo $form->textArea($model,'address',array('size'=>60,'maxlength'=>100)); ?></td>
<td><?php echo $form->error($model,'address'); ?></td></tr>
<tr><td><?php echo $form->labelEx($model,'education_background'); ?></td>
<td><?php echo $form->textArea($model,'education_background',array('size'=>60,'maxlength'=>300)); ?></td>
<td><?php echo $form->error($model,'education_background'); ?></td></tr>
<tr><td><?php echo $form->labelEx($model,'roleID'); ?></td>
<td><?php echo $form->dropDownList($model,'roleID',CHtml::listData(Role::model()->findAll(),'roleID','roleName'),
array(
'ajax' => array(
'type'=>'POST',
'dataType'=>'json',
'url'=>CController::createUrl('employee/getSalary'),
'success'=>'function(data){
$("#Employee_basicSalary").val(data.data1);
}'),
'prompt'=>'select'
)); ?></td>
<td><?php echo $form->error($model,'roleID'); ?></td></tr>
<tr>
<td><?php echo $form->labelEx($model,'basicSalary'); ?></td>
<td>
<?php echo $form->textField($model,'basicSalary',array('size'=>10,'maxlength'=>10)); ?>
</td>
<td><?php echo $form->error($model,'basicSalary'); ?></td>
</tr>
<tr><td><?php echo $form->labelEx($model,'departmentID'); ?></td>
<td><?php echo $form->dropDownList($model,'departmentID',CHtml::listData(Department::model()->findAll(),'departmentID','departmentName')); ?></td>
<td><?php echo $form->error($model,'departmentID'); ?></td></tr>
<tr><td><?php echo $form->labelEx($model,'date_employed'); ?></td>
<td> <?php $this->widget('zii.widgets.jui.CJuiDatePicker',array(
'name'=>'Employee[date_employed]',
'id'=>'Employee_date_employed',
'value'=>$model->date_employed,
'options'=>array(
'dateFormat'=>'yy-mm-dd',
'showAnim'=>'fold',
),
'htmlOptions'=>array(
'style'=>'height:20px;'
),
));
?></td>
<td><?php echo $form->error($model,'date_employed'); ?>
</td></tr>
<tr><td><?php echo $form->labelEx($model,'contractID'); ?></td>
<td> <?php echo $form->dropDownList($model,'contractID',CHtml::listData(Contract::model()->findAll(),'contractID','contractName'),
array(
'ajax' => array(
'type'=>'POST',
'dataType'=>'json',
'url'=>CController::createUrl('employee/getContDate'),
'success'=>'function(data){
$("#Employee_endcontract").val(data.data1);
}'),
'prompt'=>'select'
)); ?></td>
<td><?php echo $form->error($model,'contractID'); ?></td></tr>
<tr><td><?php echo $form->labelEx($model,'endcontract'); ?></td><td><?php echo $form->textField($model,'endcontract');?></td></tr>
<tr><td><?php echo $form->labelEx($model,'employed_by'); ?></td>
<td><?php echo $form->textField($model,'employed_by',array('size'=>50,'maxlength'=>50)); ?></td>
<td><?php echo $form->error($model,'employed_by'); ?></td></tr>
<tr><td><?php echo $form->labelEx($model,'reports_to'); ?></td>
<td><?php echo $form->dropDownList($model,'reports_to',array('Senior Manager'=>'Senior Manager','Assistant Manager'=>'Assistant Manager','System Administrator'=>'System Administrator')); ?></td>
<td><?php echo $form->error($model,'reports_to'); ?></td></tr>
<tr><td></td><td> <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?></td><td></td></tr>
</table>
<?php $this->endWidget(); ?>
</div><!-- form -->
This model does not validate and also doesn't display errors. Any help please?
You can check uniqueness of email in model (will be good to use scenarios).
You can create relations with department and role tables in your model.
Model code:
public function rules() {
// it will be good to use scenarios
return array(
array('firstName, secondName, lastName, gender, phoneNumber, email, address, education_background, basicSalary, departmentID, roleID, contractID, employed_by, reports_to, date_employed', 'required'),
array('phoneNumber, departmentID, roleID, contractID', 'numerical', 'integerOnly'=>true),
array('firstName, secondName, lastName', 'length', 'max'=>20),
array('firstName, secondName, lastName','type', 'type'=>'char','message'=>'This field accepts characters only'),
array('email','email'),
array('email','unique'),
);
}
public function relations() {
return array(
'department' => array(self::BELONGS_TO, 'Department', 'departmentID'), // Department CActivRecord model of `department` table
'role' => array(self::BELONGS_TO, 'Role', 'roleID'), // Role CActivRecord model of `role` table
);
}
Controller create action code:
public function actionCreate() {
$model=new Employee;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Employee'])) {
$model->attributes=$_POST['Employee'];
$model->password=md5(strtolower($model->firstName.'123'));
if($model->save()) {
$mailmessage="Dear ".$model->lastName." ".$model->firstName."<br> Thanks for your interest in working for XXX Company.
<br>Please note your login details are :- Email:".$model->email." Password: ".$model->firstName."123<br>
You can change password to your preference choice when you login.<br>
Kindly Regards<br>
HRMS administrator";
$this->mailsend($model->email, $mailmessage);
Yii::app()->db->createCommand()->insert('history',array(
'employeeID'=>$model->employeeID,
'action_to'=>'as '.$model->role->roleName,
'department'=>$model->department->departmentName,
'reports_to'=>$model->reports_to,
'date'=>$model->date_employed,
));
Yii::app()->session['id']=$model->employeeID;
$this->redirect(array('view','id'=>$model->employeeID));
}
}
$this->render('create',array(
'model'=>$model,
));
}
Also you can set enableClientValidation to true in CActiveForm widget to enable js validation.