Jump to content

Psycho

Moderators
  • Posts

    12,157
  • Joined

  • Last visited

  • Days Won

    129

Everything posted by Psycho

  1. Just tested out the code I posted previously and there are two minor errors: #1: change the IF statement to this: if(isset($_POST) && count($_POST)>0) These lines had a typo in the function name: $name = "'" . mysql_real_escape_string($name) . "'"; $value = "'" . mysql_real_escape_string($value) . "'"; Other than that the script *should* work. EDIT: One thing the above script does not do is validate the values with respect to the database field constraints. So, if those fields have a limit of x number of characters, expect numbers, etc. then you should be validating them. Also, since these are configuration settings I would assume each config value has specific validation that is separate from the database constraints. So, a 'domain name' would have different constraints as opposed to an "admin email" setting. I would suggest creating a function with a switch statement to validate all the particular values and call it within the loop that parses the POST data.
  2. Give this a try (not tested so there may be some syntax errors): <?php $response = ''; if(isset($_POST)) { //Changes were submitted //Put post data in another array for cleaning/parsing $configValues = array(); foreach($_POST as $name => $value) { $name = "'" . mysql_real_esacape_string($name) . "'"; $value = "'" . mysql_real_esacape_string($value) . "'"; $configValues[$name] = $value; } //Create/run update query $caseConditions = ''; foreach ($configValues as $name => $value) { $caseConditions .= " WHEN {$name} THEN {$value}\n"; } $query = "UPDATE `configTable` SET `config_value` = CASE `config_name` {$caseConditions} END WHERE `config_name` IN (" . implode(', ', array_keys($configValues)) . ")"; $result = mysql_query($query) or die (mysql_error()); $response = 'Config values were updated'; } //Get the current values $query = "SELECT `config_name`, `config_value` FROM `configTable`"; $result = mysql_query($query) or die (mysql_error()); //Process results into form fields $configFields = ''; while($record = mysql_fetch_assoc($result)) { $configFields .= "{$record['config_name']}: "; $configFields .= "<input type=\"text\" name=\"{$record['config_name']}\" value=\"{$record['config_value']}\" /><br />\n"; } ?> <html> <head></head> <body> <div style="color:red;"><?php echo $response; ?></div> <form name="updateConfigs" action="" method="POST"> <?php echo $configFields; ?> <br /> <button type="submit">Update Config Values</button> </form> </body> </html>
  3. That will not work. When file_get_contents() calls an external php file the web server will parse that php file and return the processed results (i.e. the rendered html code), it will not return the PHP code.
  4. include() is used to include PHP code - not html. Use file_get_contents() to get the html code of the html code and echo it out.
  5. // Images Directory imagesDir = "<?php echo $baseurl; ?>/wysiwyg/icons/";
  6. The problem is with the $n_american array. You are apparently wanting each "country" index to have a value of the country arrays created above. But, you are encasing the country array variables withing single quotes. variables are not interpreted when enclosed within single quotes. So, the value of the 'Mexico' index is the literal string of "$mexico" not the array of Mexican territories. But, I'm not sure it would work the way you want using doub;e quotes either. Just use this: $n_america = array( 'Mexico' => $mexico 'United States' => $us, 'Canada' => $canada );
  7. Yeah, not only did you fail to read the previous posts where a solution was provided (#3 & #7) your solution is flawed - for reasons stated in the previous posts.
  8. Nice, I did not know this Yeah, I'm a bit of a perfectionist. Checking for divisible by 4 will work for any date from 1901 to 2099. So, if we are working with birth dates of people that are currently living they would have to be 110 years old for the rule to fail - pretty unlikely. But, since developers will commonly pick up existing code to reuse for other purposes and/or the OP could be dealing with a geaneology project it's better safe than sorry to do it exactly right.
  9. Yes, that function works for any year that you pass to it and returns a true or false based upon whether that year is a leap year. Nope, leap year is not determined by whether it is divisable by 4. There is more to it than that. If the year is divisable by 4 AND it is not divisible by 100 (UNLESS it is also divisible by 400) then it is a leap year: ($year%4==0 && ($year%100!=0 || $year%400==0)) After some more though on this I was a littel curious and decided to test what the performance difference was between determining leap year via the date() function vs. determining the leap year through mathmatical calculations. Although we are talking about milliseconds, the following functin is almost 100 times faster thant he function I posted above: function isLeap($year) { return ($year%4==0 && ($year%100!=0 || $year%400==0)); }
  10. No, the date() function requires a timestamp - if you don't supply one then it assumes the current date/time. Here is a small function that you can pass a year value to to determine if it is a leap year (also included a loop for testing purposes): <?php function isLeap($year) { return (date('L', mktime(0, 0, 0, 1, 1, $year))==1); } //For testing for($year=1899; $year<2020; $year++) { If (isLeap($year)) { echo "$year : LEAP YEAR<br />\n"; } else { echo "$year : Not leap year<br />\n"; } } ?>
  11. The error is exactly what I posted. That is all of the information that I get, then a white blank screen. I can post the entire array.txt file as it sits when I get back to work (about 15-20 minutes). mjdamato, I tried to get the serialize to work but I don't have the time to wait, you mentioned this code and it worked. If it is so inefficient, why did you suggest it? As freakstyle stated you should be getting more information with the error - such as a LINE NUMBER which would tell us where the error was occuring. I said YOUR code was inefficient, not mine. If you had provided more information previously I would have been willing to help. There is no reason to read through the entire text file to create an array and THEN go through each record in the array. You can do all the work in one loop - not two. Secondly doing multiple queries in loops puts a HUGE overhead on the server. You could easily combine the values for the multiple records and insert them with one query. Lastly, your loop to insert the records as an artificial limit of 3664. If the data has less values then that you will get an error and if there are more values you will not process them all. Since you are using an array, you should be using foreach(). Even if you expect the array to always contain 3664 records you shoudl still use foreach() and just do a check of the count() on the array for validation.
  12. I'm not going to take the time to implement a right-click menu. But this should get you started. I used PHP to create the table to cut down on the code to write. If you need the raw output let me know and I can post it. The solution below only requires theuser to click once in a row to select the cells - not twice as you were asking. My implementation is more intuitive. <?php $table = "<table class=\"selector\" border=\"1\">\n"; for ($rows=0; $rows<10; $rows++) { $table .= " <tr>\n "; for ($cols=0; $cols<10; $cols++) { $table .= " <td id=\"{$rows}-{$cols}\" onclick=\"selectCells(this);\">&nbsp</td>\n"; } $table .= " <td>"; $table .= "<select name=\"{$rows}-color\" id=\"{$rows}-color\" disabled=\"disabled\" onchange=\"setColor(this)\">"; $table .= "<option value=\"\">--Select Color--</option>"; $table .= "<option value=\"#ff0000\">Red</option>"; $table .= "<option value=\"#00ff00\">Green</option>"; $table .= "<option value=\"#0000ff\">Blue</option>"; $table .= "</select>\n"; $table .= "</td>\n"; $table .= "\n </tr>\n"; } $table .= "</table>\n"; ?> <html> <head> <style> .selector td { height:20px; width:20px; } </style> <script type="text/javascript"> function selectCells(selectedCell) { var selectedRow = selectedCell.id.substr(0, selectedCell.id.indexOf('-')); var selectedCol = selectedCell.id.substr(selectedCell.id.indexOf('-')+1); //Unset all previous selections for (var row=0; row<10; row++) { document.getElementById(row+'-color').disabled = (row!=selectedRow); document.getElementById(row+'-color').disabled = (row!=selectedRow); if (row==selectedRow) { document.getElementById(row+'-color').selectedIndex = 0; } for(var col=0; col<10; col++) { var cellObj = document.getElementById(row+'-'+col); if (cellObj.selected==true) { cellObj.selected = false; cellObj.style.backgroundColor = '#ffffff'; } } } for(var col=0; col<10; col++) { var cellObj = document.getElementById(selectedRow+'-'+col); cellObj.style.backgroundColor = (col<=selectedCol) ? '#0066ff' : '#ffffff'; cellObj.selected = (col<=selectedCol) ? true : false; } } function setColor(selectObj) { var selectedRow = selectObj.id.substr(0, selectObj.id.indexOf('-')); var selectedColor = selectObj.value; if (selectedColor=='') { return false; } for (var col=0; col<10; col++) { var cellObj = document.getElementById(selectedRow+'-'+col); if(cellObj.selected) { cellObj.style.backgroundColor = selectedColor; cellObj.selected = false; } } } </script> </head> <body> <?php echo $table; ?> </body> </html>
  13. LOL, That's my code from your other post today. What is the specific line that is producing the error? In any event that is some VERY inefficient code. First of all you are creating a loop with an artificial limit instead of using the actual count of the array and you only need to do one query to insert all the values.
  14. I'm really not following what you want. Are you wanting a new window to open for each error? If so, just change the window name to an empty string. var dwindow = window.open('debug.php','',',toolbar=1,resizable=1,scrollbars=yes');
  15. You have nothing in that script that would give you any errors if something was not right. I suspect either the POST value is not set or there is a database error. Try this: <?php include("config.php"); $db_link = mysql_connect($db_server, $db_uname, $db_pass) or die ("unable to connect to database server"); mysql_select_db($db_name, $db_link) or die ("unable to select to database"); if (!isset($_POST['user'])) { echo "The post value for 'user' is not set."; } else { $user = mysql_real_escape_string(trim($_POST['user'])); $date = mysql_real_escape_string(trim($_POST['date'])); $time = mysql_real_escape_string(trim($_POST['time'])); $device = mysql_real_escape_string(trim($_POST['device'])); $info = mysql_real_escape_string(trim($_POST['info'])); $query = "INSERT INTO `alanz`.`logs` (`user`, `date`, `time`, `device`, `info`) VALUES ('{$user}', '{$date}', '{$time}', '{$device}', '{$info}')" mysql_query ($query, $db_link) or die ("Query:<br />{$query}<br />Error:<br />" . mysql_error()); } ?>
  16. This code will read the text file and convert it into an array variable. <?php $textData = file('array_data.txt'); $dataArray = array(); $tempArray = array(); $array_start = false; foreach ($textData as $line) { if ($array_start && preg_match("/\[([^\]]*)\] => (.*)/", $line, $keyValue)!= 0) { $tempArray[$keyValue[1]] = $keyValue[2]; } if(substr($line, 0, 1)=='(') { $array_start = true; } if(substr($line, 0, 1)==')') { $array_start = false; $dataArray[] = $tempArray; $tempArray = array(); } } print_r($dataArray); ?>
  17. I don't think those have anything to do with what the OP is asking for now. I believe the OP is now asking how to logically break a word at a syllable, i.e. hyphenate it. Example (lines 1 & 3): Toyota has for years blocked access to data stored in de- vices similar to airline "black boxes" that could explain crashes blamed on sudden unintended acceleration, accord- ing to an Associated Press review of lawsuits nationwide and interviews with auto crash experts. @OP: If you are creating the PDF file via the PHP PostScript functions there are a couple that may help: ps_show_boxed() and/or ps_hyphenate(). however, if you are creating the PDF through other means, e.g. writing raw postscript and distilling this may be more difficult as you will not "know" ahead of time where the word will need to break. @Dennis1986, I like how you updated your original post to include information about using substr() after I posted a solution using it.
  18. I used some PHP code to build the select lists to cut down on the HTML code needed. But, the javascript function below will ensure the "day" select list has the correct number of days according to the month/year the user has selected: <?php $monthOptions = ''; $dayOptions = ''; $yearOptions = ''; for($month=1; $month<=12; $month++) { $monthName = date("M", mktime(0, 0, 0, $month)); $monthOptions .= "<option value=\"{$month}\">{$monthName}</option>\n"; } for($day=1; $day<=31; $day++) { $dayOptions .= "<option value=\"{$day}\">{$day}</option>\n"; } for($year=1900; $year<=2010; $year++) { $yearOptions .= "<option value=\"{$year}\">{$year}</option>\n"; } ?> <html> <head> <script type="text/javascript"> function updateDays() { //Create variables needed var monthSel = document.getElementById('month'); var daySel = document.getElementById('day'); var yearSel = document.getElementById('year'); var monthVal = monthSel.value; var yearVal = yearSel.value; //Determine the number of days in the month/year var daysInMonth = 31; if (monthVal==2) { daysInMonth = (yearVal%4==0 && (yearVal%100!=0 || yearVal%400==0)) ? 29 : 28; } else if (monthVal==4 || monthVal==6 || monthVal==9 || monthVal==11) { daysInMonth = 30; } //Add/remove options from days select list as needed if(daySel.options.length > daysInMonth) { //Remove excess days, if needed daySel.options.length = daysInMonth; } while (daySel.options.length != daysInMonth) { //Add additional days, if needed daySel.options[daySel.length] = new Option(daySel.length+1, daySel.length+1, false); } return; } </script> </head> <body> Birthdate:<br /> <select name="month" id="month" onchange="updateDays();"> <?php echo $monthOptions; ?> </select> <select name="day" id="day"> <?php echo $dayOptions; ?> </select> <select name="year" id="year" onchange="updateDays();"> <?php echo $yearOptions; ?> </select> </body> </html>
  19. Unless you want the user to submit the form after selecting the month (or use AJAX) - neither of which makes sense - then you should be using JavaScript for this. Moving post to correct forum.
  20. Use substr() $limit = 10; $firstpart = substr($string, 0, $limit); //The first 10 characters $secondpart = substr($string, $limit); //The remaining characters
  21. If you want this to happen automatically and not by some user action, then why don't you just build the table with 20 cells to begin with? JavaScript has it's uses, but if you want 20 cells by default, JavaScript is not the way to go.
  22. I don't use dreamweaver, so I can't help you with any functionality implemented through that or even with how to implement your own code. But, I do have a solution. Raio groups can be an odd imlpementation to understand. A radio "group" is a collection of radio buttons. The group itself does not have a value - each of the radio buttons have thier own values. So, you need to iterrate through all of the radio options to find which one is checked to get the selected value of the group. Another problem is that if there is only one button in the group it is not treated as a group. This is typically not an issue, but I try to build my code such that it will work in any situation. Anyway, here is a function that takes all of the above into consideration and returns the selected value of a radio group (or returns false if none are selected) function radioGroupValue(groupObj) { //Check if there is only one option (i.e. not an array) if (!groupObj.length) { //Only one option in group return (groupObj.checked) ? groupObj.value : false; } //Multiple options, iterate through each option for (var i=0; i<groupObj.length; i++) { //Check if option is checked if (groupObj[i].checked) { //Return value of the checked radio button return groupObj[i].value; } } //No option was selected return false; } You need to pass an object reference to the function. So, here is an example of how you could add up all the selected options from multiple radio groups: function calcTotal() { var formElements = document.forms['formName'].elements; var total = 0; total += radioGroupValue(formElements['group1Name']); total += radioGroupValue(formElements['group2Name']); total += radioGroupValue(formElements['group3Name']); total += radioGroupValue(formElements['group4Name']); alert("The total is " + total); return; }
  23. If you are using PHP5 and zip support is configured on the server you can use the ZipArchive class. Specifically the extractto method: http://www.php.net/manual/en/function.ziparchive-extractto.php
  24. Any, why won't string_pad() work? It works fine for me: <?php $text = "My Text"; echo str_pad($text, 10, "|"); //Output: My Text||| echo str_pad($text, 10, "|", STR_PAD_LEFT); //Output: |||My Text echo str_pad($text, 10, "|", STR_PAD_BOTH); //Output: |My Text|| ?> Of course, you can also use printf()/sprintf()
  25. OK, I believe this should produce the same result as your original script with much, much less overhead. <?php if (isset($_POST['key1'])) { $key1 = $_POST['key1']; $key2 = "C 0."; $key3 = "F "; $key4 = "F 0."; //Load file into an array $fileLines = file("input.txt"); //Open same file and use "w" to clear file $fileObj = fopen("output.txt","w"); //loop through array using foreach foreach($fileLines as $line) { //Keep lines which contain $key1 & $key3, but do not contain $key2 or $key4 if (strpos($line, $key1) && !strpos($line, $key2) && strpos($line, $key3) && !strpos($line, $key4)) { fputs($fileObj, $line); //place $line back in file } } //Close the file handle fclose($fileObj); } ?> <html> <head></head> <body> <form name="keyInput" action="" method="POST"> Enter Key: <input type="text" name="key1"> <br /><br /> <button type="submit">Submit key</button> </form> </body> </html>
×
×
  • 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.