-
Posts
5,536 -
Joined
-
Days Won
192
Everything posted by mac_gyver
-
Using Ajax GET Value in another form
mac_gyver replied to MartynLearnsPHP's topic in PHP Coding Help
your database design is storing same type of data, that only differs in its value, spread out in columns, which makes any code to use that data overly complicated. you are looping over all the row(s) the first query matches (is there even more than one row per adminid?), to get a value out of the correct column, just to use it in the second query to find the actual data. if your database is designed correctly, ALL that code would go-a-way and be replaced with a single JOINed query. i reviewed your two most recent threads. it turns out i provided help in both of them. this problem of writing out huge quantities of repetitive code, was in the way, before, of getting your code to do what you want, and it is still getting in the way, now, of producing code that works or debugging why the code isn't working. i would start by getting your database table(s) designed correctly. this will make writing the code and queries easy. -
most of your Arr column values are empty, and the ones that are not have a carriage-return \r appended to them and won't match what you are using as values in the php variable. you should remove the carriage-return from the database values.
-
your code actually works for me. the only way i could reproduce your implied errors (if you post the actual errors, they may contain information that may help) is if the connection is failing and your connection code isn't exiting when there is a connection error. the only way you can get the - Warning: mysqli_query(): Couldn't fetch mysqli in your_file on line 22 and Warning: mysqli_error(): Couldn't fetch mysqli in your_file on line 22 errors, is if $conn exists, but isn't a valid connection. ^^^ given the name of your include file, do you have two different files, one for the localhost and one for a live server, and your actual code is including the live server version and it doesn't contain either any connection error checking or it doesn't contain the exit; statement in the connection error logic?
-
here's one method - 1) take all the rows that have departure/arrival times between the earliest/latest departure/arrival time entered for the trip. 2) produce all possible combinations, taken 1 at a time, 2 at a time, .. up to the maximum number of segments you are willing to use in one trip (three would probably be a good maximum.) 3) throw out any routes that don't have an overall start and end point that matches the trip's start and end point and that don't have matching endings of one segment the same as the start of the next segment for all segments in the route. 4) for the remaining routes, calculate the layover time between each segment and the overall time of each route. 5) throw out any routes where any layover time between any segments is below a reasonable amount. 6) order the remaining routes by the number of segments, from least to most, and the overall time of each route, from least to most within the the same number of segments. you may in fact be able to do this in an sql query. producing all the possible combinations, taken 1 at a time, 2 at a time, and 3 at a time, would (likely, just proposing this as a possibility) involve doing a UNION between the single segments, the segments joined to themselves once, and the segments joined to themselves twice, with JOIN, WHERE, and HAVING conditions to only include rows in the result set that have the correct overall start and end points, matching segment-to-segment end/start points, and minimum layover times between segments.
-
edit: and you already have an existing thread for this question - http://forums.phpfreaks.com/topic/298576-session-expires-too-soon/ what exactly are you trying to accomplish by extending the session? by definition and design, sessions expire when the browser is (completely) closed or when the garbage collection runs and removes old session data (the default of which is just 1440 seconds old.) you can change these, but you must have a good reason for doing so and understand under what conditions it will work.
-
if you want to test if a value is (or is not) one of several choices, define the different choices as elements in an array and use in_array() or !in_array() to perform the test.
-
the current error - Couldn't fetch mysqli in... means that the connection variable doesn't contain a valid (procedural) database connection. either your database connection code is failing or your code is closing the connection at some point. does your database connection code have error checking logic in it and when there is a connection error, does your code take an appropriate action, to prevent the rest of the code from trying to use a connection that failed? the current error is a 'follow on' error. it's not where the problem is at. do you have php's error_reporting set to E_ALL. you should be getting more (types of) php error messages that would help pin down the cause of the problem.
-
Session data outside root directory not available
mac_gyver replied to tsangaris's topic in PHP Coding Help
are you changing the master php.ini (this would require owner/root access to the server) or a local php.ini (this would be the case if you only have an account on a shared web server), what is your php version, and is php running as a CGI/FastCGI application on the web server? the most likely reason is you have a case were you are using a local php.ini and it only affects the folder that it is in. try copying the php.ini to the ajax folder or put any files that you are going to be making http requests to in the public_html folder. you may want to see the following section of the php.net documentation for the different server/version configurations that determine how php finds the settings it uses - http://php.net/manual/en/configuration.php -
the problem's not in the actual code. it's in the environment where the code is running. the code suddenly didn't alter itself and start including/requiring the same class definition multiple times just because you moved where it is running. the code is probably trying to load same-name classes, but from different locations, and it or php cannot build or resolve the different locations correctly. some suggestions - 1) make sure that the php include_path setting is the same between the old and new system and that is whatever is suggested by magento. 2) the code may be trying to use set_include_path()/ini_set() statements to control where it's telling php to search for files, but those functions are either disabled on the new server or the path separator character being used is hard-coded and doesn't match the usage for the operating system (this would only be the case if you switched to/from a Windows/non-windows operating system.) 3) php has had a few bugs involving file paths for included/required files. check the php bug reports for the php version being used and the php change log for versions higher than your version, to see if this may be the case. 4) i saw references to caching/'compiled' code when doing a search for this general error. make sure that any sort of php bytecode caching/accelerator/APC files that came from the old system have been deleted. perhaps there's a bug in the cache 'hit' logic and the actual php file and the cached/'complied' php code are both being included/required.
-
i think (it's not entirely clear what you are asking or what problem you are having with your code) you are building up the entire content in a single variable and have a 'cart before the horse problem'. you should instead build up each different piece of the content in a separate variable, then combine the variables at the end or just output each separate variable in your html document at the appropriate place. build up the navigation in its own variable, so that all the logic that contributes to the navigation can add to just the navigation.
-
you would make use of the same type of page controller logic (the conditional tests) that you have in your main file, within each of the pages that have subpages, to control what they do for any subpage they contribute to the navigation and are responsible for processing. for your example of index.php?page=0101&subpage=0102, the main page controller in index.php will include/require 0101.php. the page controller within 0101.php would test for the existence of $_GET['subpage'] and run/include/require the appropriate code for the 0102 subpage action.
-
you are also making a PDO database connection, but using mysqli statements. you cannot mix the different database api's. you must use all PDO statements to match what you used when you made the database connection. edit: you are also making the database connection inside of a user written function, but are not passing the connection back to the calling code, so whatever variable you assign the connection to won't be available in any case. if you are going to wrap the connection code in a user written function, you need to return the connection to the calling code and assign it to a variable in the calling code's scope.
-
you wouldn't add the date string to the option value. you would convert the submitted option value to the date string right before you insert the data into your database table.
- 9 replies
-
- form
- input values
-
(and 3 more)
Tagged with:
-
you can build the $expiry_options array entries with whatever values you want. store the strtotime offsets - '12 hour', '24 hour' (add them manually), '1 week' up to 'n week' (add the weeks dynamically using a loop) in the array. i'm pretty sure that the 's' is optional in the strtotime, so build the values that you need for display purposes, and the strtotime() should work. since the permitted values are now in an array, to validate that the submitted value is one of the permitted ones, before using it in your code, you can just use in_array(). see this code - <select name="expiry_date"> <?php $num_weeks = 10; // define the number of weeks you want in the option list // produce the data for the option list $expiry_options = array(); $expiry_options[] = '12 Hours'; // manually add the two hours entries... $expiry_options[] = '24 Hours'; foreach(range(1,$num_weeks) as $week_no){ // add the week entries $plural = $week_no > 1 ? 's' : ''; $expiry_options[] = "$week_no Week{$plural}"; } ?> <option value="0">Expires In</option> <?php // output the option list foreach($expiry_options as $arr){ $sel = !empty($_POST['expiry_date']) && $_POST['expiry_date'] == $arr ? 'selected' : ''; echo "<option value='$arr' $sel>$arr</option>\n"; } ?> </select>
- 9 replies
-
- form
- input values
-
(and 3 more)
Tagged with:
-
here's a tip for the expire week number option menu. you should pass the minimum of data through the form, because you must validate all form data. change the code to submit just he the week number as the value, then using the submitted week number, calculate the actual expire date in the form processing code code.
- 9 replies
-
- form
- input values
-
(and 3 more)
Tagged with:
-
cannot help with code errors without seeing the code that's in error. however, this is all the more reason to use a data driven design to dynamically produce the output based on a definition, rather than write out all the repetitive lines of code that only differ in the data values they contain. see the following (untested, my contain typos) example - <select name="expiry_date"> <?php $num_weeks = 10; // define the number of weeks you want in the option list // produce the data for the option list $expiry_options = array(); foreach(range(1,$num_weeks) as $week_no){ $plural = $week_no > 1 ? 's' : ''; $expiry_options[] = array(date("Y-m-d H:i:s", strtotime("+ $week_no week")),"$week_no Week{$plural}"); } ?> <option value="0">Expires In</option> <?php // output the option list foreach($expiry_options as $arr){ $sel = !empty($_POST['expiry_date']) && $_POST['expiry_date'] == $arr[0] ? 'selected' : ''; echo "<option value='$arr[0]' $sel>$arr[1]</option>\n"; } ?> </select> for repopulating the dynamic form fields, see the following example. the conversion to the template method is up to you. please see the use of the $data array in the code (the raw $_POST data should in general not be altered, and this internal $data array would have any sort of trimming/filtering/validation done on the values in it) - <div id="options-parent"> <h2>Add Options</h2> <button class="add_field_button">Add More Fields</button> <?php // assume that there's a trimmed/filtered/validated copy of any previously submitted post data is in $data $data = $_POST; // just make a copy of the $_POST data for this example // if $data is empty(), output one set of form fields. there are no value='...' attribute values in this case // if $data is not empty(), output a set of form fields, with values, for each set of data $count = isset($data['option_quantity']) ? count($data['option_quantity']) : 1; // determine loop count for($x = 0; $x < $count; $x++) { $quantity = isset($data['option_quantity'][$x]) ? $data['option_quantity'][$x] : ''; $retail_price = isset($data['option_retail_price'][$x]) ? $data['option_retail_price'][$x] : ''; $discount_price = isset($data['option_discount_price'][$x]) ? $data['option_discount_price'][$x] : ''; ?> <div class="options-child-row"> <div class="option-float"> <label>Quantity</label> <input type="number" name="option_quantity[]" multiple min="1" max="1000000" step="1" value="<?php echo $quantity; ?>" /> </div> <div class="option-float"> <label>Retail Price</label> <input type="number" name="option_retail_price[]" multiple min="5" max="1000000" step="1" value="<?php echo $retail_price; ?>" /> </div> <div class="option-float"> <label>Discount Price</label> <input type="number" name="option_discount_price[]" multiple min="1" max="1000000" step="1" value="<?php echo $discount_price; ?>" /> </div> </div> <?php } ?> </div> <script> $(document).ready(function() { var max_fields = 20 - <?php echo $count - 1; ?>; //maximum input boxes allowed var wrapper = $("#options-parent"); //Fields wrapper var add_button = $(".add_field_button"); //Add button ID var x = 1; //initlal text box count $(add_button).click(function(e){ //on add input button click e.preventDefault(); if(x < max_fields){ //max input box allowed x++; //text box increment $(wrapper).append( '<div class="options-child-row">'+ '<div class="option-float">'+ '<label>Quantity</label>'+ '<input type="number" name="option_quantity[]" min="1" max="1000000" step="10" value="" />'+ '</div>'+ '<div class="option-float">'+ '<label>Retail Price</label>'+ '<input type="number" name="option_retail_price[]" min="1" max="1000000" step="10" value="" />'+ '</div>'+ '<div class="option-float">'+ '<label>Discount Price</label>'+ '<input type="number" name="option_discount_price[]" min="1" max="1000000" step="10" value="" />'+ '</div>'+ '</div>' ); //add input box } }); $(wrapper).on("click",".remove_field", function(e){ //user click on remove text e.preventDefault(); $(this).parent('.options-child-row').remove(); x--; }) }); </script>
- 9 replies
-
- form
- input values
-
(and 3 more)
Tagged with:
-
for the part one problem, you are testing the wrong variable in the second <option.... code. you have - if($_POST['expiry_date'] == $expiry_1 for both <options. the second one should be $expiry_2. if you dynamically produce your option lists, from a data definition (usually an array or data stored in a database table), you can avoid errors like this, since the general purpose php code that makes use of the data definition will not make copy/paste errors or typos. for the part two problem, when you display the form, your php code needs to produce enough form fields to hold the data you have on the server-side to to redisplay. initially, with no data to display, output the the single set of form fields you now have, which wouldn't have anything to put into the value='....' attributes, but any time there is existing submitted form data, loop over the data, outputting the set of form fields with the correct data in the value='....' attributes. edit: for the html markup in your jquery code to dynamically add form fields, see the following link for a 'template' method so that you don't have to repeat the markup in your html and in the javascirpt - http://forums.phpfreaks.com/topic/298777-dynamic-for-additions/?hl=%2Btemplate&do=findComment&comment=1524053
- 9 replies
-
- form
- input values
-
(and 3 more)
Tagged with:
-
your original text file probably contains an extra new-line/blank-line at the end. you need to validate the data you are using to make sure the data is an expected value, type, format, ... i would trim() the $r value, which will remove white-space characters before/after any data, and only write the line to the output file if the trimmed data is not an empty string. note: you have a new-line on the end of each real line that you probably don't want to remove or if you do remove it, you will want to add back to the output when you write the line to the output file.
-
no one is questioning his knowledge. is anyone really reading WHAT they or anyone else is writing? you know, it takes me a long time to compose what i write. it probably took me close to an hour to write post #6 above. i review and revise what i write several times to get the statements to covey the information that i am trying to get across and i tend to be more detailed than others in my explanations, for which i get a lot of thanks and likes. i don't take the time to write out huge verbose replies because i want the typing practice, i write them to share relevant knowledge. it's too bad that others would use their time to point out 'problems' with something that someone has written, without having a clear understanding what they are replying to first.
-
Don't confuse A writing style with what is being written. do you think the Angus Macgyver character would be concerned with capital letters on what he wrote out to solve a problem or would he be concerned with the result? the methods i listed are a summery of the form processing practices that have been stated in countless replies on the forum. and again, the following has nothing to do with what was stated, and can only be reasonably accounted for by a translation problem - no one stated that how the data gets submitted is an issue and this has nothing to do with any of the suggestions. the OP did apparently get useful information out of the post since he gave it a like.
-
since English is probably not your first language, you have been given some slack in the 'off' replies you give. however, no one stated - what was stated is, you don't need and shouldn't use an isset() statement to "individually test if they (the type of fields that were specifically mentioned) exist." when you translated that to your native language, you apparently got - 'don't validate each form field' and didn't get anything else that was stated or the context in which it was stated. no one stated to not validate each expected form field. in fact, if you have read or correctly translated what was stated, someone mentioned and gave a method for -
-
since you are validating a post method form, your form processing code should first check, once, that a form has been submitted, and have all the form processing code inside of a single conditional statement. if your page will process more than one form, you would have a test for each possible form (test for a field name or a value that will always exist when the form is submitted and uniquely identifies the form) and only run the correct form processing code that matches the form that was submitted. once you have tested that a/the-correct form has been submitted, all the text, textarea, password, and select form fields will exist. it's not necessary to individually test if they exist. in fact individually using an isset() for each form field tends to hide typo errors in the field names between the form and the form processing code. you would want to get and display/log (development/live server) php errors in this case or in the case where someone is submitting their own form data and is not submitting all the expected form fields. after you have determined that a/the-correct form has been submitted, only check-box and radio-button may or many not exist, depending on if they are checked/selected and would need to use isset() statements within the form processing code to detect if they exist or not. since you will want to set up a unique validation error message for each different thing that can be wrong with the data for a form field and you will want a way to detect if there are validation errors at any point in your code, you can kill two birds with one stone by using an array to hold the validation error messages. you would add elements to the array, using the form field name as the array index and the message as the value. this same array can serve as the flag that there are validation errors by either testing that the whole array is empty() or not or you can test if individual elements/errors are set or not, using the field name index value with an isset() statement.
-
the answer is still to use explode (unless you are getting this from a file, in which case file() would be the best first step.). you explode first on the line-ending-character (varies depending on where you are getting the data from, can be \r, \n, \r\n, or \n\r), to give an array of individual lines. you then explode each line on the comma character. you would want to trim() the final data before you try to use it in case there's some white-space characters as part of the data.
-
i recommend that you read the thread i linked to in post #2 above. the OP in that thread is doing just about the same thing, with PDO, and is getting how you would dynamically build the sql query statement and dynamically bind any input parameters.