Jump to content

Psycho

Moderators
  • Posts

    12,157
  • Joined

  • Last visited

  • Days Won

    129

Everything posted by Psycho

  1. Not really. At first I was going to suggest checkboxes, but when I though about it further I realized the checkboxes were not needed either. You could just process the text fields for the quantity inputs - just show a label next to each one. If you really want to use checkboxes, you can. Sample form <input name="tools[]" type="radio" value="hammerID" /> Hammer <input name="quantity[hammerID]" type="text" /> <input name="tools[]" type="radio" value="wrenchID" /> Wrench <input name="quantity[wrenchID]" type="text" /> Sample processing code $queryValues = array(); foreach($_POST['tools'] as $itemID) { $quantity = (int) $_POST['quantity'][$itemID]; if($quantity > 0) { $queryValues[] = "($itemID, $quantity)"; } } $query = "INSERT INTO [table_name] VALUES " . implode(", ", $queryValues);
  2. You have some contradictory information in your post. If you have a radio button group to select the item then you only need one quantity field since you could only select one item. But, you don't need a separate selection for the item vs. the quantity input at all. Just create quantity text inputs for each item. Then on the processing page, only process the text fields that have values. Example form: <input name="quantity[hammerID]" type="text" /> Hammer <input name="quantity[wrenchID]" type="text" /> Wrench Then on the processing page, do something like this: $queryValues = array(); foreach($_POST['quantity'] as $itemID => $quantity) { $quantity = (int) $quantity; if($quantity > 0) { $queryValues[] = "($itemID, $quantity)"; } } $query = "INSERT INTO [table_name] (itemID, itemQty) VALUES " . implode(", ", $queryValues);
  3. Can you elaborate on your statement? I'm not sure what you are proposing? I stated in my first post that there were several methods to persist the value and that session values were my preference (with passing on URL or in cookie as two alternatives). If you have a alternative solution, please share it and the reasons you would use it. Well, the error condition would only occur id the POST value is not set AND the session value is not set. If the page can be run without a "model" value to filter on - which is a perfectly acceptable requirement - then you just need to not include the error condition. However, if it is expected - and it is not happening - then you need to determine why the error condition is occurring. Either the POST value is not getting sent/received (could be a typo between the field name and the POST index value) or the session value is not set (again, could be a typo or you are not using session_start()).
  4. There is no such thing as perfect code. I take time out of my day to help people for free who simply copy/paste the code without even trying to understand it and then have the audacity to say it doesn't work because of a simple syntax error which they should be perfectly capable of recognizing and fixing. "they" should be more appreciative of the help that is provided to them, IMHO.
  5. How do you know it is putting one record before another? The only way to "see" the records in a database (that I know of) is to run a query and then display the results. The query will determine the sort order. If you don't provide one then I believe the implied sort order is the order in which the records are created. But, I would NEVER rely upon that. If you want your records in a particular order then order them using the query.
  6. So fix it. I plainly state in my signature that I may not test the code. It would require much more time to rebuild databases and other requirements to test some code. You should be able to find and fix the simple syntax error in the code I provided.
  7. You are missing the whole point. You are still trying to create separate IDs for each record to use for showing/hiding the data. You are making it more difficult than it needs to be. All you need is a single table that holds all the records and use the ID of that table to show/hide all the records at once. You already have the table, just add an ID to it and create a trigger to call the function. Change this $category_id = ''; echo '<table align="center" width="40%" border="3" cellpadding="0" cellspacing="0">'; while($row = mysql_fetch_array($statusUpdate)) { To this: $category_id = ''; echo "<a href=\"#\" onclick=\"toggle_visibility('THE_TABLE');\";return false\">Show/Hide the table</a>"; echo '<table id="THE_TABLE" align="center" width="40%" border="3" cellpadding="0" cellspacing="0">'; while($row = mysql_fetch_array($statusUpdate)) {
  8. Why would you give the table an ID that has anything to do with the record IDs? You don't need to give the DIVs IDs since you aren't going to hide/sho those individually. You ONLY need an ID on the table and a single link to initiate the JS function. I'm giving you enough information to solve this yourself without writing the code for you.
  9. Sorry, I thought the string was defined with double quotes (so the variales would be interpreted). Try this: $outputArray[] ="<a href='/titlehistory/{$row['shortName']}'>{$row['titleName']}</a>";
  10. Where are you defining the $model variable? You could do something like this: if(isset($_POST['model'])) { $model = mysql_real_escape_string(trim($_POST['model'])); $_SESSION['model'] = $model; } elseif(isset($_SESSION['model']) { $model = $_SESSION['model']; } else { //model not set - perform error handling } Don't forget to use session_start() at the top of the page.
  11. $outputArray = array(); while ( $row = mysqli_fetch_array ( $titlesResult, MYSQLI_ASSOC ) ) { $outputArray[] ='<a href="/titlehistory/{$row['shortName']}">{$row['titleName']}</a>'; } echo implode('&nbsp|', $outputArray);
  12. You need to crawl before you walk and walk before you run. I suggest you look at some tutorials regarding MySQL queries. The problem is you are trying to filter the records based upon the ID using a WHERE clause, but you forgot the "WHERE" part of the WHERE clause. $querytext = "SELECT text FROM quiz WHERE id = '$prob'";
  13. As I said, you need to implement the onclick event to show/hide the table - not the individual records. Give the table an id (let's say "mytable"). Then implement an onclick trigger passing the table's ID as the parameter in that function call.
  14. Use an onclick event to change the visibility of the table instead of the TDs. Just give the table an ID and create an object to invoke the toggle_visibility() function passing the table id.
  15. Because it is the field directly before the password field. The browser doesn't "comprehend" the text on the page it only makes a logical determination on what the username/password fields are. The "username" field could just as likely be named "login" or just about anything else based upon the language of the person who developed the page. So, the browser is likely detecting the "password" TYPE field and selecting the immediately preceeding field as the username. If you had simply stated the fields were pre-populated with values when the page loads - THAT would have described the problem. Instead, your original post could have meant just about anything. Especially when the code posted is pulling values from the database (could have been pulling wrong values) and is saving data to the database (could be saving the wrong values).
  16. I don't need to even read through your code to know what the problem is. You apparently have a page where a user can do a search. When the user submits that page you pass the value for the results to be filtered on. In this case it is the value of $model - although you didn't show how that value is defined! But, I suspect it is via a POST value. Anyway, once the user selects another page you are reloading the current page passing a page variable - but you are not passing the $model value. Therefore the subsequent page load is trying to run a query to find record where the model value is an empty string - thus the empty results. You need to pass the model value on to each and every page load if you want the records to be continued to be filtered by that result. One way you can do that by appending the model value onto the query string (along with the page value). But, that can get ugly and requires more work to ensure the value doesn't get corrupted in the query string. Two other alternatives would be to store the model value as a cookie or a session value. I would use a session value.
  17. What do you mean "this code is making my username.......searls03 and password as default value"? I have no clue where in that long set of code where you are stating that is happening. Are one of the messages in the code stating something in particular? Can you point out the line in question where you are getting the unexpected results? That would help to trace back at where the error could be occuring?
  18. The problem is that you are running two distinct queries, one of which is a loop on the results of the first query. You apparently have multiple results from $tbl_name2 that are associated with the same record in $tbl_name. You should NEVER run queries in loops. In this case you should be doing a JOIN between the two tables along with a GROUP BY to only get the unique values. EDIT: This should get you started $likeValues = "$tbl_name2.keyword LIKE '%" . implode("%' OR $tbl_name2.keyword LIKE '%", $keywords) . "%'" $query = "SELECT * FROM $tbl_name JOIN $tbl_name2 USING(product_id) WHERE $likeValues GROUP BY product_id";
  19. OK, if I understand you correctly, the option to select parents is not an option associated with the records in the `medlemmer` table. instead it is an option when submitting the form to send the emails. So, the logic is still the same, just check the POST value in the logic I provided instead of the database value. Although I don't understand your checkbox array. If you are using the checkboxes to select the recipients, then what is the purpose of the query to get recipients? Oh well. if(in_array('Foraldre', $_POST['mailtarget'])) { //Parents checkbox NOT selected $ToEmail = $row->email; } else { //Parents checkbox selected $ToEmail = "{$row->email}; {$row->mor_email}; {$row->far_email}"; }
  20. Of course it doesn't you aren't "returning" the value, you are echoing the value into an XML file. So, if the value is >5, then redefine the value as five. Then the 5 will get written to the file. Put this right before the echo statement: if ($row['quantity'] >= 5) { $row['quantity'] = "5"; }
  21. Below is a working solution - albeit a rough one. You just need to provide the appropriate information in the five variables at the top of the script. I was going to extend the functionality to first let the user select any table, but I'll let you do that if you so choose. Here are some notes about the script: 1. I use the query "DESCRIBE [TABLE_NAME]" to get the field information for the table. Currently, the script is only using the field names, but the results contain much more information such as the field type (i.e. int, varchar, etc.). This script can be further expanded to provide only the appropriate comparison operators for each field. For example, it doesn't make sense to have a "greater than" operator for a text field, but it would be handy to have a "starts with". 2. I like phpJoeMo's suggestion above about having a select box to determine the fields to be used in the comparisons. That allows you to have the same field used twice in the WHERE clause (but that would necessitate the ability to use OR's - see #4 below) 3. After running the script, I think it would make sense to have the input fields repopulated with the submitted values when the results are displayed. That way the user can easily make minor modifications to get the results they are looking for. 4. Lastly, all of the comparisons in the WHERE clause are considered as AND comparisons. It would be beneficial to allow for OR'd conditions as well. But, then you would also need to add the ability to have parenthesized comparisons. EDIT #5: Very important! I didn't take the time to escape the user input. You will want to do that to prevent errors and possible database corruption. I'll let you read through the code to see what I did and where. <?php $__DB_SERVER__ = "localhost"; $__DB_USER__ = "XXXXX"; $__DB_PASS__ = "XXXXX"; $__DB_NAME__ = "test"; $__DB_TABLE__ = "lu_products"; mysql_connect($__DB_SERVER__, $__DB_USER__, $__DB_PASS__) or die(mysql_error()); mysql_select_db($__DB_NAME__) or die(mysql_error()); if(isset($_POST['select'])) { $selectClause = '`' . implode('`, `', $_POST['select']) . '`'; $whereParts = array(); foreach($_POST['where'] as $field) { $whereParts[] = "`{$field}` {$_POST['compare'][$field]} '{$_POST['value'][$field]}'"; } $whereClause = implode("\n AND ", $whereParts); $query = "SELECT $selectClause FROM $__DB_TABLE__ WHERE $whereClause"; echo $query; $result = mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result)==0) { $output = "There were no matching records."; } else { $header = true; $output = "<table border='1'>\n"; while($row = mysql_fetch_assoc($result)) { if($header) { $output .= " <tr>\n"; foreach($row as $label => $value) { $output .= " <th>$label</th>\n"; } $output .= " </tr>\n"; $header = false; } $output .= " <tr>\n"; foreach($row as $value) { $output .= " <td>$value</td>\n"; } $output .= " </tr>\n"; } $output .= "</table>\n"; } } //Get fields for table $query = "DESCRIBE {$__DB_TABLE__}"; $result = mysql_query($query) or die(mysql_error()); if($result) { while($row = mysql_fetch_assoc($result)) { $fieldNames[] = $row['Field']; } } $selectFields = ''; $whereFields = ''; $compareOptions = "<option value='='>equals</option>\n"; $compareOptions .= "<option value='<>'>does not equal</option>\n"; $compareOptions .= "<option value='>'>greater than</option>\n"; $compareOptions .= "<option value='<'>less than</option>\n"; $compareOptions .= "<option value='>='>greater than or equal to</option>\n"; $compareOptions .= "<option value='<='>less than or equal to</option>\n"; $compareOptions .= "<option value='LIKE'>is like</option>"; foreach($fieldNames as $field) { $selectFields .= "<tr>\n"; $selectFields .= " <td colspan=\"4\" style=\"padding-left:25px;\">\n"; $selectFields .= " <input type=\"checkbox\" name=\"select[]\" value=\"{$field}\" /> {$field}\n"; $selectFields .= " </td>\n"; $selectFields .= "</tr>\n"; $whereFields .= "<tr>\n"; $whereFields .= " <td style=\"padding-left:25px;\"><input type=\"checkbox\" name=\"where[]\" value=\"{$field}\" /></td>\n"; $whereFields .= " <td>{$field}</td>\n"; $whereFields .= " <td><select name=\"compare[$field]\">\n"; $whereFields .= $compareOptions; $whereFields .= " </select></td>\n"; $whereFields .= " <td><input type=\"text\" name=\"value[$field]\"></td>\n"; $whereFields .= "</tr>\n"; } ?> <html> <head> <style> th { text-align: left; padding-top:10px;} </style> </head> <body> <form action="" method="post"> <table border=0> <tr> <th colspan="4">SELECT</th> </tr> <?php echo $selectFields; ?> <tr> <th colspan="4">FROM <?php echo $__DB_TABLE__; ?></th> </tr> <tr> <th colspan="4">WHERE</th> </tr> <?php echo $whereFields; ?> </table> <button type="submit">SUBMIT</button> <br><br> <?php echo $output; ?> </form> </body> </html>
  22. Only if you want to use that data - so the answer is yes. But, just adding additional fields to a query isn't going to do anything with that data unless you code for it. You will also need to pull the value for the "parents" checkbox. You don't say what that field is called. So, your query would look something like this: $query = "SELECT email, tilknytning, nyhedsbrev, mom_email, dad_email, parentsCheckbox FROM medlemmer WHERE tilknytning IN ($gruppeList) && nyhedsbrev = 'Ja'"; Next, modify your code for defining the recipients for each email. I will assume that if the email is going to the parents that it is OK to send one email to the three recipients if($row->parentsCheckbox!=1) { //Parents checkbox NOT selected $ToEmail = $row->email; } else { //Parents checkbox selected $ToEmail = "{$row->email}; {$row->mom_email}; {$row->dad_email}"; }
  23. The problem you have is that checkboxes are only passed in the POST data if they are checked. So, if user checks boxes 0 and 5, the post data has checkbox fields with ids of 0 and 1. The solution is very easy though. I assume you have a loop that created all the form inputs. In that loop, I also assume you have a variable to keep track of the loops (i.e. $x). In the loop that creates the fields, set the value of the checkboxes using the $x value (i.e. 0 through 9). Also, for the field names of the fields that correspond to those checkboxes, set the index in the field name using the $x. Now, when you receive the form post, you check the values passed for the "field" checkboxes and use those values to reference the fields associated with them.
  24. You do NOT want to create a new column in your database to store the "elapsed time". That should be calculated in real-time. 1. You should store dates as dates, number as an appropriate number type (in, float, etc) and text as text. There is a date() functions to transform a date value into whatever format you want for displaying the value. Do not store data in the wrong format because of how you want to display it. You modify the value in PHP code after getting it from the database. 2. Yes you could do the calculation in PHP with the varchar value, but you shouldn't be using the varchar for a date to begin with. Even though you should store the date as a date there is a slight hurdle in converting MySQL dates to PHP dates and vice versa. Take a look at this article: http://www.richardlord.net/blog/dates-in-php-and-mysql So first, convert your submission_date field to a datetime field. Then, when saving a date, create a MySQL valid datetime value using $mysqlDate = date( 'Y-m-d H:i:s', $phpdate ); and use that value in your insert query When running a query to get your records you will need to process the value in PHP to convert it to a PHP date using strtotime(). At the same time you can calculate the elapsed time while($row=mysql_fetch_assoc($result)) { $submission_date_ts = strtotime($row['submission_date']); //Timestamp $submission_date_str = date("M-d-Y", $submission_date_ts); //String in format MMM-DD-YYYY $time_elapsed = (time() - $submission_date_ts); //Elapsed time in seconds // }
  25. In addition to what you requested, I made other modification to make your code flow in a more logic order. Not tested, so there may be some syntax errors. <?php //Create options for the DAY select list $dayOptions = "<option value="">Day</option>\n";; for($day = 1; $day <= 31; $day++) { $selected = ($day==$_POST['dobd']) ? ' selected="selected"' : ''; $dayOptions .= "<option value=\"{$day}\"{$selected}>".date('d', mktime(0,0,0,0,$day,0))."</option>\n"; } //Create options for the MONTH select list $monthOptions = "<option value="">Month</option>\n"; for($month = 1; $month <= 12; $month++) { $selected = ($month==$_POST['dobm']) ? ' selected="selected"' : ''; $monthOptions .= "<option value=\"{$month}\"{$selected}>".date('M', mktime(0,0,0,$month+1,0,0))."</option>\n"; } //Create options for the YEAR select list $yearOptions = "<option value="">Year</option>\n"; for($year = 1998; $year >= 1911; $year++) { $selected = ($year==$_POST['doby']) ? ' selected="selected"' : ''; $yearOptions .= "<option value=\"{$year}\"{$selected}>{$year}</option>\n"; } ?> <select name="dobd"> <?php echo $dayOptions; ?> </select> <select name="dobm"> <?php echo $monthOptions; ?> </select> <select name="doby"> <?php echo $yearOptions; ?> </select>
×
×
  • 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.