I have a custom module that's a multi-page form that collects one value from a user for each page of the form (3 pages, 3 total values collected). On the final page of the form (the recommendations page) it displays recommended resources from our database that match the user's selections. Because we need to put all 'timely events' first, followed by 'quick study' events, and finally 'deep dive' events the recommendations page uses 3 SQL queries to output the data to the screen.
All that works fine. However, now we'd like to add some way to allow the user to EXPORT the recommendations they see on the screen to a CSV file (or some other type) with an 'export' link. That's why I'm here asking you guys...does anyone know a great way to make this happen?
Here's the recommendations page code:
// function to return results from DB based on what user entered
function pathfindertool_recommendations_page() {
//first test if variables passed from Pathfinder tool
if (isset($_SESSION['role']) AND isset($_SESSION['level']) AND
isset($_SESSION['topic'])) {
// set variables
$role = $_SESSION['role'];
$level = $_SESSION['level'];
$topic = $_SESSION['topic'];
// gather session variables and give them string values
if ($role == 0) {
$role = "Grant Writer";
}
elseif ($role == 1) {
$role = "Nonprofit Administrator";
}
elseif ($role == 2) {
$role = "Nonprofit Founder";
}
else {
$role = "Consultant";
}
if ($level == 0) {
$level = "Novice";
}
elseif ($level == 1) {
$level = "Experienced";
}
else {
$level = "Professional";
}
if ($topic == 0) {
$topic = "Grants Research";
}
elseif ($topic == 1) {
$topic = "Grant Writing";
}
elseif ($topic == 2) {
$topic = "Grant Management";
}
else {
$topic = "Strategic Planning";
}
// set $output var
$output ='';
// print and email link styling
$output .= '<div class="print-link-rec"><span class="print_html"><a
class="print-page" rel="nofollow" title="Display a printer-friendly
version of this page." href="/print/pathfinder/recommendations"><img
class="print-icon print-icon-margin" width="16px" height="16px"
title="Printer-friendly version" alt="Printer-friendly version"
src="/sites/all/modules/contrib/print/icons/print_icon.png"
typeof="foaf:Image">Printer-friendly version</a></span></div>';
// progress bar
$output .= '<div class="progress-bar" id="progress-bar"><span
id="step-one">Primary Role</span><span id="arrow1"> > </span><span
id="step-two">Experience Level</span><span id="arrow2"> > </span><span
id="step-three">Topics of Interest</span><span id="arrow3"> > </span>
<span id="recommendations" style="background-color:
#003863;">Recommendations</span></div>';
// remind user what they selected in form
// but first, set pro and grants research to the preferred edits
if ($level == "Professional") {
$level2 = "Advanced";
} else {
$level2 = $level;
}
if ($topic == "Grants Research") {
$topic2 = "Grant Research";
} else {
$topic2 = $topic;
}
$output .= '<div class="selected">You selected <em>' . $role .
'</em>, <em>' . $level2 . '</em>, and <em>' . $topic2 . '</em>.</div>';
// sql for Timely Events
$result = db_query("SELECT n.title, n.nid, n.status, n.type,
fdfty.field_type_value, fdfi.entity_id, fdfi.field_icon_tid,
fdfti.entity_id, fdfti.field_taxonomy_icon_fid, fm.uri, fm.fid,
ua.source, ua.alias, el.field_experience_level_value, fdb.body_value,
fdfr.field_role_value, fdft.field_topics_value,
fdfed.field_event_dates_value, fdfed.field_event_dates_value2,
fdfl.field_location_value, fdfl.entity_id,
fdfo.field_organization_value, fdfo.entity_id FROM {node} AS n LEFT JOIN
{field_data_field_type} AS fdfty ON n.nid=fdfty.entity_id LEFT JOIN
{field_data_field_icon} AS fdfi ON n.nid=fdfi.entity_id LEFT JOIN
{field_data_field_taxonomy_icon} AS fdfti ON
fdfi.field_icon_tid=fdfti.entity_id LEFT JOIN {file_managed} AS fm ON
fdfti.field_taxonomy_icon_fid=fm.fid LEFT JOIN {url_alias} AS ua ON
CONCAT('node/', nid)=source LEFT JOIN
{field_data_field_experience_level} AS el ON n.nid=el.entity_id LEFT JOIN
{field_data_body} AS fdb ON n.nid=fdb.entity_id LEFT JOIN
{field_data_field_role} AS fdfr ON n.nid=fdfr.entity_id LEFT JOIN
{field_data_field_topics} AS fdft ON n.nid=fdft.entity_id LEFT JOIN
{field_data_field_event_dates} AS fdfed ON n.nid=fdfed.entity_id LEFT
JOIN {field_data_field_location} AS fdfl ON n.nid=fdfl.entity_id LEFT
JOIN {field_data_field_organization} AS fdfo ON n.nid=fdfo.entity_id
WHERE n.status=:status AND n.type=:resource AND
(fdfty.field_type_value=:webinar OR
fdfty.field_type_value=:workshoptraining OR
fdfty.field_type_value=:conference) AND
el.field_experience_level_value=:level AND fdfr.field_role_value=:role
AND fdft.field_topics_value=:topic AND ((fdfed.field_event_dates_value
IS NULL) OR (fdfed.field_event_dates_value >= CURRENT_DATE())) GROUP BY
n.nid, n.title ORDER BY CASE WHEN fdfty.field_type_value='1' THEN 0
WHEN fdfty.field_type_value='2' THEN 1 WHEN fdfty.field_type_value = '3'
THEN 2 END, fdfed.field_event_dates_value, n.title", array(':status' =>
'1', ':resource' => 'resource', ':webinar' => '1', ':workshoptraining'
=> '2', ':conference' => '3', ':level' => $level, ':role' => $role,
':topic' => $topic));
// count how many results returned
$num_rows = $result->rowCount();
$counter = $num_rows;
// test if results
if ($num_rows > 0) {
// output header
$output .= '<h2 class="recheader" id="timevents">Timely
Events</h2>';
// return results to page
while ($row = $result->fetchAssoc()) {
// set variables
$date1 = $row['field_event_dates_value'];
$timestamp = strtotime($date1);
$date2 = $row['field_event_dates_value2'];
$timestamp2 = strtotime($date2);
$title = $row['title'];
$nid = $row['nid'];
$alias = $row['alias'];
// set image to display correctly for category
$icon = image_style_url('thumbnail', $row['uri']);
// select price image
$result2 = db_query("SELECT n.title, n.nid, fdfp.entity_id,
fdfp.field_price_tid, fdfcm.entity_id, fdfcm.field_costs_money_fid,
fm2.fid, fm2.uri FROM {node} AS n LEFT JOIN {field_data_field_price} AS
fdfp ON n.nid=fdfp.entity_id LEFT JOIN {field_data_field_costs_money} AS
fdfcm ON fdfp.field_price_tid=fdfcm.entity_id LEFT JOIN {file_managed}
AS fm2 ON fdfcm.field_costs_money_fid=fm2.fid WHERE n.nid = :nid",
array(':nid' => $nid));
while ($row2 = $result2->fetchAssoc()) {
if ($row2['uri'] == 'public://dollar-sign.png') {
$dollar = image_style_url('thumbnail', $row2['uri']);
} else {
$dollar = "";
}
}
$desc = $row['body_value'];
$ftv = $row['field_type_value'];
// set dates to display correctly and add other output to
make page styled
if ($date1 != $date2) {
$output .= '<div class="recentry"><img src="' .$icon. '"
id="icon" alt="Timely Event" title="Timely Event" />';
if ($dollar != "") {
$output .= '<img src="' .$dollar. '" id="price"
alt="Costs Money" title="Costs Money" />';
} else {
$output .= "";
}
$location = $row['field_location_value'];
$organization = $row['field_organization_value'];
$output .= '<h2 class="rectitle"><a href="../' . $alias
. '">' . $title . '</a></h2><div class="recdates">' . date('l, F j, Y',
$timestamp) . ' to ' . date('l, F j, Y', $timestamp2) . '</div><div
class="recdesc">';
if (($location != NULL) && (($ftv == '2') || ($ftv ==
'3'))) {
$output .= '<div class="location">' . $location .
'</div>';
}
if (($organization != NULL) && (($ftv == '1') || ($ftv
== '2') || ($ftv == '3'))) {
$output .= '<div class="org">' . $organization .
'</div>';
}
$output .= '<div class="description">' . $desc . '</div>
</div></div>';
} elseif ($date1 == $date2) {
$output .= '<div class="recentry"><img src="' .$icon. '"
id="icon" alt="Timely Event" title="Timely Event" />';
if ($dollar != "") {
$output .= '<img src="' .$dollar. '" id="price"
alt="Costs Money" title="Costs Money" />';
} else {
$output .= "";
}
$location = $row['field_location_value'];
$organization = $row['field_organization_value'];
$output .= '<h2 class="rectitle"><a href="../' . $alias
. '">' . $title . '</a></h2><div class="recdates">' . date('l, F j, Y',
$timestamp) . '</div><div class="recdesc">';
if (($location != NULL) && (($ftv == '2') || ($ftv ==
'3'))) {
$output .= '<div class="location">' . $location .
'</div>';
}
if (($organization != NULL) && (($ftv == '1') || ($ftv
== '2') || ($ftv == '3'))) {
$output .= '<div class="org">' . $organization .
'</div>';
}
$output .= '<div class="description">' . $desc . '</div>
</div></div>';
} else {
$output .= '<div class="recentry"><img src="' .$icon. '"
id="icon" alt="Timely Event" title="Timely Event" />';
$output .= '/>';
if ($dollar != "") {
$output .= '<img src="' .$dollar. '" id="price"
alt="Costs Money" title="Costs Money" />';
} else {
$output .= "";
}
$location = $row['field_location_value'];
$organization = $row['field_organization_value'];
$output .= '<h2 class="rectitle"><a href="../' . $alias
. '">' . $title . '</a></h2><div class="recdesc">';
if (($location != NULL) && (($ftv == '2') || ($ftv ==
'3'))) {
$output .= '<div class="location">' . $location .
'</div>';
}
if (($organization != NULL) && (($ftv == '1') || ($ftv
== '2') || ($ftv == '3'))) {
$output .= '<div class="org">' . $organization .
'</div>';
}
$output .= '<div class="description">' . $desc . '</div>
</div></div>';
}
}
}
// sql for Quick Study items
$result = db_query("SELECT n.title, n.nid, n.status, n.type,
fdfty.field_type_value, fdfi.entity_id, fdfi.field_icon_tid,
fdfti.entity_id, fdfti.field_taxonomy_icon_fid, fm.uri, fm.fid,
ua.source, ua.alias, el.field_experience_level_value, fdb.body_value,
fdfr.field_role_value, fdft.field_topics_value,
fdfl.field_location_value, fdfl.entity_id,
fdfo.field_organization_value, fdfo.entity_id FROM {node} AS n LEFT JOIN
{field_data_field_type} AS fdfty ON n.nid=fdfty.entity_id LEFT JOIN
{field_data_field_icon} AS fdfi ON n.nid=fdfi.entity_id LEFT JOIN
{field_data_field_taxonomy_icon} AS fdfti ON
fdfi.field_icon_tid=fdfti.entity_id LEFT JOIN {file_managed} AS fm ON
fdfti.field_taxonomy_icon_fid=fm.fid LEFT JOIN {url_alias} AS ua ON
CONCAT('node/', nid)=source LEFT JOIN
{field_data_field_experience_level} AS el ON n.nid=el.entity_id LEFT
JOIN {field_data_body} AS fdb ON n.nid=fdb.entity_id LEFT JOIN
{field_data_field_role} AS fdfr ON n.nid=fdfr.entity_id LEFT JOIN
{field_data_field_topics} AS fdft ON n.nid=fdft.entity_id LEFT JOIN
{field_data_field_location} AS fdfl ON n.nid=fdfl.entity_id LEFT JOIN
{field_data_field_organization} AS fdfo ON n.nid=fdfo.entity_id WHERE
n.status=:status AND n.type=:resource AND
(fdfty.field_type_value=:articlereport OR fdfty.field_type_value=:blog
OR fdfty.field_type_value=:recweborpodcast OR
fdfty.field_type_value=:newslettermagazine) AND
el.field_experience_level_value=:level AND fdfr.field_role_value=:role
AND fdft.field_topics_value=:topic GROUP BY n.nid, n.title ORDER BY CASE
WHEN fdfty.field_type_value='4' THEN 0 WHEN fdfty.field_type_value = '5'
THEN 1 WHEN fdfty.field_type_value = '6' THEN 2 WHEN
fdfty.field_type_value = '7' THEN 3 END, n.title", array(':status' =>
'1', ':resource' => 'resource', ':articlereport' => '4', ':blog' => '5',
':recweborpodcast'=> '6', ':newslettermagazine'=>'7', ':level' =>
$level, ':role' => $role, ':topic' => $topic));
// count how many results returned
$num_rows = $result->rowCount();
$counter .= $num_rows;
if ($num_rows > 0) {
$output .= '<h2 class="recheader" id="quickstudy">Quick
Study</h2>';
//return results to page
while ($row = $result->fetchAssoc()) {
// set variables
$title = $row['title'];
$nid = $row['nid'];
$desc = $row['body_value'];
$alias = $row['alias'];
// set image to display correctly
$icon = image_style_url('thumbnail', $row['uri']);
$result2 = db_query("SELECT n.title, n.nid, fdfp.entity_id,
fdfp.field_price_tid, fdfcm.entity_id, fdfcm.field_costs_money_fid,
fm2.fid, fm2.uri FROM {node} AS n LEFT JOIN {field_data_field_price} AS
fdfp ON n.nid=fdfp.entity_id LEFT JOIN {field_data_field_costs_money}
AS fdfcm ON fdfp.field_price_tid=fdfcm.entity_id LEFT JOIN
{file_managed} AS fm2 ON fdfcm.field_costs_money_fid=fm2.fid WHERE n.nid
= :nid", array(':nid' => $nid));
while ($row2 = $result2->fetchAssoc()) {
if ($row2['uri'] == 'public://dollar-sign.png') {
$dollar = image_style_url('thumbnail', $row2['uri']);
} else {
$dollar = "";
}
}
$ftv = $row['field_type_value'];
$output .= '<div class="recentry"><img src="' .$icon. '"
id="icon" alt="Quick Study" title="Quick Study" />';
if ($dollar != "") {
$output .= '<img src="' .$dollar. '" id="price"
alt="Costs Money" title="Costs Money" />';
} else {
$output .= "";
}
$organization = $row['field_organization_value'];
$output .= '<h2 class="rectitle"><a href="../' . $alias
. '">' . $title . '</a></h2><div class="recdesc">';
if (($organization != NULL) && ($ftv == '6')) {
$output .= '<div class="org">' . $organization . '</div>';
}
$output .= '<div class="description">' . $desc . '</div>
</div></div>';
}
}
// sql for Deep Dive items
$result = db_query("SELECT n.title, n.nid, n.status, n.type,
fdfty.field_type_value, fdfi.entity_id, fdfi.field_icon_tid,
fdfti.entity_id, fdfti.field_taxonomy_icon_fid, fm.uri, fm.fid,
ua.source, ua.alias, el.field_experience_level_value, fdb.body_value,
fdfr.field_role_value, fdft.field_topics_value,
fdfl.field_location_value, fdfl.entity_id,
fdfo.field_organization_value, fdfo.entity_id FROM {node} AS n LEFT JOIN
{field_data_field_type} AS fdfty ON n.nid=fdfty.entity_id LEFT JOIN
{field_data_field_icon} AS fdfi ON n.nid=fdfi.entity_id LEFT JOIN
{field_data_field_taxonomy_icon} AS fdfti ON
fdfi.field_icon_tid=fdfti.entity_id LEFT JOIN {file_managed} AS fm ON
fdfti.field_taxonomy_icon_fid=fm.fid LEFT JOIN {url_alias} AS ua ON
CONCAT('node/', nid)=source LEFT JOIN
{field_data_field_experience_level} AS el ON n.nid=el.entity_id LEFT
JOIN {field_data_body} AS fdb ON n.nid=fdb.entity_id LEFT JOIN
{field_data_field_role} AS fdfr ON n.nid=fdfr.entity_id LEFT JOIN
{field_data_field_topics} AS fdft ON n.nid=fdft.entity_id LEFT JOIN
{field_data_field_location} AS fdfl ON n.nid=fdfl.entity_id LEFT JOIN
{field_data_field_organization} AS fdfo ON n.nid=fdfo.entity_id WHERE
n.status=:status AND n.type=:resource AND (fdfty.field_type_value=:tool
OR fdfty.field_type_value=:bookorworkbook OR
fdfty.field_type_value=:certificateordegreeprogram) AND
el.field_experience_level_value=:level AND fdfr.field_role_value=:role
AND fdft.field_topics_value=:topic GROUP BY n.nid, n.title ORDER BY CASE
WHEN fdfty.field_type_value='8' THEN 0 WHEN fdfty.field_type_value =
'9' THEN 1 WHEN fdfty.field_type_value = '10' THEN 2 END, n.title",
array(':status' => '1', ':resource' => 'resource', ':tool' => '8',
':bookorworkbook' => '9', ':certificateordegreeprogram'=>'10', ':level'
=> $level, ':role' => $role, ':topic' => $topic));
// count how many results returned
$num_rows = $result->rowCount();
$counter .= $num_rows;
if ($num_rows > 0) {
$output .= '<h2 class="recheader" id="deepdive">Deep Dive</h2>';
//return results to page
while ($row = $result->fetchAssoc()) {
// set variables
$title = $row['title'];
$nid = $row['nid'];
$desc = $row['body_value'];
$alias = $row['alias'];
// set image to display correctly
$icon = image_style_url('thumbnail', $row['uri']);
$result2 = db_query("SELECT n.title, n.nid, fdfp.entity_id,
fdfp.field_price_tid, fdfcm.entity_id, fdfcm.field_costs_money_fid,
fm2.fid, fm2.uri FROM {node} AS n LEFT JOIN {field_data_field_price} AS fdfp
ON n.nid=fdfp.entity_id LEFT JOIN {field_data_field_costs_money} AS fdfcm ON
fdfp.field_price_tid=fdfcm.entity_id LEFT JOIN {file_managed} AS fm2 ON
fdfcm.field_costs_money_fid=fm2.fid WHERE n.nid = :nid", array(':nid' =>
$nid));
while ($row2 = $result2->fetchAssoc()) {
if ($row2['uri'] == 'public://dollar-sign.png') {
$dollar = image_style_url('thumbnail', $row2['uri']);
} else {
$dollar = "";
}
}
$ftv = $row['field_type_value'];
$output .= '<div class="recentry"><img src="' .$icon. '"
id="icon" alt="Deep Dive" title="Deep Dive" />';
if ($dollar != "") {
$output .= '<img src="' .$dollar. '" id="price"
alt="Costs Money" title="Costs Money" />';
} else {
$output .= "";
}
$location = $row['field_location_value'];
$organization = $row['field_organization_value'];
$output .= '<h2 class="rectitle"><a href="../' . $alias
. '">' . $title . '</a></h2><div class="recdesc">';
if (($location != NULL) && ($ftv == '98')) {
$output .= '<div class="location">' . $location .
'</div>';
}
if (($organization != NULL) && ($ftv == '98')) {
$output .= '<div class="org">' . $organization .
'</div>';
}
$output .= '<div class="description">' . $desc . '</div>
</div></div>';
}
}
if ($counter == "0") {
// if no entries matched what user entered give them
alternative text.
$output .= '<div id="no-recs-returned"><p class="recdesc">'
. "Sorry, your selections did not result in any recommendations at this
time. " . '<a href=' . "/pathfinder" . '>' . "Go here to try again" .
'</a></p></div>';
}
// return variable to display results
return $output;
// if user went directly to rec page without taking questionnaire
} else {
$output = "";
// progress bar html/css styling
$output .= '<div class="progress-bar" id="progress-bar"><span
id="step-one">Primary Role</span><span id="arrow1"> > </span><span id="step-
two">Experience Level</span><span id="arrow2"> > </span><span id="step-
three">Topics of Interest</span><span id="arrow3"> > </span> <span
id="recommendations" style="background-color:
#003863;">Recommendations</span></div>';
$output .= '<p class="recdesc">' . "You haven't yet taken the
questionnaire; please " . '<a href="/pathfinder">' . "go here to get
started" . '</a>.</p>';
return $output;
}
}