Hi
I have written php code that displays a list of all tenders in a database table.
The user can sort on options displayed in the dropdown box and once a filter option is selected, and the Go button clicked, the view updates based on the selection. A filter example would be “Open”, “Closed”, “Awarded” etc.
This part is working for me.
However, I was trying to write some php code so that when you click the Download button image on the page, a csv file is created containing the contents of the filtered view that is currently displayed to the user.
This part isn’t working for me and any help pointing me in the right direction would be much appreciated.
This is my tenders function file:
<?php
function MakeOpts($options, $default)
{
$html = "";
foreach ($options as $name => $value)
{
if ($value === $default)
{
$sel = " selected";
}
else
{
$sel = "";
}
$html .= "\t<option value='$value'$sel>$name</option>\n";
}
return $html;
}
function CreateDownloadFile($sTemp, $sFile, $rows)
{
$sHeaders = "";
$sContents = "";
$sNewLine = "\n";
$sFileTo = "../wp-content/plugins/my-tenders/" . $sFile;
@rename($sFile, $sFileTo);
$fp = fopen($sFileTo, "w") or die("Unable to open file!");
$sHeaders = "ID," . "Name," . "Type," . "Issued," . "Closed," . "Status," . "Awarded To," . "Description," . $sNewLine;
if (rows)
{
$sContents = $sHeaders . $sTemp;
}
else
{
$sContents = $sHeaders . "No Matching Records Found!";
}
fwrite($fp, $sContents);
fclose($fp);
}
Here is my tenders.list file:
<?php
function my_tenders_list()
{
?>
<link type="text/css" href="<?php echo WP_PLUGIN_URL; ?>/my-tenders/css/style-admin.css" rel="stylesheet" />
<script language="JavaScript" src="<?php echo WP_PLUGIN_URL; ?>/my-tenders/js/validate-tenders.js"></script>
<?php
$options = array ('All' => '', 'Open' => 'Open', 'Closed' => 'Closed', 'Awarded' => 'Awarded',
'Archived' => 'Archived', 'Cancelled' => 'Cancelled');
if (isset($_POST["filter"]))
{
$filter = $_POST["filter"] ;
}
else
{
$filter = "All";
}
$sFilter = $filter;
$tmp = WP_PLUGIN_URL . "/my-tenders/img/town-logo.png";
$tmp1 = WP_PLUGIN_URL . "/my-tenders/img/btnDownload.jpg";
?>
<div class="wrap">
<img src="<?php echo $tmp ?>" />
<h2 style="font-size:28px;color:#0c6bb5;">Tenders List</h2>
<div class="tablenav top">
<div class="alignleft actions">
<a href="<?php echo admin_url('admin.php?page=my_tenders_create'); ?>">Add New Tender</a>
<br/>
<form name="Form1" action="" method="POST">
<input type="hidden" id="ftype" name="ftype" value="<?php echo $sFilter; ?>" >
<select name="filter" onchange="FilterTenders(this)">
<?= MakeOpts($options, $filter) ?>
</select>
<input type="submit" class="fbutton-small" value="GO">
</form>
<br/><br/>
</div>
<br class="clear">
</div>
<?php
global $wpdb;
global $sTemp;
global $sFile;
$sFile = "tenders-list.csv";
$sDelimiter = ",";
$sNewLine = "\n";
$sFilter = $_POST["ftype"];
$table_name = $wpdb->prefix . "tenders";
if ( ($sFilter == "All") || ($sFilter == "") )
{
$rows = $wpdb->get_results("SELECT * from $table_name ORDER BY id");
}
else
{
$rows = $wpdb->get_results("SELECT * from $table_name WHERE status = '$sFilter' ORDER BY id");
}
?>
<?php
if (! $rows)
{
echo "<div style='text-align:left;margin-top:160px;'>";
echo "No tender record(s) matching $sFilter were found. Try again.</div>";
return;
}
?>
<table class="wp-list-table widefat fixed striped posts" id="round" width=100%>
<tr>
<th class="table-hdr">ID</th>
<th class="table-hdr">Name</th>
<th class="table-hdr">Type</th>
<th class="table-hdr">Issued</th>
<th class="table-hdr">Closed</th>
<th class="table-hdr">Status</th>
<th class="table-hdr">Awarded</th>
<th class="table-hdr">Description</th>
<th class="table-hdr"> </th>
</tr>
<?php foreach ($rows as $row)
{
$sTemp = $sTemp . $row->id . $sDelimiter;
$sTemp = $sTemp . $row->name . $sDelimiter;
$sTemp = $sTemp . $row->type . $sDelimiter;
$sTemp = $sTemp . $row->issued . $sDelimiter;
$sTemp = $sTemp . $row->closed . $sDelimiter;
$sTemp = $sTemp . $row->status . $sDelimiter;
$sTemp = $sTemp . $row->awarded . $sDelimiter;
$sTemp = $sTemp . $row->desc . $sDelimiter;
$sTemp = $sTemp . $sNewLine;
?>
<tr>
<td><?php echo $row->id; ?></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->type; ?></td>
<td><?php echo $row->issued; ?></td>
<td><?php echo $row->closed; ?></td>
<td><?php echo $row->status; ?></td>
<td><?php echo $row->awarded; ?></td>
<td><?php echo substr($row->desc, 0, 60); ?></td>
<td><a href="<?php echo admin_url('admin.php?page=my_tenders_update&id=' . $row->id); ?>">Update</a></td>
</tr>
<?php } ?>
</table>
<br/><br/>
<?php
echo "<a href='$sFile' download>";
echo "<img src='$tmp1' border='0' alt='Download' onclick='CreateDownloadFile($sTemp, $sFileTo, $rows)' /></a>";
?>
</div>
<?php
}
?>