[英]Get data from table with CakePHP 2
What I have : 我有的 :
I have a table named Sales and 3 colums named Seller , Buyer and Price . 我有一个名为Sales的表和三个名为Seller , Buyer和Price的列 。
In the controller, I have a function myview() where I do this SQL Query : 在控制器中,我有一个函数myview() ,在其中执行以下SQL查询:
SELECT SUM(price) AS total, seller_name, buyer_name
FROM sales
GROUP BY seller_name, buyer_name
The function : 功能 :
public function myview() {
$this->set('mysales', $this->Sales->find('all',
array(
'fields' => array('SUM(price) AS total','seller_name','buyer_name'),
'group' => array('seller_name','buyer_name')
)
));
}
I display the results in myview.ctp : 我在myview.ctp中显示结果:
<table>
<tr>
<th>Seller</th>
<th>Buyer</th>
<th>Total</th>
<th>PDF</th>
</tr>
<?php
foreach ($mysales as $thesales)
{
?>
<tr>
<td><?php echo $thesales['Sales']['seller_name']; ?></td>
<td><?php echo $thesales['Sales']['buyer_name']; ?></td>
<td><?php echo $thesales[0]['total']; ?></td>
<td><input type="button" onclick="location.href='myview_pdf';" value="PDF"/></td>
</tr>
</table>
<?php
}
echo $this->Js->writeBuffer();
?>
The PDF button brings you to another view named myview_pdf where it displays (for now) a blank PDF. “ PDF”按钮将您带到另一个名为myview_pdf的视图,在该视图中(现在)显示空白PDF。
What I want : 我想要的是 :
When you click on the PDF button, it "recognizes" the 'seller_name' and the 'buyer_name' of the line where I clicked the button so I can do this SQL query : 当您单击PDF按钮时,它将“识别”我单击按钮所在行的“ seller_name”和“ buyer_name”,因此我可以执行以下SQL查询:
SELECT price, seller_name, buyer_name
FROM sales
GROUP BY seller_name, buyer_name
And display the results into the myview_pdf.ctp 并将结果显示到myview_pdf.ctp中
Problem : 问题:
I'm struggling : how do I know which line I clicked, how do I fetch the datas from the line, where do I write the SQL query, how do I pass the query results into myview_pdf.ctp... 我在挣扎:如何知道我单击了哪一行,如何从该行中获取数据,在哪里编写SQL查询,如何将查询结果传递到myview_pdf.ctp ...
Thanks for your help! 谢谢你的帮助!
You need to make this a link first instead of a button, and pass the seller and buyer names as query strings. 您需要首先使其成为链接而不是按钮,然后将卖方和买方的名称作为查询字符串传递。
<td>
<!-- <input type="button" onclick="location.href='myview_pdf';" value="PDF"/> --> <!-- Remove this -->
<?php echo $this->Html->link("PDF", array(
"controller" => "<controller name>",
"action" => "myview_pdf",
"?" => array(
"seller_name" => $thesales['Sales']['seller_name'];,
"buyer_name" => $thesales['Sales']['buyer_name'];,
)
)); ?>
And then in your controller, you can access the seller and buyer name from query string: 然后在您的控制器中,您可以从查询字符串访问卖方和买方的名称:
public function myview_pdf() {
$seller_name = $this->request->query("seller_name");
$buyer_name = $this->request->query("buyer_name");
/* Other code */
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.