I'm using cakephp 1.3,I want to create a link with to params ( id and nbr) to call function in controller but the problem is I can't get the input value inside my form..
here my code:
<tr>
<td width="25%"> <?php echo $value['StocksCour']['module_formation']; ?> </td>
<td width="25%"> <?php echo $value['StocksCour']['nb_stock']; ?> </td>
<td width="25%">
<?php
echo $form->create('StocksCour');
echo $bouton->add('-', array('id'=>'subtsract', 'div'=>false));
echo $v->input('nbr_edit',array(
'id'=>'nbr_edit',
'label'=>false,
'default'=> $value['StocksCour']['nb_stock'],
'div'=>false,
'style'=>'width:30px;'
));
echo $bouton->add('+', array('id'=>'add', 'div'=>false));
echo $v->input('id_formation',array(
'type'=>'hidden',
'name'=>'id_formation',
'id'=>'id_formation',
'value'=>$value['StocksCour']['nom_formation']
));
?>
<?php
echo $v->link(
'Valider',
array(
'controller'=> 'Sessions',
'action'=>'saveChange',
$value['StocksCour']['id_formation'] ,
$this->data['StocksCour']['nbr_edit']
),
array(
'image'=>'tick.gif',
'class'=>'button small',
'post'
)
);
?>
</tr>
the problem is she have to get the new 'nb_edit' after change ..
Related
I want to relate to a model from another model, but I got this error:
This is the other model I am pointing to from the View
CourseMaster Model
public function attributeLabels()
{
return [
'id' => Yii::t('course', 'ID'),
'course_code' => Yii::t('course', 'Course Code'),
'course_type' => Yii::t('course', 'Course Type'),
'course_title' => Yii::t('course', 'Course Title'),
'course_unit' => Yii::t('course', 'Course Unit'),
];
}
function getCourseCodes()
{
return ($this->course_code);
}
function getCourseTitles()
{
return ($this->course_title);
}
function getCourseTypes()
{
return ($this->course_type);
}
function getCourseUnits()
{
return ($this->course_unit);
}
This is the Main Model that I want to display in the view
CourseRegistrationDetail Model
public function attributeLabels()
{
return [
'id' => Yii::t('course', 'ID'),
'course_registration_id' => Yii::t('course', 'Course Registration'),
'course_id' => Yii::t('course', 'Course Title'),
'student_id' => Yii::t('course', 'Student'),
'remark' => Yii::t('course', 'Remark'),
];
}
public function getCourseMaster()
{
return $this->hasOne(CourseMaster::className(), ['id' => 'course_id']);
}
I want to be able to use array index as $key
View
<tr>
<td colspan=3 class="padding-left padding-right">
<?php $totalUnit = 0;
$courseRegistrationDetails = \app\modules\course\models\CourseRegistrationDetail::find()->where(['course_registration_id' => $model->id])->asArray()->all(); ?>
<table border="1" class="table table-border" style="width:100%;">
<tr class="header">
<th><?php echo Yii::t('report', 'SI.No'); ?></th>
<th><?php echo Yii::t('report', 'Course Code'); ?></th>
<th><?php echo Yii::t('report', 'Course Title'); ?></th>
<th><?php echo Yii::t('report', 'Course Type'); ?></th>
<th><?php echo Yii::t('report', 'Unit'); ?></th>
<th><?php echo Yii::t('report', 'Remark'); ?></th>
</tr>
<?php
foreach($courseRegistrationDetails as $key=>$value) {
echo '<tr>';
echo '<td class="text-center">'.($key+1).'</td>';
echo '<td class="text-center">'.$value->courseMasters->courseCodes.'</td>';
echo '<td class="text-center">'.$value->courseMasters->courseTitles.'</td>';
echo '<td class="text-center">'.$value->courseMasters->courseTypes.'</td>';
echo '<td class="text-center">'.$value->courseMasters->courseUnits.'</td>';
echo '<td class="text-center">'.$value['remark'].'</td>';
echo '</tr>';
$totalUnit+=$value['course_unit'];
}
?>
<tr>
<th class="text-right border-hide padding-right" colspan=4><?php echo Yii::t('report', 'Total Unit'); ?></th>
<th><?php echo $totalUnit; ?></th>
</tr>
</table>
</td>
</tr>
How do I resolve the error and use array index as $key
Query
$courseRegistrationDetails = \app\modules\course\models\CourseRegistrationDetail::find()
->with('courseMaster')
->where(['course_registration_id' => $model->id])
->all();
View
<?php foreach ($courseRegistrationDetails as $key => $value) : ?>
<tr>
<td class="text-center"> <?= ($key + 1) ?> </td>
<td class="text-center"> <?= $value->courseMaster->courseCodes ?> </td>
<td class="text-center"> <?= $value->courseMaster->courseTitles ?> </td>
<td class="text-center"> <?= $value->courseMaster->courseTypes ?> </td>
<td class="text-center"> <?= $value->courseMaster->courseUnits ?> </td>
<td class="text-center"> <?= $value->remark ?> </td>
</tr>
<?php $totalUnit += $value->course_unit; ?>
<?php endforeach; ?>
Using asArray()
Query
$courseRegistrationDetails = \app\modules\course\models\CourseRegistrationDetail::find()
->with('courseMaster')
->where(['course_registration_id' => $model->id])
->asArray()
->all();
View
<?php foreach ($courseRegistrationDetails as $key => $value) : ?>
<tr>
<td class="text-center"> <?= ($key + 1) ?> </td>
<td class="text-center"> <?= $value['courseMaster']['course_code'] ?> </td>
<td class="text-center"> <?= $value['courseMaster']['course_title'] ?> </td>
<td class="text-center"> <?= $value['courseMaster']['course_type'] ?> </td>
<td class="text-center"> <?= $value['courseMaster']['course_unit'] ?> </td>
<td class="text-center"> <?= $value['remark'] ?> </td>
</tr>
<?php $totalUnit += $value['course_unit']; ?>
<?php endforeach; ?>
I used here bootstrap. I want to red mark those row which Activity contains "N" .In my database Activity field has two characters. they are Y & N. I also used here jquery data table.What can i do?
<?php
while($reg_data = mysqli_fetch_array($result_sql2))
{
?>
<tr class="success">
<td > <?php echo $reg_data['ID'] ?> </td>
<td> <?php echo $reg_data['Name'] ?> </td>
<td> <?php echo $reg_data['Email'] ?> </td>
<td> <?php echo $reg_data['Type'] ?> </td>
<td> <?php echo $reg_data['Dept'] ?> </td>
<td> <?php echo $reg_data['Activity'] ?> </td> // based on this field //////
</tr>
One best quick and easy way to do is to give an inline style:
<td style="background-color: <?php echo ($reg_data['Activity'] == "Y") ? "green": "red" ?>;">
<?php echo $reg_data['Activity'] ?>
</td>
You might want to add !important just in case if needs an overriding:
<td style="background-color: <?php echo ($reg_data['Activity'] == "Y") ? "green": "red" ?> !important;">
Note: I know inline styles are bad.
Update: Sorry I missed that you need to do it on the row. You can just apply the same thing on the <tr> instead of that <td>. So your code will be:
<tr class="success" style="background-color: <?php echo ($reg_data['Activity'] == "Y") ? "green": "red" ?>;">
<td > <?php echo $reg_data['ID'] ?> </td>
<td> <?php echo $reg_data['Name'] ?> </td>
<td> <?php echo $reg_data['Email'] ?> </td>
<td> <?php echo $reg_data['Type'] ?> </td>
<td> <?php echo $reg_data['Dept'] ?> </td>
<td> <?php echo $reg_data['Activity'] ?> </td>
</tr>
Thanks to Darren Sweeney for pointing it out.
Check for window.onload.
Reference : https://developer.mozilla.org/en/docs/Web/API/GlobalEventHandlers/onload
Set a class based on activity on the desired <td> then upon window.onload use jQuery .parent() function and add css to the parent of <td> which containes "Y" as a class.
window.onload = function() {
$('.YClassName').parent().css('background', 'YOURCOLOR');
};
Another approach would be creating a CSS class and adding it to the element if your condition is met.
.red {
background-color: red;
}
.green {
background-color: green;
}
And then
<tr class="success <?php echo ($reg_data['Activity'] == "Y") ? "green": "red" ?>">
//table data
</tr>
As a sidenote, it'd be better to use "1" and "0" instead of "Y" and "N". Integer table-row types tend to perform better? At least, that's what I've believed until now.
my models
function get_all_events($limit = null, $offset = null) {
if ($limit) {
$this->db->limit($limit, $offset);
}
$this->db->order_by('events.added_on', 'desc');
return $this->db->get('events');
}
my controller
public function manage_events() {
$this->securepage();
$data['events'] = $this->events_model->get_all_events();
$this->load->library('pagination');
$config['base_url'] = site_url('events/manage-events');
$config['total_rows'] = $this->events_model->get_all_events()->num_rows();
$config['per_page'] = 2;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data = array(
'news_title' => 'Manage events | kanchan news.com' ,
'Keywords' => 'manage',
'url' =>'' ,
'content'=> $this->load->view('events/manage', $data, true)
);
$this->load->view('kanchan', $data);
}
My view
<div class="container">
<ol class="breadcrumb">
<li class="active"><?php echo $this->session->userdata('full_name');?></li>
<li class="active">Manage Events</li>
</ol>
<h1>Manage Events here.</h1>
<?php echo $this->session->flashdata('msg'); ?>
<div>
<?php if ($events->num_rows()): ?>
<table class="table table-condensed">
<thead>
<tr>
<th>SN</th>
<th>Add Title</th>
<th>expire date</th>
<th>Added on</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($events->result() as $u): ?>
<tr>
<td><?php echo $u->id; ?></td>
<td><?php echo $u->heading = word_limiter($u->heading,10); ?></td>
<td><?php echo $u->venue; ?></td>
<td><?php echo date('Y-m-d | h:i:a', $u->added_on); ?></td>
<td>
<span class="glyphicon glyphicon-trash"></span>
    <span class="glyphicon glyphicon-edit"></span>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>No any Events
<hr>
Add New Events
</p>
<?php endif; ?>
</div>
<?php echo $this->pagination->create_links();?>
</div>
It creates pagination <1 2 but it loads all result on page 1 and also same result on page 2 and I had tried many ideas but it does not work.
I'm using codeigniter 3 and mysql .
It happen because you are getting all records at every page.You did not pass $limit and $offset. at your function.
$data['events'] = $this->events_model->get_all_events();//getting all records every time.
Try to write your manage_event function like this way
public function manage_events($page_num=1)
{
$this->securepage();
$data['events'] = $this->events_model->get_all_events(2,($page_num-1)*2);
///keep your rest code here
//remember this 2 is from your config because you wanted view 2 record per page.You can replace it with your varible.
Note
$config['base_url'] = site_url('events/manage-events');
This should be
$config['base_url'] = site_url('events/manage_events');//underscore not hypen
I have a result.php page in view/result.php
<body>
<table border="1">
<tr>
<th>name1</th>
<th>name2</th>
<th>name3</th>
</tr>
<?php foreach($result as $row): ?>
<tr>
<td><?php echo $row->name1; ?></td>
<td><?php echo $row->name2; ?></td>
<td><?php echo $row->name3; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php echo 'Your new value is '.$name1;?>
<?php echo 'Your new value is '.$name2;?>
<?php echo 'Your new value is '.$name3;?>
</body>
html table is currently working. It retrives data from my table testing and display it. The problem is echo part in below code, it doesn't work.
<?php echo 'Your new value is '.$name1;?>
<?php echo 'Your new value is '.$name2;?>
<?php echo 'Your new value is '.$name3;?>
From the same table testing. It shows an error in the error page it shows Undefined variable name1,name2,name3
controller/example.php
<?php
class Example extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
}
function index() {
$this->load->view('example_view');
$this->getvalue();
$this->edit_content();
}
function getvalue()
{
if ($this->input->post('submit')==true) {
$data1['name1']=$this->input->post('name1');
$data1['name2']=$this->input->post('name2');
$data1['name3']=$this->input->post('name3');
$this->load->view('result',$data1);
$this->load->model('Insert_model');
$this->Insert_model->uploaddata($data);
}
}
function edit_content()
{
$data = array();
$this->load->model('Selected_model');
$this->load->helper('url');
$data['result'] = $this->Selected_model->get_contents();
$this->load->view('result',$data);
}
}
?>
<body>
<table border="1">
<tr>
<th>name1</th>
<th>name2</th>
<th>name3</th>
</tr>
<?php if(is_array($result)){ foreach($result as $row): ?>
<tr>
<td><?php echo $row->name1; ?></td>
<td><?php echo $row->name2; ?></td>
<td><?php echo $row->name3; ?></td>
</tr>
<?php endforeach; } ?>
</table>
<?php echo 'Your new value is '.$row->name1;?>
<?php echo 'Your new value is '.$row->name2;?>
<?php echo 'Your new value is '.$row->name3;?>
If your array is like in the following format.
$info = array
(
[0] => Array
(
[name1] => a
[name2] => b
[name3] => c
)
[1] => Array
(
[name1] => aa
[name2] => bb
[name3] => cc
)
[2] => Array
(
[name1] => aaa
[name2] => bbb
[name3] => ccc
)
[3] => Array
(
[name1] => aaaa
[name2] => bbbb
[name3] => cccc
)
);
Then
<body>
<table border="1">
<tr>
<th>name1</th>
<th>name2</th>
<th>name3</th>
</tr>
<?php foreach($info as $row): ?>
<tr>
<td><?php echo $row->name1; ?></td>
<td><?php echo $row->name2; ?></td>
<td><?php echo $row->name3; ?></td>
</tr>
<?php endforeach; ?>
</table>
The above loop will give you accurate result like.
name1 name2 name3
a b c
aa bb cc
aaa bbb ccc
aaaa bbbb cccc
Now the following code will generate an error because it does not know the following
$row->name1
$row->name1
$row->name1
The reason is that these are defined in the scope of foreach loop thats why its generate error.
I have a script that displays information of these fields- batchname, class, batchinstructor from the table "batch". But I want to display dynamically generated serial number on the left side when I show the data. For example:
Serial Number BatchName Class Batch Instructor
1. Solar Class Five John
2. Lunar Class six Bon Jovi
I have tried a lot but its not working. Would you please kindly help me to solve this? Please note that these serial number are not from database.
Here's my Controller:
<?php
class Batchlist extends CI_Controller{
function index(){
$this->load->library('pagination');
$config['base_url'] = base_url().'batchlist/index';
$config['total_rows'] = $this->db->get('batch')->num_rows();
$config['per_page'] = 20;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination" align="center">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$this->load->model('mod_batchlist');
$data['records']= $this->mod_batchlist->batch_list($config['per_page'],$this->uri->segment(3));
$data['main_content']='view_batchlist';
$this->load->view('includes/template',$data);
}
}
?>
Here's my Model:
function batch_list($perPage,$uri) {
$this->db->select('*');
$this->db->from('batch');
$this->db->join('teacher', 'batch.batchinstructor = teacher.teacherid');
$this->db->order_by('batchid','DESC');
$getData = $this->db->get('', $perPage, $uri);
if($getData->num_rows() > 0)
return $getData->result_array();
else
return null;
}
Here's my View
<h1>Batch List </h1>
<?php if(count($records) > 0) { ?>
<table id="table1" class="gtable sortable">
<thead>
<tr>
<th>Batch Name</th>
<th>Class</th>
<th>Batch Instructor</th>
<th>Edit/Delete</th>
</tr>
</thead>
<tbody>
<?php foreach ($records as $row){ ?>
<tr>
<td><?php echo $row['batchname'];?></td>
<td><?php echo $row['class'];?></td>
<td><?php echo $row['teachername'];?></td>
<td> <img src="<?php echo base_url(); ?>support/images/icons/edit.png" alt="Edit" />
<img src="<?php echo base_url(); ?>support/images/icons/cross.png" alt="Delete" />
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
<div class="tablefooter clearfix">
<div class="pagination">
<?php echo $this->pagination->create_links(); ?>
</div>
</div>
Maybe I misunderstood, but...what about a simple counter?? (after you ordered your records in SQL).
You need to pass to the view the number of items per page (in this snippet: $per_page), then you retrieve the current page from the URI ($this->uri->segment(n)).
At page 1, counter starts from (20*0)+1, i.e. 1. At page 2, starts from (1*20)+1 ie 21, at page 3 from (2*20)+1 ie 41 and so on...
<?php
$cur_page = $this->uri->segment(n) ? intval($this->uri->segment(n)) : 1;
$i = (($cur_page-1) * $per_page) +1;
foreach ($records as $row) :
?>
<tr>
<td><?php echo $i;?>.</td>
<td><?php echo $row['batchname'];?></td>
<td><?php echo $row['class'];?></td>
<td><?php echo $row['teachername'];?></td>
<td> <img src="<?php echo base_url(); ?>support/images/icons/edit.png" alt="Edit" /><img src="<?php echo base_url();?>support/images/icons/cross.png" alt="Delete" />
</td>
</tr>
<?php
++$i;
endforeach;
?>
You can place the counter wherever you want, I added a column for simplicity but if you want it somewhere else just place the counter there.
You said the serial numbers are not from the database. Where are they from then? If you want the database to generate them you can create a column named id and set it as the primary key and have it auto increment.