SaranacLake
Members-
Posts
648 -
Joined
-
Last visited
Everything posted by SaranacLake
-
@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?
-
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!
-
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...
-
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! 👍
-
Constraint on Child-table based on value in Parent-table
SaranacLake replied to SaranacLake's topic in MySQL Help
True, but even I make mistakes, and guard-rails can be nice. 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... -
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...
-
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. I will put your code in my hat to use after I go live with my website. Thanks for the headstart! 👍
-
Good to hear! 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.) 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. 🙂
-
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.)
-
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.
-
I just didn't know if this comment...
-
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!) 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. 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.) That is one of many things that scares me about OOP. 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! Agreed. 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?!)
-
@kicken Thanks for the tips! 👍
-
How to check when a form field is left blank
SaranacLake replied to SaranacLake's topic in PHP Coding Help
@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! -
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.) 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.) 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. 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!) 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. 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". 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! 😀
-
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.
-
@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...
-
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.
-
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 ..
-
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...
-
@gw1500se Yes, I forgot the "!" empty() seems to do the trick. Thanks.
-
It looks like this might work? if (empty($myArray)){ // Handle array }else{ // Handle empty array }
-
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?
-
How to check when a form field is left blank
SaranacLake replied to SaranacLake's topic in PHP Coding Help
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!) -
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...