...

View Full Version : Ned Help Please with code



bnorris
01-31-2012, 03:40 PM
Hi,

New Php programmer here and I have a problem. Somehow I need to get this form to sort alphabetically might someone please help me? I can paste the whole code but I think what we need is in the snippet:


$newdata[3] = ($newdata[3] == "Order") ? $newdata[3] . "_" . $r : $newdata[3]; // need to give it an extra 'tag' to help differentiate

if ($newdata[3] == $_REQUEST['respondentid']) {
$respondentoptions .= "<option value=\"$newdata[3]\" selected=\"selected\">$newdata[3]</option>\n";
} else {
$respondentoptions .= "<option value=\"$newdata[3]\">$newdata[3]</option>\n";
}

if ($newdata[3] == $rid) { // matched username
$data_array = $newdata; // so $data_array now contains array of responses for the selected respondent
$respondenttable = makeRespondentResultsTable($data_array,$_SESSION['admin_sid'],"TRUE","View results");
$respflag = "TRUE";

mlseim
01-31-2012, 05:07 PM
Where do the options come from? A database of some kind?
I don't see a loop there, so it's only going to display one option?

bnorris
01-31-2012, 05:11 PM
Where do the options come from? A database of some kind?
I don't see a loop there, so it's only going to display one option?

Im not good with code, yet but Im trying. Its flat files Here is the whole page:


<?php

session_start();

require_once('includes/config.inc.php');
require_once('includes/functions.inc.php');

//require_once('login.inc.php');

switch ($_REQUEST['action']) {

case "EditData" :

include('includes/header.php');

$qid = "surveys/data_" . $_SESSION['admin_sid'] . ".dat";
$loaded = loadFile($qid);
if ($loaded == "Array") { $loaded = ""; }

$_SESSION['statusmsg'] = null;

$outputform = "
<form action=\"respondentreport.php\" method=\"post\" class=\"clearbox\">
<div id=\"dataeditform\" width=\"99%;\">
<textarea cols=\"80\" rows=\"25\" wrap=\"off\" name=\"rawdata\">$loaded</textarea>
<input type=\"hidden\" name=\"action\" value=\"SaveData\" />
<p style=\"text-align:center;\"><input type=\"submit\" value=\"Save changes to data\" /></p>
</div>
</form>
";

break;

case "SaveData" :

$qid = "surveys/data_" . $_SESSION['admin_sid'] . ".dat";

$saved = saveFile($qid,$_POST['rawdata']);

if ($saved) {
$_SESSION['statusmsg'] = "The data file was saved.";
} else {
$_SESSION['statusmsg'] = "Sorry, the data file could not be saved.";
}
header("Location: respondentreport.php?action=EditData");

break;

case "DownloadData" :

$rid = "surveys/data_" . $_SESSION['admin_sid'] . ".dat";
$loaded = loadFile($rid);

$qid = "surveys/quest_" . $_SESSION['admin_sid'] . ".xml";
$loadedq = loadQuest2($qid);

$qtitle = $loadedq['meta']['title'];

$fields = makeFieldRow($_SESSION['admin_sid']);
$questions = makeQuestionRow($_SESSION['admin_sid']);

$dodgy = array(" ","'",",",".");

$now = date("d-M");
$nicefilename = substr(strtolower(str_replace($dodgy,"_",$qtitle)),0,10) . "_" . $now . "_data.xls";

header("Content-Type: text/plain");
// a load of extra headers here so IE doesn't choke on the file...
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
// define filename for download as export.txt
header("Content-Disposition: attachment; filename=\"".$nicefilename."\"");

echo ($fields) . ($questions). ($loaded);
exit();

break;

default : // show per-respondent report

include('includes/header.php');

if ($_REQUEST['sid']) {
$_SESSION['admin_sid'] = $_REQUEST['sid'];
}

$directory = (substr(dirname($_SERVER['PHP_SELF']),-1) == "/") ? dirname($_SERVER['PHP_SELF']) : dirname($_SERVER['PHP_SELF']) . "/";
$reportlink = $protocol . $_SERVER['HTTP_HOST'] . $directory . "respondentreport.php?sid=".$_SESSION['admin_sid'];

if ($_SESSION['admin_sid']) { // show form

$rid = $_GET['respondentid'];

$datafile = "surveys/data_" . $_SESSION['admin_sid'] . ".dat";
$rawdata = loadFile($datafile);
$rawdata_array = explode("\n",$rawdata);
for ($r=0;$r<count($rawdata_array);$r++) {
if (strlen($rawdata_array[$r])>0) {
$newdata = explode("\t",$rawdata_array[$r]);

$newdata[3] = ($newdata[3] == "Order") ? $newdata[3] . "_" . $r : $newdata[3]; // need to give it an extra 'tag' to help differentiate

if ($newdata[3] == $_REQUEST['respondentid']) {
$respondentoptions .= "<option value=\"$newdata[3]\" selected=\"selected\">$newdata[3]</option>\n";
} else {
$respondentoptions .= "<option value=\"$newdata[3]\">$newdata[3]</option>\n";
}

if ($newdata[3] == $rid) { // matched username
$data_array = $newdata; // so $data_array now contains array of responses for the selected respondent
$respondenttable = makeRespondentResultsTable($data_array,$_SESSION['admin_sid'],"TRUE","View results");
$respflag = "TRUE";
}
}
}

$replink = "<p id='surveylink'><img src=\"images/icons/web_16x16.gif\" width=\"16\" height=\"16\" class='icon' alt=\"Link to report\" border=\"0\" />The link to this report is: <a href='{$reportlink}'>{$reportlink}</a></p>";
$respondentmenu = "
<p><form action=\"respondentreport.php\" method=\"get\" id='results' style='text-align:center;'><span style='font-size: 1.3em;'>View responses for user ID: </span> <select name=\"respondentid\" onchange=\"this.form.submit()\"><option value=\"\">-- select a respondent --</option>\n{$respondentoptions}</select>
<input type=\"submit\" value=\"Go\" /></form><br /><br /></p>\n\n";

$outputform .= "
<style type='text/css'>
span.questionnumber {
display: none;
}
</style>
";

$outputform .= $replink . $respondentmenu . $respondenttable;
if ($respflag == "TRUE") { // repeat the menu at the foot of the page for easy switching
$outputform .= $respondentmenu;
}
} else {
echo ("<h1 class='clearbox'>Sorry</h1><p class='clearbox'>No questionnaire data is available to display.</p>");
}

break;
}

echo ($outputform);

//include ('includes/footer.php');
?>
<center><p><font size="5">Certificate Number</font></p>
***NOTE USE CERTIFICATE NUMBER ON CUSTOMER PRINTED FORM AT END**
<br>
<?php
echo rand() . "\n";

echo rand(5);
?>
<center><a href="javascript:window.print()">Print Report and File</a>

mlseim
01-31-2012, 06:02 PM
Post a portion of your flat-file database so I can see how it's formatted and delimited.

bnorris
01-31-2012, 06:22 PM
Post a portion of your flat-file database so I can see how it's formatted and delimited.

Hope this helps :) Field 3 is the $newdata[3] in the code, the customers name


