Jump to content

mac_gyver

Staff Alumni
  • Posts

    5,450
  • Joined

  • Days Won

    175

Everything posted by mac_gyver

  1. @hansford, it's usually better to state what's wrong, rather than to post 'fixed' code. by stating what the problem is, some learning will take place, rather than mimicking. the OP may not even realize everything you changed in the code.
  2. your query statements are definitely producing an error, due to the LIMIT clause being supplied string values. do you have PDO's error mode set to exception (you have to specifically do this after you make the database connection) so that any of the errors from the PDO statements will throw an exception (that you can catch in one place to handle db errors) to let you know they failed? at a minimum, the two bound parameters for the LIMIT ?, ? must use PDO::PARAM_INT for the types and you cannot pass these in the ->execute() method (it treats all values as strings) and since you are binding the parameters, you shouldn't be passing anything in the ->execute() method. also, if the two values for the LIMIT x,y are entirely produced within your code, there's no need to bind them into the query, just put them directly in the query statement.
  3. your database query would retrieve the data you want, in the order that you want it. you would store the data from the query into an array, then extract the heading information from the array of data. then just loop over the data and output it the way you want it. see the following post for an example of doing this - http://forums.phpfreaks.com/topic/298003-data-display-in-wrong-column/?do=findComment&comment=1520525
  4. if this portion of your ajax is basically just moving functionality from the server to the browser, for when javascript is enabled, you can practice DRY (Don't Repeat Yourself) programming. you already have server-side code that's producing the values, html, .... instead of outputting those values/html when the page is requested, output them as 'generic' json encoded data in the ajax response. see the following example. code on your main page - <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Generic ajax - use server output to dynamically specify and populate elements in the dom without writing out code for each different thing</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $.ajax({ url: 'api.php', data: "", //optional data to pass to api.php dataType: 'json', success: function(data) { var vals = data.val; // text and textarea values if(vals){ $.each(vals, function(key, value){ $(key).val(value); }); } var htmls = data.html; //inner html if(htmls){ $.each(htmls, function(key, value){ $(key).html(value); }); } var styls = data.styles; //css class name if(styls){ $.each(styls, function(key, value){ $(key).attr("class", value); }); } var sels = data.sel; // select/option - selected if(sels){ $.each(sels, function(key, value){ var v = value != '' ? true : false; $(key).prop("selected", v); }); } var checks = data.che; // radio and checkbox - checked if(checks){ $.each(checks, function(key, value){ var v = value != '' ? true : false; $(key).prop("checked", v); }); } } }); }); }); </script> </head> <body> <form method='post' action='your_formaction.php'> <div id='seats'></div> <input type='submit'> </form> <button>DEMO button to Get JSON data. You can also just automatically load the data when the page is ready in the browser.</button> </body> </html> example api.php code - <?php // the first array index is the 'type' that corresponds to the val, html, styles, ... in the javascript code // the second array index is the jquery selector (typically an id selector '#id') that will be used by the javascript code // output sample data - // html example $number_value = 3; $content = "<select name='seat'>"; foreach(range(1,$number_value) as $value){ $content .= "<option value='$value'>$value</option>\n"; } $content .= "</select>"; $array['html']['#seats'] = $content; // val/value example $array['val']['input[name="course_number"]'] = $course_number; // from wherever you are getting the course number echo json_encode($array); this method can replace your existing .val(), .html() usage. i have included one .val() example that should work with your existing page.
  5. this is the whole point of variables and control structures in programming. you can set a variable to any value, from wherever you want, and use control structures, such as a loop, to test/use the value that's in the variable to determine what to do. foreach(range(1,$number_value) as $value){ echo "<option value='$value'>$value</option>\n"; }
  6. your form should submit the id and you should store the id in the second table to relate the row in the second table back to the source information in the first table. information like the sitename/username should only exist once, in the source table where it is defined/stored.
  7. here's an example - <?php session_start(); // post method form processing code if($_SERVER["REQUEST_METHOD"] == "POST") { // load if(isset($_POST['load'])){ // process the groupid input, for brevity, just save it in the session variable // you would normally validate/cast it as a positive integer and detect if one of the choices has been selected $_SESSION['groupid'] = $_POST['groupid']; // do whatever processing you want when a groupid has been selected } // dump if(isset($_POST['dump'])){ // do whatever processing you want for this condition // clear the groupid unset($_SESSION['groupid']); } // any other post method form processing code.... } ?> <?php // at the part of your code where you are producing the output for the form - ?> <form method='post'> <select name="groupid" <?php echo isset($_SESSION['groupid']) ? 'disabled':''; ?>> <option value='0'>GROUP</option> <?php foreach(range(1,10) as $group){ // you should be retrieving the group numbers from wherever you have the group information stored, not hard-coded in the code $sel = isset($_POST['groupid']) && $_POST['groupid'] == $group ? 'selected' : ''; echo "<option value='$group' $sel>$group</option>\n"; } ?> </select> <?php if(!isset($_SESSION['groupid'])){ // no groupid selected echo "<input type='submit' name='load' value='Load'>"; } else { // a groupid is selected, output the stuff for when there is a groupid echo "<input type='submit' name='dump' value='Dump'>"; // whatever else you want to output } ?> </form> if would help you greatly in creating the program logic that does what you want if you organize your code with the form processing code grouped together and the code that produces the output on your page grouped together.
  8. do the disabling of the groupid select/option menu in your php code, rather than using javascript. when you submit the groupid select/option menu and redisplay the page, use the fact that you now have a non-default/non-zero groupid value to cause the disabled attribute to be output in that select/option menu. i would also only output the 'dump' button and any other form controls that are dependent on having a valid groupid, if you actually have a non-default/non-zero groupid value. submitting the 'dump' button would clean up any data and clear the fact that you have a groupid selected. this would cause the page to be displayed without the disabled attributed in the groupid select/option menu and would prevent the output of the 'dump' button and the rest of the form controls.
  9. disabled form controls are not submitted with the form data. so, i'm guessing that is what is causing the rest of the code, that you didn't show us, from working. how is this second select/option menu being requested/produced? why are you choosing to disable the first one? would making it read-only accomplish your goal, as read-only form controls are submitted with form data.
  10. no it's not that simple. you would do this like your bank or credit-card company does it, by storing a record for each transaction that lists the date, amount (credits are +amounts, debits are -amounts), and information about the transaction (confirmation number for payments, name/reason for each debit). why would you have any code in place that would allow a user to directly affect these amounts? the user should only have 'permission', through your code, to display the amounts and cause events to occur that enter a transaction for that user. for credit (+) transactions, you would verify with the payment gateway that the user made a payment before entering a row in the database table with the amount. for debit (-) transactions, you would only enter those if you know who the user is and if this is dealing in real money, you would re-verify who the user is by having him/her re-enter their username/password.
  11. GROUP BY in the query doesn't do that. it consolidates the rows having the same group value into a single row. it's primarily used when you want to use aggregate functions (sum, count, ...) on the data in the group. to do what you are asking, you would order (use ORDER BY topic) the rows in the result set to get the rows for the same topic together, then when you are outputting the data, each time the topic value changes you would close out the previous topic section and start a new topic section.
  12. if this is a 'contact us' form and you always want to send to a specific email address, use one of the phpmail classes (phpmailer, switftmailer) and use smtp authentication against your receiving email account and send the emails directly to your remote email account. this will make your php script 'look' like an authenticated/logged-in email client connecting to your email account and eliminate the sending mail server at your web host from the process. you can also use this method to send emails through your remote email account, with whatever quantity restrictions your mail provider imposes. if you do need to send through the mail server at your web hosting, i would start by finding out from them if your emails are actually being sent. just because the mail() function is returning a true value, doesn't mean that the mail server has sent or has any intention of sending the email. some mail servers are configured to return a true value/no errors regardless of what happens to the email so as to not allow hackers to use the returned errors to find mail box names... next, the BT email server may be doing a more thorough check of the DNS records at your web hosting than what gmail is doing. the ONLY information a receiving mail server gets with any email is the ip address of the server/device the email is being sent from (from the tcp/ip data packets) and information in the email, i.e. the from address. the receiving mail server will use these two pieces of information to try and validate that the email is actually coming from where it says it is by checking the DNS records at the sending ip address and at the domain in the form address. perhaps the BT mail server is looking for a secondary piece of information that gmail isn't and something is either missing (missing values usually aren't a problem) or is set incorrectly (incorrect values will get an email discarded.) to help resolve this, there is usually a 'postmaster' web page at the major email hosts that will tell you what is required to successfully send an email to their server. there is usually a contact email where you can either find if a sending mail server has been specifically blacklisted at that email host or to request that a sending mail server be white-listed. also, you can use the various tools at a site like dnsstuff.com to check for and check for errors in the DNS records for your domain and the mail server at your web host.
  13. i'll try to answer some of the points from your last post above. sorry ahead of time for the bluntness that may ensue, because many of these things are basic/prerequisites that you should have, and may have, been shown before you were expected to use them - that's the syntax for a foreach() loop. reading the php.net documentation up to the control structure section and then reading the specific page for a foreach() loop would have given you the information you need to understand what the syntax means. no guessing is required. programming languages are one of the most heavily documented subjects on the planet because it's impossible to use them without having a good language reference. unfortunately, just being shown something and told what to type in a situation doesn't teach the meaning of it. that's mimicking/parroting. learning something technical like a programming (or a linguistic) language (which is not the same as learning how to program) is a process where you define a part of the language, then use it in an example to reinforce and internalize the meaning. you then build on what has been learned before. for example, if you haven't learned the general syntax for a php statement (what terminates a statement), learned the syntax for php strings, learned the syntax for php variables, learned the syntax for an assignment statement, and learned the definition for an echo statement, you won't know the actual meaning of every character in the following - $some_variable = 'hello'; echo $some_variable; that would let you generalize and use those same elements when writing your own code. since a foreach loop operates on an array of data (or something that's Traversable, like a PDOStatement object), you would need to get the result from the query into an array. the clearest way (that also separates the database statements from your presentation code ), would be to use the pdo ->fetchAll() method. you can find information about the pdo class in the php.net documentation. since your code is dependent on the pdo class, reading just about all of the pdo documentation at php.net would be to your advantage. the data from a post method form will be available in the php $_POST array. the form field name='some_name' attributes determine the $_POST array index names - $_POST['some_name]. at this point, the submitted data is in a php variable that you can use any way you want in your code, such as binding it with a place-holder in a prepared sql query statement. this is real basic/fundamental php information that there are probably 5,000,000 examples posted on the web to find and examine. again, the documentation for the php statements you are using defines what it all means. you don't have to guess, just make use of the documentation to learn the basics of the php language. that is the creative part of programming, which is different form learning the php language itself. see my post above about defining, then breaking the problem down into the steps that accomplish the task, to help with how to put statements together in a meaningful way.
  14. in addition to needing a basic understanding of the php language before you try to use it to do anything, the biggest thing that helps with programming is to define what you are trying to do, before you try to write the code for it. trying to write code or modify existing code (which would also apply to code snippets you find or are given) without a clear definition of what end result you are trying to produce, just wastes a lot of time. so, what are you trying to do, 1) a form with inputs for city/state, and 2) form processing code that takes the submitted city/state values, retrieves the matching information, if any, from the database table, and displays the results in a particular format. you would then break down each of those main tasks into smaller and smaller steps, until you can finally write the code that accomplishes each step. for the form processing code, those steps would be - 1) create a database connection, handling any connection errors, and as has been recommend, set the error mode to exceptions so that all the rest of the database statements will throw exceptions that you can catch and handle in one place. 2) check that a form was submitted - you were shown/given a method='post' form. however, this form is controlling what will be displayed. it should use method='get'. post method forms are for changing data on the server. 3) condition/filter/validate the submitted form data. 4) if there were no validation errors, use the submitted data in a database query. 4.1) form and run the sql statement for a SELECT query with a WHERE clause that will use the submitted city/state values, using place-holders for the input parameters. 4.2) prepare the query. 4.3) bind the input parameters to the place-holders in the sql statement. 4.4) execute the query. 5) retrieve the data that the query matched, if any. since your assignment mentions using a foreach() loop, the intent would be to fetch all the rows that the query matched into an array, then use the foreach() loop to loop over that array. 6) output the data, if any, in that particular format. if there is no matching data, output a message stating so. this list of steps that you have defined will become comments in the code that you will write. give me a couple of minutes and i may (no promises) post an example using pdo to do this.
  15. if you are not even sure that a $ starts a variable (programming is not about guessing or assuming, it is an exact science), you need to start with the basics. i would recommend that you read (so that you can internalize the information) at least the Getting Started and Language Reference (which covers variable naming) sections of the php.net documentation. php.net supplies the documentation in several forms, both on-line and down-loadable. i find the down-loadable .chm file particularly useful since it has a search-able index and in the cases where the index doesn't find what you want, you can use the search tab. next, if you have a question to post on the forum, limit yourself to just the relevant verbiage. we can only give an answer when we know of a specific question. just telling us you don't know what something means isn't specific enough. you must tell us what exactly you don't understand, because we are not providing free tutoring services. we are providing direction and sharing programming knowledge.
  16. you would loop over - $myArray['query']['results']['quote'] and then reference the ['close'] element. the following (untested) should work - foreach($myArray['query']['results']['quote'] as $arr){ echo $arr['Close']; } if you want to display a day number/counter, initialize a php variable before the start of the loop, echo and increment it inside the loop to provide the day number.
  17. you should have been getting php errors from your code. having php's errors turned on is critical for debugging problems. if you still are not seeing any php errors, after deliberately introducing problems into your code, the settings for error_reporting/display_errors may be commented out or are using invalid (false values) settings. if you need help with the error settings, post the actual lines for them from your php.ini file.
  18. switching to a different database api requires that you learn enough about the statements you are using, so that you can apply them to your own code. the PDO database api is the best and most consistent choice as a replacement for the mysql_ statements. the best place to start learning about any of the php statements is the php.net documentation. there are usually workable code examples in the documentation. next, your database table design is bad. databases are not spread-sheets. laying out a table with a sequence of name/numbered columns makes for a lot of extra code and queries to create, find/display, update, or delete any of the data. there should be one row for each separate data item in a database table.
  19. there's no guarantee of what php.ini php is using. to find the php.ini that php is using, create a .php script file with the code - <?php phpinfo(); ?> in it and browse to the url of the file. the Loaded Configuration File line in the output is the php.ini that php is using.
  20. 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? the most likely problem is that the mysql_ extension is not installed/enabled in your php configuration. next, the mysql_ functions are obsolete and will be removed from php soon. you should be using the PDO (the best option as it has a more consistent interface) or mysqli_ database functions in your php code. the php version you are using will be throwing a php error at the mysql_connect() statement to alert you about this. lastly, the mysql_ functions don't have an OOP style interface, so there is no ->connect_error properly at all to test. this in itself should be throwing a php error.
  21. there are three (or more) possibilities - 1) you actually have two rows in your database table (did you refresh the display of the information to make sure?) 2) the code you have shown us is inside of a loop of some kind. 3) you have managed to store that string of data into two of the database fields, with the rest of the fields empty.
  22. for debugging purposes, you can make a test page with a form that submits some dummy post data to your ipn.php script so that you can SEE what your script is doing. you would also temporarily set php's error_reporting to E_ALL and display_errors to ON in your ipn.php code. doing this will run your code and let you see any php errors (i suspect you may have a permission problem with the log file) and let you see if the curl statements are producing errors. this will at least get you to the point of having an "INVALID" response back from paypal and getting the error logging to work, since the dummy post data won't match a real transaction. you can also use this test method and 'fake' a "VERIFIED" response in your logic to let you test and debug your database code.
  23. that's more than a couple of errors. a couple of errors would be 2 - did you look at and try to find why your code is producing any of those errors? and given that cyberRobot told you the reason for one of them, why didn't you at least fix that one? with your 60 posts and 2+ months on this forum, you should be at the point, after just a couple (2) or a few (3-4) weeks, of being able to find and fix simple things like undefined variables and undefined indexes in posted form data. in one of your previous threads, someone suggested a code layout for your page. this was primarily to add origination to your code, but it was also to help make your code foolproof, so that you won't have undefined variables that are dependent on either a query retrieving the variables or a form submission supplying the same variables. there was in fact a comment in code in that thread that read -
  24. when paypal makes a http request to your ipn php page, the only input will be the $_POST data. there will be no $_SESSION data, because the request isn't coming from the visitor. your ipn script must also go through a data verification process with the paypal server to insure that the ipn request actually came from paypal. i recommend that you research the ipn documentation and search the web for existing ipn php scripts to see how they are coded.
  25. easier version - $file = 'formdata.log'; $data = date('Y-m-d H:i:s')." Agent:$_SERVER['HTTP_USER_AGENT']\nPost:".print_r($_POST,true)."\n"; file_put_contents($file,$data,FILE_APPEND);
×
×
  • 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.