Jump to content

SaranacLake

Members
  • Posts

    648
  • Joined

  • Last visited

Posts posted by SaranacLake

  1. @Barand

    Sorry for the late response, but I have been tied up with another part of my website for the last week or so....

    Thanks for the sample array above!

    I am trying to get my head back into things, and am sure I will have follow-up questions.

    Q1.) For starters, in your multi-dimensional array above, 'title', 'intro', and 'sections' are [keys[/b] for the outermost array, right?

     

    Q2.) I forgot, what do you call arrays where the keys are text values like your example?

    Associative arrays?

     

    Q2b.) And what do you call arrays where the keys are numeric?

     

    Q3.) I assume that text keys cannot/should not have spaces in them like 'first name', right?

     

     

  2. I still hate arrays! 🙁

    Could use some help expanding an array by adding another dimension.  (I'm sure this will be super simple for the gurus here!)

    Currently I have an array that holds my questionnaire questions (HMTL-formt) like this...

    questionnaireArray
       array
          1 =>    <li>Question-1 here</li>
          2 =>    <li>Question-2 here</li>
          3 =>    <li>Question-3 here</li>
          4 =>    <li>Question-4 here</li>
    	

     

    I want to expand this PHP page so that a series of questions are part of a "Section" that may (or may not) contain some lead-in text.  (The idea being that sometimes it is necessary to have a short intro to a series of questions, and that text is better placed next to the questions, as opposed to at the top if the page.)

     

    The end result I want would look like this...

    	QUESTIONNARIE
        
    	<Intro Synopsis>
        
    	<section-1 intro is blank>
    	Q1.)
    	Q2.)
    	 
    	<section-2 intro = "Some text here...">
    	Q3.)
    	Q4.)
    	 
    	<section-3 intro = "Some more text here...">
    	Q5.)
    	Q6.)
    	

     

    So I could use help taking my current array (above), and nesting it within a parent array that will contain each Section's intro text - which could be blank.

    And to be clear, what I need help with now is *visualizing* what the new array will look like - versus how to create it.  

    (Once I understand what it should look like and what the keys and values are, then i can probably figure out the PHP coding part!)

     

    For instance, what is the $key for my parent array? 

    What are the $values for the parent array?

    And how does my current array (above) fit into this?

     

    Thanks!

     

     

  3. On 2/19/2021 at 8:11 PM, kicken said:

    Only the basics if you stick to what the library can natively do given it's options.   The more JS you know the more fancy/custom stuff you can do with it, but for basic chart rendering you only really need to include the library and write:

    Good to know.

    I suppose that I will open many new doors once I learn Javascript?!

    Hopefully I can get to that later this year or next...

  4. 14 hours ago, kicken said:

    If you do decide to step things up a level I highly suggest just finding a good library to do your chart rendering.  Chart.js is my tool of choice currently.  It offloads the chart drawing to the client so all you have to do on the server side is generate a json structure.

     

     

    VERY impressive, I must say!

     

    1.) Is that open-source?  (I might not feel so bad "cheating" and using someone else's library as long as it is NOT from Google or some other evil corporation!)

     

    2.) So I guess I'd need to know how to do Javascript, right?

     

    3.) And I'd need to know how to use JSON?  (Have heard of that but have no clue what it is?!)

     

    Thanks for the tip!  👍

  5. 10 hours ago, Barand said:

    I wouldn't bother. It's not as though your users will be creating these "answer" records. It's completely under your control and testing will show any "accidents".

    True, but even I make mistakes, and guard-rails can be nice.

     

    10 hours ago, Barand said:

    And what if it really is a 5-answer group but you accidentally enter "4" or  "6" in the group table?

    It's equivalent to storing a derived total instead of getting the total from the data. Let the number of answers dictate the group size.

    Good point, so let me re-state.

    I have a "question_type" table that would define a question as say "rating-5pt".  To your point, I could store a "5" in the "question_type" lookup so that as I add in hundreds of questions, I am always pulling that "5" from the lookup table thus eliminating the issue I believe you are pointing out.

    To your earlier question...  What could happen?

    Well, I could end up with an UN-balanced set of choices (e.g. 1-Strongly Disagree, 2-Disagree, 3-Neither, 4-Agree) with "5-Strongly Agree" missing and thus skewing the question and responses.

     

    I assume what i want to do could be fairly easily done coding a trigger, but not having any hnds-on experience with triggers or stored procedures I'm not certain.

    Am hoping someone can at least explain if what i want to do is doable...

  6. Hello.

    This problem deals with survey questions and their corresponding answer-choices.

    Let's say I have a satisfaction scale like this...

    	1 - Strongly Disagree
    	2 - Disagree
    	3 - neither
    	4 - Agree
    	5 - Strongly Agree
    	

     

    I created a new table called "answer_group" to bind together a bunch of related choices...

    	answer_group -||-----|<- answer
    	


    Here is what I want to happen...

    In the "answer_group" table, for a given "answer group" (record) there would be a column called: "no-of-choices" and it would have a value like "5".

    I want MySQL to look at that value of "5" in the parent table/record, and as I add child records in the "answer" table - linked by a foreign key, I want MySQL to prevent the database user from adding/linking more than "5" child records.

    Why?

    Because if a "Question" has a 5-Choice satisfaction question, then I don't want to accidentally display 7 Choices?!  (Especially since my PHP code paints however many Choices you tell it in the database.)

    This is actually a pretty easy data-entry / data-linking mistake to make, so I want MySQL to keep me honest!

     

    It seems like I would need either a "trigger" or a "stored procedure" (or maybe both) to do this? 

    And I'm not sure how involved something like this is?

    But it never hurts to ask, and to start investigating things. 

    (For v2.0, another thing I want to do is become proficient with triggers and stored procedures to build a more sophisticated database.)

     

    My best guess at how to do this would be to add a "Before Insert" trigger to the (child) "answer" table - on the FK column - and have it count how many new sub-records are created, and once counter >= "answer_group"."no_of_choices" for a FK, then prevent MySQL from adding any more sub-records in the "answer" table.

    Just my guess...

     

     

  7. 16 hours ago, Barand said:

    TIP: If you are creating home-grown charts, plotting the values is the easy bit. 95% of the coding effort will be in the drawing of chart area, plot area, axes, axis labels, scaling, titles etc.

    Yes, I agree that is the harder part.

     

    @Barand

    WOW!  I am blown away by your example below!

    That is a super good-looking horizontal bar chart!

    As you mentioned, it takes more work to get adjustable scales, titles, etc, but I think what you have below is more than sufficient for my needs.  And I think most users would appreciate a "visual", even if it is a simple one, because that is all I think most people want.

     

    16 hours ago, Barand said:

    You can sidestep this with a simple table with horizontal bars. EG

    image.png.d874ad1d5aa7ae244a0c55ce7a3d197d.png

    CODE EXAMPLE...

    
    <?php
    
    $values = [
                 'Strongly Disagree' => 7,
                 'Disagree'          => 10,
                 'Neither'           => 12,
                 'Agree'             => 25,
                 'Strongly Agree'    => 41 
              ];
              
    function valueChart(&$values)
    {
        $out = "<table class='chartTable'>
                  <tr><th>Response</th>
                      <th>Total</th>
                      <th>Percent</th>
                  </tr>
               ";
        $totalVal = array_sum($values);
        foreach ($values as $resp => $n) {
            $out .= "<tr><td>$resp</td>
                         <td class='ra'>$n</td>
                         <td>" . bar($n / $totalVal * 100) . "</td></tr>\n";
        }
        $out .= "</table\n";
        return $out;
    }
    
    function bar($val=0)
    {
        $a = '#3399ff';
        $b = '#e6f2ff';
        $c = '#0066cc';
        $bg = '#eee';
        $width = 300;
        $height = 25;
        $svg = <<<SVG
            <svg width='$width' height='$height' viewBox='0 0 $width $height'>";
            <defs>
            <linearGradient id="bargrad"  x1="0" y1="0" x2="0" y2="1">
                <stop offset="0%" style="stop-color:$a"/>
                <stop offset="25%" style="stop-color:$b"/>
                <stop offset="100%" style="stop-color:$c"/>
            </linearGradient>
            </defs>
            <rect x='0' y='0' width='$width' height='$height' style='fill:$bg' stroke='#999'/>
    SVG;
        $w = $val/100 * $width;
        $svg .= "<rect x='0' y='0' width='$w' height='$height' style='fill:url(#bargrad)' />";
        $svg .= "</svg>\n";
        return $svg;
    }
    
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Chart Example</title>
    <head>
    <style type='text/css'>
        .chartTable {
            font-family: arial, sans-serif;
            font-size: 11pt;
        }
        th {
            padding: 4px 16px ;
        }
        td {
            padding: 0 16px;
        }
        .ra {
            text-align: right;
        }
    </style>
    </head>
    <body>
        <?=valueChart($values)?>
    </body>
    </html>

    Hope this helps.

     

    I will put your code in my hat to use after I go live with my website.

    Thanks for the headstart!  👍

     

     

  8. 1 minute ago, requinix said:

    It's a perfectly valid approach, sure.

    Good to hear!

     

    1 minute ago, requinix said:

    As long as you have the data in some form at all then you have what you need. Beyond that the only thing to gain is convenience.

    Well, not entirely true.  It is possible to collect data, but not have it normalized properly, and end up with a real PITA.

    But data modeling is my strength, and I think I have a good data model. 

    (My only issue is that I am the king of "scope creep", and every time I build something or fix something - like my survey data validation thanks to @maxxd suggestion - I get the urge to add on more functionality!  I can see a few things in my current data model that could be a pain down the road, so i am thinking a few steps ahead and making sure my database is scalable.)

     

     

    1 minute ago, requinix said:

    The chart stuff is non-trivial. Are you sure you want to make it yourself? Whatever you come up with won't be of the same quality that you can get with a third-party library.

    Valid argument.

    Yeah, I am pig-headed and I prefer rolling my own solutions.

    I did a quick Google search during supper, and it looks like it isn't too hard to create decent pie charts and bar charts using CSS3.  (I think the hard part is using PHP to grab the data from the database, and dynamically feeding the graph things like adjustable scales so as more people respond the graph adapts.

    Reporting is a v2.0 thing, or at least a v1.1 thing.

    But my question for now is more making sure I don't paint myself into a corner that i can't easily fix later.

    Sounds like at first inspection you think I'm on the right path.  And, now I just need to figure out how much I want to "gold-plate" my current database design. 🙂

     

     

     

  9. 16 minutes ago, requinix said:

    Well you can't do much with them while they're still sitting in the database...

    I figure the "right" approach depends on what you have to provide to generate the chart. You probably need to end up with an array, naturally, but in what format? An array of key/value pairs? An array of arrays? Array of objects?
    What is the end result as far as the code is concerned?

    Well, I don't know exactly what I need since I don't know how to create a bar chart from scratch yet!

    After posting, here is what I came up with in my head...

    - In MySQL, create a query using COUNT( ) and GROUP BY to get a query-set that contains the 5 stubs and their count values.

    	1, Strongly Disagree, 7
    	2, Disagree, 10
    	3, Neither, 12
    	4, Agree, 25
    	5, Strongly Agree, 41 
    	

    - Then pull that into a multi-dimensional array in PHP

    - Then iterate through that array / multi-dimensional array and build my bar chart.

     

    In general, does that sound like a good start?

     

    Again, the main thing I am worried about is not having what I need on the backend.

    I have my survey script running now, plus data-validation working, and I have a few data model tweaks in mind, but I want to be sure that I have the tables and relationships to build the graphs/charts/reports that one would expect.

    Since I will be (hopefully) displaying all survey results visually using PHP, I think that gives me more flexibility than if i could only present data in a tabular format - thus why I now have 'cross-tab" queries in my mind.

    (If a person wanted to describe survey results using only tables, then I think that would be a hellish query since in my case I have disparate question types - Yes/No, Rating Scales, Multiple Choice, Multi-Response, etc.  I think by breaking the data down to a per question basis and using a graph to represent that particular type of data, it will be much easier, and probably not as rigid on the table design side of things.)

  10. This question is part PHP, part MySQL.

    I have a survey_responses table...

    	- id
    	- survey_id
    	- question_id
    	- member_id
    	- response
    	

     

    For satisfaction questions (i.e. 1 = Strongly Disagree, 2 = Disagree, 3 = Neither, 4 = Agree, 5 = Strongly Agree), I'm trying to figure out how to get the counts for each of those responses into PHP so I can then feed them to a Bar Chart I hope to create with HTML/CSS.

    1.) Should I create some kind of loop in PHP, and look for each value (e.g. 1 = Strongly Disagree) and one at a time plug them into variables called: $stronglyDisagree, $disagree, $neighter, $agree, $stronglyAgree?

     

    2.) Or do I first need a "cross-tab" query in MySQL?

     

    3.) Would this be easier if the database values were in a PHP array?

     

    Not sure where to begin with this, and more importantly, I want to make sure that my data model in MySQL is sufficient so i can do reporting like this!!

     

    Thanks.

     

  11. 11 minutes ago, kicken said:

    Rule #3 is about avoiding something like this:

     

    I just didn't know if this comment...

    I'm currently dealing with a code base that has a 180-line if-elseif-else tree and it makes me wanna barf. 

    meant an IF-THEN-ELSE with 180 lines in between the THEN and ELSE, or if @maxxd meant 180 IF-THEN-ELSEIF-ELSEIF-ELSEIF...

     

    Either way, I get what both of you are saying, and thank you for the code examples.

     

    Yes, that is one thing I suffer with in my code base - lots of scrolling because I do have all of the detail in one place.

    Although, my solution to that is that I use comments like...

    	// ****************************
    	// DO SOMETHING (1)
    	// ****************************
    	code here....
    	 
    	// ****************************
    	// DO SOMETHING (2)
    	// ****************************
    	more code here....
    	

     

    Not justifying things, but I am saying that looking for my "section heading" - plus anything that is grey in netBeans - makes it pretty easy to scroll and find what I need.

    Every script also have a table of Contents...

    	Do one thing (1)
    	Do another (2)
    	:
    	:
    	Do even more (30)
    	

     

    So I am pretty adept searching (or scrolling for "Do this (27)" to get quickly to where I need.

    It's sorta like any disability....  You learn to over compensate in other areas and you end up surviving.

    But having two eyes, two ears, two hands, etc would be nice moving forward!  😉

     

  12. 3 minutes ago, maxxd said:

    First off, tip #3 is something everyone who codes should live by. To add to it, don't string together a million conditionals - I'm currently dealing with a code base that has a 180-line if-elseif-else tree and it makes me wanna barf. For what is supposed to be a conditional logic operation, this is shockingly devoid of both condition and logic.

    Not sure if I follow completely what you mean - as in how many nested IF-THEN-ELSE's?

    Although, I think my IF-THEN-ELSE's would make you turn to violence?!  ☺️

    I have IF-THEN-ELSE's that cover the majority of some of my scripts, and so they can be well over 1,000 lines of code.  (Surprisingly, I have become very adept at reading them, but I'm sure there is an easier way!)

     

    3 minutes ago, maxxd said:

    As far as length, methods should do one thing, and objects should handle one situation. What you've got now is what's called a god object in OOP, and it's - as you're finding - difficult to reason about and maintain.

    Not so much a "God class", but rather a particular "use-case".  trouble is, the start-to-finish path of a use-case is often well over 1,00 lines of code.  Most of my scripts average 800-1,500 lines of code, and I have over 50 of them.

     

    3 minutes ago, maxxd said:

    On the other hand, the issue you can run into with one function per file is that it can become a mess of code relationship that's hard to follow if you're not extremely diligent.

    True.  (If you want to want to know what's involved with say "checking out", it's all in one script minus a few functions.  So while scrolling can be a bitch on my laptop, I never have to go on an Easter egg hunt across 50 files to follow the flow of things.)

     

    3 minutes ago, maxxd said:

    It also absolutely requires a full test suite because chances are you'll be including the same file in multiple places, and this can lead to hard to find bugs when you forget you use a function somewhere in a different part of the code.

    That is one of many things that scares me about OOP.

     

    3 minutes ago, maxxd said:

    And finally, just to beat the horse some more, format everything appropriately. I've spent untold hours at work trying to parse SQL queries because the original author wrote them out in one long line instead of using line breaks and indenting to make it make sense.

    I probably average one line f comments for one line f code, and my code is some of the prettiest you'll ever see, so preaching to the chior on that one.  But still good to be said!

     

     

    3 minutes ago, maxxd said:

    Obviously PHP isn't Python or Ruby in that indentation doesn't actually affect the code, but there's a reason the authors of some programming languages chose to use indentation instead of brackets or curly braces - it makes the code easier to read, and illuminates the intention of the functionality.

    Agreed.

     

     

    3 minutes ago, maxxd said:

    Wait - I lied; one more thing (it's late but I'm not sleepy and my mind is going, sorry). Absolutely use descriptive and appropriate method and variable names, but document as well. Just because your method is called `applySalesTax` doesn't mean I feel like parsing the entire method to figure out how or why you're applying sales tax. For instance you can assume that people know that sales tax is different across states, but if you don't charge sales tax in North Carolina as a business decision or due to a legal loophole, put that in the docblock.

    As mentioned, probably 40-50% of my code is comments, so I get you.  (Not to say YOU would understand everything, but it is enough so that I have been able to read and understand 90% of my code from 8-10 years ago!  (And the few places I couldn't understand things - mainly some big ass queries - i should have done a better job documenting the business logic?!)

     

     

  13. 17 hours ago, maxxd said:

    Make an array of the required form fields in php, then loop through that array on submission. If any of the fields are empty, add a message to an errors array. In the end of the actual, full form processing script if the errors array isn't empty, loop through the errors array and print out each individual error.

    @maxxd

    I was able to take your advice and write a block of code to do what you suggested last night.

    have gotten error message starting to appear for some question types, but now I need to figure out how to logically incorroate this new code into my old code base which is going to be a bear - but I am getting there!

     

  14. 7 minutes ago, gizmola said:

    I haven't seen a lot of your code, but the other thing that jumps out is that having functions where you are passing 4-5 parameters indicates that you probably have blocks of code that are trying to do too many things, and are not discrete enough.

    Not just functions - all of my code.

    The way I learned to code is procedurally, so I code things based on what you want to do.  For instance, my "article.php" script - which displays an article and user comments below and all functionality related to that is a little over 3,000 lines of PHP, because I coded some "use-case" as a single script.  (Not the way OOP works.)

     

    7 minutes ago, gizmola said:

     I certainly could look back at code I wrote in the past and admit I did the same thing on many occasions.  This leads to the type of problems you are concerned about:  large code base, concerns over side effects if you make a change, cascading throughout the code, lack of DRYness etc.

    Bingo!

    if you were almost done with a decade long project, would you go in and tweak some section for fear it could cascade and break thousands of lines of code?  (Of course that can apply to any large system, but more so the way i coded things.)

     

    7 minutes ago, gizmola said:

    This tends to happen when you build everything from scratch, as your code evolves into a thing "that works for what I have" rather than something designed from the outset to be clean and maintainable, and unit testable.  

    Well, I disagree here.

    My code isn't poorly written, it is just hard to work with since it is a birth-to-death approach!  (I recall reading from some OOP expert that a class should never be more than 100 lines of code.)

    I have spent the last few weeks walking through my code base line by line and getting my head back into how it works.

    Except for this current script, i would say that all of my code made sense - although some took a long time to load into my brain and turn on a lightbulb! - and not surprisingly to me, I have only had to tweak a *few* lines of code after reading over 40,000 lines so far.

    So to an outsider, getting into my code might be scary, i think it reads like a book once you see my coding style. Also, i have my code so heavily commented, most of it is self-explanatory.

    Also, I want to point out that the reason this site has taken over a decade is that it IS well thought-out.  (In fact, a little too much so!)

    My challenge is to learn how to break a 3,000 line script into 20 components that are shorter, easier to update, and more reusable.

     

    7 minutes ago, gizmola said:

    While you can't go back and change large swaths of the system now, you certainly can improve it incrementally by writing smaller more discrete functions for anything new or currently undergoing modification, and refactoring whenever possible.

    Once I go live, I am going to update any architectural designs I have written out on paper, update them, and then start coding a new v2.0 from scratch.

    That way I have a working site, and I can ferret out all of the annoying things that I want fixed but that would take too much energy to do.  (Better to start with a new frame and body versus trying to twist a wreck back into an original!)

     

    7 minutes ago, gizmola said:

      Sometimes a small improvement can be game changing in terms of system quality and maintainability.

    • Write functions that do one thing, and always return a result of the same type.
    • Most functions should have a handful of parameters.  If there are more than a few essential parameters, look at ways to break the function into smaller more discrete functions/pieces that you can use to assemble your results

    Most of my functions have 3-4 parameters, so that isn't too bad.  What is bad is how I intermingled "presentation' with "business logic".  Frankly, I think PHP encourages that kind of bad coding, which is why I'd like to get into something more substantial like Python, but time will tell.

     

    7 minutes ago, gizmola said:

    Obviously having functions with 1000's of lines of code is hard to work with.

    Exactly.

    What is worse, is that i have a single 'functions.php" script with ALL of my functions and it is like 8,000 - 10,,000 lines of code long?!

    In the future, I am thinking "One function, one script" i sthe way to go, as I think that is how OOP works where they have "One class, one file".

     

     

    7 minutes ago, gizmola said:

    Most editors have ways to open multiple windows and add bookmarks in your code, when you have to move from one to another.  Some of the fancier IDE's also let you jump back and forth in your code, by creating linkages from the files where classes and functions were defined that the editor can then use to open the appropriate file when needed, akin to clicking on a hyperlink.  PHPStorm has that type of function, and can really help when navigating a large codebase. 

    I know I need to strengthen my skills in IDE's.  I use NetBeans and am probably only using 10% of its features which is sad.  In fact, I think THAT is mor eimportant than learning Git - although I am planning on doing that.

     

    There is also one other thing you left out...

    I am 10-15 years older - and more senile - than I was when I started with LAMP, and at my age, those 10-15 years are SIGNIFICANT!!!

    I'm not dead yet, but I am also no longer 20-something, and I am starting to "appreciate' how my mind isn't as sharp - nor my stamina as strong - as it was in say 2000.

    That is even more reason to get damn good with IDE's, Git, MVC, OOP, and use generally accepted coding 'best practices" ASAP.

    In the end, I know a lot more than many people give me credit for, HOWEVER, thee is much I can do to take things "to the next level", and if I want to become a millionnaire, then i better hurry up and start doing those things in v2.0!

     

    😀

  15. 3 minutes ago, gizmola said:

    We don't have enough of your code to look at but to be clear -- when passing by value, you can update the array inside the function.  You can echo out values, make assignments etc.  and everything will look fine.  Any of those changes will be discarded once the function exits, however, as inside the function you are working with a copy of the original variable as it was when the function was invoked.  

    If this is not the issue, then we need to see some actual code, to help further, as anything at this point is simply an educated guess.

    Points duly noted.

    Thanks to @maxxd suggestion last night, I think I am over the hump on things, and I think I know where the problem is, but there is so much code to sort through...

    Let me get back to you on this, but i don't think using "by reference" will help here, although thanks for the lesson!  🙂

    ******************

    And THAT is what I hope to accomplish in v2.0 - even more than Git @gizmola 😉 - is doing a better job of compartmentalizing my code.  It is such a b*tch reading through thousands of lines of code to fix something like I am on now.  I forgot how involved my survey actually is?!  I think that using MVC and getting smarter with functions, and likely using OOP will make it easier to work with my code.  But who knows?!

    Right now my website is between 50,000-60,000 lines of code and that is A LOT to me.  I also have single script that go over 3,000 lines, so scrolling up and down and up and down looking at IF-THEN-ELSE statements on a tiny laptop can be overwhelming!  And for this current issue, it means going back and forth between multiple scripts, so by the time I get to functions.php, I have forgotten what i was looking at in xyz.php.

    I guess even the best written code struggles from such complexities to a point, but THIS is what iw ant to get better at so i can code better and faster.

     

  16. @gizmola,

    Thanks for the demo.  I am familiar with passing by value vs by reference, but your example is helpful.

    The problem, however, is not that, because I am using $errorArray within the function - which re-generates each survey question.  (If I was referencing $errorArray outside of the function, and I needed the $errorArray to be updated based on the function, then your example would apply.)

    One dumb thing I was doing that caused one issue, is that I had

    	var_dump($errorArray);
    	exit();
    	

    turned on inside my array, but that created the following issue...

     

    The first time I call $generateSurveyItem( ), I need it to execute in full so that my survey page gets painted.  (That is why i was seeing NULL, because my test code above stopped the page from being painted!)

     

    However, if I comment out those two lines, load the survey page, then uncomment those two line, and submit the form, then I get a var_dump( ) displaying all required fields left blank.

    So my $errorArray *IS* getting populated by my new check for required fields code, and $errorArray *IS* getting passed properly to my function.

    Now the question is, "Why aren't my survey questions getting appended with the error message from $errorArray??

    Gotta look at my function code one more time...  :facewall:

     

     

     

  17. 12 minutes ago, Barand said:

    You send and receive the array OK.

    That just leaves your processing of it inside the function.

    I don't understand your response.

    In my main script, I did a var_dump($errorArray) and I get values if I submit the form with no values or without required fields being answered.

    But inside and at the top of the function, I do another var_dump($errorArray) and PHP is showing.

    	/public_html/utilities/function.php:3010:null
    	

     

    So clearly something is happening to the data in $errorArray from when it is passed to my function and then inside the function.

  18. 7 minutes ago, Barand said:

    What's your code that calls generateSurveyItem() ?

     

    Here is a snippet

    if (empty($errorArray)){
        // Write to database
        
    }else{
        // Send $errorArray to generateSurveyItem() to re-build survey items with error messages.
        $questionNo =1;
        
        while(mysqli_stmt_fetch($stmt2)){
            $surveyItemArray[$questionID] = generateSurveyItem($dbc, $questionNo, $questionID, $surveyResponseArray, $errorArray);
            
            $questionNo = $questionNo + 1;
        }
    }
    	

    xx

    ..

     

  19. In my main script I set $errorArray with values when required survey questions go unanswered.

    The ne code that checks for missing required values is working perfectly, and right before I pass $errorArray to a function which rebuilds the survey questions - this time with the error messages - I do a var_dump($errorArray) and that array has errors in it as expected.

    But while my function that re-builds the survey questions gets called and does indeed re-build the survey questions, the $errorArray isn't getting pased for some strange reason?

    (I'm not sure if i have ever passed an array to a function before.)

    In my function I have...

    	function generateSurveyItem($dbc, $questionNo, $questionID, $surveyResponseArray=NULL, $errorArray=NULL){
    	 
    	}
    	

     

    When I var_dump($errorArray) at the top of the function, and submit a blank form, I end up with a blank page and a file path to my PHP script ending with...

    	/public_html/utilities/function.php:3010:null
    	

     

    Questions:

    1.) What is happening?

     

    2.) I thought =NULL in the parameter list just meant that the argument is "optional"?

     

    3.) I am able to successfully pass $surveyResponseArray to this function, so what is up with $errorArray?

     

    P.S.  When I run things, and submit an empty form - thus no "required" fields get completed - the survey gets re-displays, and any values chosen are "sticky", but my error messages should also be displayed to let the user know what is mising, and that is the part that isn't working?

    This has to be something silly stupid to fix...

     

  20. I am trying to compare an array containing required questions to an array holding survey responses.

    When a user submits a survey, I take the $_POST array and write the values to a new array called $surveyResponseArray.

    How can I determine if $surveyResponseArray exists?

     

     

  21. 11 hours ago, maxxd said:

    Make an array of the required form fields in php, then loop through that array on submission. If any of the fields are empty, add a message to an errors array. In the end of the actual, full form processing script if the errors array isn't empty, loop through the errors array and print out each individual error.

    I am trying to use your advice above to address this issue.

    (Now that I got my query and fetch working in my other thread, hopefully I can figure out how and where to use this advice to fix things.

    (These scripts that I am working on now were written 6 1/2 years ago, are very intense, and use LOTS of arrays - which I still hate to this day - so it's a real bear to del with.  But the purpose of my code review is to get my head back into things, and try to fix/improve things that should be addressed.  if I can just get this and a similar script fixed, then I am in the home-stretch to completing reviewing my entire website, although I still have to cod ethe e-commerce module, but the end is in sight!)

  22. I just grabbed another block of code that did a similar thing, pasted it into BBEdit, stripped out all of the specifics, and looked at my working code structure.

    At the very end, I see that my working code has...

    	while(mysqli_stmt_fetch($stmt2)){
    	

     

    Then I noticed that I have an EXTRA...

    ]code]

    mysqli_stmt_fetch($stmt10)

    [/code]

    in my broken code.

     

    NOW I see what Iw as doing wrong!

    (I haven't been outside in like 3 1/2 days - so that is the real problem.

    We had an ice storm, but I need to try and go to the store or something and clear my brain...

     

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.