31-Jan-2012 06:19 Anonymous 1/30/2012 Mike Johnson mike@mile 3e2er qw TRUE TRUE TRUE TRUE
31-Jan-2012 06:25 Anonymous 1/30/2012 Jack Wheeler jack@ Ted F11 TRUE TRUE TRUE TRUE
31-Jan-2012 06:27 Anonymous 1/30/2012 Al Greene Al Bill H1 TRUE TRUE TRUE TRUE

bnorris
01-31-2012, 09:25 PM
Im assuming I need an array or something

mlseim
01-31-2012, 11:03 PM
So, to summarize then ...
You want to alphabetize (sort) the names from "A" to "Z"?
I see first name / last name ...
Are you sorting by first name or last name?

What will happen is this ...
You will read the file into an array, and create a new
array with field 3 in the front, then sort that array.

What separates the fields in your data file (what is the delimiter)?
And also ... sort by first name or last name? You could make it
easier if you save the names like this: Johnson, Mike ...
but don't use a comma if that is your field delimiter.


Note:
This is why MySQL is so much easier.
You can search, sort, insert, update with simple query strings.

Also Note:
I see you're saving dates as well. I recommend you always
save your dates like this: 2012-01-31 instead of 31-Jan-2012

The format YYYY-MM-DD is easiest to work with for date comparisons,
and it also sorts very nicely ... for flat-file databases. That may be
important if you wish to sort by date instead of by name.



.

bnorris
01-31-2012, 11:05 PM
So, to summarize then ...
You want to alphabetize (sort) the names from "A" to "Z"?
I see first name / last name ...
Are you sorting by first name or last name?

What will happen is this ...
You will read the file into an array, and create a new
array with field 3 in the front, then sort that array.

What separates the fields in your data file?

Note:
This is why MySQL is so much easier.
You can search, sort, insert, update with simple query strings.

I agree about a data base. Last name I think would be best. Thank you so much for your help

mlseim
01-31-2012, 11:11 PM
What separates the fields in your lines? A comma? A pipe | ?


I just noticed there's a function called: loadFile
$loaded = loadFile($qid);

That function must be in here: includes/functions.inc.php

I think we'll need to see that function also.

.

