Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
How to select row with highest auto increment value?
Psycho replied to python72's topic in MySQL Help
That query would allow you to get the next auto-increment value (among other data about the table) - not the current highest auto-increment value. The OP stated he wanted the data for the record with the highest ID value. Determining the next auto-increment value would not help in that situation. Futher, determining the next auto-increment value does not necessarily allow you to determine the ID of the highest value for that field in the table. The auto-increment values do not take into consideration any previous records that were deleted. So, if the last record created used the ID 20 and that record was deleted; then the next "Auto_increment" value for the table would be 21, but the record with the highest ID would be 19 - not 20 (since it was deleted) -
Don't use Javascript to create the confirm message. Although I agree that a non-JS confirmation would be a better solution in this instance, from a purely academic perspective there is no reason you cannot implement both. There are plenty of articles about to implement JS that onnly enhances the user experience and not a requirement. In this instance you are using a link to initiate a delete. So, you could implement a JS function for the onclick event which will call for the confirmation and - if true - performs the delete from the js. (by the way I would suggest making a standalone function instead of piling a bunch of code into the onclick event). But, the onclick event should return false. By returning false you are telling the HTML page NOT to follow the href in the link. So, if JS is not enabled the user will go to the href of the link. That href should call an HTML confirmation page. Psuedo code function confirmDelete(recID) { if(confirm('Are you sure you want to delete?')) { window.location = 'deleteRecord.php?id='+recID; } return false; } <a href="htmlConfirmationPage.php?id=12" onclick="return confirmDelete(12);">Delete Record 12</a>[/code[
-
MOVED: How to select row with highest auto increment value?
Psycho posted a topic in PHP Coding Help
This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=321437.0 -
How to select row with highest auto increment value?
Psycho replied to python72's topic in MySQL Help
<< Moving topic to MySQL forum >> SELECT * FROM TABLE ORDER BY ID DESC LIMIT 1 Order the results from highest to lowest by ID then LIMIT the results to just the first record -
I don't know if you are familiar with the construction estimation process or if you are just winging this as you go. Whatever the case you need to make sure you fully understand the process before you start coding this stuff. I've worked with estimation processes in a couple of industries (one where I was very familiar and another where I wasn't). You need to walk through every single aspect of the estimation process to determine what the variables are. E.g. how is each material estimated for the project. THEN, analyze the processes to build an appropriate model. When that analysis is done you might find that there are different variables for materials based upon different factors. For example, I would expect that material such as copper pipes, drains, solder, etc. are only dependant upon the number plumbing fitures (sinks, bathtubs, toilets, etc). Or maybe they prefer to estimate based upon the number of full/half baths. So, for those "class" of supplies you would provide the appropriate parameters. For the latter example above you could provide fields for Qty per kitchen, qty per full bath, qty per 1/2 bath. Or if it will be based upon the number of plubming fixtures it may be adequate to have a single value for qty per fixture. But, if the qty will vary based upon ficure then you would need to be able to provide fields for each fixture type (qty per sink, qty per toilette, qty per tub, etc.) With caulking, drywall, joint compound, baseboards, etc, you probably need a different class of materials. These would be estimated based upon squarefootage, height of walls, # of doors, etc. If you don't want to go to that level of detail you could try the kitchen sink approach (pun intended). Create a form for each product that includes every possible parameter: Qty per rooms Qty per doors [.5] Qty per sqr foot [.02] (1 tube/50ft) Qty per sinks Qty per tubs Qty per ... Then let the user determine which parameters are appropriate for the material. As for labor, again, it depends on what the model is. However, based upon the formula you have above "(squareFootage / postSpacing - 1) * 0.65?" I think it would be very difficult to create an intuitive form if the calculations will be that variable. I would think that a labor amoutn per unit of material would be workable. But without completely evaluating the requirements from the user it is impossible for me to suggest a best solution. It really would take a deep analysis of the current estimation process.
-
Find substring with unknown character? ( test_X_here)
Psycho replied to lfernando's topic in Regex Help
Just to elaborate on AbraCadaver's excellent solution, you would just run that one function against the entire string ($old) with multiple divs. The line will gnerate the variable $new with all the divs removed, but the contents of the divs will remain. However, looking at your original post it is unclear if there are other divs (i.e. that don't have an ID in the format "id=test_XX_here") which you DO NOT want replaced. If that is the case, then a small modification is in order: $new = preg_replace('~<div id=test_[^>]*>(.*?)</div>~is', '$1', $old); That will remove the opening and closing div tags where the opening div starts with "<div id=test_". -
Ideally you would have a single table to describe the individual "titles" then in the events table you would reference the title for each record using a foreign key. But, there is a simple enough solutin using what you have. But, looking at your original question, I don't see any correlation in that request and your last response. Take the time to write a clear request and you will get better quality responses in a faster time. One solution is to simply create a query to return the distinct title values from your table SELECT DISTINCT title FROM events ORDER BY title Or the equivalent using GROUP BY SELECT title FROM events GROUP BY title ORDER BY title
-
I think you are going to have to try and explain more. Are you saying you have a form with a select list and you want the form to post all the values of the select list? If so, why? I would assume it is your code that created the select list so you should be able to logically determine the values that make up a select list. Plus, why have a select list of values if you don't want the user to select a value?
-
help making number ranges with different steps
Psycho replied to coolguydudeman's topic in PHP Coding Help
Hmm... I guess I understand now. But, as Pikachu stated you would obviously need to to something with the values. Anyway, here is a simple loop to do as you ask. I've made it very flexible so you can change the parameters as needed. THis just echos the values to the page, but you can do with it what you want. //User configurable values $value = 1; //The start value $maxValue = 30; ??The maximum value before the loop will exit $stepValues = array(11, 7); //The values to be added on each step, you can add more values - will repeat //Non user configurable value $step = 0; //The loop while($value <= $maxValue) { echo "{$value}<br />\n"; $value += $stepValues[$step++%count($stepValues)]; } -
help making number ranges with different steps
Psycho replied to coolguydudeman's topic in PHP Coding Help
Put them in separate arrays then use array_intersect() on all the array to generate an array with only the values that exist in all the arrays. -
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING
Psycho replied to mark103's topic in PHP Coding Help
Were you looking at the right line? The problem is pretty obvious $subject = $type'; You need to remove the single quote at the end. -
Your problem has to do with variable scope. A variable created/used in one function is not the same as another variable of the same name in another function - UNLESS the variables are declared as global. Different programming languages can handle variable scope differently. For JavaScript any variable declared outside of a function will be global. Variables declared inside functions are not. You can, however, pass variables into a function as needed. Whether you should use a global variable or just pass them to the needed functions depends greatly on the context. I'd suggest doign a google seach for "Javascript variable scope" for more resources.
-
Without you providing any pertinent information I don't see how we can be of assistance. What steps did you take to debug the issue and what were the results?
-
It really depends on how secure this really needs to be. For a "casual" site I would just use the fact that the user is logged in (i.e. username is set as a session variable) and use the username to query the database for the additional information.
-
In the login script you are already saving the username to the session data, you should use the query results to save the email to the session data as well. Then on any, page, just use those session values to display on the page as you wish. However, you are currenly storing the "modified" username from stripslashes() and mysql_real_escape_string(). So, I'd probably use the value from the query. Lastly, why are you storing the password to the session? Not a good idea. As for your current code; you are starting the session AFTER you are trying to save session variables - need to reverse that. And, the strip_slashes() will be problematic on a server without magic quotes turned on. Check the documentation on a way to dynamically apply stip_slashes only when needed. And as beegro stated some of those functions are deprecated. You don't need any replacement functions, just set the session variables directly. Revised code <?php include("php/dbconnect.php"); //connects to the database when connected //get the username and password from the login form on the index page $username = trim(stripslashes($_POST['username'])); $password = stripslashes($_POST['password']); //stop an MySQL Injection $sql_username = mysql_real_escape_string($username); $sql_password = mysql_real_escape_string($password); //Select email from the database that matches the password and username $query = "SELECT email FROM users WHERE username='{$sql_username}' and password='{$sql_password}'"; $result = mysql_query(query); //Check if there was a match if(mysql_num_rows($result) != 1) { //Authentication failed echo "Wrong Username or Password"; } else { //Authentication passed, set session values and continue session_start(); $_SESSION['username'] = $username; $_SESSION['email'] = mysql_result($result, 0); header("Location: [url=http://webdev/schools/hhs/psy_bookings/memberspage.php]http://webdev/schools/hhs/psy_bookings/memberspage.php[/url]"); } mysql_free_result($result); ?> <?php session_start(); //Check if user is authenticated if(!isset($_SESSION['username']) { //User not logged in, redirect to login page header( "Location: [url=http://webdev/schools/hhs/psy_bookings/]http://webdev/schools/hhs/psy_bookings/[/url]" ); } else { //User is logged in, contiue (use session vars to diplay username/email) echo 'Welcome, {$_SESSION['username']}. You are still logged in. <br />'; echo 'Your email address is: {$_SESSION['email']}.'; } ?>
-
There is a much simpler solution in my opinion. You only need one form to both create and update the "pages". Just include a hidden field in the form for the id of the form being edited. When the user chooses to edit a page, populate the ID in that hidden field. When the user selects the option to create a new page, the field will not have a value. Now, when the user submits the form, check if the ID field has a value - if so, then you know they are editing an existing page. If not, then they are trying to create a new page.
-
OK, I am assuming that if spouse != null you are wanting to get additional data for the record (presumably spouse info) to display on a supporting row below the main row for the record. If that is the case, then you don't want to check the spouse record for each record and run an additional query. Instead, you want to modify your original query to get that information if it exists. Then run just that one query and use the PHHP logic to create that additional row if warranted.
-
thanks for that. good to know. i was stumped. Yeah I had no idea either. But, it was right there in the manual for constants! Who would have thought to look there?
-
A list, where you can move positions of list items.. Hmm
Psycho replied to zrweber's topic in PHP Coding Help
Of course it is possible - using PHP for a year and a half I would think you should have at least a basic understanding of what would be required. Not knowing how you intend to use this information it is difficut to give a suggestion on the best wat to implement. First, you need some way to know what name to add to the list. Are users "logged in" to the site where you can get there name dynamically or do you need the user to supply their name. Second, how do you want to store the information? How you plan to use the information might help decide this. You could use a database - or if this is for a very limited number of users you might be able to use a flat file. Third, create a form with the button (and possibly the text field to enter their name). Fourth, Create a processign page to receive the form data (could actually be the same as the form page if you have it post to itself). That page will take the input, verify it is valid (i.e. not null, not malicious, etc). Then, check if the value already exists in the list. If not, add it to the top of the list and move all the other records down. If so, remove the value from it's current position and move to the top of the list. This would actually be much easier with a database by just setting a modification timestamp for each record. Then just display the records based upon the timestamp. -
OK, a quick search shows a solution using constants define ('TEST_VAR', 'hello'); $var = "TEST"; echo constant("{$var}_VAR"); //Output: hello
-
The brackets {} are only used within a double quoted string (or hredoc method) to have a variable interpreted. One of the following methods would work (I'd go with the first) echo $var._VAR; echo "{$var}"._VAR; EDIT: Scratch that. I misread what you were trying to do. You are defining a constant and then trying to access that constant dynamically. I don't know of a way to do that. You could instead define the TEST_VAR as an array $config = array(); $config['TEST_VAR'] = 'hello'; $var = "TEST"; echo $config["{$var}_VAR"];
-
You could also do it just as easily within the PHP code (not sure which would be more efficient on the server, but you could test it). (Assumes $row['phone'] is the db value) $formattedPhone = substr($row['phone'],0,4).substr($row['phone'],4,3).substr($row['phone'],7); You could even do it with a preg_replace() $formattedPhone = preg_replace("/(.{3})(.{4})(.{3})/", "\\1 \\2 \\3", $row['phone']);
-
Why are you giving credit to Maq (not that I have anything against him)? I identified the problem in the first response.
-
This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=320749.0
-
How is this a PHP question? Moving to MySQL forumn. You should always create your queries as a string variable so you can echo the entire query to the page for debugging purposes. But, from the little information you have provided I can see that there is no opening quotation mark for the state value. Change your code to $query = "INSERT INTO user (username, email, last, address1, address2, City, State, Postcode, Country, Phone) VALUES ('$frist', '$mail', '$lastname', '$add1', '$add2', '$city', '$state', '$post', '$country',' $number')"; mysql_query()or die("Query:<br />{$query}<br />Error:<br />".mysql_error()); EDIT: Also, if id is an auto-increment value, there is no need to include it in the query at all. EDIT #2: That code should work assuming there are no other errors, but I don't think you meant to name the variable for the first name as $frist. You used the same name when you set it and when you use it, so it would work, but I'm guessing it was a typo that you copied/pasted