Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
strtolower($a)==strtolower($b)!=match!?!? Me == WTF!?
Psycho replied to Jerred121's topic in PHP Coding Help
Look at the HTML code. Are there any leading/trailing spaces or other white-space characters. Is the space in one value the space character and an in the other? Also, if you are comparing strings you can use strcasecmp() to do a case-insensitive comparison without using strtolower(). Try the following to see what you get function fncResult ($expVal, $actVal) { //Set default values $class = 'null'; $title = 'The value in the XML was a negative null.'; $text = 'Negative Null'; // $negNulls=array("-5","-10","-15","-20","-25"); if (!in_array($expVal, $negNulls)) { if(strcasecmp($expVal, $actVal)===0) { $class = 'match'; $title = 'The values match'; $text = 'Match'; } else { $class = 'notMatch'; $title = 'The values do not match.'; $text = 'No Match: expVal: ['.htmlspecialchars($expVal).'], actVal: ['.htmlspecialchars($actVal).']'; } } echo "<td class=\"{$class}\" title=\"{$title}\">{$text}</td>\n"; echo "</tr>\n"; } -
EDIT: Maq beat me to it, but I'll post this anyway ===================================== As Maq insinuated you don't need to pull records using a SQL statement just to insert them in another table. You can do everything with a single query. Just use the first query (which you don't show) to generate the values for the select statement. Also, since I assume id is an auto-increment row you do not need to include it in the INSERT query. $query = "INSERT INTO `current_members` (`membername`) SELECT first_name FROM sometable";
-
On second thought, preg_replace() probably would be easier $testv = '<span id="sobi2Details_field_street">www.google.com</span>'; $testv = preg_replace("#<span[^>]*>(.*)</span>#", "$1", $testv); echo $testv; //Output: www.google.com
-
Assuming that you ONLY want the content in the SPAN tags and there is nothing outside the SPAN tags you need, then I'd use preg_match to just find the text within the SPAN tags. $testv = '<span id="sobi2Details_field_street">www.google.com</span>'; preg_match("#<span[^>]*>(.*)</span>#", $testv, $matches); $spanContent = $matches[1]; echo $spanContent; //Output: www.google.com
-
urlencode()/urldecode()
-
If it was set you wouldn't be getting that error message. The only place you set $message is within several IF conditions - four to be exact. If any one of those conditions fails $message will not be set. Here are the four conditions: if (isset($_POST['submitted'])) { // Handle the form. if ($fn && $ln && $e && $p) { // If everything's OK... if (mysqli_num_rows($r) == 0) { // Available. if (mysqli_affected_rows($dbc) == 1) { // If it ran OK. So, one of the above conditions is returning FALSE. If you just don't need a message if one of the above is false, then set a default value for message at the beginning of the script $message = '';
-
Forget about the checboxes for now. You need to first solve the problem with your queries. I have rewritten the entire code based upon some assumptions of how the tables area associated. Go ahead and try it - but I'm not guaranteeing it will work. Since I can't test it as I don't have your database there may be syntax errors. <?php /***Pre-School Level***/ $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die(mysqli_error($dbc)); //Run query $query = "SELECT sl.subject_level_id, ts.subject_name, IF(tols.tutor_id='{$_GET['tutor_id']}', 1, 0) as checked FROM tutor_subject_level AS sl INNER JOIN tutor_level AS tl USING (level_id) INNER JOIN tutor_subject AS ts USING (subject_id) LEFT JOIN tutor_overall_level_subject AS tols ON (tols.tutor_overall_level_subject=sl.subject_level_id) WHERE tols.tutor_id = '{$_GET['tutor_id']}' ORDER BY subject_level_id ASC LIMIT 7"; $sql = mysqli_query($dbc, $query) or die(mysqli_error($dbc)); //Process the results $checkboxes = ''; //Create variable for output $recordCount = 0; //Initiate the counter while($data = mysqli_fetch_array($sql)) { //Increment counter $recordCount++; //Start new row if needed if ($recordCount % 5 == 1) { echo "<tr>\n"; } //Display the record $subjectID = $data['subject_level_id']; $checked = ($data['checked'] == 1) ? ' checked="checked"' : ''; $checkboxes .= " <td>"; $checkboxes .= " <input name=\"subject_level[]\" class=\"subject_a\" type=\"checkbox\" {$checked}"; $checkboxes .= " id=\"subject_level_{$subjectID}\" value=\"{$subjectID}\"/>\n"; $checkboxes .= " <label for=\"subject_level_{$subjectID}\" class=\"subject_1\">{$data['subject_name']}</label>\n"; $checkboxes .= "</td>\n"; //Close row if needed if ($recordCount % 5 == 0) { echo "</tr>\n"; } } //Close last row if needed if ($recordCount % 5 != 0) { echo "</tr>\n"; } ?> <input name="level[]" type="checkbox" id="level_1" value="1"> <span class="zone_text_enlarge"><label for="level_1">Pre-School</label></span> <br/> <table> <?php echo $checkboxes; ?> </table><br/> If that doesn't work because of syntax errors, please fix them or post the errors. If it does run, but doesn't give the correct results, then we can work of fixing the query.
-
After looking at your code a little closer I do see you are using that query - but the code is a little disorganized. If I am reading that code correctly you are getting a list of subjects int eh first query then doing a second query to get all the subjects associated with a particular tutor. Then you are taking the first results and displaying checkboxes associated with each tutor for each subject returned in the first query. Seems odd to me. Why not run one query to get the results you need? If I understood yur tables and their relationships I could propose a query. Also, your code for creating the checkboxes is overly complex. The only difference is whether the checkbox is checked or not. So, instead of having two blocks of code, just create a variable to determine whether you will check the checkbox or not. It simplifies your code and ensure it willbe consistent. Example: foreach($selected_subjects as $selected_subject) { $subjectID = $data['subject_level_id']; $checked = ($subjectID == $selected_subject) ? ' checked="checked"' : ''; echo "<td>"; echo "<input name=\"subject_level[]\" class=\"subject_a\" type=\"checkbox\" {$checked}"; echo " id=\"subject_level_{subjectID}\" value=\"{subjectID}\"/>"; echo "<label for=\"subject_level_{subjectID}\" class=\"subject_1\">{$data['subject_name']}</label>"; echo "</td>\n"; $count++; //Increment the count }
-
What?! If this is your code then you do realize you are using a mysql database, right? That code doesn't make any sense, you have one SELECT query you are running ("$query"), but then you don't do anything with the results. So, that has no purpose. You are running $query1 to get the list of subjects. That query is generating duplicates. I can't tekll you if that is because ofpoor database design or not. But, a simple fix would be to add a GROUP BY clause. But, if there is a problem with the db design or current data you will want o fix that. $query1 = "SELECT subject_level_id FROM tutor_overall_level_subject WHERE tutor_id = '{$_GET['tutor_id']}' GROUP BY subject_level_id";
-
I'm not following you. You seem to imply that the "secured" data is being displayed using View<Source. View<Source will display anything that is sent to the browser. If you need that data displayed in the web page you can't secure it from the user. If you don't need it displayed to the user - don't send it in the page. If you do need it sent to the user you can "secure" it in transmission (i.e. protect from someone trying to harvest the data between the server and the user) by using https. Now, if you are trying to secure it from users who may be unauthorized then, yes, you would want to use some sort of passcode system. You *could* store the data in a flat file, but a database is preferred. Either way, a malicious user would need to get the password for the FTP server or for the DB server to get that data. What is really important is that you hash the password so even if the data is compromised someone cannot determine what any particular user's password is. Many people seem to think that the password hashing has something to do with the security of the site. It does not. If a malicious user has access to the hashed password they likely have access to all of the data anyway. The hashing is to protect the integrity of the password. Since many users use the same password on different sites, having it exposed in one site could jeopardize any other sites they use. That is why you should always use different passwords on different sites. ANyway, there are plenty of tutorials on how to hash a pasword and create a login system, so a forum post is not the proper medium to try and explain how to do that.
-
urlencode()
-
Ideas on how to do an android lock screen type of secure login
Psycho replied to sptrsn's topic in PHP Coding Help
I see three possible approaches: 1. PHP only. This would require you to submit the page on each button click. Since there will be a slight delay after each button click I wouldn't go this route. 2. Javascript only. This would provide the best performance. But, the "code" would be required to be set within the HTML code. But, since you say this isn't for security purposes that might be acceptable. 3. PHP + JavaScript: Use JavaScript to keep track of the button presses until the user is complete, then submit the page for PHP validation by submitting the page normally or through AJAX. -
Ok, it doesn't work. What have you don't to determine why it doesn't work? What debugging have you performed and what were the results? Did you at least look at the HTML source code to see what was being generated? Here is a sample page to illustrate the process: <?php session_start(); //Determine if the user selected a style if(isset($_POST['style_sheet'])) { $_SESSION['style_sheet'] = $_POST['style_sheet']; } if(isset($_SESSION['style_sheet'])) { $style = "The selected stylesheet is {$_SESSION['style_sheet']}.css"; } else { $style = "A style has not been selected."; } ?> <html> <head> <link type="text/css" rel="stylesheet" href="style/<?php echo $_SESSION['stylesheet']; ?>.css" /> </head> <body> <div><?php echo $style; ?></div> <form action="" method="post"> Select a style: <select name="style_sheet"> <option value="fancy">fancy</option> <option value="fast">fast</option> </select> <br > <button type="submit">Go</button> </form> </body> </html>
-
OK, so you want the first demo file that comes before the report time. The following code will do what you want assuming the date/time used for the file name and for the report timestapm are actually in sync. As I stated above, the timestamp you showed is in the year 2012. Anyway, the following code should work for you. Just be sure to set the correct value for $demoFldr in the getDemoFile() function. Just pass the report date to the function getDemoFile() and it will return the closest, previous demo file for that date from the same day or previous day. If there are none, it will return false. <?php function getDemoFile($rptDate) { $demoFldr = 'demo_files/'; $fileExt = 'dem'; //Convert report date to same format as demofile names for comparison $reportDateTime = date('ymdHis', $rptDate); //Year month day hour min sec //Get demofiles for SAME day of report date $patternDate = date('ymd', $rptDate); $pattern = "{$demoFldr}{$patternDate}??????.{$fileExt}"; $matches = glob($pattern); $demoMatch = prevDemoFile($reportDateTime, $matches, $demoFldr); //Check if match was made for same day if($demoMatch!==false) { return $demoMatch; } //No match made for SAME day, get files for PREV day of report date $patternDate = date('ymd', strtotime(date('d F Y', $rptDate).' -1 day')); $pattern = "{$demoFldr}{$patternDate}??????.{$fileExt}"; $matches = glob($pattern); //Return result (either file from prev day or false) return prevDemoFile($reportDateTime, $matches, $demoFldr); } function prevDemoFile($reportDateTime, $demoFilesAry, $demoFldr) { //Create vars for the output $prevDemoFile = false; $smallestDiff = false; //Find the closest demo file before the report date foreach($demoFilesAry as $demoFile) { $fileDateTime = substr($demoFile, strlen($demoFldr), 12); if( $fileDateTime<$reportDateTime) { if(!$smallestDiff || ($reportDateTime-$fileDateTime)<$smallestDiff) { $smallestDiff = ($reportDateTime-$fileDateTime); $prevDemoFile = $demoFile; } } } return $prevDemoFile; } //Usage $demofile = getDemoFile($report_date); ?>
-
By the way, I don't know where you are coming up with those timestamps but 1336837680 is not 29th december 2010, 15h, 47m, 00s it is 12th may 2012, 10h, 48m, 00s (in CST) I don't know what timezone you are in, but that wouldn't acctount for a 2+ year difference! You need to figure out what values you are actually getting if you want to compare them properly.
-
It is actually pretty simple to take the unix date and convert it to the format used for the demo files. But, one thing that is not clear in your post above is if there will be an exact match between the unix data and the demo file name. If you are only looking for an exact match this is very simple. If you are looking for a demo file that is closest to the report data you need to provide more information: are you wanting the one that is mathmatically closest or do you need the one that is closest but not past or before???
-
Without knowing the format of the input file it is difficult to provide any guidance. What data is in the data file that you can use to determine when a paragraph ends and another begins. If a hard line break is used to determine paragraphs you might want to look at fgets(). There are some problems in your code above where you are mixing things that shouldn't be. You have fopen() to open the file for reading, but then you use file_get_contents() that doesn't need fopen() to be called before it. So, if you do use fgets() then you do want to use fopen(). Another option is to use file() which will read the file into an array with each line a separate element in the array. Secondly, the function getParagraph() is written so it tries to read ALL the paragraphs from the file, not just the next one. Also, the function getFirstWord() is trying to get the word from the file. You should be passing the paragraph to the function. The process you seem to be trying to achieve - based on the names of the functions seems appropriate. But the functions are not doing what they are named. So, fix the function to getParagraph() either returns the next paragraph from the file or returns false if the end of the file is reached. Then change getFirstWord to parse out the first word from the paragraph. Then in your main function you can create a while loop and perform whatever operations you need to. Here is another tip. Only work on ONE process at a time. I would suggest just working to extract and echo each paragraph to the page. Then move on to doing something more with that data. There's no need trying to parse out the first word when you aren't even getting the paragraphs. So, start with a process such as this: while($paragraph = getParagraph($const)) { echo "Paragraph: " . $paragraph . "<br /><br />\n"; }
-
Put this at the top of every page (or better) put into an include file that is included in all pages. This assumes the radio button group is names "style_sheet". session_start(); if(isset($_POST['style_sheet'])) { $_SESSION['style_sheet'] = $_POST['style_sheet']; } Then, just use the line of code I posted previously wherever you want to include the style sheet.
-
Help with generating discounts using JavaScript
Psycho replied to barrycorrigan's topic in Javascript Help
You have duplicative code there that you can clean up. For example, in each of the IF statements you are calculating the discount AND populating the discount field. You should only calculate the discount in the IF statement, then have a single line after all those IF statements to populate the value. (Again, what is the purpose of eval() where you have used it?) Also, there is a bug in your logic. You are not checking for the decimal valules between the min/max values. In the code above a total of 249.50 would generate a $0 discount. This would do the exact same as the first block of code in your last post with a lot less work (and corrected logic) var Discount = 0; //Set default discount if ( Totamt >= 55 && Totamt < 130 ) { Discount = Totamt * 10 / 100; } else if ( Totamt >= 130 && Totamt < 250 ) { Discount = Totamt * 15 / 100; } else if ( Totamt >= 250 ) { Discount = Totamt * 25 / 100; } document.ofrm.discount.value = dm(Discount); -
Help with generating discounts using JavaScript
Psycho replied to barrycorrigan's topic in Javascript Help
C'mon, if you want help don't make us do all the work. I'm not going to read throuogh all that code to try and determine where you are wanting to calcualte the discount. Based upon your statements above it would seem the discount is generated after the user submits - but I would think you would want to display it to the user. The code you provided does not work. There is an error int he JS that is preventing it from compiling and I'm not going to try and figure it out. But, calculating the discount is pretty strait forward once you have the total: Here is the basic structure you can use, integrate it as you see fit. You code has so many flaws I'm not going to try and integrate it. BYt he way - remove all the eval() statements in your JS code, the way you are using it has no purpose and eval() should (almost) never be used. var discountPct = 0; if(Totamt>=15) { discountPct = .10; } if(Totamt>=130) { discountPct = .15; } if(Totamt>=250) { discountPct = .25; } var discountAmt = parseFloat(Math.round(Totamt*discountPct*100)/100).toFixed(2); alert('Discount percent is ' + discountPct); alert('Discount amount is ' + discountAmt); alert('New total is ' + (Totamt-discountAmt)); -
This topic has been moved to CSS Help. http://www.phpfreaks.com/forums/index.php?topic=323206.0
-
Moving to the CSS forum as this has nothing to do with PHP/Math! You will also want to show the code that defines the textblack class used above.
-
Because the OP stated that the radio button options are on the login page I will assume that the user will be required to submit the value and AJAX is not needed. Just make sure the radio button options are included in the same form that is submitted for login. There are several different approaches you can take on the actual implementation. You could dynamically set the background color within the style properties by either writng the value into the page's style properties or within an external style sheet that is a PHP file. Or, you can have two different style sheets (one called fancy.css and the other called fast.css. I would go with the latter aproach. You could change up much more than just the background between the two styles if you wish. Anyway, when the user submits the login page you can take the submitted value and save it to a session variable (or cookie). Then on each page load get the session value and include the line for the css file. Something like this <link type="text/css" rel="stylesheet" href="style/<?php echo $_SESSION['stylesheet']; ?>.css" />
-
That logic is a little backwards. You are trying to list all the characters to replace. It's easier to define the regex expression to replace all characters except numbers. Then you still need to check if there are the right number of digits. I think this will work better for you function checkPhone($number) { //Remove all non digit characters $number = preg_replace("#[^\d]#", '', $number); //If less than 10 digits return false if(strlen($number)<10) { return false; } //Return formatted number return preg_replace("#(\d{3})(\d{3})(\d{4})#", "$1-$2-$3", $number); }
-
Right, as I suspected there is a problem that prevents the JavaScript from being compiled correctly. So, it isn't being run . . . at all. Where that error is can be difficult to determine. I don't see anything that jumps out at me from the code above. If there is any JS code that you have not posted above the problem behat code. The first step I would take to debug this problem would be to install the Firebug extension for Firefox. That is a great tool for JS debugging. If that doesn't help, then I would suggest commenting out sections of code until you can get the JS to run. Once it starts to run then uncomment code until you find the offending line. Then you know what needs to be ficed.