douwan4993 2017-04-18 16:34
浏览 31

创建一个单独的php查询(包含变量)以将搜索结果作为csv下载按钮返回

I am beginning to learn how to code in PHP and Postgres.

I understand how to export sql searches to csv but i am struggling how to export to csv when my sql query contains variables.

I have added a button already to my html search return page which i believe i use GET as the action.

I have searched the interweb but all i can find is how to export a normal sql query containing table fields. Now i'm not asking for the code solution just some direction as to how i reference the variables $find and $field from my originally run search in my new sql for my php csv export as the export php will be on another page.

<?php
$find = $_POST['find'];
$field = $_POST['field'];


if ( ($_POST['searching'] !='yes') ||  (isset($_POST["find"]) &&       empty($_POST["find"] ) ))        
{
$htmlpage = <<<HTMLPAGE
HTML FORM STUFF
HTMLPAGE;

echo $htmlpage ;
exit;
}
else
{

$sql = <<<SQLQUERY
SELECT
id,
job_ref_number,
job_title,
applicant_name,
app_emp_number,
line_manager_name,
line_manager_emp_number,
CASE WHEN temp_role = 'f' THEN 'No'
ELSE 'Yes - Contact Line Manager for Counter Signature' END,
details
FROM line_manager_forms
WHERE {$field} '{$find}'
ORDER BY job_ref_number 
SQLQUERY;

$dbh = pg_connect('host=blah  dbname=blah  user=blah  password=moodle25dev');
if (!$dbh) {
die('Error in connection: ' . pg_last_error());
}

$result = pg_query($dbh, $sql);
if (!$result) {
die('Error in SQL query: ' . pg_last_error());
}


while ($row = pg_fetch_array($result)) {


echo '<div style="border:1px solid #000000; padding:15px; margin-bottom: 10px;">';
echo '<p><strong>Unique ID:</strong><br> ' . $row['id'] . '</p>';
echo '<p><strong>Job Reference Number:</strong><br> ' .   $row['job_ref_number'] . '</p>';
echo '<p><strong>Job Title:</strong><br> ' . $row['job_title'] . '</p>';
echo '<p><strong>Applicant Name:</strong><br> ' . $row['applicant_name'] . '</p>';
echo '<p><strong>Applicant Employee Number:</strong><br> ' . $row['app_emp_number'] . '</p>';
echo '<p><strong>Line Manager Name:</strong><br> ' . $row['line_manager_name'] . '</p>';
echo '<p><strong>Line Manager Employee Number:</strong><br> ' . $row['line_manager_emp_number'] . '</p>';
echo '<p><strong>Temporary:</strong><br> ' . $row['case'] . '</p>';
echo nl2br('<p><strong>Supporting Information:</strong><br> ' . $row['details'] . '</p>');
echo '</div>';

echo '</tr>';}


echo '</table>';

$rows = pg_num_rows($result);

if ($rows == 0)
{
echo 'Sorry, but we can not find an entry to match your query<br><br>';
}

##echo '<b>Searched For:</b> ' .$find;

echo '<b>Number of rows :</b> ' .$rows;



pg_free_result($result);

pg_close($dbh);

}



?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程