-
Posts
5,450 -
Joined
-
Days Won
175
Everything posted by mac_gyver
-
you can display the next probable id, but your code or the visitor cannot use this value, because if more than one instance of your form/page is being displayed at the same time, the value being shown will be same on all the instances of the form/page, but the actual value that gets inserted by the database will be different. so, the question is, why do you want to do this as it will just confuse the visitor with a piece of information that he doesn't need to know at the time the form is being displayed, if at all?
-
you are passing a string and an array containing a quantity into your function. that doesn't make any sense from a definition standpoint. these should just be two values. there's no point in the quantity being an element in an array. wouldn't you just pass in the book-name/book-id and a quantity? next, why are you storing the quantity in each row? if you are making a row for each book (30 rows for your stated example, 12 rows for your apparent test data), storing the quantity in each row isn't relevant. what data do you want to store in each row? finally, for what purpose are you doing this? if this if for an inventory system, you would want a single row holding the quantity, not a separate row for each copy of a book.
-
the browser should just be displaying the current state. the control over what happens should be by the server-side code, regardless of how many times the current state is requested/displayed. what exactly is occurring when a page gets requested/refreshed that is causing a problem? are you using post method forms to submit data, with one-use-tokens to prevent double-submission, followed by a header() redirect to the exact same url that the form submitted to, to cause a get request to display the current state?
-
the total quantity is a derived value. you should not maintain a value for it, but calculate it when needed. your cart appears to be storing the quantity of each item as the value for each array entry (and if it's not, that is how you should be storing the quantity for each item.) you can just use array_sum($_SESSION['cart']) to get a total count of things in the cart. using count($_SESSION['cart']) would tell you how many different items are in the cart. your class method doesn't use/need an input parameter. why are you defining and calling it with one?
-
based on what i learned about the code, i finally figured out what the OP is referring to in this thread. this is the reason why you need to write applications without it taking a wall of repetitive code and why you need to put comments in your code to document what it is doing, so that anyone looking at the code can figure it out. the select/option menu in question is letting the visitor select among the different costs, with the value='....' attribute being the actual cost. the missing $chosencost_1 variable is actually present, after the registration form has been submitted, because the form code is being included back into the Check_Registration_Form11.php page, which is where the $chosencost_1 variable is at. the reason you are likely getting a 1 for a cost value, is because whoever/a-bot-script that is submitting the form, has figured out that you are using the submitted cost, so they are trying to get a $1 bargain on a course. this code is completely flawed in how it is operating. the most direct fix would be to do the following - 1) only the course id, the value in $recid on the Course_Details page, should be submitted between steps (your course registration data structure should actually only store the course id, student id (the unique information about a student should be stored in a user/student table, not in the course registration table), and any unique information about each registration, but i doubt you are going to change your data to do it this way.) 2) any page that needs to use or display the course data should use the submitted $recid value (after validating it) to query for the course information on that page. 3) any cost section should use a type value that identifies a choice among the available different costs, not the actual cost value. the actual cost the code uses should take the submitted type and get the actual cost from wherever it is stored. you should also validate that the type that was submitted is available/valid for the user, using whatever rules you have. which begs the question - why is the user even picking what price he gets? shouldn't that be an administrative choice, not a user accessible choice? 4) you should pass a minimum of information between pages in the form and any information you do pass through a form must be validated at each step. you may want to consider using a session variable to hold an array of the validated data so that you don't need to re-validate the data at each step.
-
@benanamen, it's against the forum rules to solicit work, except in the freelancing/services-offered forum section.
-
went through the code trying to figure out the program flow. here's some notes - Course_Details11.php - this page receives an id as an input, uses that id to retrieve and display the information for the course the id corresponds to. if the visitor chooses to register for the course, about 40 pieces of course information are passed to the next page as get parameters in the form's action='....' attribute. the only post data is the submit button. Registration_Form11.php - this page copies/validates/casts the data from the course_details page (18-19 items out of the 40 that are put into the url), puts this data (~11 items) into hidden form fields (without any html encoding, which may be why some form values don't work, if the values/upstream-values contain any quotes, breaking the html syntax), displays the user form fields, with any validation errors (see the check_registration_form notes next), and submits to the Check_Registration_Form11.php page. Check_Registration_Form11.php - this page copies/validates/casts ALL (hopefully, didn't bother to count the hard-coded line after line of code) the data from the registration form page. if there are validation errors, it includes the registration form page (which is a complete html document itself, giving two nested documents at this point) so that the form and any errors can be displayed. since there is no specific form processing conditional statement in any of this code, doing the include, causes all form processing/form display logic in the registration page to run again. if there are no validation errors, some/all of the data is saved in a 'Online_Activity' database/table, but this isn't used by the code to save on passing data to the next step. the user data is displayed as a conformation check and ALL the $_POST data (this time with html encoding, though not with all quote types encoded, which is amazing because the display section is using html encoding with all quote types encoded) is put into hidden form fields for the next page. to correct any data, you must use the browser's back button. this page submits to the process registration page. Process_Registration11.php - this page validates JUST the original course_details data (19 items). it doesn't validate the user entered data. it finally saves the data to a 'Web Registration Information' database/table. then sends some emails, and updates some more database information. @ the OP, if this is something that is actually being used for registration at a school, and it is beyond your abilities to find and fix the problem among the wall of 6800+ lines of uncommented code, you should expect to hire someone to fix this for you, rather than to get free programming services on a programming help forum.
-
the most likely reason for the random failures of the code is probably due to different browsers handling markup errors differently. the html in the pages being produced needs to be valid to have a chance at working the same in all browsers/browser-versions. what a mess. this code is long over due to be written from scratch, using general, data driven, DRY, programming. there's a ton of hard-coding (i even saw a 2015 year being used in a path that would need to be manually changed each new year, and i'm guessing this is what the 11 in the file names are there for, to distinguish different hard-coded instances of the site) and repetition of form/form-processing code between main and included files. of the 6800+ lines present in the .zip, there's probably only about 2000 lines needed to do this task.
-
see the following example code that shows several of these suggestions - <?php include "dbinfo.php"; // do this once, since both your form and form processing code needs a db connection // the defined constants DB_NAME, DB_USER, DB_PASS, and DB_HOST would be defined in your dbinfo.php file $pdo = new pdo("mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=utf8",DB_USER,DB_PASS); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); // form processing code - if(isset($_POST['update'])) { echo '<pre>',print_r($_POST,true),'</pre>'; // examine what the submitted form data is // i'll leave it up to you to work on the form processing code } // members table - id, member (i'm going to assume this is a something like a username or should it be the member's/user's firstname/lastname) // divisions table - id, name, image // member_div table - member_id, division_id $id=$_GET['id']; // the member to show the data for // one JOIN'ed query that gets the data you want in the order that you want it $query = " SELECT d.id div_id, d.name div_name, d.image div_image, m.username, m.id user_id, md.member_id IS NOT NULL AS checked FROM divisions d JOIN members m LEFT JOIN member_div md ON d.id = md.division_id AND m.id = md.member_id WHERE m.id = ? ORDER BY div_name "; $stmt = $pdo->prepare($query); // prepare the query $stmt->bindvalue(1,$id); // bind the input data to the place-holder $stmt->execute(); // run the query $result = $stmt->fetchall(); // fetch all the rows. this will be an empty array if the query didn't match any row(s) // produce the output from the $result array of data from the database query if(empty($result)){ echo 'There is no data to display'; // this won't occur unless there is no divisions and members data. } else { // there is data to display $record = $result[0]; // make a copy of the first row for the heading information ?> <h3>Updating divisions for member: <?php echo $record['username'];?></h3> user's id: <?php echo $record['user_id'];?> <br> <form method="post" > <table width="60%" border="1" align="left"> <th width="5%">id</th><th width="45%">division</th><th width="45%">image</th><th width="5%">status</th> <?php foreach($result as $row) { echo "<tr><td>{$row['div_id']}</td>"; echo "<td>{$row['div_name']}</td>"; echo "<td>{$row['div_image']}</td>"; $chk = $row['checked'] ? 'checked' : ''; // produce the checked state for each checkbox echo "<td><input type='checkbox' name='chk[{$row['div_id']}]' $chk /></td>"; echo "</tr>\n"; } ?> </table> <input name="update" type="submit" value="Update"> </form> <?php } ?> note: the db column names in this example may not match your's and would need to be modified to run at all.
-
checked check-boxes will be set in the submitted form data. unchecked check-boxes won't be present in the submitted form data. therefore, the value isn't really important and can be left out of the code. it's easy to detect what check-boxes are checked, but you must also be able to remove the stored data for check-boxes that go from checked to not checked. the easiest way to do this is to just remove all the relevant data and then set/insert the data for the checked check-boxes. your code also isn't using the id='....' attribute, so don't produce that in your code unless you need it. your checkbox name='....' attribute should be an array name with the array key/index being the division id value. this will give you a php array from the submitted form data with the division id values that were checked. the line in your code would look like - echo '<td><input type="checkbox" name="chkbox['.$record2['id'].']" /></td>'; your members table should just hold the unique information about each member. it should not hold this division data. you actually need a 3rd table to hold this data, with one row for each piece of data, with member_id, and division_id columns. the member_id and division_id columns need to be set up with a unique composite index to enforce unique data pairs. to retrieve the data to display it, you should use one JOINed query, then just loop over the data and output it the way you want. now the bad news - the mysql_ database library of functions are obsolete and will be removed from php in the near future. you need to learn and use either the PDO (the best choice) or the mysqli_ library of functions so that you are learning current technology. you also need to properly treat external data being put into sql query statements. the best way of doing this is to use prepared queries, with place-holders/bound data in the sql statement for the data values. both the PDO and mysqli_ libraries support prepared queries, but PDO is easier and more consistent to use. P.S. $_PHP_SELF is also obsolete (and i'm pretty sure that's got an extra _ in it) and should not be used. for html5, you can just leave the action='...' attribute out of the <form....> tag or you can use action='#'
-
if your server is set up correctly, and if it's not, you can fix to be so, $_SERVER['DOCUMENT_ROOT'] refers to the document root folder and can be used for building an absolute file system path for include/require statements.
-
@sigmahokies, the php programming help forum section is for asking for help with code you have written. it is not for asking how you can learn something or for links to sites, because we don't know the complete requirements for what you are looking for. this is not a suggestion for you to try and list all the requirements that you are looking for. it's a suggestion that it's up to you to do the research for the information that you want to know, not to ask others to do this for you, because you are the only one here that knows what exactly you are looking for and at what level of detail that matches your programming knowledge and experience. if you have specific questions about how you would design something, you can post them in the Application Design forum section, but please read the rules/stick-posts for that forum section, it is not to get someone to completely do your design for you, just to answer specific questions you have while you are doing the design - see the sticky/pinned post http://forums.phpfre...ication-design/
-
i recommend using a 'template' method, where the first instance of your form field(s) is(are) inside of a <div></div> container and become a source template that you use when appending the dynamically added form fields. this eliminates the need to duplicate the markup for the form fields in both the html and in the javascript, i.e. DRY (Don't Repeat Yourself) programming. next, you need to use an array name for your form fields. this eliminates the need to maintain a counter in the javascript code and allows you to use php's array functions to loop over the submitted data. see the following example - <script type="text/javascript"> function addField() { // create an empty div element var div1 = document.createElement('div'); // get the template html and put it into the empty div div1.innerHTML = document.getElementById('template').innerHTML + "<input type='button' onClick='removeField(this)' value='-'>"; // append the new div to the target area on the page document.getElementById('add_here').appendChild(div1); } function removeField(div){ document.getElementById('add_here').removeChild(div.parentNode); } </script> <body> <form method='post' action='formaction.php'> Children <!-- Template. This first instance of "whatever" will be appended in the add_here div --> <div id="template"> <input type="text" name="child[]" placeholder="Child's Name" style="width:250px; margin-left:7px; margin-right:4px;"> <input type="text" name="title[]" placeholder="Title" style="width:250px; margin-left:7px; margin-right:4px;"> <input type="button" onClick="addField()" value="+"> </div> <!-- container to hold the dynamically added instances of "whatever" --> <div id="add_here"> </div> <input type='submit'> </form>
-
a unix timestamp would be stored as an integer data type. the functions used to produce the integer value would be those that produce a unix timestamp, such as php's time()/mktime()/strtotime() functions or mysql's UNIX_TIMESTAMP() function. a mysql timetamp would be defined as a mysql timestamp data type, with the values being supplied to it as either a 'YYYY-MM-DD HH:MM:SS' string or a YYYYMMDDHHMMSS number.
-
yes, but what does happen? we are not sitting there with you and don't know what you saw. what did happen, even if nothing happened, and what did you expect to happened?
-
What is the best method to write long if - else conditions?
mac_gyver replied to thara's topic in PHP Coding Help
if you find yourself editing your program logic, just to add, remove, or change values that determine what the logic does, you need to use a data driven design. after your write and test your program logic, you shouldn't be touching the code unless you are adding or changing the features in the code. using the general purpose method 02, in order to change the set-point values, add or remove the quantity of settings, or reuse the program logic (which should be a function/class-method) for a different set/purpose of mapping values, all you have to do is change or supply a different data definition. the data definition(s) can also then be stored in a database where an administrator interface can be provided to easily define/edit the definition(s). -
is your database holding a UNIX Timestamp or a MYSQL Timestamp, they are not the same things (though a MYSQL Timestamp is internally stored as a unix timestamp and has the same range restrictions.) a UNIX Timestamp is an integer that represents the number of seconds since '1970-01-01 00:00:00' UTC. a MYSQL Timestamp is formatted/treated as either a 'YYYY-MM-DD HH:MM:SS' string or a YYYYMMDDHHMMSS number. any mysql or php functions that operate on your data would need to be specific to what your data type actually is. are you using a UNIX Timestamp/integer or you are using a MYSQL Timestamp data type?
-
i looked at your CLASS code closer (i initially misread the $session_uname|$f_uname) and doing that won't do what you think. that's producing the bitwise OR between those two values, in the php code. i suspect you want a logical OR in the sql query statement, which would require sql syntax to do that. you cannot bind sql syntax, only values. you would need to form the correct sql syntax, with place-holders for EACH value, then bind each value. also, using the same place-holder name more than once is not proper usage. it does work with emulated prepared statements, but this is likely a bug that could get fixed at any time and you shouldn't rely on it in your code.
-
your program logic makes no sense. you are looping over the first query result to just get the id values, looping over any results from the second query for each id, then at the end looping over the first query result again. you are also building the output in variables but you are re-assigning the variable, rather than concatenating to it, on each pass through the loops. do what i suggested and run one JOINed query to get the data you want. you will have just one loop and a minimum of code to produce the output.
-
is the query giving you one row, which is what a count(*) will do since there's no GROUP BY in the query, with the COUNT(*) value in the row being a zero, or is the COUNT(*) value in the row a 1?
-
that would indicate that $context doesn't contain what you expect. what does adding var_dump($context);, right before the call to the function show?
-
are you sure the code is running at all? are you getting any output on the page (a blank .php page is usually due to a fatal parse or runtime error.) what is the actual value in $context['user']['mentions'] at the point where you are calling the function?
-
are you calling the function? a function consists of two parts - 1) the definition, which starts with the 'function' keyword, and 2) calling the function at the point in your code where you want to make use of what the function does. next, the input parameter(s) to the function should be the actual value that the function expects, so that the function is general purpose, and can be called with the input coming from any source. this function is testing if the input parameter is a number greater than zero. the input parameter should just be the number, without any context about where it is coming from. it's your calling code that knows the context of where the value is coming from. use the following for the first few lines of your function definition - function growl_notification($num) { if ($num > 0) and use this where you are calling the function - growl_notification($context['user']['mentions']);
-
even if you use a SELECT query to get the initial value, only use this for display purposes. any updating of the value should be done in a single query, so that you don't loose data when there are multiple concurrent instances of your code running, each trying to modify the value. or better yet, don't maintain a count/accumulator in a table column. instead, insert a separate row into a database table for each transaction that modifies a value, then sum up the values from the rows to get the current total. this will give you an audit trail so that you can detect things like double-page requests, programming errors, or if someone manages to find a security hole in your code and submits an arbitrary data value that modifies the count/accumulator by more than you want.