I created a web page with two tabs, each page having a list of links. When I click on a link on first tab, the link on the second tab is activated. That is unless its a row number that exceeds the number of rows on the second tab. The output HTML is correct. Both tabs by themselves work fine.
I am using Yii 1.1.14, php 5.4.11
Controller Action:
public function actionTest5() {
$fileNew = new File;
$criteria = new CDbCriteria;
$criteria->select='id,name,description';
$criteria->condition='t.user_id='.(int)Yii::app()->user->id;;
$criteria->order='t.date_updated desc, t.date_entered desc';
$criteria->limit=5;
$templateList=File::model()->findAll($criteria);
$criteria = new CDbCriteria;
$criteria->select='id,name,description';
$criteria->condition='t.user_id='.(int)Yii::app()->user->id;;
$criteria->order='t.date_updated desc, t.date_entered desc';
$criteria->limit=3;
$listFile= Listfile::model()->findAll($criteria);
$docI = new Document;
$fileNew = new File;
$url = Yii::app()->createUrl('test/setup');
if(isset($_POST['File']) OR isset($_POST['Document']))
$this->redirect($url);
$this->render('test5', array(
'model'=>$docI,
'templateList'=>$templateList,
'listFile'=>$listFile,
'fileNew'=>$fileNew,
));
}
View File test5:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'setup-form',
'enableClientValidation'=>true,
));
$tabArray=array();
$tabArray["Tab1"] = array(
'id'=>'1',
'content'=>$this->renderPartial('_testTab1',
array(
'tabNum'=>1,
'form'=>$form,
'model'=>$model,
'templateList'=>$templateList,
),TRUE));
$tabArray["Tab2"] = array(
'id'=>'2',
'content'=>$this->renderPartial('_testTab2',
array(
'tabNum'=>2,
'form'=>$form,
'model'=>$model,
'listFile'=>$listFile,
),TRUE));
$this->widget('zii.widgets.jui.CJuiTabs',array(
'tabs'=>$tabArray,
'id'=>'MyTab-Menu',
));
?>
<?php $this->endWidget(); ?>
</div>
This is the partial view _testTab1:
<table class="setup">
<?php echo '<thead><th></th><th>File Name</th><th>Load File</th><th>Delete</th></thead>' ?>
<?php foreach ($templateList as $i=>$file): ?>
<tr id="trow">
<td><?php echo CHtml::activeHiddenField($file, "[$i]id"); ?></td>
<td><?php echo 'fileID: ['.$file['id'].']'; ?></td>
<td><?php
echo CHtml::link('Reload File','#',
array('submit'=>array('setupFile','id'=>$i),
'id' => 'setup_' . $i . '_reload',
'value' => (int) $i,
'name' => 'setupFile',
));
?></td>
<td><?php
echo CHtml::link('Delete','#',
array('submit'=>array('fileDelete','id'=>$i),
'id' => 'setup_' . $i . '_delete',
'value' => (int) $i,
'name' => 'delete',
'confirm' => 'Are you sure you want to delete file '.$file['id'].'?',
));
?></td>
</tr>
<?php endforeach; ?>
</table>
This is the partial view _testTab2:
<table class="setup">
<?php echo '<thead><th></th><th>File Name</th><th>Load File</th><th>Delete</th></thead>' ?>
<?php foreach ($listFile as $i => $file): ?>
<tr id="trow">
<td><?php echo CHtml::activeHiddenField($file, "[$i]id"); ?></td>
<td><?php echo 'fileID: ['.$file['id'].']'; ?></td>
<td><?php
echo CHtml::link('Reload File','#',
array('submit'=>array('setupFileList','id'=>$i),
'id' => 'setup_' . $i . '_reload',
'value' => (int) $i,
'name' => 'setupFileList',
));
?></td>
<td><?php
echo CHtml::link('Delete','#',
array('submit'=>array('fileListDelete','id'=>$i),
'id' => 'setup_' . $i . '_delete',
'value' => (int) $i,
'name' => 'delete',
'confirm' => 'Are you sure you want to delete file '.$file['id'].'?',
));
?></td>
</tr>
<?php endforeach; ?>
</table>
On post, the File[x]['id']'s are set with the x=row number and id being the hidden field (same applies to Listfile for second tab rows). When Reload is clicked, the following action is loaded. It gets the x value (row number) and from that determines the id value. Then it loads the test/uploadFile/id page. The problem is that when I click on the Reload (or delete) link for the first three rows on the first tab, I get the id for the second tab. When I do this for the last two rows on the first tab, I get the correct id value. All links on the second tab work as expected as do each of the tabs if they are loaded individually.
public function actionSetupFile($id) {
$fileName='';
if(isset($_POST['File'])) {
if(isset($_POST['File'][(int)$id]['id'])) {
$selected = $_POST['File'][(int)$id]['id'];
$file=new File;
$fileName=$file->getFileName($selected);
$url = Yii::app()->createUrl('test/UploadFile/'.CHtml::encode((int)$selected));
}
} else {
$msg = 'Unable to complete request to upload file; try again ';
$url = Yii::app()->createUrl('test/setup');
}
$this->redirect($url, array(
'fileName'=>$fileName,
));
}
public function actionSetupFileList($id=null) {
$fileName='';
if(isset($_POST['Listfile'])) {
if(isset($_POST['Listfile'][(int)$id]['id'])) {
$selected = $_POST['Listfile'][(int)$id]['id'];
$file=new Listfile;
$fileName=$file->getFileListName($selected);
$url = Yii::app()->createUrl('test/UploadFileList/'.CHtml::encode((int)$selected));
}
} else {
$msg = 'Unable to complete request to upload file; try again ';
$url = Yii::app()->createUrl('test/setup');
}
$this->redirect($url, array(
'fileName'=>$fileName,
));
}
The post indices must be different for each tab. One way to do this is to use a multiplier for the second tab, defined as $k=100*($i+1); $i+1 because $i is 0 first and index for first row would be 0 for both tabs. Of course, some other multiplier could be used.
<table class="setup">
<?php echo '<thead><th></th><th>File Name</th><th>Load File</th><th>Delete</th></thead>' ?>
<?php foreach ($listFile as $i => $file): $k=($i+1)*100; ?>
<tr id="trow">
<td><?php echo CHtml::activeHiddenField($file, "[$k]id"); ?></td>
<td><?php echo 'fileID: ['.$file['id'].']'; ?></td>
<td><?php
echo CHtml::link('Reload File','#',
array('submit'=>array('setupFileList','id'=>$k),
'id' => 'setup_' . $k . '_reload',
'value' => (int) $k,
'name' => 'setupFileList',
));
?></td>
<td><?php
echo CHtml::link('Delete','#',
array('submit'=>array('fileListDelete','id'=>$k),
'id' => 'setup_' . $k . '_delete',
'value' => (int) $k,
'name' => 'delete',
'confirm' => 'Are you sure you want to delete file '.$file['id'].'?',
));
?></td>
</tr>
<?php endforeach; ?>
</table>
Alternatively, pass offset from view file (test5) where offset for second and subsequent tabs is count of entries in ALL prior tabs (i.e. 2nd tab: $offset=count($templateList)). Then instead of $k above, use $i + $offset):
Related
I have a problem with my form. When i input the form and submit, the form didn't save to my database.
In my form showing code transaction, and email customer. And if customer buying product in my website.
I just confirmation the order with click form code transaction.
In form code transaction showing, code transaction and email customer. And me input the logistic name, no. order of logistic to customer tracking their order, and choice the order packet has sending.
But in my problem the form succes save the order packet information "has sending" , but for logistic name and no. order of logistic didn't save.
This is my view.
<div id="<?php echo $row['codetrans'] ?>" class="reveal-modal">
<h3>Detail Order</h3>
<?php echo form_open('order/edit_stats_order');
$codetrans = array(
'name' => 'codetrans',
'class' => 'column',
'size' => '30'
);
$email = array(
'name' => 'email',
'class' => 'column',
'size' => '30'
);
$no_order = array(
'name' => 'no_order',
'class' => 'column',
'size' => '30'
);
$log = array(
'FedEx' => 'FedEx',
'RPX' => 'RPX');
?>
<span>Code :</span>
<?php echo form_input($codetrans,$row['codetrans']) ?><br/>
<span>Email :</span>
<?php echo form_input($email,$row['email']) ?><br/>
<span>Logistic Name :</span><br/>
<?php echo form_dropdown('log', $log,$row['log']); ?><br/><br/>
<span>No. Order :</span>
<?php echo form_input($no_order,$row['no_order']) ?><br/>
<?php $options = array(
'0' => 'Not Sendirng',
'1' => 'Has Sending'); ?><br/>
<?php echo form_dropdown('stats_order', $options,$row['stats_order']); ?><br/>
<?php echo form_hidden('input_hidden_id',$row['codetrans']);?>
<?php echo form_submit('submit', 'Submit', 'class=searchbutton'); ?>
<?php echo form_close();?>
<a class="close-reveal-modal">×</a>
</div>
<?php
}
?>
</ul>
<?php echo anchor('order/stats_order', 'Open All Stats', 'class="btn btn_aS"' ); ?>
</div>
This my controller.
public function edit_stats_order() {
$this->load->model('Order_model');
$codetrans = $this->input->post('codetrans');
$email = $this->input->post('email');
$log = $this->input->post('log');
$no_order = $this->input->post('no_order');
$stats_order = $this->input->post('stats_order');
if($email !='' && $codetrans !='' && $log !='' && $no_order !=''){
$this->Order_model->edit_stats_order($codetrans,$email,$log,$no_order);
redirect('order/main');
}
else{
echo "<script>alert('Check Again!');</script>";
echo "<script>location.href = document.referrer</script>";
}
}
This is my model.
public function edit_stats_order($codetrans,$email,$log,$no_order){
date_default_timezone_set('xxxxx');
$data['email'] = $email;
$data['no_order'] = $no_order;
$data['log'] = $log;
$data['stats_order'] = $stats_order;
$data['date'] = date('Y-m-d');
$this->db->where('codetrans',$codetrans);
$this->db->update('ordering',$data);
}
Why the fault in my code?
Thanks
You are trying to use $stats_order in model function which is not defined in model. So you have to pass $stats_order to model function.
Add one more parameter to the function call for $stats_order in order to pass it to the model.
$this->Order_model->edit_stats_order($codetrans,$email,$log,$no_order,$stats_order);
Also add one more parameter in model function for the same. Change the first line of the function in model to,
public function edit_stats_order($codetrans,$email,$log,$no_order,$stats_order){
Hi guys i am trying to display the first td values as static so i have keep those values in one array and i try to increase the values and try to display but when i get it is displaying depending on foreach values if i have one record in foreach it is displaying one value and if i have 2 records it is displaying 2 values.
But i want to display all td value if i have values in foreach or not also.
Here is my code:
<tbody>
<?php
$arr = array(0=>'On Hold',1=>'Asset Incomplete',2=>'SME Discussion',3=>'a',4=>'b',5=>'c',6=>'d',7=>'e',8=>'f',9=>'g',10=>'h');
$i = 0;
foreach($getCourse as $report)
$status= $report->status;
{
?>
<tr>
<td><?php echo $arr[$i]; ?>
<?php $i++; ?></td>
<td><?php
if($status==1)
{
echo "On Hold";
}
elseif($status==2)
{
echo "Asset Incomplete";
}
elseif($status==3)
{
echo "Yet to Start";
}
elseif($status==4)
{
echo "SME Discussion";
}
elseif($status==5)
{
echo "Development";
}
elseif($status==6)
{
echo "PB Review";
}
elseif($status==7)
{
echo "PB Fixes";
}
elseif($status==8)
{
echo "PB2 Review";
}
elseif($status==9)
{
echo "PB2 Fixes";
}
elseif($status==10)
{
echo "Alpha Development";
}
elseif($status==11)
{
echo "Alpha Review";
}
elseif($status==12)
{
echo "Alpha Fixes";
}
elseif($status==13)
{
echo "Beta Review";
}
elseif($status==14)
{
echo "Beta Fixes";
}
elseif($status==15)
{
echo "Gamma";
}
?></td>
<td><?php echo $report->coursename; ?></td>
<td><?php echo $report->statuscount;?></td>
<td></td>
</tr>
<?php
}
?>
</tbody>
Here is my controller:
public function index()
{
if ($this->session->userdata('is_logged')) {
$data['getCourse']=$this->Report_model->getTopicReports();
$this->load->view('template/header');
$this->load->view('reports/report',$data);
$this->load->view('template/footer');
}
else {
redirect("Login");
}
}
Here is my model:
public function getTopicReports()
{
$this->db->select('count(t.status) as statuscount,t.topicName as topicname,c.coursename,t.status')
->from('topics t')
->join('course c', 't.courseId = c.id')
->where('t.courseid',1)
->where('t.status',5);
$query = $this->db->get();
return $query->result();
}
This is how i want to display here is my referrence image:
Can anyone help me how to do that thanks in advance.
FINAL UPDATED & WORKING VERSION
For me this was tricky. This turned out to be what I felt to be a awkward indexing issue.
The problem is that your data is not optimized very well to accommodate the task you are asking for. You have to make odd comparisons as you traverse the table. I was able to get it accomplished.
I would actually be very interested in seeing a more refined approach. But until that happens this code will dynamically generate a table that matches the output of the sample pictures that you posted in your question.
I have tested this code with some sample data that matches the array structure that you posted in your comments.
Here is the code:
//Create an array with your status values.
$rowName = array(
'On Hold',
'Asset Incomplete',
'Yet to Start',
'SME Discussion',
'Development',
'PB Review',
'PB Fixes',
'PB2 Review',
'PB2 Fixes',
'Alpha Development',
'Alpha Review',
'Alpha Fixes',
'Beta Review',
'Beta Fixes',
'Gamma'
);
//Generate a list of class names and get rid of any duplicates.
foreach($getCourse as $report){
$courseNames[] = $report->coursename;
}
$courseNames = array_unique($courseNames);
//Create the header.
echo
'<table>
<thead>
<tr>
<th>#</th>';
foreach($courseNames as $course){
echo '<th>' . $course . '</td>';
}
echo
'<th>Total</th>
</tr>
</thead>
<tbody>';
//Iterate across the array(list) of status values.
for($i = 0; $i < count($rowName); $i++){
echo
'<tr>';
echo '<td>' . $rowName[$i] . '</td>';
//Iterate through all combinations of class names and status values.
for($j = 0; $j < count($courseNames); $j++){
//Set flags and intial values.
$found = FALSE;
$total = 0;
$value = NULL;
for($k = 0; $k < count($getCourse); $k++){
//***Note - The ""$getCourse[$k]->status - 1" is because the status values in your data do not appear
//to start with an index of 0. Had to adjust for that.
//Sum up all the values for matching status values.
if(($getCourse[$k]->status - 1) == $i){
$total += $getCourse[$k]->statuscount;
}
//Here we are checking that status row and the status value match and that the class names match.
//If they do we set some values and generate a table cell value.
if(($getCourse[$k]->status - 1) == $i && $courseNames[$j] == $getCourse[$k]->coursename){
//Set flags and values for later.
$found = TRUE;
$value = $k;
}
}
//Use flags and values to generate your data onto the table.
if($found){
echo '<td>' . $getCourse[$value]->statuscount . '</td>';
}else{
echo '<td>' . '0' . '</td>';
}
}
echo '<td>' . $total . '</td>';
echo
'</tr>';
}
echo '</tbody>
</table>';
Try this. I am storing course status in $used_status then displaying the remaining status which are not displayed in foreach.
$arr = array ( '1' =>'On Hold', '2' => 'Asset Incomplete', '3' => 'SME Discussion', '4' => 'a', '5' => 'b', '6' => 'c', '7' =>'d', '8' => 'e', '9' => 'f', '10' => 'g', '11' => 'h' );
$used_status = array();
foreach($getCourse as $report) { ?>
<tr>
<td>
<?php $course_status = isset($arr[$report->status]) ? $arr[$report->status] : "-";
echo $course_status;
$used_status[] = $course_status; ?>
</td>
<td>
<?php echo $report->coursename; ?>
</td>
<td>
<?php echo $report->statuscount; ?>
</td>
</tr>
<?php }
foreach($arr as $status) {
if(!in_array($status, $used_status)) { ?>
<tr>
<td>
<?php echo $status; ?>
</td>
<td>
<?php echo "-"; ?>
</td>
<td>
<?php echo "-"; ?>
</td>
</tr>
<?php }
} ?>
I am writing a shopping cart and have my data stored in the $_SESSION array, but would like to calculate a total. below it the code I thought would work to do this, but it returns '1' in stead of a total!
$total = array($_SESSION['qty'],$_SESSION['pr']);
/* I'll give you more code...thanks for your help!!
here is the code for my php cart:
<?php
function item_list()
{
if(isset($_SESSION['qty'])){
$total = array($_SESSION['qty'],$_SESSION['pr']);
foreach($_SESSION['qty'] as $key => $value)
{?>
<tr>
<td align="center"><?php echo $_SESSION['item'][$key]; ?></td>
<td align="center"><?php echo $value; ?></td>
<td align="center"><?php echo $_SESSION['pr'][$key]; ?></td>
<td align="center"><?php echo array_product($total); ?>
</tr><?php
}
}
}
session_start();
if(isset($_POST['clear']) && ($_POST['clear'] == 'clear'))
{
session_destroy();
unset($_SESSION['qty']);
unset($_SESSION['item']);
unset($_SESSION['pr']);
unset($_POST['qty']);
unset($_POST['item']);
unset($_POST['pr']);
}
if(!isset($_SESSION['qty'])) $_SESSION['qty'] = array();
if(!isset($_SESSION['item'])) $_SESSION['item'] = array();
if(!isset($_SESSION['pr'])) $_SESSION['pr'] = array();
if(isset($_POST['qty']))
{
foreach($_POST['qty'] as $value)
{
if(!$value == '') array_push($_SESSION['qty'], filter_var($value,
FILTER_SANITIZE_SPECIAL_CHARS));
}
foreach($_POST['item'] as $key => $value)
{
if(!$_POST['qty'][$key] == '') array_push($_SESSION['item'], filter_var($value,
FILTER_SANITIZE_SPECIAL_CHARS));
}
foreach($_POST['pr'] as $key => $value)
{
if(!$_POST['qty'][$key] == '') array_push($_SESSION['pr'], filter_var($value,
FILTER_SANITIZE_SPECIAL_CHARS));
}
}
?>
That is a strange way to structure a shopping cart, but here's how to do it with that structure:
foreach($_SESSION['qty'] as $key => $value)
{
$total = $_SESSION['qty'][$key] * $_SESSION['pr'][$key];
?>
<tr>
<td align="center"><?php echo $_SESSION['item'][$key]; ?></td>
<td align="center"><?php echo $value; ?></td>
<td align="center"><?php echo $_SESSION['pr'][$key]; ?></td>
<td align="center"><?php echo $total; ?>
</tr><?php
}
If you wanted to get a total of all quantity and cost of the cart:
function getTotals()
{
$total = array('qty' => 0, 'price' => 0);
foreach($_SESSION['qty'] as $key => $qty)
{
$total['qty'] += $qty;
$total['price'] += ($_SESSION['pr'][$key] * $qty)
}
return $total;
}
$total = getTotals();
echo $total['qty']; // output the total quantity of items
echo $total['price']; // output the total cost for all items and quantity
I would recommend a better structure though, something like:
$_SESSION['cart']['items'] = array(
array(
'name' => 'Screwdriver',
'price' => 5,
'qty' => 2,
),
array(
'name' => 'Hammer',
'price' => 10,
'qty' => 1,
)
);
As per your cart array it is not able to hold multiple products you have to use multy dimensional array like this
$_SESSION['cart_items'] = array(
array( "qty"=>5, "item"=>"tshirt", "pr"=>50.20),
array( "qty"=>2, "item"=>"Cell Phone", "pr"=>50.20),
array( "qty"=>7, "item"=>"", "pr"=>50.20),
)
then you can write your code like this
function item_list()
{
foreach($_SESSION['cart_items'] as $item_array)
{?>
<tr>
<td align="center">Item:<?php echo $item_array['item']; ?></td>
<td align="center">Qty: <?php echo $item_array['qty']; ?></td>
<td align="center">Price :<?php echo $item_array['pr']; ?></td>
<td align="center">Total : <?php echo $item_array['qty'] * $item_array['pr']; ?>
</tr><?php
}
}
You should create yourself a Card class that is able to import/export data from the $_SESSION superglobal (or some other array if you mock it for tests, testing with $_SESSION can be akward) which is able to handle your data-structure easily and can calculate the total, too:
$cart = new Cart();
$cart->importFromArray($_SESSION);
// or:
$cart->importFromArray($_SESSION['cart']);
// later on:
$total = $cart->getTotal();
// somewhere else:
$cart->addItem(...);
...
$_SESSION['cart'] = $cart->exportToArray();
That will allow you to more easily change the code over time.
The code shown below is used to manually fill an array.
<?php
include_once 'include/DatabaseConnector.php';
$data = array(
array(0,array("111",' EE112',' AA','FT445'),"2004-03-01 10:00","2004-03-01 14:00"),
array(1,array("111",' BC124',' RYA','FE675'),"2004-03-01 16:00","2004-03-01 18:00"),
array(2,array("11",' BE225',' FA','AE667'),"2004-03-01 09:00","2004-03-01 10:00"),
array(3,array("11",' TC828',' BA','FF745'),"2004-03-01 06:00","2004-03-01 08:00")
);
?>
Now I want to fill this array from the database:
$query1="SELECT * FROM MyData;";
$result1=DatabaseConnector::ExecuteQueryArray($query1);
<?php foreach ($result1 as $row):?>
<tr>
<td><?php echo $row['resReg']; ?></td>
<td><?php echo $row['resTitle']; ?></td>
<td><?php echo $row['resAvailability'] ? 'Yes' : 'No';?></td>
</tr>
<?php endforeach;?>
How to assign the columns of result1 to the columns of the array?
Am i right assuming you want to do something like this:
<?php
$query1 = "SELECT name,color1,color2 FROM MyPets;";
$result1 = DatabaseConnector::ExecuteQueryArray($query1);
$petArray = array();
foreach($result1 as $pet):
$petArray[] = array('name' => $pet['name'],
'colors' => array(
$pet['color1'],
$pet['color2'])
);
?>
Hello
i'm started to learning cakephp framework.And just for start i wanted to write some simple game.
I have user controller wited already (not finished but works :D) and now i started to write equipment. But i stuck at the moment i'm trying to sort foreach by type (helmet, armor, shield etc) and now script output table like this:
Id Owner Name Status Type Cost
1 23 Krasnoludzka Salada B H 100
2 23 Jakieś spodnie B L 10
3 23 Zbroja B A 123
But i wanna to make it like this:
Id Owner Name Status Type Cost
Helmets:
1 23 Krasnoludzka Salada B H 100
4 23 Smocza Salada B H 100
Legs:
2 23 Jakieś spodnie B L 10
Armors:
3 23 Zbroja B A 123
Mine equipments_controller.php:
<?php
class EquipmentsController extends AppController {
var $name = 'Equipments';
var $helpers = array('Html', 'Form');
function index() {
$this->set('equipments', $this->Equipment->find('all', array('conditions' => array('owner='.$this->Session->read('Auth.User.id'), 'status=\'B\''))));
//$this->set('equipments', $this->Equipment->find('owner='.$this->Session->read('Auth.User.id')));
}
function view ($id = null) {
$this->Equipment->id = $id;
$owner = $this->Equipment->read('owner');
if ($owner['Equipment']['owner']==$this->Session->read('Auth.User.id')) {
$this->redirect(array('controller' => 'equipments', 'action' => 'index'));
$this->Session->setFlash('To nie twój przedmiot!');
} else {
$this->set('equipment', $this->Equipment->read());
}
}
}
And equipments/index.ctp:
<!-- File: /app/views/news/index.ctp (edit links added) -->
<h1>Plecak</h1>
<table>
<tr>
<th>Id</th>
<th>Owner</th>
<th>Name</th>
<th>Status</th>
<th>Type</th>
<th>Cost</th>
</tr>
<!-- Here's where we loop through our $news array, printing out news info -->
<?php foreach ($equipments as $equipment): ?>
<tr>
<td><?php echo $equipment['Equipment']['id']; ?></td>
<td><?php echo $equipment['Equipment']['owner']; ?></td>
<td><?php echo $equipment['Equipment']['name']; ?></td>
<td><?php echo $equipment['Equipment']['status'];?></td>
<td><?php echo $equipment['Equipment']['type']; ?></td>
<td><?php echo $equipment['Equipment']['cost']; ?></td>
</tr>
<?php endforeach; ?>
</table>
Can anyone help me?
You can add a 'group' option to your find()...
$this->set(
'equipments',
$this->Equipment->find(
'all',
array(
'conditions' => array(
'Equipment.owner' => $this->Session->read('Auth.User.id'),
'Equipment.status' => 'B'
),
'group' => array(
'Equipment.type'
),
'order' => array(
'Equipment.type',
'Equipment.name',
),
)
)
);
Hopefully you've actually got a Model for those types, and can use that model instead of those Equipment.type values, something like EquipmentType.name would be useful in your view. If you had that, then you'd be able to output a new heading row each time the EquipmentType.id changed.
I I'm understanding you correctly, you'd like to be able to sort your table of data by the titles in the table head tags.
If this is the case, I'd suggest using cake's built in paginator.
Your controller should look like:
class EquipmentsController extends AppController {
var $name = 'Equipments';
var $helpers = array('Html', 'Form');
public $paginate = array(
'Equipment' => array(
'limit' => 25,
'order' => array(
'Equipment.id' => 'ASC'
)
)
);
function index() {
$owner = $this->Session->read('Auth.User.id');
$equipments = $this->paginate('Equipment', array(
'Equipment.owner' => $owner,
'Equipment.status' => 'B'
));
$this->set(compact('equipments'));
}
}
Then in your views/equipments/index.ctp:
<!-- File: /app/views/news/index.ctp (edit links added) -->
<h1>Plecak</h1>
<table>
<tr>
<th><?=$this->Paginator->sort('Id', 'Equipment.id')?></th>
<th><?=$this->Paginator->sort('Owner', 'Equipment.owner')?></th>
<th><?=$this->Paginator->sort('Name', 'Equipment.name')?></th>
<th><?=$this->Paginator->sort('Status', 'Equipment.status')?></th>
<th><?=$this->Paginator->sort('Type', 'Equipment.type')?></th>
<th><?=$this->Paginator->sort('Cost', 'Equipment.cost')?></th>
</tr>
<!-- Here's where we loop through our $news array, printing out news info -->
<?php foreach ($equipments as $equipment): ?>
<tr>
<td><?php echo $equipment['Equipment']['id']; ?></td>
<td><?php echo $equipment['Equipment']['owner']; ?></td>
<td><?php echo $equipment['Equipment']['name']; ?></td>
<td><?php echo $equipment['Equipment']['status'];?></td>
<td><?php echo $equipment['Equipment']['type']; ?></td>
<td><?php echo $equipment['Equipment']['cost']; ?></td>
</tr>
<?php endforeach; ?>
Using the paginator this way will generate links in your table headers that will automatically sort the data coming out of the db.