Jump to content

mac_gyver

Staff Alumni
  • Posts

    5,370
  • Joined

  • Days Won

    173

Everything posted by mac_gyver

  1. there already IS a variable assigned to the data from the fields, the variable you fetched the data into, $row. the code you have been trying/posting is already using elements of the array variable here -> $DisplayName = $row["DisplayName"];. just use $row["DisplayName"] everywhere and forget amount $DisplayName.
  2. yes, see the reply posted above your's that the ipb forum programmers Chimps couldn't figure out how to reliably notify you of while you were posting your reply. the code you posted implements some of the things i mentioned, but still has some issues - 1) by testing $result after you have tried to fetch data from the query, that code will throw a php error at the fetch statement any time the query fails due to an error. you would need to test $result before you try to fetch any data. however, if you enable exceptions like suggested, you don't need to have any logic in your code testing $result. if there is a query error, an exception will be thrown and the code trying to fetch data will never be executed. 2) by hard-coding the echoing of $mysqli->errno and $mysqli->error in the code, you will expose information in the errors to a visitor. if you use the suggested exception method, what happens with the error information is controlled by php settings, so that on a live server, you would log the information, rather than to display it, simply by changing a couple of php settings. 3) the first code has this - $row = $result->fetch_assoc() for the fetch statement. you now have this - $row=mysqli_fetch_array($result,MYSQLI_ASSOC). while both are functionally equivalent, why are you making unnecessary changes and going from simpler to more verbose syntax? Keep It Simple. 4) see what i wrote above about not creating discrete variables. your current method has you typing things in the SELECT list, two times in each $some_var = $row['some_column']; assignment statement, and then where you use the data. using the suggested method eliminates all the assignment statements. you would typically fetch the data into a variable named to indicate what the data means. so, rather than $row, you would use something like $stream_data. 5) lastly, i didn't write it above, but php closes the database connection for you when the php script ends. unless your code is taking a large amount of time to run after fetching the data from the database, there's no good reason to close the database connection yourself. so, for the last posted code, this is all you would really need - $sql = "SELECT gpsStatus, DisplayName, ChaserLocation, StreamStatus, CurrentViewers, TimeStamp FROM streamdb WHERE id = 1"; $result = $mysqli->query($sql); $stream_data = $result->fetch_assoc(); // just use the elements in $stream_data in the rest of the code
  3. your current code is missing a opening { for the while() loop. this would be producing a php syntax error if you had php's error_reporting set to E_ALL and display_errors set to ON in the php.ini on your development system. if you put these settings into your code, they won't help with php syntax errors in the same file, because your code never runs to cause the settings to be in effect. speaking of (writing about) the while() loop, if you are running a query that you expect to match a single row, don't loop to fetch the results. this is just cluttering up your code. just fetch the row as an array into a variable. speaking of (writing about) variables, what's wrong with an array variable? by creating discrete variables from each element of an array, all you are doing is spending time typing, fixing typo errors, and changing the code every time you change what gets SELECTEd by a query or reuse the code for a different query and also make the same changes to the code that uses the data. you should just fetch the data from the query into one php array variable, then use that array variable everywhere in the rest of your code. Keep It Simple. Programming is already a tedious task. Don't make more work for yourself by typing out line after line after line of code that you must keep editing any time something changes. next, what is this - if (!$sql) { ? the $sql variable is a php string that represents the sql query statement you have built. unless it's an empty string, it will never be false. i suspect this usage is trying to handle errors? if so, you need to use exceptions to handle errors. this will eliminate the need to write logic around every database statement that can fail. your main code will only have to deal with error free database statement execution. if you enable exceptions for the mysqli extension, any error will throw an exception. if you let php handle the uncaught exception, it will use the php error_reporting/display_errors/log_errors settings to determine what happens with the actual error information. for the mysqli extension, if you enable exceptions before you try to make the database connection, any connection error will also throw an exception. to enable exceptions for the mysqli extension, add the following before your connection code - $driver = new mysqli_driver(); $driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT; // MYSQLI_REPORT_ALL <-- w/index checking; w/o index checking --> MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT
  4. it requires nothing. it's just a dang example and this stuff is called software for a reason. if it doesn't do what you want, you can change it so that it does.
  5. you should use an array name for the form field, with the array index being a unique name if you need to identify which field a file was selected for. if you leave the index value out, you will get integer indexes, starting at 0. next, you MUST test if a file was successfully uploaded before you can reference any of the file information. if a file wasn't selected, the ['error'] element of the uploaded file information will be - the ['error'] element will be - UPLOAD_ERR_OK if the file was successfully uploaded. see example #3 at this link - http://php.net/manual/en/features.file-upload.post-method.php for how you can define the form field and loop over the file information.
  6. i recommend that you tell or show us what symptom or error you are getting that leads you to believe that something doesn't work. we are not sitting there with you and don't know what it is you are seeing in front of you. why are you using both the msyql and mysqli extensions in one script? do you even have a connection using the mysql extension? there would be php error's if you don't. do you have php's error_reporting set to E_ALL and display_errors set to ON so that php would help you by reporting and displaying all the errors it detects? your code should ALWAYS detect and handle errors with database statements, so that it doesn't try to run logic that's dependent on database statements working when they haven't and test to make sure that a query matched a row of data before using the data. why are you using a multi_query() statement for a single query? this opens the door for sql injection to run ANY type of query, especially since you are not doing anything to protect against sql injection. even if you currently trust the data source, someone at some point can get nefarious data onto the site you are getting your data from. you must ALWAYS protect against sql injection. btw - the php mysql extension has been removed from php for almost a year. you should not be using it at all at this point in time.
  7. your $_POST data isn't the problem. your code is most likely being executed. why you are getting a symptom that makes it appear like your code isn't being executed is due to either php's output_buffering (hides output from your code and php errors if there is a header() redirect) and/or php's error reporting/display_errors (php errors are not being reported/displayed) settings that are set on your server. you should be getting an error like this - you need to have the following set in the php.ini on your development system - error_reporting = E_ALL display_errors = ON output_buffering = OFF make sure these are set as shown, restart your web server to get any changes made to the php.ini to take effect, and confirm that the settings actually got changed by checking the values with a phpinfo() statement in a .php script. this will get your development system set up so that it will help you by showing any output from your code and from php errors. once you are getting that particular Notice: ... error, it should be easy to find and fix the problem in your code. next, you need to use prepared queries, with place-holders in the sql query statement for data values and use exceptions to handle database errors. this will actually simplify both your sql query and php program logic. unfortunately, the php mysqli extension is not the best choice. if you can, switch to use the php PDO extension. it is more consistent and easier to use than the mysqli extension.
  8. to cause all php detected errors to be reported and displayed, you need to set php's error_reporting to E_ALL (it should always be set to this) and set display_errors to ON. these should be set in the php.ini on your development system, so that ALL php errors will be reported (setting them in your code won't help with php syntax errors) and so that you don't need to remember to set them to get php to help you or forget to remove them form your code when you put it onto a live server (you should log all php errors on a live server, not display them to the visitor.) you still have some problems in the posted code - 1) htmlentities() is an OUTPUT function. it is used when you output data to the browser. it is not used on input data and it has nothing to do with data used with an sql query. 2) $mysqli. if you have switched to using the PDO extension, name the variable holding the instance of the PDO class something helpful like $pdo. 3) the first parameter in your bindParam() statement isn't correct. it's either the place-holder number (starting at 1), when using ? place-holders, or its the named place-holder. basic information like this can always be found in the php.net documentation. 4) your sql query is only SELECT'ing the username. therefor, $row['password'] won't exist. this would be throwing an undefined index error once you have php's error_reporting/display_errors set as suggested. 5) you also need to fetch the row and test if the query matched a row, all at the same time. the current code will throw php errors at the $row['password'] if the query doesn't match any row. you can do this - if($row = $query->fetch(PDO::FETCH_ASSOC)) { // the query matched a row, use password_verify() here... }
  9. you would start by going through the examples in the documentation. mpdf expects you to either capture the html you want to convert or to add each line of html as you are producing it. the easiest way of coding this would be to do it on the page where you are producing the html output, so that you don't need to duplicate the code that's responsible for searching the database and producing the html. you would also want to do it completely on the server-side, since accepting the html input from the browser would leave the application open to inserting 'any' content into the pdf document, not just the output from your report code.
  10. your form doesn't have any field named 'id', so, $_POST['id'] doesn't exist. this would be throwing php errors if you had php's error_reporting set to E_ALL and display_errors set to ON (preferably in the php.ini on your development system.) with no id value, the WHERE clause in the query is false, so the affected rows will always be zero.
  11. yes, but have you been looking at what your code is doing? you have added a completely separate sql query for the company name, instead of modifying the existing sql query so that it would match the data you want. in programming (and for any goal), you must define what you want the code to do, before you can write any code to do it. otherwise, you can end up spending an infinite amount of time working on something without accomplishing anything useful. just ask the programmer's who wrote this forum software. so, first define what you want to the WHERE clause in the sql query to be for the four possibilities of date range and company name selection - 1) no date range (empty date form fields) and no company name (the ALL default choice) - you want the query to match all data, i.e. a query with a WHERE clause without any terms in it for the date and company name. if these are the only things being used in the WHERE clause, you can leave the entire WHERE clause out of the query. 2) a date range and no company name - you want the query to match data - WHERE sl_iv.DOCDATE BETWEEN 'the_start_date' AND 'the_end_date' 3) no date range and a company name - you want the query to match data - WHERE COMPANYNAME = 'the_selected_company_name' 4) both a date range and a company name - you probably want the query to match data with both conditions - WHERE sl_iv.DOCDATE BETWEEN 'the_start_date' AND 'the_end_date' AND COMPANYNAME = 'the_selected_company_name' given that the original code has this - $queryCondition = "";, your task would be to build the correct WHERE clause in that variable, it's already being put into the sql query statement. a general purpose way of doing this, that will support any number of different terms/filters, is to add each term as an element to an array. you can then just implode() the array using the " AND " keyword to produce a WHERE clause containing all of the terms. this will work regardless of the number of terms you add to the array (provided you define an empty array first.) you also need to do something that i suggested at the top of this thread and use a get method form. you would also want to re-populate the form fields with any previous selection so that the form is 'sticky' and retains the values and you need to display the data from the query someplace beside inside the search form. lastly, you need to protect against sql special characters in the data from breaking the sql syntax (which is how sql injection is done) and causing errors. the best way of doing this is to use a prepared query with place-holders in the sql statement for data values, then supply the data when the query is executed. unfortunately, doing this for a dynamic sql statement, which what you have with different possible filters, isn't very straight-forward using the php mysqli extension. the php PDO extension is an all around better choice to use over the mysqli extension and you should switch to it if you can.
  12. what do you mean, 'you hope.' that's why you test code, to confirm if it produces the correct result. that would tell you if the code is correct or if you must go back and find what it is doing wrong.
  13. change this - $ques[] = $row['q_text']; to this - $ques[$row['question_id']] = $row['q_text'];
  14. when the submitted value is the ALL choice, you would leave the corresponding term out of the WHERE clause.
  15. it's not entirely clear, since there's no context or explanation in the attached spread-sheet, what the data is and which of it is even relevant. i'll venture some guesses - 1) The data is the result set/rows from an sql query? if so, if you store that data into an array in your code and use var_export() on that array, you can paste valid php code that we could use as test data. 2) Expr2 is the user/employee id and the Expr3 is the text answer you want to display? if so, here's a way of producing the output - 1) query for and retrieve the question id and question text, in the order that you want to display them (in case they are not to be displayed by the id ordering), separately from the main data retrieval query. store the question id/text into an array, using the question_id as the array index and the question text as the array value. you would leave out the question text in the main data retrieval query. 2) when you retrieve the main data, loop over it and pre-process/pivot it and store it into a multi-dimensional array. the first array dimension/index would be the employee_id. the second array dimension/index would be question_id - $data[employee_id][question_id][] = 'text answer to display'; // the last [] is to accommodate multiple replies to any question. 3) to produce the output, produce the table header by outputting the first column, then loop over the array from item #1 above. to produce the data section, loop over the pre-processed data. this will give you the employee_id and the array of replies for that employee id, with the reply array index being the question_id. loop over the array question id's, from item #1, to get the question_id's in the order that you are displaying the questions/replies. use the question_id to reference the correct reply for the current employee id. this will give you an array of replies for that question_id. loop over or implode() this array to output the text in each table cell. see the following simplified example - // some made up questions $q = array(); $q[1] = 'q1'; $q['2A'] = 'q2 a'; $q['2B'] = 'q2 b'; $q[3] = 'q3'; $q[4] = 'q4 mult'; // some made up data $data = array(); $data[1][1][] = 'e1 q1'; $data[1]['2A'][] = 'e1 q2 a'; $data[1]['2B'][] = 'e1 q2 b'; $data[1][3][] = 'e1 q3'; $data[1][4][] = 'e1 q4 1'; $data[1][4][] = 'e1 q4 2'; $data[2][1][] = 'e2 q1'; $data[2]['2A'][] = 'e2 q2 a'; $data[2]['2B'][] = 'e2 q2 b'; $data[2][3][] = 'e2 q3'; $data[2][4][] = 'e2 q4 1'; $data[2][4][] = 'e2 q4 2'; // produce table header echo "<table>\n"; echo "<tr><th>Employee ID</th>"; // loop over array of questions for the header foreach($q as $question) { echo "<th>$question</th>"; } echo "</tr>\n"; // produce table data foreach($data as $employee_id=>$replies) { // $replies is an array with the question_id as the array index echo "<tr><td>$employee_id</td>"; // loop over the question id's in the order they are being displayed foreach(array_keys($q) as $q_id) { // is there a reply for this quesiton_id if(isset($replies[$q_id])) { // there is a reply for this question // there can be multiple replies to a question - separate them with a <hr> in the table cell echo "<td>"; echo implode('<hr>',$replies[$q_id]); // note: this works correctly if there is a single reply, the result is just the single array element by itself echo "</td>"; } else { // no reply, output whatever you want for this case echo "<td>n/a</td>"; } } echo "</tr>\n"; } echo "</table>\n";
  16. we can only help you with problems in your code when you post your code. post your attempt with both filters in it. btw - you should be using a method='get' form for controlling what will be displaying on the page.
  17. you would need to add some php code to actually use the data from the COMPANYNAME select/option menu to add the correct term to the WHERE clause in the sql query statement. there's nothing in the code now.
  18. return, the point where the sql syntax error is occurring at is a reserved mysql (database server) keyword. are you sure the original query wasn't as follows, with back-ticks around the table name - "SELECT * FROM `return` WHERE nMemberID='$nMemberID' AND nInfoID='$nInfoID' ORDER BY nReturnID DESC"; next, just converting the mysql_ statements to mysqli equivalents is not enough. you must also safely supply data values to the sql query statement. if $nMemberID or $nInfoID are coming from external data or they could contain any sql special characters, they must either be properly escaped (see the mysqli real escape string function/method) or you need to use a prepared query with place-holders in the sql statement for the data values and then bind the actual data to the place-holders. a prepared query is actually the best choice since it is impossible for injected sql in the data to be treated as sql syntax, whereas using the escape string function must have the proper character encoding set for the database connection to match what the database tables are set up for.
  19. if you are just learning, start with one or two form fields and get the code to work so that you are learning and understanding how the code works. if you are doing this for real and you have more than about three form fields, you should NOT write out lines of code for each possible form field. you would instead dynamically process the form data, by defining the expected fields in a data structure (array or database table.) you would then loop over the defining data structure and dynamically reference the data, to first validate it, then dynamically produce the email message body, all without writing out line after line of code that only differs in the form field it corresponds to. next, any data you put into the email message body needs to be passed through htmlentities() so that any html/javascript/css in the submitted data won't be operated on by the receiving email client. any data you put into the email header must be validated that it is exactly and only an expected value. lastly, the email is not being sent FROM the email address that someone entered in the form. the email is being sent from the mail server at your web host. the From: email address should be a valid email address at your web host, so that the From: email address corresponds to the sending mail server and so that bounce messages from the receiving mail server have a place to go to.
  20. your loop is also looping forever (until the php time limit is reached), since the condition being tested by the loop is a fixed value. you don't even need a loop for what you are doing. your sql query should be trying to match one row, based on the username. if the username/one row is found, you would fetch that one row, then verify if the hashed version of the entered password matches the hashed password from when the user registered. see php's password_hash() and password_verify() functions for this.
  21. look at your code, between where you are executing the sql query and where you are looping over the data from that query.
  22. is the query failing (from error handling logic that you should always have in your code) or are the php statements failing (form having php's error_reporting/display_errors turned fully on in the php.ini on your development system)? if your original posted code was using the php mysqli extension, why are you now using the php mysql extension? the mysql extension has been removed from the php since the end of last year. you should also be using a prepared query (supported by the php mysqli and pdo extensions) to supply the data values to the sql query statement and this will also make executing the query inside of a loop slightly more efficient, since you will prepare the query only once before the start of the loop. also, if you are still asking questions in this thread, you should de-select the solved post so that people will see that the thread is not actually solved and will read it to see the new questions.
  23. what debugging have you done to find what the code IS doing? the code i posted is tested and submits and logs the data. aside from the note about the actual database statements not being used and therefore not being tested, the code works. is your data being being displayed? does your database table have an id column, that's named 'id'? what does the developer console in your browser show? is the 'log.txt' file being created on the server?
  24. so, i went through your code to figure out what it is doing, in order (pun intended) to figure out how to make this work. starting with your html markup - 1) the id='...' attribute you have for the column div's, should start with a letter, to make them valid. i choose to use id='COL_n', where n = 1, 2, ..., 6 2) the portlet div's needs an id attribute so that the serialize/toArray functions have something to use as data. i choose to use id='ID_n', where n is the job id auto-increment column value from your database table (if you don't have a job id, you need one.) 3) to allow the php code to dynamically produce the html, for the different status values 1-6, you need a way of mapping the status value to the display label - 'New' through 'To Be Invoiced'. i choose to use an array, and since you (probably) want to output each status section, regardless of if there is any data for it, you would loop over this array to produce the sections on the page, then loop over any data for each section. the html, from the <div style="clear:both;"></div> to the end of the page should look more like the following (note: data values that you output to the browser should be passed through htmlentities(). this is not in the example code and is left up to you as a programming exercise) - <?php // define status value to label mapping $status_map = array(); $status_map[1] = 'New'; $status_map[2] = 'Artwork Rec'; $status_map[3] = 'Approved & Ordered'; $status_map[4] = 'In Production'; $status_map[5] = 'Delivered'; $status_map[6] = 'To Be Invoiced'; // query for all the data you want, in the order that you want it $query = "SELECT * FROM jobs ORDER BY status ASC, job_title DESC"; $result = mysqli_query($con,$query); $data = array(); while($row = mysqli_fetch_assoc($result)) { $data[$row['status']][] = $row; // index/pivot the data using the status value - 1..6 } // note: i used some made-up data in the $data array at this point. the above query code should work, but is untested. // loop over the data and produce the output foreach($status_map as $key=>$label) { echo "<div class='column' id='COL_$key'>\n"; echo "<h3>$label</h3>\n"; if(isset($data[$key])) // is there any data from the database for this key/status { foreach($data[$key] as $row) { echo "<div class='portlet' id='ID_{$row['id']}'>\n"; echo "<div class='portlet-header'>{$row['job_title']}</div>\n"; echo "<div class='portlet-content'><a href='pdfs/{$row['pdf_link']}' target='_blank'>View PDF</a></div>\n"; echo "</div>\n"; } } echo "</div>\n"; } ?> </body> </html> next, the jquery you have that is using an id selector - "#portlet" should be removed since this exercise is operating on a class basis, not an id. also, you would not use the update : method, since this triggers for every column that gets updated. if you move something from one column to another, it triggers two times. you need to use the stop : method. see the following javascript/jquery that i came up with - <script> $(document).ready(function(){ $( ".column" ).sortable({ connectWith: ".column", handle: ".portlet-header", cancel: ".portlet-toggle", placeholder: "portlet-placeholder ui-corner-all", stop: function() { var dat = []; var i = 0; $(".column").each(function() { dat[i++] = [this.id,$(this).sortable("toArray")]; // this.id is the column id, the 2nd element are the job id's in that column }); $.ajax({ method: "POST", url: "save_order.php", data: { data: dat } }); } }); $( ".portlet" ) .addClass( "ui-widget ui-widget-content ui-helper-clearfix ui-corner-all" ) .find( ".portlet-header" ) .addClass( "ui-widget-header ui-corner-all" ) .prepend( "<span class='ui-icon ui-icon-minusthick portlet-toggle'></span>"); $( ".portlet-toggle" ).on( "click", function() { var icon = $( this ); icon.toggleClass( "ui-icon-minusthick ui-icon-plusthick" ); icon.closest( ".portlet" ).find( ".portlet-content" ).toggle(); }); }); </script> this will submit an array of data to the .php file, in $_POST['data']. see the following example code that extracts the column number and job id (if any), and logs the information to log.txt - <?php // for some reason, the portlet toArray 'adds' an empty element to the start of each array if(isset($_POST['data'])) { foreach($_POST['data'] as $arr) { //$arr[0] is the column id - COL_1, COL_2 (these are the status number 1-6 = New - To Be Invoiced) //$arr[1] is an array of the ids that are in the column - ID_1, ID_5 // get the status (column) number list($not_used,$status) = explode('_',$arr[0]); // get the id's in each status/column $arr[1] = array_filter($arr[1]); // remove empty elements if(empty($arr[1])) { // an empty status/column $str = "Status: $status, empty"; file_put_contents('log.txt',print_r($str,true)."\n",FILE_APPEND); } else { // non-empty status/column foreach($arr[1] as $element) { // get the id number list($not_used,$id) = explode('_',$element); $str = "Status: $status, Id: $id"; file_put_contents('log.txt',print_r($str,true)."\n",FILE_APPEND); } } } }
  25. I was going to ask if you would post a sample of your jobs data, in a php array format (see var_export()), so that we would have something to test with, but in looking at your code, you need to first write a single sql query that gets ALL the data you want in the order that you want it, rather than having 6 queries. then fetch all the data into an array. your code would then loop over that array to produce the output. at this point, you could use var_export() on that array to supply us with some data to test with.
×
×
  • 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.