bnorris
01-31-2012, 11:22 PM
What separates the fields in your lines? A comma? A pipe | ?


I just noticed there's a function called: loadFile
$loaded = loadFile($qid);

That function must be in here: includes/functions.inc.php

I think we'll need to see that function also.

.
Its huge so I had to paste it, pastbin is down but here it is: http://adf.ly/1359932/banner/www.heypasteit.com/clip/07OP

mlseim
01-31-2012, 11:51 PM
Let's try this first ...
We'll try to sort by name as it appears ... first name/last name.
If that works, we can break it down to sort by last name.

The part between the ============= are the lines I've added or changed.
Make a safe copy of your original, because I can't test what I've done.



if ($_SESSION['admin_sid']) { // show form

$rid = $_GET['respondentid'];

$datafile = "surveys/data_" . $_SESSION['admin_sid'] . ".dat";
$rawdata = loadFile($datafile);
$rawdata_array = explode("\n",$rawdata);

// ====================================================================
// sort by name
$sorted_array=array();
$i=0;
foreach($line as $rawdata_array){
$temp_data = explode("\t",$line);
$sorted_array[$i] = $temp_data[3]."\t".$line;
$i++;
}
sort($sorted_array)
array_shift($sorted_array);
for ($r=0;$r<count($sorted_array);$r++) {
if (strlen($sorted_array[$r])>0) {
$newdata = explode("\t",$sorted_array[$r]);
// ========================================================================

$newdata[3] = ($newdata[3] == "Order") ? $newdata[3] . "_" . $r : $newdata[3]; // need to give it an extra 'tag' to help differentiate

if ($newdata[3] == $_REQUEST['respondentid']) {
$respondentoptions .= "<option value=\"$newdata[3]\" selected=\"selected\">$newdata[3]</option>\n";
} else {
$respondentoptions .= "<option value=\"$newdata[3]\">$newdata[3]</option>\n";
}

if ($newdata[3] == $rid) { // matched username
$data_array = $newdata; // so $data_array now contains array of responses for the selected respondent
$respondenttable = makeRespondentResultsTable($data_array,$_SESSION['admin_sid'],"TRUE","View results");
$respflag = "TRUE";
}
}
}





.

bnorris
02-01-2012, 12:48 AM
Let's try this first ...
We'll try to sort by name as it appears ... first name/last name.
If that works, we can break it down to sort by last name.

The part between the ============= are the lines I've added or changed.
Make a safe copy of your original, because I can't test what I've done.



if ($_SESSION['admin_sid']) { // show form

$rid = $_GET['respondentid'];

$datafile = "surveys/data_" . $_SESSION['admin_sid'] . ".dat";
$rawdata = loadFile($datafile);
$rawdata_array = explode("\n",$rawdata);

// ====================================================================
// sort by name
$sorted_array=array();
$i=0;
foreach($line as $rawdata_array){
$temp_data = explode("\t",$line);
$sorted_array[$i] = $temp_data[3]."\t".$line;
$i++;
}
sort($sorted_array)
array_shift($sorted_array);
for ($r=0;$r<count($sorted_array);$r++) {
if (strlen($sorted_array[$r])>0) {
$newdata = explode("\t",$sorted_array[$r]);
// ========================================================================

$newdata[3] = ($newdata[3] == "Order") ? $newdata[3] . "_" . $r : $newdata[3]; // need to give it an extra 'tag' to help differentiate

if ($newdata[3] == $_REQUEST['respondentid']) {
$respondentoptions .= "<option value=\"$newdata[3]\" selected=\"selected\">$newdata[3]</option>\n";
} else {
$respondentoptions .= "<option value=\"$newdata[3]\">$newdata[3]</option>\n";
}

if ($newdata[3] == $rid) { // matched username
$data_array = $newdata; // so $data_array now contains array of responses for the selected respondent
$respondenttable = makeRespondentResultsTable($data_array,$_SESSION['admin_sid'],"TRUE","View results");
$respflag = "TRUE";
}
}
}





.

I changed that section Didnt work

mlseim
02-01-2012, 05:54 AM
Did it just not sort ... or was there an actual fatal error?
What didn't work.

It's hard for me because I can't test anything I do.

bnorris
02-01-2012, 01:03 PM
Did it just not sort ... or was there an actual fatal error?
What didn't work.

It's hard for me because I can't test anything I do.

Blank page sir, can I give you FTP access to the script? I really appreciate your helping me like this.

mlseim
02-01-2012, 02:51 PM
PM me with the FTP (don't post in this public thread).
I'll take a look.

bnorris
02-01-2012, 03:33 PM
PM me with the FTP (don't post in this public thread).
I'll take a look.

Thanks! Sent!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum