Jump to content

mac_gyver

Staff Alumni
  • Posts

    5,449
  • Joined

  • Days Won

    174

Everything posted by mac_gyver

  1. what have you tried? the purpose of programming help forums are to help with code you have written. we are not here to find, write, or give you things that you want. if all you are are doing is posting your assignment and haven't made any attempt at solving the task(s), you don't have anything that we can help with.
  2. i have some additional recommendations - 1) you need to ALWAYS have php's error_reporting set to E_ALL and for development, set display_ errors to ON so that php will help you by reporting and displaying all the errors it detects. on a live(public) server, you would instead turn display display_errors OFF and turn log_errors ON so that any php detected errors would be logged instead. 2) you need to ALWAYS test if your (my)sql queries are working or not (returns a false value.) for development, you would display any database errors, on a live server, you would log any database errors. you can use trigger_error() to do this because it uses the same error_reporting/display_errors/log_error settings that i mentioned in item #1 above. 3) Don't Repeat Yourself (DRY.) in programming, you should not find yourself repeating code that only varies in the value it operates on or in the output it displays. this is what variables are for, to let you use ONE instance of code with different input values or to produce different output to be displayed on a web page using the same layout. using this DRY method, you will have less code to write, test, debug, fix, and to make changes to. 3.1) Ch0cu3r has already given you an example of doing this for the UPDATE query. you would use something similar for the INSERT query. i notice that you have some sort of mapping between the pin number and sensor number? since this is all software, afaik the inputs are coming from a software controlled device, you might as well use logical pin numbers like 1,2,3,... if by some chance you do need to have an arbitrary mapping between the pin number and the sensor number, use a 'look up' array to do the mapping. there's no need to write out 8/16 sets of code when the only thing that changes is a value. a look up array would be like - $map_inputs[4] = 1; // pin 4, sensor 1 $map_inputs[17] = 2; // pin 17, sensor 2 ​... repeat for all sets of values to use this, assuming $pin contains a valid (integer) pin number (see some of the code Ch0cu3r has given) - $sensor_number = isset($map_inputs[$pin]) ? $map_inputs[$pin] : false; 3.2) your <html> ... </html> page layout should only exist once in your code, near the bottom of the code, after a majority of the php logic (you have some missing html now because of the current repetition.) to accomplish this, you would build the specific output that's different in any section of code in php variable(s), then echo those php variables in one copy of the <html> ... </html> output. this is essentially using php as a template engine. for what you are doing, you would have a $title and a $content variable from each section of code. 4) your various tests of the 'action' value need to use a switch/case statement. this will clean up the code and make it more readable. edit: since your INSERT queries seem to be related to your UPDATE queries, why did you make a separate conditional branch? i think if you do item #4 on my list, your code will be greatly simplified (you will be able to see the forest, instead of just the trees.)
  3. if that's the code that produced the above schedule, team 20 is going to be very tired by the end of the season as they are playing two games each week all season long. here's an example of the array shift method (add enough 'bye' entries to make the array an even number of entries, plus groups of two 'bye' entries if you want to reduce the number of games in any week, to make the season longer) - <?php $num_teams = 41; $teams = range(1,$num_teams); if(count($teams) % 2){ $teams[] = 'bye'; // make even } $numteams = sizeof($teams); $team_list = array_keys($teams); $tmp_list = array_splice($team_list, 1, sizeof($team_list) - 1); $count = sizeof($tmp_list) + 1; $days = array(); for ($i = 0;$i < $numteams - 1;$i++) { $days[$i] = array(); $day_list = array_merge(array($team_list[0]), $tmp_list); for ($j = 0;$j < $count / 2;$j++) { $days[$i][] = $teams[$day_list[$j]] . ' vs ' . $teams[$day_list[$count - $j - 1]]; } // rotate $tmp_list $elm = array_shift($tmp_list); array_push($tmp_list, $elm); } echo "<pre>"; print_r($days); echo "</pre>"; ?> btw - if you want every team to play every other team at least once, it will take many more weeks then 10 for 42 teams (you actually only show 40 teams in the result.) and because the bye's, if you have more than one, would be grouped if added to the end of the team array, you would need to randomize the $days array in the above code so that teams don't sit out multiple weeks in a row. edit: if you have more than one 'bye', it would be better to just evenly distribute them in the original array.
  4. the way this is normally done is to have your teams in an array, make a copy of that array, rotate the second array, taking the end entry and putting it back into the start, match it up with the original team array, ignoring the match-up between the identical team in both arrays. if i can find an example among my archived code, i will post it.
  5. if you goto the mysql workbench page, the select platform drop-down lets you select a variation for mac os x.
  6. mysql has a DATE_FORMAT() function that allows you to do this directly in your sql query - http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format the problem with using strtotime() and date() is they aren't just changing the format of the information, they are doing a conversion to/from a unix timestamp as a php integer value, which has a serious problem with the range of values it can represent that varies with the php version and the maximum integer size that the system php is running on supports. this limits the year that can be represented to either 1901 or 1970 on the low end and 2038 on the high end. php's date/time functions that don't rely on php integer values as inputs/outputs should not have this problem (they are supposed to store values internally as a 64-bit number.)
  7. at the point where the variables are being referenced, they won't exist until after the form has been submitted once. the isset() ? : logic prevents php undefined variable errors.
  8. i previously gave you this advice - this is your current code that is displaying the content of the cart - $sql="SELECT * FROM products WHERE id_product IN ("; foreach($_SESSION['cart'] as $id => $value) { $sql.=$id.","; } $sql=substr($sql, 0, -1).") ORDER BY name ASC"; $query=mysql_query($sql); $totalprice=0; while($row=mysql_fetch_array($query)){ $subtotal=$_SESSION['cart'][$row['id_product']]['quantity']*$row['price']; $totalprice+=$subtotal; ?> <tr> <td><?php echo $row['name'] ?></td> <td><input type="text" name="quantity[]" size="5" value="<?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?>"</td> <td>RM <?php echo $row['price'] ?></td> <td>RM <?php echo $_SESSION['cart'][$row['id_product']]['quantity']*$row['price'] ?></td> </tr> <?php } make a COPY of this code and change it so that the name, quantity, and price values it has are used in an INSERT query.
  9. your last posted code doesn't have any mail() statement in it, so, there's no way it can send an email. i recommend reviewing your code to make sure it is doing what you expect. @davidannis, a header() redirect has no problem using url's with parameters. if you had a case where it didn't work, i can list more than six reasons why. and afaik, the lack of a space isn't a problem (just tested.)
  10. you will need to put your $tblprefix value back into the above query, but only in the three places where the full table names are used.
  11. the problem is with the logic, you are not getting any sort of count/accumulation for each possible answer. as mentioned, you have too many queries and too much code to go along with them. see the following single query - $query = "SELECT category, answer, ROUND(COALESCE(100*SUM(NOT ISNULL(ua.id))/(SELECT COUNT(*) FROM user_answer WHERE question_id = q.question_id),0),2) AS ave FROM question q JOIN answer a ON 1=1 LEFT JOIN user_answer ua ON q.question_id = ua.question_id AND a.answer_id = ua.answer_id WHERE q.category = 'Service Availability' AND q.section = 'Information Technology Services' GROUP BY q.question_id, a.answer_id"; run this query using a tool like phpmyadmin to see what it does, then run it using whatever database class/wrapper you are using in your php code. you can simply loop over the result from this query to DYNAMICALLY produce all the information in your html, including the category heading Service Availability, each answer All of Time, Most of Time, ... label, and each result. you should not have these things hard-coded into your code, let the query get the values, in fact, the query will work for any number of categories simply by altering the WHERE clause in the query and it could be expanded to operate on any section(s) that the query matches as well.
  12. care to share which one is working and what is wrong with output from the other ones, what are you getting, what do you expect, and what is the data in your database tables that the code is using to produce the output? btw - all your code can probably be replaced with one query (you shouldn't be running query(ies) inside of loops), then simply loop over and display the result from that one query (if someone has time, they will post an example.)
  13. you would add an ENCLOSED BY ... term to the query.
  14. the error is coming from some other call to your function as the column names don't match. also, you should not be making the database connection inside your function, running one query, and closing the database connection. your main code should make ONE database connection and pass it into any functions that need it as a call time parameter (or inject it into any class that needs it.)
  15. the goal of the code you are working on is to take the contents of $_SESSION['cart'] and insert it into a database table. that the button that tells the code to do that is part of an exiting update form is coincidental. that you started a new thread for an existing problem, means that those reading this thread don't necessarily know what you are trying to accomplish. you must always keep in mine what goal you are trying to achieve when writing code. in fact, it wouldn't hurt to have that goal as a comment right above the block of code in your program. if you haven't defined what the inputs your code will receive (in this case you have a form button name/value, a $_SESSION['cart'] array with product id's and quantities, and a database table, products, that holds the name and price for the items in the cart), what goal the code is trying to accomplish, and what result or output the code will produce (rows in an `order`, that would be your first step before writing any code. btw - the reason i suggested in your previous thread to name the table order_details was to give it a meaningful name (it holds details about orders) and to avoid all the problems you have in this thread trying to use `order` as the table name. you also should not put the product name into this table, you should use the product_id (whatever you are calling it), so that the product name only exists in one place, in the products table.
  16. the error is likely coming from your INSERT query (during your debugging you need to pin down where in the code the error is occurring at.) the query is using place-holder/parameter names - :user, :pass the ->execute() method is using ':username' => $_POST['username'] and ':password' => $_POST['password']. the names must match up.
  17. if the 'expression_.txt' filename can be specified by the visitor in any way shape or form (pun not intended), via a form field, hidden or not, a parameter in a link, a cookie, or via a database query where sql injection is possible..., then yes, they can get your code to download any file they want. another possibility, is if your site allows uploading files, someone could have uploaded a .php script and has their own file manager script somewhere on your site.
  18. a visitor to your web site cannot see or access the raw php code, assuming it's stored in .php files. if your database credentials are stored in a file ending in a non .php extension and someone has guessed the file name, they can request the file and see the contents of the file. they could likely then access the web host's phpmyadmin page and make alterations. however, if your web host hasn't secured the folders for each account so that only that account has permission to access its folders, other accounts can access anything on the server. another possibility is that you have code that allows anyone to read/display/download an arbitrary file and someone used it to cause your own code to output the contents of your .php files.
  19. your ->get_records_sql() method is at fault. it would take knowing the code for it to help. it should be creating an array of objects for each main index value, not just an array with one object in it.
  20. your code needs to ALWAYS test if the query ran without errors before trying to use any information from the query. you also need to remove the @ error suppressors from your code. you would have found the days problem a lot quicker, and since your table/column names seem to be randomly changing in the posted code, its likely your query is failing due to an error, which would cause your code to think there isn't a matching row and would run the insert query. since your column is a date/time value, you need to use NOW() in the interval calculation to give a date/time for the comparison with the column. i'm pretty sure comparing a date/time value with a date (that CURDATE() would return) won't compare correctly (though mysql may cast the date as a date/time value.) have you ran the sql query statement directly against your database using a tool like phpmyadmin so that you know the query is doing what you expect? edit: and in case it hasn't already been mentioned, the mysql_ functions are depreciated and obsolete. you should not spend your time using them in any new code as they will be removed in a future php version and the time you spend using them now will be wasted. you should be using either the mysqli_ or PDO database functions. edit2: $errmsg_arr[ ] = '...'; $errflag = true; you can use the $errmsg_arr array as the error flag. just test if the array is empty or not. this will reduce the number of lines of code, but will produce the same result.
  21. ^^^ if you use an array to hold the error messages, you can use the array itself as the flag davidannis suggested. if the array is empty, there's no errors. if the array is not empty, there was an error.
  22. the current errors are because your sql query is not matching any rows. your code should ALWAYS test if the query failed due to an error AND when it does run without any errors, your code should test how many rows the query matched and take an appropriate action in either case. if your code did have logic in it to test for these two things, it would be self-diagnosing and would tell you why it isn't doing what you expect. the reason why it is not matching any rows is because you are using single-quotes to start and end the sql query statement and the php variable in it is not being evaluated and replaced with its value. the query being ran is literally - SELECT * FROM products WHERE id = "$proda_id", and since id will never be the string of characters $,p,r,o,d,a,_,i,d, the where clause is false. some tips - 1) you should always form the sql query statement in a php variable. this separates it from the code running the query so that you can echo/log it to see what it actually is and so that you can copy/paste the sql statement and run it directly against your database to see if it does what you expect. 2) you should always use double-quotes to start and end the sql query statement. this will cause any php variables inside the sql query statement to be evaluated and replaced with their value. 3) you should always use single-quotes inside the sql query statement around string data. since the id is numerical, you shouldn't have anything around the value inside the sql query statement. next, your code has other functional problems. 1) you should not create the database connection inside your function. this results in repeated code and settings, in every function that needs to use the database (what happens if you ever need to change how the connection is made or the connection credentials? you should only have ONE place in all your code where you would need to change these things), and results in repeatedly making database connections, which take a relatively long time. your application should create ONE database connection and use that ONE connection everywhere it is needed in the code. to pass the database connection into your functions, you would pass it as a call time parameter, like the $proda_id variable is being use now. since the database connection is required for any database dependent function or operate, it should be the first parameter in the list, for consistency reasons. 2) the purpose of the get_product_id() function is to return data for ONE id. it should not have a while(){} loop (there's also a problem with the while loop, see the next item in this list.) all you should be doing is to fetch and return the ONE expected row that the query returns. 3) your current while(){} loop is returning the expected data as an array of arrays (when the query actually works), and is also returning a final NULL entry in that array that will cause any code that access the final entry to throw errors. when you fix item #2 above in this list, this problem with go a way and your code will get one single row array back from the function and you will be able to access the elements in it using things like $product["id"]. 4) lastly, the mysql_ functions are depreciated and obsolete. you should not spend your time trying to learn them as they will be removed in a future php version and the time you spend learning them will be wasted. you should be learning and using either the mysqli_ or PDO database functions.
  23. to make an array of arrays, change the $instock[$id] = $location; line to the following - $instock[$id][] = $location;
  24. what you are doing is not complicated, but it does require that you understand what your existing code is doing so that the new code doesn't get put in the wrong logical place. your php code would be organized as follows - // your existing update cart code if(isset($_POST['submit'])){ ... } // your new insert to database code if(isset($_POST['submit2'])){ ... }
  25. what sort of issues? we can only help with specific questions. loops are a conditional statement, like an if(some_condition_being_tested){ ... }, except instead of running just one time when the condition being evaluated is true, they LOOP as long as the condition being evaluated is true.
×
×
  • 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.