Jump to content

SaranacLake

Members
  • Posts

    648
  • Joined

  • Last visited

Everything posted by SaranacLake

  1. Insulting me and my code does nothing to help me understand why the above code is not working. I have used similar code all along, so I'm not sure what is wrong - although the answer is probably right in front of me. mysqli_stmt_fetch($stmt10); should return all of the applicable rows from my query - which works fine in phpMyAdmin. And while (mysqli_stmt_fetch($stmt10)){ $requiredItemsArray[$requiredItem] = 'xxx'; } should populate my array with (1, 3, 5, 6, 7) as the keys and 'xxx' as junk values since all I need are the keys. So what am I doing wrong?
  2. Not getting anywhere today... ;-( I have a query in my script using prepared statements. The query returns a listing of required questions. if (mysqli_stmt_num_rows($stmt10)>0){ mysqli_stmt_bind_result($stmt10, $requiredItem); mysqli_stmt_fetch($stmt10); while (mysqli_stmt_fetch($stmt10)){ $requiredItemsArray[$requiredItem] = 'xxx'; } When I copy and paste my query into phpMyAdmin, it gives met {1,3,5,6,7} When I run the above PHP code, my var_dump($requiredItemsArray), then I get {3=>'xxx', 5=>'xxx', 6=>'xxx', 7=>'xxx'} Where is 1? 1.) Ideally, I want an array that just lists {1,3,5,6,7} like my query. 2.) I would like to figure out why I am missing "1"?! Thanks.
  3. Is it possible to create a PHP array that contains only keys? array 1 3 5 6 7 I have a query that returns row ID's that I want to put into an array to do some work. There is no real concept of a "value" - I just need to store a list of the database table rows that i want and that's it!
  4. Well, the snippet above doesn't reflect my more complex script, so that doesn't apply. Yeah, but the article survey is voluntary... IF someone decides to complete it, then I sorta expect they answer all of the Yes/No and multiple choice questions - although not necessarily the open-ended ones. Let me try to explain what I have, because there is n way to post thousands of lines of code and include a large database. My scripts create surveys based on a survey map which defines the questions, plus linked tables that build things like multiple choices. Everything runs great except I never really figured a way to do data validation on my more complex survey. If a question in the survey map is Yes-No, then I had this snippet... (Hint: This is a snippet, and the syntax isn't the issue, so pardon brevity or type-o's.) This script builds each survey item... case 'YN': $item = "<li> blah blah <input id='question_$questionID" . "_Y' name='questionResponse[$questionID]' type='radio' value='1' " . ((isset($responseArray[$question]) && $reviewArray[$questionID] == '1') ? "checked='checked'" : "") . " />" and so on... Then I have my calling script which which is similar to my earlier posts. However, in the simpler "gender" example, that is doable because all form values are hard-coded, so I can say... if (!isset($trimmed['gender'])){ // $gender = 0; $errors['gender'] = "Please choose a gender"; // NEW What makes my current problem so tricky is that things aren't hard-coded, they are dynamic. And if I submit the form with not entries, the only thing that shows up in the $_POST array are text boxes as ' '. So I can't check if they answered "Q1.) Did you like this article? (Y/N)?" because it never registers in the $_POST array?! With my original code - snippet above - I am getting a $_POST array like this if I complete answers... array 'questionResponse' => array 1 => 1 2 => I liked this article because it was detailed... 3 => 1 4 => I learned how to.... I thought that adding a [yn] "hook" like this might help... $item = "<li> blah blah <input id='question_$questionID" . "_Y' name='questionResponse[$questionID][yn]' type='radio' value='1' " . ((isset($responseArray[$question]) && $reviewArray[$questionID] == '1') ? "checked='checked'" : "") . " />" and so on... and in the calling script this... foreach($_POST['questionResonse'] as $questionID => $surveyResponse){ if (!isset($surveyResponse['yn'])){ set some error message... } } I think this would be a great place to use Javascript to do data validation on the front-end, but that isn't a solution for now. Besides, I would like to see how to do this using PHP. if I can't get this working, my script does work otherwise, and I do have a PHP check to not allow submittal of a completely blank survey, but I would prefer people answer all of the TF, YN, multiple-chocie questions as there are only like 4-5 of them. They can skip the open-ended questions fi they like.) Is it possible to make this work using PHP?
  5. I can't make this work for my more complex script which has a multi-dimensional array... On a side note, if you had a survey on your website about an article, would it be necessary to require the user to complete all questions, or is that undesirable? (That is why I am struggling with this old code - I wanted to add error messages for any fields left unanswered. But maybe I don't want to do that even if I can figure it out technically?) :confused;
  6. .. I think this addresses the issue with my simpler script... <?php if ($_SERVER['REQUEST_METHOD']=='POST'){ // var_dump($_POST); // exit(); $trimmed = array_map('trim', $_POST); if (!isset($trimmed['gender'])){ // $gender = 0; $errors['gender'] = "Please choose a gender"; // NEW }else{ if (($trimmed['gender'] == 1 || $trimmed['gender'] == 2)){ $gender = $trimmed['gender']; }else{ $errors['gender'] = "Gender must be 'Male' or 'Female'."; } } //and so on... } ?> <input id="gender_opt1" type="radio" name="gender" value="1" <?php echo (isset($gender) && gender == "1") ? 'checked="checked"' : ''; ?> /> <input id="gender_opt2" type="radio" name="gender" value="2" <?php echo (isset($gender) && gender == "2") ? 'checked="checked"' : ''; ?> /> And, yeah, I guess I did have the answer, but it threw me off not seeing anything in the $_POST array. I guess PHP is smart enough to respond in the negative if I ask for a component of the $_POST array that isn't there (e.g. $trimmed['gender'])
  7. I just manually retyped in my code from another computer. Yeah, I had a few type-o's, but that's not the issue.
  8. I am reading through some old code which generates a dynamic survey and trying to get my head into things. My old code - which is too complicated to post here - doesn't check if something like "gender" gets left blank, and I;d like to fix that. So I was looking at another script that is much simpler, but now it seems that I don't understand that one either?! Here is a snippet from my simpler script - which definitely works - but which isn't helping me figure out my more complicated script... <?php if ($_SERVER['REQUEST_METHOD']=='POST' var_dump($_POST); exit(); $trimmed = array_map('trim', $_POST); if (!isset($trimmed['gender'])){ $gender = 0; }else{ if (($trimmed['gendr'] == 1 || $trimmed['gender'] == 2)){ $gender = $trimmed['gender']; }else{ $errors['gender'] = "Gender must be 'Male' or 'Female'."; } } //and so on... ?> <input id="gender_opt1" type="radio" name="gender" value="1" <?php echo (isset($gender) && gender == "1") ? 'checked="checked"' : ''; ?> /> <input id="gender_opt2" type="radio" name="gender" value="2" <?php echo (isset($gender) && gender == "2") ? 'checked="checked"' : ''; ?> /> Haven't been outside in 3 days due to horrible weather, and my brain is fried, so maybe you all can help me see something obvious that I can't seem to see?!
  9. I have a form with a gender control made up of two radio buttons. If the user doesn't fill out this form, nothing gets submitted (i.e name='gender') in the $_POST array. So how ae you supposed to verify that the form field was left blank, and use that information to say display an error message on the form?
  10. I have a junction table (child) linking two parent tables. In the junction table I have a UK on 4 columns. The first column is the FK back to parent table-1. The second column is the FK back to parent table-2. In MySQL, can I just have a UK on the first 4 columns and have that satisfy the need for the UK plus the need to have indexes on columns that are FKs? Or do I need to double up, and add a straight idx on column 1 and then on column 2 along with my 4-column UK - which is an index also.
  11. This question may sound like a MySQL question, but I think it relates more to PHP... Currently I have the following (pseudo-code) query in my PHP script... SELECT id, col-a, col-b, col-c, usefulCount, usefulResponses, (usefulCount/usefulResposnes) as usefulPct newMedianCalculationHere FROM( SELECT DISTINCT col-a, col-b, col-c, (SELECT COUNT(xtc.id) FROM table-7 INNER JOIN table-8 ON WHERE something AND respone_yn = 1) AS usefulCount, (SELECT COUNT(xtc.id) FROM table-7 INNER JOIN table-8 ON something WHERE something) AS usefulResponses (Add Median calculation here) AS newMedianCalculationHere FROM INNER JOIN WHERE ) AS wrapper ORDER BY whatever Now I need to calculate the median of some data and drop that in my inner query. Here is some sample code that I found online... https://www.eversql.com/how-to-calculate-median-value-in-mysql-using-a-simple-sql-query/ SET @rowindex := -1; SELECT AVG(g.grade) FROM (SELECT @rowindex:=@rowindex + 1 AS rowindex, grades.grade AS grade FROM grades ORDER BY grades.grade) AS g WHERE g.rowindex IN (FLOOR(@rowindex / 2) , CEIL(@rowindex / 2)); Because my query is in a PHP script, I'm now sure how to incorporate these lines... SET @rowindex := -1; (SELECT @rowindex:=@rowindex + 1 AS rowindex, Oh, if it matters, I use prepared statements in my PHP... Thanks.
  12. P.S. I'm always interested in learning better ways to do things, but I'm not sure your code will help, because I need to capture each bad word and ultimately write it into a table, which is why I created a $matchesArray. It doesn't look like your code would allow me to grab a bad word at a time and then insert it into a table, but I could be wrong. (Fwiw, my code does work, I just don't understand how the couple of lines mentioned above actually work.)
  13. Yes, I am trying to understand how my code works... Thanks for the code. Have been looking at it, but am not entirely following it even after loking things up at php.net... 1.) What is $matches? And where does it come from? 2.) What is "use"? 3.) Please explain what this is doing... function($matches) use (&$matchesArray) { 4.) I am guessing that $matches is an array. But what is this doing? list($word) = $matches; I am used to seeing list used like this... $info = array('coffee', 'brown', 'caffeine'); list($drink, $color, $power) = $info; 5.) Am not getting tis... list($word) = $matches; $matchesArray[] = $word;
  14. I just explained why I am creating $matchesArray... I use that to gather the "bad words" from a given user post and log them into my database under their user ID. I should have added the comment //What does this do? I know what the loop does, what i don't understand is this... /* If I comment (1) and (3) and uncomment (2), * then var_dump($matchesArray) displays nothing. * Not really sure what this block of code does why this code behaves as it does... */
  15. Here is an updated file that will show where I'm getting confused... <?php // Initialize Variables. $patternsArray = array(); $replacementsArray = array(); $tempArray = array(); $matchesArray = array(); // Un-censored text. $text = "Cassie, slipped on some glass and cut her ass!<br> 'Goshdamnit! Who left this damn mess out?' she cried.<br> 'Well, SHOOT, you should be more careful!' Mary laughed.<br> 'Hello! Why don't you clean up after yourself!' Cassie screamed.<br> 'Go to HELL!' Mary yelled back.<br> Then suddenly, blood started shooting everywhere.XXX"; // Create Patterns. $patternsArray[0] = "/\b(ass)(s?)\b/i"; // Word pattern $patternsArray[1] = "/damn/i"; // Substring pattern $patternsArray[2] = "/\b(shoot)(s?)\b/i"; // Word pattern $patternsArray[3] = "/\b(hell)(s?)\b/i"; // Word pattern // $patternsArray[4] = "/XXX/i"; // Substring pattern // Create Replacements. $replacementsArray[0] = '***'; // ass $replacementsArray[1] = '****'; // damn $replacementsArray[2] = '****'; // shoot $replacementsArray[3] = '****'; // hell // $replacementsArray[4] = '***'; // XXX // Sort Arrays. ksort($patternsArray); ksort($replacementsArray); // What does this do???? foreach ($patternsArray as $pattern){ preg_match_all($pattern, $text, $tempArray, PREG_PATTERN_ORDER); // (1) // preg_match_all($pattern, $text, $matchesArray, PREG_PATTERN_ORDER); // (2) $matchesArray = array_merge($matchesArray, $tempArray[0]); // (3) /* If I comment (1) and (3) and uncomment (2), * then var_dump($matchesArray) displays nothing. * Not realy sure what this block of code does... */ } // Clean text. $cleanText = preg_replace($patternsArray, $replacementsArray, $text, $limit=-1, $allCount); echo "<b>DIRTY TEXT:</b><br> $text"; var_dump($patternsArray); var_dump($replacementsArray); var_dump($tempArray); var_dump($matchesArray); echo "<b>CLEAN TEXT:</b><br> $cleanText"; ?> Run that code as is, and for var_dump($tempArray) you will see... array 0 => array 0 => 'HELL' array 1 => array 0 => 'HELL' Uncomment these two lines... // $patternsArray[4] = "/XXX/i"; // Substring pattern // $replacementsArray[4] = '***'; // XXX And then for var_dump($tempArray) you will see... array 0 => array 0 => 'XXX' In either case, the $matchesArray seems to yield the results I really care about. So why is $tempArray changing values but it doesn't seem to impact $matchesArray? And, therefore, what does this line of code really do? preg_match_all($pattern, $text, $tempArray, PREG_PATTERN_ORDER); // (1)
  16. I don't follow your observation. I used a foreach loop to loop through my $patternsArray - which contains the regex used to match a given bad word - and then inside the loop I use that given bad word regex in... preg_match_all($pattern, $text, $tempArray, PREG_PATTERN_ORDER); // (1) $matchesArray = array_merge($matchesArray, $tempArray[0]); // (3) ...to create the $matchesArray which I use to log each bad word a user used and I log that in my database. (That code is not listed above.) So this line of code actually *** out the ad words... // Clean text. $cleanText = preg_replace($patternsArray, $replacementsArray, $text, $limit=-1, $allCount); But I was asking about the code under " // What does this do????" because I do not understand why I was saving the bad words in $tempArray and then using array_merge to get things into $matcehsArray Follow me now? P.S. I wrote this function in 2015, and while I unit-tested all of my code and it was working, i don't undrstand what I was doing above, and wonder if there is a better way?
  17. So I have been reviewing my codebase that i stepped away from for a couple of years. Surprisingly (or not), so far I understand almost all of my code. But I have a function that **** out dirty words, and there is one section of the code that isn't making sense. (I added var_dumps, but I'm just not seeing what it is doing.) Below is slightly simplified code - since my real code uses the database plus a bunch of words I can't post here! I have added comments where I'm hung up... <?php // Initialize Variables. $patternsArray = array(); $replacementsArray = array(); $tempArray = array(); $matchesArray = array(); // Un-censored text. $text = "Cassie, slipped on some glass and cut her ass!<br> 'Goshdamnit! Who left this damn mess out?' she cried.<br> 'Well, SHOOT, you should be more careful!' Mary laughed.<br> 'Hello! Why don't you clean up after yourself!' Cassie screamed.<br> 'Go to HELL!' Mary yelled back.<br> Then suddenly, blood started shooting everywhere."; // Create Replacements. $replacementsArray[0] = '***'; // ass $replacementsArray[1] = '****'; // damn $replacementsArray[2] = '****'; // shoot $replacementsArray[3] = '****'; // hell // Create Patterns. $patternsArray[0] = "/\b(ass)(s?)\b/i"; // Word pattern $patternsArray[1] = "/damn/i"; // Substring pattern $patternsArray[2] = "/\b(shoot)(s?)\b/i"; // Word pattern $patternsArray[3] = "/\b(hell)(s?)\b/i"; // Word pattern // Sort Arrays. // ksort($patternsArray); // ksort($replacementsArray); // What does this do???? foreach ($patternsArray as $pattern){ preg_match_all($pattern, $text, $tempArray, PREG_PATTERN_ORDER); // (1) // preg_match_all($pattern, $text, $matchesArray, PREG_PATTERN_ORDER); // (2) $matchesArray = array_merge($matchesArray, $tempArray[0]); // (3) /* If I comment (1) and (3) and uncomment (2), * then var_dump($matchesArray) displays nothing. * Not realy sure what this block of code does... */ } // Clean text. $cleanText = preg_replace($patternsArray, $replacementsArray, $text, $limit=-1, $allCount); ksort($matchesArray); echo "<b>DIRTY TEXT:</b><br> $text"; var_dump($patternsArray); var_dump($replacementsArray); var_dump($tempArray); var_dump($matchesArray); echo "<b>CLEAN TEXT:</b><br> $cleanText"; ?> In the test I run locally - again I couldn't really post that here - my function seems to work, but I don't see how the parts i commented above work/help get the end results I want. The goal of reading all of my code line-by-line is to UNDERSTAND it, so that when I go to code my final module, I know my head is in the game. Thanks.
  18. I did add a message to my "Contact Us" form, but since my error logging relies on the SESSION, all I get is a catch-all message. Would be nice to have a way to log that the issue is the user's cookies are off. Not a real worry for v1.0 No, it isn't. I don't plan on learning Javascript before I go live. And I certainly wouldn't take time to learn it so i can know f the user disabled cookies on my site. My site will through a general error in any places that rely on SESSIONS, so that is good enough. As far as GDPR, yeah i sorta need to add that into my site before I go live - I guess...
  19. Is there a way for my PHP to check if a user has disabled cookies in their browser, thus breaking my $_SESSION? Since I don't know Javascript, that isn't an option.
  20. Okay, good. Aha! My bad, I copied and pasted code from my .htaccess that I was playing around with things, and didn't notice that I had those lines commented out when I pasted here. My bad! Okay. I am setting up CloudFlare on my VPS now, so we'll see if this code works with CloudFlare - although I just have a dummy inex.php up, so i won't be able to test how it works with my work-in-progress site for a while, but I guess I can add some dummy folders and files and do a quick spot check. Thanks!
  21. Sorry if I am missing something, but isn't this code - written as two separate blocks - the same as yours? RewriteCond %{HTTPS} off #RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule (.*) https://www.domain.com%{REQUEST_URI} [L, R=301] RewriteCond %{HTTP_HOST} !^www\. RewriteRule (.*) https://www.domain.com%{REQUEST_URI} [L, R=301] Otherwise, how is this code causing an "infinite redirect"? I just switched the order and check for the HTTPS off first sicne that covers 4 of the 5 scenarios which I want to rewrite... I guess it was just wanting prettier code. Btw, does it matter if I have [L,R=301] versus [R=301,L] ??
  22. How so? (Hopefully you didn't take my *IF*, *AND*, *THEN* literally...) I was trying to do things with a single RewriteRule, although upon reflection, I guess having two RewriteRule statements doesn't imply two redirects... But that is basically what I have now.... I was hoping to get this to work... RewriteCond %{HTTPS} off RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR] RewriteCond %{HTTP_HOST} !^www\. RewriteRule (.*) https://www.domain.com%{REQUEST_URI} [L, R=301] That way there is only one RewriteRule. Can i make the above code behave the way I implied earlier? or must I break things up into two blocks each having its own RewriteRule?
  23. This is regarding an old thread where I was having issues with rewriting from non-www and www to https://www and ClouFlare. This was the code you helped me come to... RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^https://www.domain.com%{REQUEST_URI} [L,R=301] As I recall, without CloudFlare, that worked fine. But with CloudFlare turned on, my webhost suggested this... #RewriteCond %{HTTP_HOST} !^www\. #RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] #RewriteCond %{HTTPS} off #RewriteCond %{HTTP:X-Forwarded-Proto} !https #RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] And I was hoping for something cleaner and without an unnecessary redirect like maybe this... (Pseudo-code added for clarity) *IF* #RewriteCond %{HTTPS} off *AND* #RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR] #RewriteCond %{HTTP_HOST} !^www\. *THEN* RewriteRule (.*) https://www.domain.com%{REQUEST_URI} [L, R=301] This is for a new server, so all of this is untested, but I hope to get a new CloudFlare account for this new server today, and try to get everything working, all with having a well-written, and efficient mod_rewrite!
  24. Hello. I am trying to streamline a mod_rewrite that I have. Generically, I will have 3 RewriteCond lines. Logically, I want this... (RewriteCond1 AND RewriteCond2) OR RewriteCond3 In essence, I need Apache to see parentheses around the 1st and 2nd RewriteCond and then take the results of that and OR it with the 3rd RewriteCond. How do I do that?
×
×
  • 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.