Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
It is impossible to get the results you displayed unless the ID of the first parent record is the boolean false and I find that hard to believe. You show this as the first three lines <ul> </ul> </li> Then ending UL and LI tags would be generated in the IF condition at the bottom of the code displayed below. But $currentParentID is set as false before the loop runs so that IF condition could never be true in the first instance of that loop. while ($row = mysql_fetch_assoc($result)) { if($currentParentID != $row['ID']) { //This is a new parent page if($currentParentID!==false) { //If not first parent, close last submenu UL and parent LI $menuHTML .= "</ul>\n"; $menuHTML .= "</li>\n"; } Output your tables and attach to the post.
-
Please explain why should I help you when you modify the code I provide you which then leads to the code to fail? If you don't understand something - ask. I specifically formatted the comparison operator (although I missed one) to prevent a potential problem which would occur if you had a parent ID of 0. But you do have a parent ID of 0, don't you? And you, in your infinite wisdom, decided that you knew better. Which resulted in the code not working. Change the following two lines This: if($currentParentID != $row['ID']) To this: if($currentParentID !== $row['ID']) AND This: if($currentParentID==false) To this: if($currentParentID===false) The problem is that when comparing values a 0 (zero) will compare to a bolean false if you don't use a strict comparison (as will a null value or empty string). By adding the additional equal sign you are comparing if the two values are equal AND of the same type. http://php.net/manual/en/language.operators.comparison.php
-
FYI the following lines serve no purpose in the above code $count = mysql_num_rows($result); if ($count > 0){ The while() loop will take care of an empty result set as it is.
-
Huh, I said post the code not the HTML markup. I see problems in the output you are posting that I don't see as possible from the code I gave you. I want to see the PHP code as you have implemented it.
-
If you only have two levels then, no, you don't want recursion. Also, what you are posting is completely impossible based upon the code I provided. PLease post the code you have.
-
Sorry, change this line if($currentParentID===false) To this if($currentParentID!==false) By the way, have you ever done a google search on the word "recursion"? Only a "geeky" company such as google would let someone implement what they did.
-
You created a function, but never called the function. How do you expect that code to get executed? You don't need a function in this instance. <?php // Portal Password if (isset($_POST['password'])) { switch($_POST['password']) { case "pass1": header("Location: http://www.google.com/"); exit(); case "pass2": header("Location: http://www.yahoo.com/"); exit(); default: header("Location: http://www.404.com/"); exit(); } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Form</title> </head> <body> <form name="commportalForm" id="commportalForm" method="post" action=""> <input name="password" type="password" id="password" maxlength="5" /> <input name="login" type="button" id="login" value="Go" /> </form> </body> </html>
-
What you are asking for... ...is invalid HTML! You need to remove the closing </li> after the parent name. You already have the correct closing LI at the end of the list of submenus. The code I provided should work as you ask.
-
Hmm... I know he must have seen salathe's post since he replied at 12:19PM. Yet, I received a PM from this person at 2:48PM. @Fotographer96, I know you want a solution and all, but this is a volunteer forum. I only respond to the posts that I want to respond to. That typically means posts where the user has done a sufficient job of explaining the problem and providing the necessary info. And, to be honest, you actually did a better job than most. But, I too was turned off by the fact that you didn't provide any code and it looked like you just wanted someone to do it for you. The ONLY people I would help via an unsolicited PM would be those that I have developed a relationship with. Sending a PM to someone you don't know, out of the blue, asking them to do something for you is no better than spam. But, in the interest of being a forgiving person, I will give you a solution anyway. But, please do not take this as an opportunity to do the same in the future or I will make sure to never respond to your posts again. Not tested, so there may be some syntax errors: $query = "SELECT pp.ID, pp.Name, p.pageName, p.pageID FROM parent_pages AS pp JOIN pages AS p ON p.parentID = pp.ID ORDER BY pp.Name"; $result = mysql_query($query) or die(mysql_query()); //Open the parent UL $menuHTML = "<ul>\n"; $currentParentID = false; while ($row = mysql_fetch_assoc($result)) { if($currentParentID != $row['ID']) { //This is a new parent page if($currentParentID===false) { //If not first parent, close last submenu UL and parent LI $menuHTML .= "</ul>\n"; $menuHTML .= "</li>\n"; } $currentParentID = $row['ID']; //Open new parent LI, create link and open submenu UL $menuHTML .= "<li class=\"mainnav\">"; $menuHTML .= "<a rel=\"nofollow\" href=\"view.php?id={$row['ID']}\">{$row['Name']}</a>"; $menuHTML .= "<ul>\n"; } //Create submenu LI $menuHTML .= "<li class=\"subnav\">"; $menuHTML .= "<a rel=\"nofollow\" class=\"sublink_nav\" href=\"viewpage.php?pageid={$row['pageID']}\">{$row['pageName']}</a>"; $menuHTML .= "</li>\n"; } //Close last child UL, parent LI and parent UL $menuHTML .= "</ul>\n"; $menuHTML .= "</li>\n" $menuHTML .= "</ul>\n"; echo $menuHTML;
-
If you leave the action parameter for a form empty, the form posts to itself. You can add the PHP code to the top of the form page (but only run it conditionally if the form was posted) or you can specify the PHP processing page in the action parameter.
-
Remove this line val = document.loginForm.password.value; Although, you could have done this: $val = $_POST['password']; switch($val) {
-
switch($_POST['password']) { case "password1": header("Location: http://www.google.com/password1-page/"); exit(); case "password2": header("Location: http://www.google.com/password2-page/"); exit(); default: header("Location: http://www.google.com/sorry/"); exit(); }
-
No need to point out the obvious. Although, I will say it is refreshing to see someone who understands the difference between "your" and "you're".
-
PHP Newb: How can I get absolute path for images?
Psycho replied to xstortionist's topic in PHP Coding Help
Another possibility is to use output buffering. Turn on output buffering to "hold" output (i.e. echo/print content) in memory until you are done running the page. Then before sending the output run a regex replacement against the content to do a replacement of all the image URLs to include the full path. Then, send the output to the browser. -
if(isset($review)){ $content.='<div style="margin-top: 400px;">'.$review.'</div>'; } No, you are only checking if the variable $review isset - which you do at the top of the script. So, yes, it would always display. I think you would want to use something like if(isset($_GET['review']))
-
Did you even TRY the code I provided? As for "I'm the sort of person who like to know WHY things work so I can customize them for future use." There is a manual for PHP you know. That is how I learned PHP. I looked at others' code and walked thorough it line-by-line to see what they were doing and where. If I ran into a line I didn't understand I would look at the manual for the function, look at the examples, etc. until I did understand the line of code. You don't need to declare anything. The $a and $b are variables declared as part of the function. The function is called from within the usort() function. The usort() function will pass every combination of two values from the array to the function as $a and $b then, based upon the result of the function will sort the two values accordingly. Take a look at the manual for usort(). THEN, if you have any questions you can ask them here. http://us.php.net/manual/en/function.usort.php Run this function ($a, $b) { if ($a['name'] == $b['name']) { return 0; } return ($a['name'] > $b['name']) ? 1 : -1; } echo "<pre>Before sorting:\n"; print_r($gradout); usort('sortByScore', $gradout); echo "\n\nAfter sorting:\n"; print_r($gradout); echo "<pre>";
-
Did you notice you are doing the same thing (trim and stripslashes) for many, many inputs? Even though it is just two functions you should always make a function if you are doing something multiple times. If you were to move to a different server (or the current server's configuration was changed) where stripslashes was not needed you would have a LOT of editing to do. You can also make the function return the whole text needed for the email to server the purpose of your request here. I would also advise categorizing the input fields based upon the sections they will exist in the email (i.e. party platters, salads, appetizers, etc.). It will give more structure to the data and make the whole process easier to maintain. But, the code I provide below doesn't take that into account. Here is some code to get you started. This is how I would rewrite the code to handle the CONTACT and EVENT text. You should be able to continue on with the rest of the fields. The function formatPOST() does two things. 1) It modifies the input as necessary for use in the email AND if adds the label needed for the email text. The label is optional so you can use it to escape text without appending the label. function formatPOST($postValue, $label=false) { $postValue = trim(stripslashes($postValue)); //If nothing was posted return false if(empty($postValue)) { return false; } //Post value submitted, format value with label (if exists) if(!$label) { return $postValue; } else { return "{$label}: {$postValue}\n" } } function formattedAddress($street, $city, $state, $zip) { $street = formatPOST($street); $city = formatPOST($city); $state = formatPOST($state); $zip = formatPOST($zip); $parts = array(); if($street) { $parts[] = $street; } if($city) { $parts[] = $city; } if($state) { $parts[] = $state; } if($zip) { $parts[] = $zip; } return implode(', ', $parts); } //Format complete billing address $billingAddress = formattedAddress($_POST['billing_address'], $_POST['billing_city'], $_POST['billing_state'], $_POST['billing_zip']); //Format complete event address $eventAddress = formattedAddress($_POST['event_address'], $_POST['event_city'], $_POST['event_state'], $_POST['event_zip']); //Create CONTACT text $Body = "CONTACT INFORMATION\n"; $Body .= formatPOST($_POST['full_name'], "Full Name"); $Body .= formatPOST($_POST['title'], "Title"); $Body .= formatPOST($_POST['company'], "Company"); $Body .= "Billing Address: {$billingAddress}\n"; $Body .= formatPOST($_POST['phone'], "Phone"); $Body .= formatPOST($_POST['alt_phone'], "Alternate PhoneAlternate Phone"); $Body .= formatPOST($_POST['how_did_you_hear'], "How did you hear about us?"); $Body .= formatPOST($_POST['full_name'], "Full Name"); $Body .= $terms_conditions_details; //Create EVENT text $Body .= "\n\nEVENT INFORMATION\n"; $Body .= formatPOST($_POST['event_date'], "Event Date"); $Body .= formatPOST($_POST['guests'], "Number of Guests"); $Body .= formatPOST($_POST['event_start_time'], "Event Start Time"); $Body .= formatPOST($_POST['event_end_time'], "Event End Time"); $Body .= "Billing Address: {$eventAddress}\n"; $Body .= formatPOST($_POST['event_type'], "nType of Event"); $Body .= formatPOST($_POST['event_type_other'], "Type of Event: Other"); $Body .= formatPOST($_POST['event_theme'], "Event Theme"); $Body .= formatPOST($_POST['special_requests'], "Special Requests"); $Body .= formatPOST($_POST['budget'], "Budget"); $Body .= formatPOST($_POST['service_type'], "Type of Service");
-
Can you explain the array structure a little more? I would think that name and userid would be a one-to-one relationship, so it seems odd you would have multiple userid within a name key. But, take a look at this code and see if it does what you want. function ($a, $b) { if ($a['name'] == $b['name']) { return 0; } return ($a['name'] > $b['name']) ? 1 : -1; } usort('sortByScore', $gradout);
-
I would never advise using AJAX for this, but it's your application. I would always have the user do an explicit submission before doing ANYTHING with quantity. What about the situation where someone enters in an amount equal to all the remaining quantity but never submits the order? No one would be able to order that product. As for your question though. If a user enters in a value and you immediately subtract the available amount, then the user changes the amount, you will have to keep track of what the user previously had entered into the field. I see two options: 1) you can keep track of this in the JavaScript code. When the user changes the value you need to pass the previously entered value in the AJAX call so you know how to do the calculation in the PHP code 2) You can keep track of the value in PHP. If the user has entered a quantity and you are subtracting it from the available quantity in the DB then you should have a corresponding record in the DB to account for it. But, since the user has not officially ordered the product I don't know where you would keep track of it. That is just one reason why I think what you are trying to do is too problematic to consider. An even more significant problem is if the user enters a value, you subtract from the available quantity, and then the user closes their browser without placing the order. Now, you have a reduced available quantity where no order was placed. Even if you do move the quantity to some temporary order table you would have no way to know when the user closes their browser to move the quantity back to the available count. This is just a bad concept on multiple levels (IMHO). Good luck.
-
There's nothing in the code I provided that would produce that error. Echo the query to the page so you can find out what is wrong with it.
-
Is it possible to achieve what I am attempting?
Psycho replied to Bl4ckMaj1k's topic in PHP Coding Help
Not quite. The implode merely formats the text for multiple records into a format so we can generate a single query for inserting multiple records. As I said previously you never want to run queries in a loop (i.e. inserting one record on each iteration of the loop). I think you meant to say you want to store five pieces of data in the RECORD - not the field. And, further, you want three of those pieces of data to be the same for ech record inserted. In that case you just need to modify the loop that creates the $queryValues something like this: $queryValues[] = "($tool, $quantity, $variable1, $variable1, $variable1)"; Of course you would need to modify the query to include those three additional fields. There are no NULL values. Implode simply concatenates the values of an array. The first parameter defines the string that will be used between each each value. In the code I provided I am creating an array where each value in the array represents the complete contents of one record to be inserted into the DB. I then use implode to format those values into the proper format to insert multiple records in one query. Here is a simple query to insert ONE record int he db INSERT INTO table (field1, field2) VALUES (value1, value2) Here is how you format a query to insert multiple records at once INSERT INTO table (field1, field2) VALUES (valueA-1, valueA-2), (valueB-1, valueB-2), (valueC-1, valueC-2) -
I'm not really following your scenarios. Are you trying to prevent the user from ordering too much before they submit the page (javascript) or after they sybmit the page (PHP)? In either case the solution is pretty simple. If you want to prevent the user from submitting a request for too many items then when the form page loads you need to query the current available quantity and store that as a JS variable. Then, when the user attempts to submit the page run a validation function comparing the ordered quantity to the available quantity that you saved when the page loaded. On the receiving page you would do basically the same thing but do it all on the server-side. I guess I am having a problem understanding what you are trying to explain here Are you taking about a 2nd order? If so, what does it matter that they ordered 3 in the last submission? the available quantity should have been updated before the 2nd submission. And, as for the issue with multiple users, there really isn't a foolproof method - which is why you would have to validate the available quantity in PHP anyway. Basically, the first person to order wins. If there is 1 unit left of a product and user A and user B have the order page open at the same time, whoever places their order first should "win".
-
I see what you are saying. However, if there are a large number of records, then I wouldn't want to run a ternary operator on each iteration (they are less efficient than normal IF statements). Instead, I would probably use a tracking variable. E.g. $total = 0; while ($row = mysql_fetch_array($result)) { $value = $row['EF0000000DBBDD1D']*6/1000; $total =+ $value; $data[] = $value; $data1[] = strtotime($row['DATETIME']); $data2[] = $total; }
-
Is it possible to achieve what I am attempting?
Psycho replied to Bl4ckMaj1k's topic in PHP Coding Help
Rather than respond to all of your comments I will provide my own explanation of the code. But, first let me answer your two specific questions. 1. Yes. But, if you are properly constructing your database, the tools should be in a separate table with a unique ID for each record. Then that ID would be used as a foreign key to reference the tools from other tables. 2. See explanation below. OK, I don't think you even need the checkboxes (as I explained earlier), but it sounds as if that is what you want to do. So, I will explain that code. The form (modified to not use an ID value for the tools): <input name="tools[]" type="radio" value="Hammer" /> Hammer <input name="quantity[Hammer]" type="text" /> <input name="tools[]" type="radio" value="Wrench" /> Wrench <input name="quantity[Wrench]" type="text" /> In the form you have separate checkboxes to select each tool and a corresponding text field to record the quantity for each tool. The name of each checkbox is "tools[]" with the value being the name of each tool. By naming the fields in this manner, the PHP code in the receiving page can reference the selected tools using $_POST['tools']. Note; only the values of the CHECKED checkboxes are passed in the POST data. So, on the receiving page, the variable $_POST['tools'] will be an array of all the checked tool inputs. The quantity fields are named in the manner "quantity[TOOL_NAME]" where TOOL_NAME is the name of the tool. So, in the receiving PHP page you can reference the quantities in the format $_POST['quantity']['TOOL_NAME']. Now, since you have a list of all the selected tools in the $_POST['tools'] array, you just iterate through that array to find the associated quantity values. PHP Processing page (minor modification to use the tool name instead of ID) $queryValues = array(); foreach($_POST['tools'] as $tool) { $quantity = (int) $_POST['quantity'][$tool]; if($quantity > 0) { $queryValues[] = "($tool, $quantity)"; } } $query = "INSERT INTO [table_name] (`toolname`, `toolvalue`) VALUES " . implode(", ", $queryValues); The first thing we do is create an array ($queryValues) to hold the values to be inserted into the database. The reason for this is that you should NEVER run queries in loops. Instead gather all the data in the loop and run one query. So, I will store a separate piece of text in this array for each record to be inserted in the DB. Next, we will loop through each of the selected tools from the POST data and process that tool and it's associated quantity to create the values for the insert query. Within the loop $tool will be set as each selected tool name during each iteration. Within the loop, we will first get the associated quantity for the selected tool. The (int) will ensure the value is converted into an integer. This serves two purposes 1) It ensure the value is a number and 2) it ensures the value will be safe for a DB query. Then there is an if statement to ensure the value is greater than 0. If 0 is an allowed value for your purposes, you can remove the IF statement. If the quantity is valid we then add a new piece of text to the $queryValues array that will be used to insert a record in the DB. I assumed that the records being inserted will have (at a minimum) values for two columns: the tool name and the tool quantity. lastly, after the foreach() loop completes, we have an array containing multiple records to be inserted into a table. So, we create a single INSERT query that will insert ALL the record in ONE query. This is done by imploding the records. -
EDIT: btherl beat me to it, but I will post this anyway since my solution is much more efficient (no offense btherl) Um, yeah. If you notice the code you put in the post title is NOT the same as the code you posted in the description. There is a huge difference - mainly that $data2 is an array! $data2[] = $data2 + $data; How can you create a new value by adding an array to a numeric value? You need to think about what you are really trying to accomplish in simple terms and then translate it into code. Here is how I would state the problem: "I want to generate a value on each iteration which is the sum of the current iteration value and all previous iteration values". There is a very easy solution to your problem. Since $data contains the current iteration value and all previous iteration values, you just need the sum of all the values in that array!: while ($row = mysql_fetch_array($result)) { $data[] = $row['EF0000000DBBDD1D']*6/1000; $data1[] = strtotime($row['DATETIME']); $data2[] = array_sum($data); }