Jump to content

mac_gyver

Staff Alumni
  • Posts

    5,450
  • Joined

  • Days Won

    175

Everything posted by mac_gyver

  1. no it doesn't. if it did it, it would always produce 3 checkboxes with the existing choices pre-checked. you need to break down problems into the individual steps need, then design, write, test, and debug the code for each step, before going on to the next step. you need to get to the point where you can first, always, produce the 3 checkboxes. the code i posted above is a very simple stand-alone working example. i assumed that $table is somehow the definition of the checkboxes, which what you do need to loop over to produce the output. however, it is apparently the existing choices, which is not the data you need to be looping over. if you run my code, you can observe and learn how it works. there will initially be 3 unchecked checkboxes, because there's no code to query for the existing choices. if you check any/combination of the boxes and submit the form, the boxes will retain their checked state. this is a feature you will need as part of general-purpose form processing/form code, where there can be other form fields, and you don't want to reset all the fields back their initial values should there be a validation error in any of the fields. once you understand how to dynamically produce the 3 checkboxes, you can move onto the next step of querying for and retrieving the existing choices to initially use to pre-check the checkboxes with.
  2. what you are missing, relevant to both my reply above and the reply by @Barand, is a table where the (currently 3) vehicle choices are DEFINED. it is this definition that will allow you to produce the checkbox markup without writing out code for every possible value. see the following example form processing/form code, related to the method stated in my reply above (there are comments in the code at the point where you would query for and retrieve the existing data to be edited) - <?php // you would query to get the following data from whereever it is stored // assuming that $table in the OP's code is an array of fetched data defining the checkbox choices $table = []; // the index is the id // do you really want the 'I have a' text with every entry? how about just a heading to select the vehicles the user has? $table[1] = ['name'=>'I have a yacht']; $table[2] = ['name'=>'I have a super car']; $table[3] = ['name'=>'I have a plane']; $post = []; // array to hold a trimmed working copy of the form data $errors = []; // array to hold user/validation errors // recursive function to trim data function _trim($val){ if(is_array($val)){ return array_map('_trim',$val); } else { return trim($val); } } // post method form processing code if($_SERVER['REQUEST_METHOD'] == 'POST') { // trim all the data at once $post = array_map('_trim',$_POST); // validate the data here, storing validation errors in $errors, using the field name as the array index // if no errors, use the submitted data if(empty($errors)) { // insert new data or update existing data here... } } // get method business logic - get/produce data need to display the dynamic content on the page // if editing existing saved choices, there would be a get input to validate, then used to query // for and retrieve the existing choice data, but only if the form has never been submitted ($post // is empty), storing the fetched data in $post, in a sub-array named 'vehicle', in the same format // as the form will submit it as. // html document starts here... ?> <form method='post'> <?php // display the checkboxes, with any existing choices pre-checked foreach($table as $id=>$arr) { $chk = in_array($id,$post['vehicle'] ?? []) ? ' checked' : ''; // note: if you put the <label></label> around the form field, you don't need to generate unique ids to make it work echo "<label class='boxstyle'><input type='checkbox' name='vehicle[]' value='$id'$chk> {$arr['name']}</label><br>\n"; } ?> <input type='submit'> </form>
  3. if you find yourself writing out code for each possible value, it's a sign that you are doing something the hardest possible way. just to add a value, you would need to edit the code and add another repeated block of code. the list of (currently 3) choices should be defined in a data structure (database table, array), with a numerical id (auto-increment primary index) and a name/label. it is this definition that you would loop over to produce the output. if you are pre-checking existing choices when you are looping to produce the output, either based on some stored choice ids or due to submitted ids from a form, you would test if the current id being output is in an array (see php's in_array()) of the existing choices to determine if the 'checked' attribute should be output.
  4. all that did was hide the actual problem. if your code is expecting an array of data as its input, it's an application error if it isn't. either a programming mistake or an incorrect user action (directly requesting a delete action page without first visiting the page selecting what to delete) caused the expected input to not be the expected data type. for programming mistakes, you need to find an fix the root cause of the problem. for an incorrect user action, you need to setup and display a message telling the user either what they did wrong, i.e. you cannot directly request this page, or what they need to do to correct the problem, i.e. you must select at least one notification to delete.
  5. wherever you found this, forget you saw it. here's everything that's bad about this - don't use output buffing unless you WANT to buffer output. by using it to make bad code work, you are hiding mistakes in the code and it will make finding and fixing problems harder. don't conditionally start a session. if you want to use sessions, simply, unconditionally start them once, in an initialization/configuration file. as already stated about the error, and stated in the documentation for session_set_cookie_params(), you must call this before the session_start. session_set_cookie_params(0) is only valid in php8, where that usage would set the session.cookie_lifetime to zero, but the default is already zero. set_time_limit(0) and ini_set('max_execution_time','3600') set the same thing, so these are first setting the max_execution_time to zero (which you should never do since a programming mistake that causes a script to never end will require that you restart the web server to regain control), then sets it to 3600. you cannot set post_max_size in a php script, because the value is used by the server before a php script is ever invoked.
  6. $response[0]['results'] is the array of data that you should be looping over in the foreach loop. $value['campaign']['name'] would be what to echo (don't create variables for nothing.)
  7. as the error states, you cannot set the session cookie parameters after a session has been started. you must call session_set_cookie_params() before calling session_start().
  8. your print_r() output (you should use var_dump instead) actually confirms this. when you print_r a true value, you get a 1. when you print_r a false value, you get nothing. the print_r output indicates a false eof value, i.e. you are not at the end of the file, with a zero unread_bytes value.
  9. the php documentation for socket_get_status/stream_get_meta_data has a note to not use the unread_bytes value in a script, which is what the posted code's do/while loop is using, probably because it is just the number of bytes currently contained in the PHP's own internal buffer. this does not indicate that you are done reading the stream, just that there's a pause in the data received such that you have emptied php's internal buffer. in the same documentation, for the eof value - whoever wrote and tested this code probably had a situation such that unread_bytes was always non-zero until the end of the data.
  10. has a post method request ever worked on this server? do you have any .htaccess redirecting or is this server behind a proxy server?
  11. i did NOT state that you should do that. we are trying to determine why a post method form does not work.
  12. these type of problems, where it looks like the code should technically work, are often due to copy/pasting code found on the web, where it has been published and the characters, due to the character encoding being used, aren't actually what they appear to be, or someone typing non-ascii characters as part of the syntax (the base characters for the actual syntax must be straight ascii characters.) what does changing the print_r($_POST) to print_r($_GET) show, i.e. the default form method is get when an unknown value is used for the method attribute? you can also add echo $_SERVER['REQUEST_METHOD']; at that point in the code to see what the browser is submitting the data as. if this shows get data/get method, delete and TYPE the entire method='post' attribute (you may in fact need to delete and re-type the entire <form ...> tag if there are some non-ascii characters in it.) btw - your form and form processing code should be on the same page. this will result in the simplest code and the best User eXperience (UX.)
  13. when you fetch and index/pivot the data, index it by both the year and the month - $data = []; foreach ($results as $result) { $data[$result->Year][$result->Month][] = $result; } to produce the output - foreach($data as $year=>$arr) { // start a new year section here... echo "<h3>$year</h3>"; foreach($arr as $month=>$rows) { // start a new month section here... $monthName = date('F', mktime(0, 0, 0, $month, 10)); echo "<h4>$monthName</h4>"; foreach($rows as $row) { // output the data under each month here... echo "<p>$row->Title</p>"; } } }
  14. you are missing a logical operator after one/between the isset() statements. most of this typing is unnecessary. when a form has been submitted all the form fields with be set except for unchecked radio and checkbox fields, which you would detect and validate separately. are any of these inputs radio or checkboxes? If not, then that code is a waste of typing.
  15. it means that there is no fetch() method. if you are going to use a while(){} loop, you would use the fetch_array() method. if you are going to use a foreach(){} loop, you can directly iterate over the rows in the result set.
  16. repeating things that you have seen is not learning. it is just you typing something at your location that someone else has told you to do. you must actually look at and learn what the words and syntax that you are using mean, i.e. internalize the information. there are a huge amount of examples for all the fundamental programming operations to be found on the internet. if you didn't do a web search for sql where with multiple conditions before starting this thread, why not? the second query has two conditions in the WHERE clause, with a logical AND operator between them (must include a AND b.) how did you arrive at that? what did you learn by doing that? to add the third column BETWEEN ... AND ... condition, you would just AND it with the rest of the WHERE conditions (must include a AND b AND c.)
  17. you could always make an attempt and observe the result to see if your query does what you want. SELECT the columns you want FROM your table WHERE all the where conditions here...
  18. the errors are being appended to the end of the downloaded file. in fact, there may be a php error appended to the end of a large file that would explain why it isn't working. for a file download application, you would want to log all php errors.
  19. when you require a .php file using a URL, on a correctly configured and functioning web server, you don't get the php code in the file, you only get any output produced by that php code. you must use a filesystem path in order to require .php files.
  20. if you do the things that have been suggested, you won't get undefined variable/index errors. if there might not be a related row in the candidate_paye table, you would use a LEFT JOIN in the single query. this will result in null values for the columns you select from that table. the column (index) will exist in the fetched data. when you echo a null value in the php code, there is no output, i.e. a blank value.
  21. additionally, for a query that's expected to match data for the page to work, its an application error if it doesn't and you should setup and display a message for the visitor telling them so, rather than to blindly try to use data that doesn't exist. don't use a loop to fetch data from a query that's expected to match at most one row of data. just fetch that single row of data and test if a row was fetched (like the 2nd query code is doing.) you need to validate all inputs before using them. the session variable is an input to this block of code. if it doesn't contain an expected value, that's an application error and you should setup and display a message for the visitor telling them so. don't put external, unknown, dynamic values directly into sql query statements. use a prepared query instead. lastly, don't copy variables to other variables for nothing. just use the original variables. after you execute the single (LEFT?) JOIN query, just fetch the data into an appropriately named php array variable, then use elements in this array variable throughout the rest of the code.
  22. template method - <?php require 'Mustache/src/Mustache/Autoloader.php'; Mustache_Autoloader::register(); $m = new Mustache_Engine; // define template $tpl = " <table{{#class}} class='{{class}}'{{/class}}> <thead> <tr> {{#headings}} <th>{{.}}</th> {{/headings}} </tr> </thead> <tbody> {{#rows}} <tr> {{#.}} <td>{{.}}</td> {{/.}} </tr> {{/rows}} </tbody> </table> "; // fake your existing data $class = "data clickable admin"; $data = []; $data[] = ['c1'=>'r0v1','c2'=>'r0v2','c3'=>'r0v3','c4'=>'r0v4','c5'=>'r0v5']; $data[] = ['c1'=>'r1v1','c2'=>'r1v2','c3'=>'r1v3','c4'=>'r1v4','c5'=>'r1v5']; $data[] = ['c1'=>'r2v1','c2'=>'r2v2','c3'=>'r2v3','c4'=>'r2v4','c5'=>'r2v5']; // array_map call-back function to get only array values function _array_values($arr) { return array_values($arr); } // populate template data $tpl_data = []; $tpl_data['class'] = $class; $tpl_data['headings'] = array_keys($data[0]); $tpl_data['rows'] = array_map('_array_values',$data); // echo the rendered template echo $m->render($tpl, $tpl_data); result - <table class='data clickable admin'> <thead> <tr> <th>c1</th> <th>c2</th> <th>c3</th> <th>c4</th> <th>c5</th> </tr> </thead> <tbody> <tr> <td>r0v1</td> <td>r0v2</td> <td>r0v3</td> <td>r0v4</td> <td>r0v5</td> </tr> <tr> <td>r1v1</td> <td>r1v2</td> <td>r1v3</td> <td>r1v4</td> <td>r1v5</td> </tr> <tr> <td>r2v1</td> <td>r2v2</td> <td>r2v3</td> <td>r2v4</td> <td>r2v5</td> </tr> </tbody> </table> note: htmlspecialchars is applied to all values by default, so any html special characters in a value won't break the html syntax, or allow html, css, or javascirpt to be operated on by the browser.
  23. that's a LOT of typing, especially for the static sections containing no dynamic values. you need to use templates, with tags in it for the dynamic sections, then either write your own templating code or use one of the existing templating engines. here's the tag documentation for one of the popular templating engines showing how to display values, implement if conditional statements, and loops - https://github.com/bobthecow/mustache.php/wiki/Mustache-Tags
  24. the operation is to simply either add one day or subtract one day from the current date - <html> <head> <title></title> </head> <body> <form method="post" name="f1"> <?php error_reporting(E_ALL); ini_set('display_errors', '1'); // Default dates $today = date('Y-m-d H:i:s',strtotime('today')); // Check the form was submitted if ($_SERVER['REQUEST_METHOD'] == 'POST') { $today = $_POST['today']; if($_POST['Submit'] == 'Tomorrow'){ $today = date('Y-m-d H:i:s', strtotime($today . '+1 days')); } if($_POST['Submit'] == 'Yesterday'){ $today = date('Y-m-d H:i:s', strtotime($today . '-1 days')); } } ?> <table border="1" align="center" width="100%"> <input type="hidden" name="today" value=<?php echo $today;?>> <tr> <td align="center" width="20%"><input type="Submit" name="Submit" value="Yesterday"></td> <td align="center" width="60%"> <?php echo $today;?></td> <td align="center" width="20%"><input type="Submit" name="Submit" value="Tomorrow"></td> </tr> <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { //<!--- Form view ---> echo"<tr><td>query</td></tr>"; } else{ //<!--- Default view ---> "<tr><td>default</td></tr>"; } ?> </table> </form> </body> </html>
  25. the cause has already been posted - only use full opening tags <?php so that your php code will always be seen as being php code.
×
×
  • 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.