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);
}
?>