-
Posts
16,734 -
Joined
-
Last visited
-
Days Won
9
Everything posted by PFMaBiSmAd
-
How to implement the awesome Search-as-you-Type (SayT) in PHP
PFMaBiSmAd replied to theITvideos's topic in PHP Coding Help
Actually, that code (search-responder.php) was not written very well. The main code (last 17 lines of the file) should not know or care where the data is stored at (i.e. it should only be necessary to modify the code in the one function that actually searches for the matching data.) To modify that file to use a database, you would - 1) Remove the GetData() function definition, remove the line in the main code that calls GetData(), and remove the $data parameter from the GetResults() function call and the GetResults() function definition parameter list. 2) Rewrite the code in the GetResults() function so that it searches your database table for entries that have the same first x characters the same as the supplied $query parameter and returns the matching results. -
I'm going to guess you either have some url rewriting that does not pass the query string on the end of the url to the rewritten url or you have some code in the file that is clearing the $_GET variables.
-
The error either means that your query failed or that there are zero rows in the result set (mysql_result() has the misfortune of being the only mysql statement that produces an error simply because there are no rows, other methods of fetching data from a result set return a false value when there are no rows.) It is also possible that you are reusing a variable or overwritting it or using the wrong variable name in the mysql_result() statement. You would need to troubleshoot A) If the query failed or not, and B) if your query worked, why there are no matching rows. Your code should already testing if the query worked before attempting to access any of the data from the query and also using mysql_num_rows() to check if there are any rows in the result set before attempting to use mysql_result().
-
All of your string data values (everything between the pair of single-quotes) in your queries have a space character in them, so of course you are getting an extra space stored with the data.
-
So, you already have the necessary code in place to do this and all it would take is adding one line of code to set a session variable with the id in your log in code and then another line of code to test that value in your member profile page.
-
By default, session variables last one browser session (the session cookie lifetime is set to zero so that the cookie is only held in the browser's cookie cache, which is deleted when the browser is closed.) To make a session last longer requires that you both extend the session cookie lifetime setting and the session garbage collection maxlifetime setting. If you are on a shared web server you must also make the session save path point to a 'private' folder within your account's folder tree so that only your session settings affect your sessions. The normal way this is accomplished is to use a regular cookie to just identify the visitor (using a unique and hard to guess and hard to produce value) and his logged in status is kept in your user table in the database.
-
The code on the page must check if the current visitor is both logged in and has permission to access that page, either because it is his information or the current visitor is the site's administrator.
-
Given that variable variables take three times longer to reference (to set or to get a value) than an array, that you apparently have a lot of data you are processing this way, and that your code was probably already taking two-three times longer than necessary (based on what you did show you are doing), taking the advice from experienced programmers and making simple changes to the method you are using would likely result in an overall 4x-5x performance increase over what you have ended up with. I would take that kind of increase in performance any day, especially if the script will be used over and over. So yes, we also see newbies not taking simple advice to cut their losses and make the process of getting a working script go easier for them; take many times longer to produce working code because of the amount of unnecessary code and fighting a bad design; and coming up with code that is so complicated for what it is actually accomplishing that it cannot be understood by the person who wrote it and now wants help with it or anyone trying figure out what is doing in order to give that help.
-
Weird weird PHP error Fatal error: Allowed memory... Please help!
PFMaBiSmAd replied to shlomikalfa's topic in Applications
Either your script is doing something that requires a large amount of memory, such as resizing an image, reading a huge amount of data and creating arrays in memory from the data OR your script contains a logic error where it loops forever and consumes all the available memory. Posting the script that produces the error would be he quickest way of getting help with the problem. -
Unless a person is dead, their age is not a fixed value (changes once a year for most people.) I recommend storing the date of birth in your table and calculating the age when necessary.
-
Edit: Basically says the same ^^^ Please simply just use an array. Arrays are for sets of related data. Creating a series of numbered variables takes three times longer to reference, takes more overall code, and also requires that you keep track of how many of them you created. Arrays don't have any of these drawbacks.
-
Add the following two lines of code after the line with your first opening <?php tag to get php to report and display all the errors it detects - ini_set("display_errors", "1"); error_reporting(E_ALL);
-
No one asked for a link to your site. Someone asked for a link to the site where you got the login script so that you could be helped on this forum. A captcha won't stop someone from attempting to inject sql. Your code must do that by validating AND escaping the user supplied values being put into the query. If your goal is to add a captcha to either your registration or comment forms, you would need to start with the actual working code for your page. The code you have posted here is clearly not working code. Your sign up/registration form link on your site also does nothing.
-
You (your browser) accesses images on web pages using their URL. Php accesses files on the server using the file system path. Those two things are not the same. $_SERVER['DOCUMENT_ROOT'] is the file system path to your document_root folder (if it is setup correctly) and only has meaning to php's functions that act on files on the server. To get help with what your code is doing or not going, you would need to describe the symptom you see in front of you when you try it.
-
Time is fairly specific and changes all the time. Every new second that goes by makes it kind of hard to match other time values. select * from your_table where DATE(your_datetime_field) = CURDATE();
-
NOTE: This problem was continued in another thread and solved in that thread.
-
Did you even read the error message? It concerns the data type of one of the parameters being supplied to the Imagettftext() function call.
-
The code you have posted is still logically incorrect. You are looping over the data and overwriting the single $x,$y,and $text variable. After the end of the loop, they only have the last values. Logically, you are trying to accomplish this - <?php //connect $db = mysql_pconnect($dbhost,$dbuser,$dbpass); mysql_select_db("$dbname",$db); $query="SELECT * FROM info"; $results = mysql_query($query); $im = ImageCreateFromPng("test.png"); $black = ImageColorAllocate($im, 255, 255, 255); while ($row = mysql_fetch_assoc($results)) { $x = $row['x']; $y = $row['y']; $text = $row['text']; Imagettftext($im, 12, 0, $x, $y, $black, 'verdana.ttf', "$text"); } header("Content-Type: image/jpeg"); Imagejpeg($im, '', 100); ImageDestroy($im); ?>
-
In real applications, where you can have hundreds of functions, it creates more work and wastes time keeping track of all the variables you are passing into functions using the global keyword because you cannot duplicate any of the names between functions in order to prevent interference between the functions. Using functions are supposed to make coding easier and take less time. Not more time. Don't use the global keyword.
-
Please don't suggest using the global keyword. In the example you posted, what if I have more than just two variables in my application that I need to use with the function and want to reuse the function? <?php $a = 1; $b = 2; $w = 44; $x = 184; $y = 12; $z = 13; ?> ^^^ I need to use the function to find what output it gives for $w and $x and for $y and $z. Writing the function using global, it is not a general purpose function and it can only operate on the data it was hard coded to work with. You would need to use more code to copy the $w and $x values to $a and $b, call the function, then copy $y and $z to $a and $b before you can call the function again. Functions should aways be written to be general purpose and use parameters when there are called.
-
See the Edit: in my post above. Have you missed the point of looping over data and using the data in a (one) Imagettftext() statement that is in the code that is inside of your loop. From your first post - Imagettftext($im, 12, 0, $X, $Y, $black, 'verdana.ttf', "$text"); ^^^ You use that line of code and simply set the $x and $y variables from the x and y values stored in the database table.
-
Your other thread probably did not receive any replies because the question did not make enough sense for anyone to know what the question was and when you started bumping it, you did not attempt to supply any further information. You should not be storing php code statements in a database. You should be storing DATA. That's kind of why they are called databases. The only thing that appears to be data in what you are doing is the x,y coordinate of each point that was picked. Only those two values (and any other relevant data) should be stored in your database table. When you retrieve the data, you simply put each set of x,y coordinates into a Imagettftext() statement when you loop through the result set from the query. Edit: Also, you can only output one content-type header, followed by the image data at one time, so the code inside your while(){} loop would never work with more than one row in your table. You would need to move most of that code outside of the while loop so that you only try to create one image with all the points on it. Edit2: And frankly, this IS the same problem as in your previous thread and you should have continued it in that thread instead of starting a new thread for it.
-
There are at least 3 things that could prevent what you are doing from working - 1) Something you have in your image output code that you have not shown in the post, such as using a short open tag or intentionally echoing something before the code you have shown, is preventing the code from being seen as code or is preventing the content-type header from working. Could you post the WHOLE contents of that file? Your "sys/functions.php" file or your dbConnect(); function could also be causing some inadvertent output to the browser that is preventing the content-type header from working. 2) Your image output code file could have the Byte Order Mark (BOM) characters at the start of the file (put there by your editor when the file is saved for a UTF-8 encoded file) and the content type header is not working. The display_errors/error_reporting settings that have been suggested is something you should always have set as suggested when developing and DEBUGGING php code and would point out if there are any problems that php can detect that would help solve the problem. Having the display_errors/error_reporting settings set as suggested would also help find if there is any other output, like mentioned in item #1 above, that is preventing the content-type header from working. 3) magic_quotes_runtime could be on which would cause your image data to be double escaped and invalid when you retrieve it and output it. What does the following php code show for the magic_quotes_runtime setting - <?php var_dump(get_magic_quotes_runtime()); ?>
-
In a way, that is really really funny ^^^. The separator characters you implied as being part of the data is simply something you echoed between each piece of data as you were iterating over it from the array it is already in. Your data is already in an array. You are already iterating over it using a foreach() loop. ALL YOU NEED TO DO is INSERT the data into your database table inside of the foreach() loop you already have.