Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    21
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Ned Help Please with code

    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:

    PHP Code:
    $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"

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,456
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    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?

  • Users who have thanked mlseim for this post:

    bnorris (02-01-2012)

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    21
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    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 Code:
    <?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>
    Last edited by bnorris; 01-31-2012 at 04:14 PM.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,456
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    Post a portion of your flat-file database so I can see how it's formatted and delimited.

  • Users who have thanked mlseim for this post:

    bnorris (02-01-2012)

  • #5
    New Coder
    Join Date
    Jan 2012
    Posts
    21
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    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

    Code:
    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
    Last edited by bnorris; 01-31-2012 at 05:41 PM.

  • #6
    New Coder
    Join Date
    Jan 2012
    Posts
    21
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Im assuming I need an array or something

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,456
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    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.



    .
    Last edited by mlseim; 01-31-2012 at 10:09 PM.

  • Users who have thanked mlseim for this post:

    bnorris (02-01-2012)

  • #8
    New Coder
    Join Date
    Jan 2012
    Posts
    21
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    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

  • #9
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,456
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    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.

    .
    Last edited by mlseim; 01-31-2012 at 10:13 PM.

  • Users who have thanked mlseim for this post:

    bnorris (02-01-2012)

  • #10
    New Coder
    Join Date
    Jan 2012
    Posts
    21
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    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.hey....com/clip/07OP

  • #11
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,456
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    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.

    PHP Code:
    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";
                        }
                    }
                } 


    .

  • Users who have thanked mlseim for this post:

    bnorris (02-01-2012)

  • #12
    New Coder
    Join Date
    Jan 2012
    Posts
    21
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    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.

    PHP Code:
    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
    Last edited by bnorris; 02-01-2012 at 12:03 AM.

  • #13
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,456
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    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.

  • Users who have thanked mlseim for this post:

    bnorris (02-01-2012)

  • #14
    New Coder
    Join Date
    Jan 2012
    Posts
    21
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    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.

  • #15
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,456
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    PM me with the FTP (don't post in this public thread).
    I'll take a look.

  • Users who have thanked mlseim for this post:

    bnorris (02-01-2012)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •