Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
Looking for best practice regarding paths in include/require
Psycho replied to martin05rc's topic in PHP Coding Help
I will use a variation on the last code example you provided. I will have a config.php or similar page that is utilized on any pages. This will include things such as database connection info and paths (of course this is not located in a web accessible folder). In that file I will create variables for the different folders I use. These may be internal paths (i.e. for the server to access, such as includes) or they may be external paths such as to use for image links that the user will access. This allows me to easily change the directory structure without recoding all the pages. Also, the paths are never relative. Here is a good article which speaks to file structure with respect to security and implements a manner to create those path variables. http://www.phpguru.org/static/ApplicationStructure -
"test" != "%test%" They are two different strings and are not equal. I think you are confusing the MySQL functionality when using LIKE. E.g. SELECT * FROM table WHERE field LIKE '%test%' That would match records where the field1 value is 'test'. The same does not hold true when doing a string comparison in PHP. I think you are looking for a regex solution. But, since you have not provided details of what you are trying to accomplish I can't say for sure.
-
Help you with what? You already have the value! Although you might want to trim it. $date = "Wednesday, June 23, 2010"; preg_match ("/[,](.*)[,]/", $date, $matches); $valueYouWant = trim($matches[1]);
-
That really doesn't answer the question. Just because the file is named classes.php doesn't mean anything. Is that line in a properly declared CLASS. Example: <?php class Time { var $sTime; function GenerateCurrentTime(){ $this->sTime = gmdate("d-m-Y H:i:s"); return $this->sTime; } } ?>
-
I already explained why your function would not work because of variable scope and recursion in your function. Although, it can be a difficult concept to understand. I also provided a revised function that should prevent those problems and it provides the ability for debugging if it still doesn't work. Did you even try the code I provided? I will try to provide a more detailed explanation using the example you provided above, This is based upon your first posted code to explain why it did not work. 1. Check the parent number of 5. (iteration #1 of the function) 2. Parent number is 4 3. Increment level 0 to 1 (in iteration #1 of the function) 4. Check parent number of 4 (iteration #2 of the function) 5. Parent number is 2 6. Increment level 1 to 2 (in iteration #2 of the function) 7. Check parent number of 2 (iteration #3 of the function) 8. Parent number is 0 9. Return increment level 2 (to iteration #2 of the function) In each iteration of the function $level is a different variable and the last iteration of the function returns the $level to the previous iteration and there is no way to return that value to the initial iteration and return to the originally calling line of code. You can either fix it as I shoed by returning the level to each iteration by using if ($parent_id != "0") { $level = check_level($parent_id, ++$level); } return $level; Or, you could also pass $level by reference so you are dealing with the same $level on each iteration. This is kind of difficult to explain in a forum post as it deals with some unique issues concurrently. You can take a look at the manual dealing with variable scope: http://php.net/manual/en/language.variables.scope.php
-
Without knowing what parameters are being passed to the function and what data is being returned from the database, there is no way for us to know what should and should not be returned. You will need to debug the code. However, because you combined multiple functions into the line to define $check, that cannot be done effectively. You are also suppressing errors which doesn't help either. Lastly, when you do call check_level() subsequent times, the return value is not being used due to variable scope and a recursive function! Let me try to explain. When check_level() is called the first time $level has a value. If the check value doesn't equal 0 you make a subsequent call to check_value passing an incremented $level value. In the second running of check_value(), the $level value is a different variable than the one in the first running of the function due to variable scope. So, let's say that second call does have a check value of 0 and returns the $level. It is returning that value to the first funttion call - not to the original line that called the function. It is kind of difficult to explain. But here are a couple of examples function getThree($input) { if($input<3) { getThree(++$input;); } else { return $input; } } echo getThree(1); //Ouput: - nothing - function getThree($input) { if($input<3) { $input = getThree(++$input;); } return $input; } echo getThree(1); //Ouput: 3 Try running the following: function check_level($id, $level=0) { $query = "SELECT parent FROM page_categ WHERE id = '$id'"; $result = mysql_query($query); $record = mysql_fetch_assoc($result); $parent_id = $record['parent']; //Echo info for debugging purposes echo "ID: $id<br />\n"; echo "Level: $level<br />\n"; echo "Parent ID: $parent_id<br />\n"; if ($parent_id != "0") { $level = check_level($parent_id, ++$level); } return $level; } NOTE: The first call to check_level() will not require the second parameter since it is now defined in the function parameters.
-
As Neil suggests change the somefile.js to somefile.php and include the necessary code to define and write the _DIR_ value into the file. Of course you need to change any of the files that call the js file to point to the new file name: <?php // Insert code to define _DIR_ ?> $(document).ready(function() { $('div.menu_list').cluetip( { width:570, cluetipClass: 'jtip', arrows: true, dropShadow: false, hoverIntent: true, sticky: true, positionBy: 'mm', hoverClass: 'highlight', mouseOutClose: true, closePosition: 'title', leftOffset: 0, closeText: '<img src="<?php echo _DIR_; ?>plug/cluetip/stop_win.png" alt="close" />' });
-
Well, how do you plan in accepting payments? Probably the easiest method is to use PayPal and use the developer tools they have available. https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/library_documentation
-
using addition and division in a MySQL table row?
Psycho replied to shmeeg's topic in PHP Coding Help
That IS one of the uses for UPDATE. No need for an ADD UPDATE table SET field = field + $newValue Although, I am in agreement with ignace. Do not store calculated data. Instead create a new record for each players results from each game. You can then easily get calculated totals when you query the database. Example SELECT SUM(goals) + SUM(assists) as total GROUP BY player_id Databases are for storing the 'data'. By compiling your data into totals you can prevent yourself from doing other types of calculations later. For example, what if you wanted to see what is the most points any player had in a single match? -
That's it - I'm done with this thread. I've spent way too much time trying to interpret your requests due to vague details and writing full examples for you to use. Then you want to change the requirements. Plus you don't even make an attempt at trying things. If it doesn't work exactly how you want then you just respond back with another poorly described request. This is a JavaScript HELP forum, to get help with your code. I think you should try the Freelancing forum to see if someone is will to take this on as a paid project. Good luck to you.
-
Identify the checkbox that is checked, in a group of checkboxes
Psycho replied to help_lucky's topic in Javascript Help
You are not understanding what I previously said. When a form is submitted checkbox values are only included in the POST data only for checkboxes that are checked. So, if you have two checkboxes and one is checked on form submission only the one checked checkbox field and value will be included in the post data. The other will not be included in the post data as it is not checked. If you are getting both fields included in your post data then they are both checked. You're obviously doing something wrong. -
This will generate the passwords into an array for you as you specify. Use implode() to convert to a string to write into the file: <?php function generatePasswords($count, $length) { $passwordAry = array(); $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789$*%><)(]['; for($i=0; $i<$count; $i++) { $passwordAry[$i] = $characters[rand(0, 51)] . $characters[rand(0, 51)]; $passwordAry[$i] .= substr(str_shuffle($characters), 0, $length-2); } return $passwordAry; } $passwords = generatePasswords($password_count, $password_length); ?>
-
Which documentation are you referring to? I think the PHP documentation is excellent. For the mysql_query() function the documentation states in part (emphasis added): The problem with the documentation is that many people don't read all of it (myself included). We read enough to understand the basic usage and then plod ahead from there falling into the same traps that the documentation warned us against.
-
You are assigning the RESULT of the query to the variable $output. So, the variable is simply pointing to that result. You need to extract the record(s) from that result as shown by F1Fan. Although, here was my method: $result= mysql_query("SELECT COUNT(*) FROM email_subscriptions"); $count = mysql_result($result, 0); echo "Join {$count} other users on our mailing list";
-
Or even easier: $output = preg_replace("/(\r\n| )+/", ", ", trim($input)); This will replace one (or more in succession) occurances of a line break with a single comma space. Also trims out unneeded extra spaces: $input = "V265141004 V265141004 V241889035 "; $output = preg_replace("/(\r\n| )+/", ", ", trim($input)); echo $output; Output: V265141004, V265141004, V241889035
-
You are using a custom class for the database operation. There is no way for us to help you unless we understand that class.
-
That might make sense if the user entered text is of relatively short length. If it is quite large, looping through each and every word doesn't seem too efficient. Here's a quick exampe of building a regex pattern for each banned word. Most importantly, it uses the word boundry placeholder \b so it will only find exact matches. I also made it case-insensitive (Note I used \\b in the code below to overcome JS's escaping of \) <html> <head> <script type="text/javascript"> var special_words_arr=new Array("cheer","bloody","war","terror"); function checkForSpecialWords(inputStr, specialsAry) { var pattern; for(var i=0; i<specialsAry.length; i++) { pattern = new RegExp('\\b'+specialsAry[i]+'\\b', 'gi'); if(inputStr.match(pattern)) { return true; } } return false; } function checkInput() { var userInput = document.getElementById('input').value; if(checkForSpecialWords(userInput, special_words_arr)) { alert('Specials were used'); } else { alert('Specials were NOT used'); } return; } </script> </head> <body> <textarea name="input" id="input"></textarea> <button onclick="checkInput();" >Check Input</button><br /> </body> </html>
-
<?php $SQL = "Select * from table"; $RESULT = mysql_query ($SQL) OR die(mysql_error()); $names = array(); $valuess = array(); while($ROW=mysql_fetch_assoc($RESULT)) { $names[] = $ROW['name']; $values[] = $ROW['value']; } $nameStr = implode('|', $names); $valueStr = implode(',', $values); //Want to take all the values from name and value from the while loop and stick them in a google chart like so. But won't know how may there are ?? */ echo '<img border="0" src="http://chart.apis.google.com/chart?cht=p3&chco=17186B,348017,CC0000&chd=t:$,'.$valueStr.'&chs=500x200&chl='.$nameStr.'" />'; ?>
-
Well, there is a lot going on there. And, to be honest, I'm not going to read through all of that and try to understand it. However, I do see that you should be using JOINS in your queries. At the beginning you are doing a query and dumping the results into an array. Then you do a foreach loop on the results and run a query for each result. insted you should just do a single query using a JOIN to get all the records in one query instead of many. I don't know what makes up the entire queries so I can't provide any guidance on how to create the properly joined query.
-
Perfect Canonical Link | Need Help Altering Syntax
Psycho replied to Bryan Hadaway's topic in PHP Coding Help
You need to review the scripts you are running on that page when it loads. After that page initially loaded it took almost 10 seconds for it to become responsive and it was hitting my CPU and memory usage pretty hard. -
Identify the checkbox that is checked, in a group of checkboxes
Psycho replied to help_lucky's topic in Javascript Help
Something isn't right. When you submit a form, checkboxes are only included in the submitted values if they are checked. Since both checkboxes are included in your post data, that means that both were checked. I would guess that your Javascript is "checking" them before the submission. -
function imgRotate() { var imgObj = document.getElementById('rotate'); imgObj.src = "http://localhost/test/test.php?"+Math.random(); setTimeout ( "imgRotate()", 30000 ); } window.onload = function() { var now = new Date(); var nextImage = (30 - now.getSeconds() % 30) * 1000; setTimeout ( "imgRotate()", nextImage ); }
-
The JS code I provided previously will reload the image every 30 seconds without reloading the page.
-
Well, I tested that code so I know it works. I'm guessing the problem is you are not specifying the correct path to the images. The PHP file is in the root of your site and in that file you are specifying JUST the image name. That will only work if the images are also in the root of your site. Otherwise, you need to provide that. You could either provide the path for each image in the array that defines the images. But, if all the images are in the same directory then just set it within the header command. For example, if the images are all in a folder as follows: [root]/images/rotating/ Then change the PHP script to this: <?php $images = array( "image1.jpg", "image2.jpg", "image3.jpg" ); $secondsSinceMidnight = time() - mktime(0, 0, 0, date('m'), date('d'), date('Y')); $imgIndex = ($secondsSinceMidnight/30) % count($images); $host = $_SERVER['HTTP_HOST']; $image = $images[$imgIndex]; header("Location: http://$host/images/rotating/$image"); ?> If that doesn't work, right-click on the missing image icon and check the properties to see if the path is correct for the image you want to display.