/
export.php
708 lines (694 loc) · 22.2 KB
/
export.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
<?php
/*
#############################################################
# >>> PHP Surveyor #
#############################################################
# > Author: Jason Cleeland #
# > E-mail: jason@cleeland.org #
# > Mail: Box 99, Trades Hall, 54 Victoria St, #
# > CARLTON SOUTH 3053, AUSTRALIA
# > Date: 20 February 2003 #
# #
# This set of scripts allows you to develop, publish and #
# perform data-entry on surveys. #
#############################################################
# #
# Copyright (C) 2003 Jason Cleeland #
# #
# This program is free software; you can redistribute #
# it and/or modify it under the terms of the GNU General #
# Public License as published by the Free Software #
# Foundation; either version 2 of the License, or (at your #
# option) any later version. #
# #
# This program is distributed in the hope that it will be #
# useful, but WITHOUT ANY WARRANTY; without even the #
# implied warranty of MERCHANTABILITY or FITNESS FOR A #
# PARTICULAR PURPOSE. See the GNU General Public License #
# for more details. #
# #
# You should have received a copy of the GNU General #
# Public License along with this program; if not, write to #
# the Free Software Foundation, Inc., 59 Temple Place - #
# Suite 330, Boston, MA 02111-1307, USA. #
#############################################################
*/
require_once("config.php");
if (!isset($sid)) {$sid=returnglobal('sid');}
if (!isset($style)) {$style=returnglobal('style');}
if (!isset($answers)) {$answers=returnglobal('answers');}
if (!isset($type)) {$type=returnglobal('type');}
if (!$style)
{
sendcacheheaders();
echo $htmlheader
."<br />\n"
."<table align='center'><tr>"
."<form action='export.php' method='post'>\n"
."<td>\n"
."<table width='350' align='center' style='border: 1px solid #555555' cellpadding='1' cellspacing='0'>\n"
."\t<tr bgcolor='#555555'><td colspan='2' height='4'>"
."<font size='1' face='verdana' color='white'><b>"
._EXPORTRESULTS;
if (isset($_POST['sql'])) {echo " ("._EX_FROMSTATS.")";}
echo "</b></font></td></tr>\n"
."\t<tr><td height='8' bgcolor='silver'>$setfont<font size='1'><b>"
._EX_HEADINGS."</b></font></font></td></tr>\n"
."\t<tr>\n"
."\t\t<td>\n"
."\t\t\t$setfont<input type='radio' name='style' value='abrev' id='headabbrev'>"
."<font size='1'><label for='headabbrev'>"
._EX_HEAD_ABBREV."</label><br />\n"
."\t\t\t<input type='radio' checked name='style' value='full' id='headfull'>"
."<label for='headfull'>"
._EX_HEAD_FULL."</label>\n"
."\t\t</font></font></td>\n"
."\t</tr>\n"
."\t<tr><td height='8' bgcolor='silver'>$setfont<font size='1'><b>"
._EX_ANSWERS."</b></font></font></td></tr>\n"
."\t<tr>\n"
."\t\t<td>\n"
."\t\t\t$setfont<input type='radio' name='answers' value='short' id='ansabbrev'>"
."<font size='1'><label for='ansabbrev'>"
._EX_ANS_ABBREV."</label><br />\n"
."\t\t\t<input type='radio' checked name='answers' value='long' id='ansfull'>"
."<label for='ansfull'>"
._EX_ANS_FULL."</label>\n"
."\t\t</font></font></td>\n"
."\t</tr>\n"
."\t<tr><td height='8' bgcolor='silver'>$setfont<font size='1'><b>"
._EX_FORMAT."</b></font></font></td></tr>\n"
."\t<tr>\n"
."\t\t<td>\n"
."\t\t\t$setfont<input type='radio' name='type' value='doc' id='worddoc'>"
."<font size='1'><label for='worddoc'>"
._EX_FORM_WORD."</label><br />\n"
."\t\t\t<input type='radio' name='type' value='xls' checked id='exceldoc'>"
."<label for='exceldoc'>"
._EX_FORM_EXCEL."</label><br />\n"
."\t\t\t<input type='radio' name='type' value='csv' id='csvdoc'>"
."<label for='csvdoc'>"
._EX_FORM_CSV."</label>\n"
."\t\t</font></font></td>\n"
."\t</tr>\n"
."\t<tr><td height='2' bgcolor='silver'></td></tr>\n"
."\t<tr>\n"
."\t\t<td align='center' bgcolor='silver'>\n"
."\t\t\t$setfont<input $btstyle type='submit' value='"
._EX_EXPORTDATA."'>\n"
."\t\t</font></td>\n"
."\t</tr>\n"
."\t<input type='hidden' name='sid' value='$sid'>\n";
if (isset($_POST['sql']))
{
echo "\t<input type='hidden' name='sql' value=\""
.stripcslashes($_POST['sql'])
."\">\n";
}
echo "\t<tr>\n"
."\t\t<td align=\"center\" bgcolor='silver'>\n"
."\t\t\t<input $btstyle type='submit' value='"
._CLOSEWIN."' onClick=\"window.close()\">\n"
."\t\t</td>\n"
."\t</tr>\n"
."</table>\n"
."</td>";
//FIND OUT HOW MANY FIELDS WILL BE NEEDED - FOR 255 COLUMN LIMIT
$query="SELECT * FROM {$dbprefix}survey_$sid LIMIT 1";
$result=mysql_query($query) or die("Couldn't count fields<br />$query<br />".mysql_error());
$afieldcount=mysql_num_fields($result);
$query="SELECT private FROM {$dbprefix}surveys WHERE sid=$sid"; //Find out if tokens are used
$result=mysql_query($query) or die("Couldn't get privacy data<br />$query<br />".mysql_error());
while ($rows = mysql_fetch_array($result)) {$surveyprivate=$rows['private'];}
if ($surveyprivate == "N")
{
$query="SHOW TABLES LIKE '{$dbprefix}tokens_$sid'"; //SEE IF THERE IS AN ASSOCIATED TOKENS TABLE
$result=mysql_query($query) or die("Couldn't get table list<br />$query<br />".mysql_error());
$tablecount=mysql_num_rows($result);
}
if ($afieldcount > 255 || $tablecount > 0) //Do second column
{
//OPTIONAL EXTRAS (FROM TOKENS TABLE)
if ($tablecount > 0)
{
echo "<td valign='top'>\n"
."<table align='center' style='border: 1px solid #555555' cellpadding='1' cellspacing='0'>"
."\t<tr>\n"
."\t\t<td height='8' bgcolor='#555555'><font face='verdana' color='white' size='1'><b>"
."Token Options</b>\n"
."\t\t</font></td>\n"
."\t</tr>\n"
."\t<tr>\n"
."\t\t<td bgcolor='silver' height='8'><b>$setfont<font size='1'>\n"
."Include Token Fields:"
."\t\t</font></font></b></td>\n"
."\t</tr>\n"
."\t<tr>\n"
."\t\t<td>$setfont<font size='1'>"
."<input type='checkbox' name='first_name' id='first_name'>"
."<label for='first_name'>"._TL_FIRST."</label><br />\n"
."<input type='checkbox' name='last_name' id='last_name'>"
."<label for='last_name'>"._TL_LAST."</label><br />\n"
."<input type='checkbox' name='email_address' id='email_address'>"
."<label for='email_address'>"._TL_EMAIL."</label><br />\n";
$query = "SELECT * FROM {$dbprefix}tokens_$sid LIMIT 1"; //SEE IF TOKENS TABLE HAS ATTRIBUTE FIELDS
$result = mysql_query($query) or die ($query."<br />".mysql_error());
$rowcount = mysql_num_fields($result);
if ($rowcount > 7)
{
echo "<input type='checkbox' name='attribute_1' id='attribute_1'>"
."<label for='attribute_1'>"._TL_ATTR1."</label><br />\n"
."<input type='checkbox' name='attribute_2' id='attribute_2'>"
."<label for='attribute_2'>"._TL_ATTR2."</label><br />\n";
}
echo "\t\t</font></font></td>\n"
."\t</tr>\n"
."</table>"
."</td>";
}
}
echo "</tr>\n"
."\t</form>\n"
."</table><br />\n"
.htmlfooter("instructions.html", "General PHPSurveyor Instructions");
exit;
}
//HERE WE EXPORT THE ACTUAL RESULTS
if ($type == "doc")
{
header("Content-Disposition: attachment; filename=survey.doc");
header("Content-type: application/vnd.ms-word");
$s="\t";
}
elseif ($type == "xls")
{
header("Content-Disposition: attachment; filename=survey.xls");
header("Content-type: application/vnd.ms-excel");
$s="\t";
}
elseif ($type == "csv")
{
header("Content-Disposition: attachment; filename=survey.csv");
header("Content-Type: application/download");
$s=",";
}
else
{
header("Content-Disposition: attachment; filename=survey.doc");
}
sendcacheheaders();
//Select public language file
$query = "SELECT language FROM {$dbprefix}surveys WHERE sid=$sid";
$result = mysql_query($query);
while ($row=mysql_fetch_array($result)) {$surveylanguage = $row['language'];}
$langdir="$publicdir/lang";
$langfilename="$langdir/$surveylanguage.lang.php";
if (!is_file($langfilename)) {$langfilename="$langdir/$defaultlang.lang.php";}
require($langfilename);
//STEP 1: First line is column headings
//$s = "\t";
$lq = "SELECT DISTINCT qid FROM {$dbprefix}questions WHERE sid=$sid"; //GET LIST OF LEGIT QIDs FOR TESTING LATER
$lr = mysql_query($lq);
$legitqs[] = "DUMMY ENTRY";
while ($lw = mysql_fetch_array($lr))
{
$legitqs[] = $lw['qid']; //this creates an array of question id's'
}
//Get the fieldnames from the survey table for column headings
$surveytable = "{$dbprefix}survey_$sid";
$dquery = "SELECT $surveytable.*";
if (isset($_POST['first_name']) && $_POST['first_name']=="on")
{
$dquery .= ", {$dbprefix}tokens_$sid.firstname";
}
if (isset($_POST['last_name']) && $_POST['last_name']=="on")
{
$dquery .= ", {$dbprefix}tokens_$sid.lastname";
}
if (isset($_POST['email_address']) && $_POST['email_address']=="on")
{
$dquery .= ", {$dbprefix}tokens_$sid.email";
}
if (isset($_POST['attribute_1']) && $_POST['attribute_1']=="on")
{
$dquery .= ", {$dbprefix}tokens_$sid.attribute_1";
}
if (isset($_POST['attribute_2']) && $_POST['attribute_2']=="on")
{
$dquery .= ", {$dbprefix}tokens_$sid.attribute_2";
}
$dquery .= " FROM $surveytable";
if ((isset($_POST['attribute_1']) && $_POST['attribute_1']=="on") || (isset($_POST['attribute_2']) && $_POST['attribute_2']=="on") || (isset($_POST['email_address']) && $_POST['email_address']=="on"))
{
$dquery .= ""
. " LEFT OUTER JOIN {$dbprefix}tokens_$sid"
. " ON $surveytable.token = {$dbprefix}tokens_$sid.token";
}
$dquery .=" ORDER BY id LIMIT 1";
$dresult = mysql_query($dquery) or die(_ERROR." getting results<br />$dquery<br />".mysql_error());
$fieldcount = mysql_num_fields($dresult);
$firstline="";
for ($i=0; $i<$fieldcount; $i++)
{
$fieldinfo=mysql_field_name($dresult, $i);
if ($fieldinfo == "token")
{
if ($answers == "short")
{
if ($type == "csv")
{
$firstline.="\"Token\"$s";
}
else
{
$firstline .= "Token$s";
}
}
if ($answers == "long")
{
if ($style == "abrev")
{
if ($type == "csv") {$firstline .= "\"Participant\"$s";}
else {$firstline .= "Participant$s";}
}
else
{
if ($type == "csv") {$firstline .= "\"Participant Name\"$s";}
else {$firstline .= "Participant Name$s";}
}
}
}
elseif ($fieldinfo == "lastname")
{
if ($type == "csv") {$firstline .= "\""._TL_LAST."\"$s";}
else {$firstline .= _TL_LAST."$s";}
}
elseif ($fieldinfo == "firstname")
{
if ($type == "csv") {$firstline .= "\""._TL_FIRST."\"$s";}
else {$firstline .= _TL_FIRST."$s";}
}
elseif ($fieldinfo == "email")
{
if ($type == "csv") {$firstline .= "\""._EMAIL."\"$s";}
else {$firstline .= _EMAIL."$s";}
}
elseif ($fieldinfo == "attribute_1")
{
if ($type == "csv") {$firstline .= "\"attr1\"$s";}
else {$firstline .= _TL_ATTR1."$s";}
}
elseif ($fieldinfo == "attribute_2")
{
if ($type == "csv") {$firstline .= "\"attr2\"$s";}
else {$firstline .= _TL_ATTR2."$s";}
}
elseif ($fieldinfo == "id")
{
if ($type == "csv") {$firstline .= "\"id\"$s";}
else {$firstline .= "id$s";}
}
elseif ($fieldinfo == "datestamp")
{
if ($type == "csv") {$firstline .= "\"Time Submitted\"$s";}
else {$firstline .= "Time Submitted$s";}
}
else //A normal question field. Break the fieldname up into constituent parts to find $sid, $gid, and $qid
{
list($fsid, $fgid, $fqid) = split("X", $fieldinfo);
if ($style == "abrev") //Print out abbreviated question title
{
if (!$fqid) {$fqid = 0;}
$oldfqid=$fqid;
while (!in_array($fqid, $legitqs)) //checks that the qid exists in our list. If not, have to do some tricky stuff to find where qid ends and answer code begins:
{
$fqid = substr($fqid, 0, strlen($fqid)-1); //keeps cutting off the end until it finds the real qid
}
if (strlen($fqid) < strlen($oldfqid))
{
$faid = substr($oldfqid, strlen($fqid), strlen($oldfqid)-strlen($fqid));
$oldfqid="";
}
//Now we know what the qid is, we'll get the question text and then print it out (in abbreviate format of course)
$qq = "SELECT question FROM {$dbprefix}questions WHERE qid=$fqid";
$qr = mysql_query($qq);
while ($qrow=mysql_fetch_array($qr, MYSQL_ASSOC))
{$qname=$qrow['question'];}
$qname=substr($qname, 0, 15)."..";
$qname=strip_tags($qname);
$firstline = str_replace("\n", "", $firstline);
$firstline = str_replace("\r", "", $firstline);
if ($type == "csv") {$firstline .= "\"$qname";}
else {$firstline .= "$qname";}
if (isset($faid)) {$firstline .= " [{$faid}]"; $faid="";}
if ($type == "csv") {$firstline .= "\"";}
$firstline .= "$s";
}
else //Print out extended question title
{
if (!$fqid) {$fqid = 0;}
$oldfqid=$fqid;
while (!in_array($fqid, $legitqs)) //checks that the qid exists in our list
{
$fqid = substr($fqid, 0, strlen($fqid)-1); //keeps cutting off the end until it finds the real qid
}
if (strlen($fqid) < strlen($oldfqid))
{
$faid = substr($oldfqid, strlen($fqid), strlen($oldfqid)-strlen($fqid));
$oldfqid="";
}
$qq = "SELECT question, type FROM {$dbprefix}questions WHERE qid=$fqid"; //get the question
$qr = mysql_query($qq);
while ($qrow = mysql_fetch_array($qr, MYSQL_ASSOC))
{
$ftype = $qrow['type']; //get the question type
$fquest = $qrow['question'];
}
switch ($ftype)
{
case "R": //RANKING TYPE
$lq = "SELECT * FROM {$dbprefix}answers WHERE qid=$fqid AND code = '$faid'";
$lr = mysql_query($lq);
while ($lrow = mysql_fetch_array($lr, MYSQL_ASSOC))
{
$fquest .= " [".$lrow['answer']."]";
}
break;
case "O": //DROPDOWN LIST WITH COMMENT
if ($faid == "comment")
{
$fquest .= " - Comment";
}
break;
case "M": //multioption
if ($faid == "other")
{
$fquest .= " ["._OTHER."]";
}
else
{
$lq = "SELECT * FROM {$dbprefix}answers WHERE qid=$fqid AND code = '$faid'";
$lr = mysql_query($lq);
while ($lrow = mysql_fetch_array($lr, MYSQL_ASSOC))
{
$fquest .= " [".$lrow['answer']."]";
}
}
break;
case "P": //multioption with comment
if (substr($faid, -7, 7) == "comment")
{
$faid=substr($faid, 0, -7);
$comment=true;
}
if ($faid == "other")
{
$fquest .= " ["._OTHER."]";
}
else
{
$lq = "SELECT * FROM {$dbprefix}answers WHERE qid=$fqid AND code = '$faid'";
$lr = mysql_query($lq);
while ($lrow = mysql_fetch_array($lr, MYSQL_ASSOC))
{
$fquest .= " [".$lrow['answer']."]";
}
}
if (isset($comment) && $comment == true) {$fquest .= " - comment"; $comment=false;}
break;
case "A":
case "B":
case "C":
case "E":
case "F":
case "Q":
$lq = "SELECT * FROM {$dbprefix}answers WHERE qid=$fqid AND code= '$faid'";
$lr = mysql_query($lq);
while ($lrow=mysql_fetch_array($lr, MYSQL_ASSOC))
{
$fquest .= " [".$lrow['answer']."]";
}
break;
default:
//echo "---------- $ftype ----------";
//if (mysql_field_name($dresult, $i) == "token")
// {
// $tokenquery = "SELECT firstname, lastname FROM {$dbprefix}tokens_$sid WHERE token='$drow[$i]'";
// if ($tokenresult = mysql_query($tokenquery)) //or die ("Couldn't get token info<br />$tokenquery<br />".mysql_error());
// while ($tokenrow=mysql_fetch_array($tokenresult))
// {
// echo "{$tokenrow['lastname']}, {$tokenrow['firstname']}";
// }
// else
// {
// echo "Not found";
// }
// }
//else
// {
// echo str_replace("\r\n", " ", $drow[$i]);
// }
break;
}
$fquest = strip_tags($fquest);
$fquest = str_replace("\n", " ", $fquest);
$fquest = str_replace("\r", "", $fquest);
if ($type == "csv")
{
$firstline .="\"$fquest\"$s";
}
else
{
$firstline .= "$fquest $s";
}
}
}
}
$firstline = trim($firstline);
$firstline .= "\n";
echo $firstline; //Sending the header row
//Now dump the data
if (isset($_POST['sql'])) //this applies if export has been called from the statistics package
{
if ($_POST['sql'] == "NULL") {$dquery = "SELECT * FROM $surveytable ORDER BY id";}
else {$dquery = "SELECT * FROM $surveytable WHERE ".stripcslashes($_POST['sql'])." ORDER BY id";}
}
elseif ((isset($_POST['attribute_1']) && $_POST['attribute_1']=="on") || (isset($_POST['attribute_2']) && $_POST['attribute_2'] == "on") || (isset($_POST['email_address']) && $_POST['email_address'] == "on"))
{
$dquery = "SELECT $surveytable.*";
if (isset($_POST['first_name']) && $_POST['first_name']=="on")
{
$dquery .= ", {$dbprefix}tokens_$sid.firstname";
}
if (isset($_POST['last_name']) && $_POST['last_name']=="on")
{
$dquery .= ", {$dbprefix}tokens_$sid.lastname";
}
if (isset($_POST['email_address']) && $_POST['email_address']=="on")
{
$dquery .= ", {$dbprefix}tokens_$sid.email";
}
if (isset($_POST['attribute_1']) && $_POST['attribute_1']=="on")
{
$dquery .= ", {$dbprefix}tokens_$sid.attribute_1";
}
if (isset($_POST['attribute_2']) && $_POST['attribute_2']=="on")
{
$dquery .= ", {$dbprefix}tokens_$sid.attribute_2";
}
$dquery .= " FROM $surveytable "
. "LEFT OUTER JOIN {$dbprefix}tokens_$sid "
. "ON $surveytable.token={$dbprefix}tokens_$sid.token "
. "ORDER BY $surveytable.id";
}
else // this applies for exporting everything
{
$dquery = "SELECT * FROM $surveytable ORDER BY id";
}
if ($answers == "short") //Nice and easy. Just dump the data straight
{
$dresult = mysql_query($dquery);
while ($drow = mysql_fetch_array($dresult, MYSQL_ASSOC))
{
if ($type == "csv")
{
echo "\"".implode("\"$s\"", str_replace("\"", "\"\"", str_replace("\r\n", " ", $drow))) . "\"\n"; //create dump from each row
}
else
{
echo implode($s, str_replace("\r\n", " ", $drow)) . "\n"; //create dump from each row
}
}
}
elseif ($answers == "long")
{
$dresult = mysql_query($dquery);
$fieldcount = mysql_num_fields($dresult);
while ($drow = mysql_fetch_array($dresult))
{
if (!ini_get('safe_mode'))
{
set_time_limit(3); //Give each record 3 seconds
}
for ($i=0; $i<$fieldcount; $i++)
{
if (mysql_field_name($dresult, $i) != "id" && mysql_field_name($dresult, $i) != "datestamp" && mysql_field_name($dresult, $i) != "token")
{
list($fsid, $fgid, $fqid) = split("X", mysql_field_name($dresult, $i));
}
else
{
$fsid=""; $fgid=""; $fqid="";
}
if (!$fqid) {$fqid = 0;}
if ($fqid == 0)
{
$ftype = "-";
}
else
{
$oldfqid=$fqid;
while (!in_array($fqid, $legitqs)) //checks that the qid exists in our list
{
$fqid = substr($fqid, 0, strlen($fqid)-1);
}
$qq = "SELECT type, lid FROM {$dbprefix}questions WHERE qid=$fqid";
$qr = mysql_query($qq) or die("Error selecting type and lid from questions table.<br />".$qq."<br />".mysql_error());
while ($qrow = mysql_fetch_array($qr, MYSQL_ASSOC))
{$ftype = $qrow['type']; $lid=$qrow['lid'];}
}
if ($type == "csv") {echo "\"";}
switch ($ftype)
{
case "-": //JASONS SPECIAL TYPE
echo $drow[$i];
break;
case "R": //RANKING TYPE
$lq = "SELECT * FROM {$dbprefix}answers WHERE qid=$fqid AND code = '$drow[$i]'";
$lr = mysql_query($lq);
while ($lrow = mysql_fetch_array($lr, MYSQL_ASSOC))
{
echo $lrow['answer'];
}
break;
case "L": //DROPDOWN LIST
$lq = "SELECT * FROM {$dbprefix}answers WHERE qid=$fqid AND code ='$drow[$i]'";
$lr = mysql_query($lq);
while ($lrow = mysql_fetch_array($lr, MYSQL_ASSOC))
{
//if ($lrow['code'] == $drow[$i]) {echo $lrow['answer'];}
echo $lrow['answer'];
}
break;
case "O": //DROPDOWN LIST WITH COMMENT
$lq = "SELECT * FROM {$dbprefix}answers WHERE qid=$fqid ORDER BY answer";
$lr = mysql_query($lq) or die ("Could do it<br />$lq<br />".mysql_error());
while ($lrow = mysql_fetch_array($lr, MYSQL_ASSOC))
{
if ($lrow['code'] == $drow[$i]) {echo $lrow['answer']; $found = "Y";}
}
if ($found != "Y") {if ($type == "csv") {echo str_replace("\"", "\"\"", $drow[$i]);} else {echo str_replace("\r\n", " ", $drow[$i]);}}
$found = "";
break;
case "Y": //YES\NO
switch($drow[$i])
{
case "Y": echo _YES; break;
case "N": echo _NO; break;
default: echo _NOTAPPLICABLE; break;
}
break;
case "G": //GENDER
switch($drow[$i])
{
case "M": echo _MALE; break;
case "F": echo _FEMALE; break;
default: echo _NOTAPPLICABLE; break;
}
break;
case "M": //multioption
case "P":
if (substr($oldfqid, -5, 5) == "other")
{
echo "$drow[$i]";
}
elseif (substr($oldfqid, -7, 7) == "comment")
{
echo "$drow[$i]";
}
else
{
switch($drow[$i])
{
case "Y": echo _YES; break;
case "N": echo _NO; break;
case "": echo _NO; break;
default: echo $drow[$i]; break;
}
}
break;
case "C":
switch($drow[$i])
{
case "Y":
echo _YES;
break;
case "N":
echo _NO;
break;
case "U":
echo _UNCERTAIN;
break;
}
case "E":
switch($drow[$i])
{
case "I":
echo _INCREASE;
break;
case "S":
echo _SAME;
break;
case "D":
echo _DECREASE;
break;
}
break;
case "F":
$fquery = "SELECT * FROM {$dbprefix}labels WHERE lid=$lid AND code='$drow[$i]'";
$fresult = mysql_query($fquery) or die("ERROR:".$fquery."\n".$qq."\n".mysql_error());
while ($frow = mysql_fetch_array($fresult))
{
echo $frow['title'];
}
break;
default:
if (mysql_field_name($dresult, $i) == "token")
{
$tokenquery = "SELECT firstname, lastname FROM {$dbprefix}tokens_$sid WHERE token='$drow[$i]'";
if ($tokenresult = mysql_query($tokenquery)) //or die ("Couldn't get token info<br />$tokenquery<br />".mysql_error());
while ($tokenrow=mysql_fetch_array($tokenresult))
{
echo "{$tokenrow['lastname']}, {$tokenrow['firstname']}";
}
else
{
echo "Tokens problem - token table missing";
}
}
else
{
if ($type == "csv")
{echo str_replace("\r\n", "\n", str_replace("\"", "\"\"", $drow[$i]));}
else
{echo str_replace("\r\n", " ", $drow[$i]);}
}
}
if ($type == "csv") {echo "\"";}
echo "$s";
$ftype = "";
}
echo "\n";
}
}
?>