-
Posts
5,449 -
Joined
-
Days Won
174
Everything posted by mac_gyver
-
sorry, but you cannot sneak up on a problem in programming (programming is like a vague/sneaky-person detector.) you must fully define what the inputs are, what processing you are going to do for those inputs, and what result you are trying to produce. based on the information you have posted, the replies you have gotten are all that anyone can tell you. if you want a programming forum to help with your problem, you must communicate - what a sample of the input data is, what processing you are going to do for those inputs, and what result you are trying to produce.
-
Appending Dynamic Data to existing XML file in PHP
mac_gyver replied to prash91's topic in PHP Coding Help
xml is designed/intended to be used as a data exchange format between different systems. it is not designed to be a database. all the extra code and processing needed to treat is as a database, and in your case to prevent adding duplicate data, is not efficient. in order to prevent duplicated data, you would need to search/parse through the "xmlphp1.xml" values and test to make sure that the newly submitted data isn't already present. unless your assignment is to devise how to find duplicate data using xml, an alternate approach would be to store the actual data in a database or at a minimum a serialized/json array stored in a file, so that php can easily read it into a native php array and process it. testing/preventing duplicates would occur either in a database query or using php's array functions. you would then simply iterate over the actual data and output it as xml only when needed. -
it is. the PDOStatement object is traversable using foreach (as of php5.4 the mysqli result object is too.)
-
apparently the tutorials are only visible to members above some level. here is the php code that is found in that tutorial - <?php // database connection info $conn = mysql_connect('localhost','dbusername','dbpass') or trigger_error("SQL", E_USER_ERROR); $db = mysql_select_db('dbname',$conn) or trigger_error("SQL", E_USER_ERROR); // find out how many rows are in the table $sql = "SELECT COUNT(*) FROM numbers"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_row($result); $numrows = $r[0]; // number of rows to show per page $rowsperpage = 10; // find out total pages $totalpages = ceil($numrows / $rowsperpage); // get the current page or set a default if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { // cast var as int $currentpage = (int) $_GET['currentpage']; } else { // default page num $currentpage = 1; } // end if // if current page is greater than total pages... if ($currentpage > $totalpages) { // set current page to last page $currentpage = $totalpages; } // end if // if current page is less than first page... if ($currentpage < 1) { // set current page to first page $currentpage = 1; } // end if // the offset of the list, based on current page $offset = ($currentpage - 1) * $rowsperpage; // get the info from the db $sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); // while there are rows to be fetched... while ($list = mysql_fetch_assoc($result)) { // echo data echo $list['id'] . " : " . $list['number'] . "<br />"; } // end while /****** build the pagination links ******/ // range of num links to show $range = 3; // if not on page 1, don't show back links if ($currentpage > 1) { // show << link to go back to page 1 echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } // end if // loop to show links to range of pages around current page for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { // if it's a valid page number... if (($x > 0) && ($x <= $totalpages)) { // if we're on current page... if ($x == $currentpage) { // 'highlight' it but don't make a link echo " [<b>$x</b>] "; // if not current page... } else { // make it a link echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; } // end else } // end if } // end for // if not on last page, show forward and last page links if ($currentpage != $totalpages) { // get next page $nextpage = $currentpage + 1; // echo forward link for next page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; // echo forward link for lastpage echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } // end if /****** end build pagination links ******/ ?> as to using prepared queries for this, the queries in this code don't have any direct external values being put into them and won't benefit by using a prepared query.
-
handling the random link using the pagination method would/could work as follows - 1) pass the 'current' page number in the link (so that you can exclude the current image/page from the random selection), and pass a second parameter in the link that indicates you want to get a random image/page number.) 2) after you have gotten the total number of images/pages in the pagination code, use range() to create an array of all the possible images/pages, then remove the 'current' image/page number from that array. randomize the array and get the first entry. use this value as input to the pagination logic for the image/page number to retrieve. a possible enhancement to this would be to store the 'current' page number that comes with the link as an array in a session variable. then you can remove all the previously seen images/pages from the result of the range() function so that you don't repeat any images/pages until all of them have been viewed.
-
your problem has two parts - 1) accept a page request and retrieve and display the correct image. 2) produce previous, next, and random links that when submitted tell part #1 what it should do. to keep the number of database queries to a minimum, your previous, next, and random links should only tell the retrieval code what to do, rather than to preselect the id's to use. the only time you run any queries should be in step #1. there are two ways of handling the previous/next operation. 1) you can use pagination with a page size of one (this uses a LIMIT x,y in the query to select the correct row(s) from your table), or 2) you can use 'rowination' where you operate by finding the next lower or higher row's id. pagination is more general purpose, since you can change it to display any number of images simply by changing the page size value the code uses. there's a good pagination tutorial here - http://forums.phpfreaks.com/page/tutorials/_/basic-pagination lastly, as to converting your code to use prepared queries, after you get the logic doing what you want, you need to separate the code running the queries (the business logic) from the presentation code containing the html markup. by having this separation, the code running the queries will be grouped together and converting it will only involve making changes in the business logic. you won't have to touch the presentation code.
-
the reason you have to log into phpmyadmin before you can enter and run a query is to prevent someone from visiting your web site and either grabbing all your database contents and/or dropping all your database tables. if you are going to do this, your first priority would be to have a login system that prevent access to the form and the form processing code. next, the error is because the query failed with an error of some kind. you would need to have error checking logic in your code to get php/mysql to tell you why the query failed. lastly, to do this in general, you don't know what columns are being selected by the query, so there's no way of writing any specific code like - $scode = $data['code'];. you would need to extract the column information from the result set and use the actual column names that were selected in the query for any display purposes.
-
since you cannot specify a column name using a place-holder ? in a prepared query, your first query won't work anyway. prepared queries, for the purpose of preventing sql injection, use place-holders and bind data values into the query for numbers and strings. the order by id part in your first query is 'static' there's nothing in this that uses an external data value. if you had a where clause in your original query, something like WHERE id = $id, you could convert this to change the $id into a place-holder ? and use a prepared query. the reason Ch0cu3r asked about what your logic is trying to do (he is not asking if you are trying to convert to it to use prepared queries, we already know that) is because your code is difficult to follow, i.e. we cannot tell what it is supposed to be doing at all because it is a jumble of queries mixed in with html markup, using numeral array offsets that don't tell us the meaning of the data... so, rather than posting your code, describe the end result you are trying to produce for the images and links.
-
yes. because php has now changed what it does to external data, any data that is (incorrectly) stored with the \'s in it cannot be searched for and found and by unconditionally running stripslashes() on any data, it will remove an real \'s in the data (which i see from looking at your previous threads - http://forums.phpfreaks.com/topic/255193-problem-searching/ you are doing.) prior to php5.4, what you should have been doing is test if php's magic_quotes_gpc is ON and if it was use stripslashes to remove the escaping php was doing, then escaping the data yourself (note: this logic will still work in php5.4 and later as the function that tests if magic_quotes_gpc is on will return a false value.) the best recommendation would be to remove any extra \ characters that are stored in your database tables.
-
the slashes \ should not be in the actual data in the database. they should only be in the sql query statement to indicate those characters that have special meaning that are part of the data and that are not to be interpreted as part of the query syntax. how exactly are you looking at the data when you see the \ characters, because this is either a problem with php escaping your input data in addition to you escaping it or php escaping the data when you retrieve it from the database. note: this php behavior has been removed as of php5.4.
-
to start with, you cannot repeat the same id values in the html and you would never open a database connection, run a query that fetches the same result set each time it is ran, and close the database connection inside of a loop. assuming you actually reference the ids at all (if you are not referencing them, don't put them in the code at all), you would need to append a changing value to them to make them unique. the query you are running inside the while ($i<=$q) loop produces the same result set every time (i.e. there's nothing dynamic in it that changes for each iteration through the loop.) you should run that query once, before the start of the loop and store the output it produces in a php variable. then just use that php variable inside the while ($i<=$q) loop. your code is producing individual forms, each with an opening/closing form tag, inside the while ($i<=$q) loop. in this case, only one of them can be submitted at a time and there won't be multiple sets of data to be inserted. if your goal is to be able to repeat the sets of form fields, you would have one opening form tag, before the start of your loop, and one closing form tag, after the end of your loop, and you would just be repeating the form fields inside of the loop. you need to fix the above problems, before you can get to the point of submitting the multiple sets of form fields all at once to be in a position of needing to insert multiple sets of data. once you do this, the array names you are using for your form fields will result in arrays of data being submitted to your php code. you would loop over the arrays to get the corresponding data from each set of form fields.
-
nothing is being submitted because you don't have an opening <form ... > tag for the select menu. i'm pretty sure they still teach 'estimating' in math classes, where you estimate the magnitude of the answer you expect so that you know if the work you are doing is even in the correct 'ball-park' to produce the result you expect? the same concept applies in programming. you must have an idea what result (in this case a html form) you are trying to produce is supposed to look like so that you know the work you are doing is going to produce the result you expect.
-
the main reason your code isn't producing the result you expect is your while(){} loop, which you don't need any ways. after the end of your (empty) while loop, $row in a false value (the last value assigned to it.) since your query is expected to match either zero or one row, there's no point in using a loop to fetch the result from the query (if you did have a need to use a loop to fetch data from a query, you would use the fetched data inside the loop.) for your purpose, you would first test if the query worked at all, then test if it matched any row(s) to know if the username/password being tested in the query matched a row in the database table. then you would use just $row = mysql_fetch_array($result); (or even better yet learn to use the mysqli or PDO functions as has already been stated) to fetch that row (assuming you need to retrieve the user id for that row to remember who the logged in user is in a session variable.)
-
the php code you posted isn't using any $_POST data, so i don't know why you used var_dump($_POST); in it. you might want to use var_dump($arr); right before the point you have echo json_encode($arr); to see what data the query is retrieving. if your php code is retrieving the expected data, either there's something about that data or something in the calendar javascript that is causing the problem. what the author's site for the calendar javascript are you using?
-
is the php.ini that you changed the one that php is using (a phpinfo() would conform that the session.gc_maxlifetime setting got changed) and if you changed the master php.ini, did you restart the web server to get the change to take effect and if you changed a local php.ini, do you have multiple folders and must have the local php.ini settings in each folder? you could also have a logic error or are redirecting between url's that have and don't have the www. on them (by default the session id cookie will only match host-name/sub-domain where it was set at.)
-
this also seems to be the same problem as in the OP's existing thread - http://forums.phpfreaks.com/topic/286677-not-display-the-form/
-
(re)read what someone suggested -
-
if would probably help if you posted your table definition.
-
you would make the combined prof_id,day columns a composite/unique key.
-
you need a simple 'page controller' that will determine what action the code on the page takes and what content is displayed on the page when it receives a request. you can use a switch/case statement with an include statement in the different case conditions to include the appropriate code you need for each case.
-
sorry, as this won't directly help you, but programming help forums are not here to find or to write code for you. programming help forums are for people who have a problem with code they have written and need help. if you haven't written any code or even understand the code you do have so that you would understand any replies given about that code, we won't be able to help you. there is a 'job offers' forum section that you can post in to hire someone to do this for you. your previous (or future) coder should have designed the system to be general purpose and data driven so that all you would need to do for any team/schedule/yearly changes is to either log into an administrator page and add/remove/change the defining data or perhaps upload a csv file that defines the data the site uses for the current year. if your intent is to do this yourself, you will first need to learn enough of the php language (and probably mysql sql query language) so that you can look at the existing code and get the gist of what it is doing, then make an attempt at changing an 'off-line' version of your site on a localhost development system. if you get stuck, you can post the relevant code and error/symptom on a programming help forum. those that make an honest attempt at coding get helpful replies in a programming help forum. those that just dump their code, errors, and data on a forum and expect someone to fix the problems for them, don't.
-
I can't for the life of me move from mysql_* to prepared queries
mac_gyver replied to r3wt's topic in PHP Coding Help
for prepared queries, using the PDO library results in much cleaner code than using mysqli. for queries that don't have any external input values, using a prepared query is a waste of resources. a prepared query requires two 'round-trip'/communication exchanges with the database server, one to send the query to be prepared and a second to send the bound data and execute the query. your two queries in the posted code can and should be written as one join'ed query. running queries inside of loops is a performance killer since the communication needed to send the query statement (it's a string of characters) or bound data for a prepared query (each one is a string of characters and afaik binary data transfer isn't yet being used) to the database server is longer than the time it takes to execute most straightforward/basic queries. the only time a msyql_result() is as efficient as using a fetch_assoc/row/array statement is if you are fetching ONE column from a query. mysql_result() always preforms a data seek, followed by a fetch, even if you are fetching multiple columns from the same row. using multiple mysql_result() statements takes proportionally more time than using one fetch_assoc/row/array statement. there isn't a mysqli version of the mysql_result, probably due to the performance issue (and writing a function to emulate it is going to be about four times slower then a fetch_assoc/row/array statement) and there isn't an exact functional equivalent in PDO (there is a fetch column method that fetches a numbered column from a row and advances the row pointer and a fetch mode that will fetch a specific column from all rows.) -
there's really no way that the code you posted for the register_LiveSaleItem() function is the actual code. it contains an obvious name mismatch between the defined parameter names and the variables being used in the function code. also, why are you defining the $user_id as a parameter but not using it in the function or suppling it when you call your function? there would be a number of php errors from that code (do you have php's error_reporting/display_errors turned on so that php will help you?) here's hint as well. the only difference between the code for the register_saleItem() and register_LiveSaleItem() functions is the database table name it uses. instead of having two different functions, why not just have one function that accepts the table name as a call time parameter.