|
Firequirks posted:I had been using the Form Helper functions to make my form Yeah, his code is far from best practice, as it's a hack job to show how fast things can be in a hypothetical best-case world. You can cheat if you really want the "simplicity" by either manually making the submit button, leaving the name off and changing your if statement: code:
code:
|
# ? Jan 16, 2010 23:52 |
|
|
# ? May 16, 2024 10:16 |
|
Thanks for all the suggestions guys. I think I like this one the most:thedaian posted:Other possible options, beyond what's already been suggested: Create a phone/address book. A todo list (with bonus 'email me when something is nearly due option). A simple Facebook Application. Any number of simple turn based games (tic tac toe, connect 4, etc). More specifically, the todo list. I've been hearing some things about HTML5 being able to cache databases offline so I did some light reading on the subject, and it seems these databases are stored with javascript (forgive me if my terminology is wrong). I'd like to build an iPhone webapp similar to a todo list with local database storing, but also able to sync with a MySQL database so that all isn't lost if the person clears their cache or whatever. Is this possible? I guess this might not belong in this thread but I figure I'll try.
|
# ? Jan 17, 2010 01:34 |
|
Lumpy posted:or you can do it the way you are doing it, and unset 'submit' before you process: Thanks for your help! I think the unset option is the best one for me as it keeps everything the most tidy. I expect these forms to grow in the future, so it is a treat to keep the code for handling them short. (Also I tried your last suggestion, and no, it still doesn't work because it prints out name="" in the form. Oh well!)
|
# ? Jan 17, 2010 19:35 |
|
I have a function I use for cleaning user input that's expected to be a string. One of the things it does (dependent on an argument) is to run htmlspecialchars() on the string. I want it to just convert >, <, " and & into entities, which is what it says on the php site it should do. But I find that when users enter some non-western characters, like eastern european accented letters or Chinese characters, they get turned into character entities too, which I wasn't expecting. I would add the optional argument specifying a character set, but I don't know what to try, since it claims that specifying UTF-8 will have the same effect as leaving it at the default latin1. Why is it behaving like this?
|
# ? Jan 18, 2010 21:26 |
|
Hammerite posted:I have a function I use for cleaning user input that's expected to be a string. One of the things it does (dependent on an argument) is to run htmlspecialchars() on the string. I want it to just convert >, <, " and & into entities, which is what it says on the php site it should do. But I find that when users enter some non-western characters, like eastern european accented letters or Chinese characters, they get turned into character entities too, which I wasn't expecting. I would add the optional argument specifying a character set, but I don't know what to try, since it claims that specifying UTF-8 will have the same effect as leaving it at the default latin1. Why is it behaving like this? If you only want <, >, and & to be converted, just use str_replace: < = < > = > & = & amp; You may prefer to use strip_tags if that's all you're trying to do?
|
# ? Jan 18, 2010 21:42 |
|
gwar3k1 posted:If you only want <, >, and & to be converted, just use str_replace: Of course you're right, I don't know why I didn't just think to do that before. I'm a bit wary of using strip_tags, because I want to allow through certain tags but I don't want to allow them to have any attributes. To be more specific, I've been doing something like the following: code:
|
# ? Jan 18, 2010 22:11 |
|
The documentation does seem to suggest that htmlspecialchars() just changes those four characters, so maybe I am blaming the wrong thing. Is it possible that because the webpage the form comes from is marked as charset=iso-8859-1, the user's browser is converting foreign characters to character entities before they're sent to the site? That would explain the behaviour I see.
|
# ? Jan 18, 2010 23:07 |
|
I don't know about your new question, but could you use a regex for allowing certain tags but knocking off attributes?
|
# ? Jan 19, 2010 00:29 |
gwar3k1 posted:I don't know about your new question, but could you use a regex for allowing certain tags but knocking off attributes? I've already been scolded for trying to use a regex on html. Maybe try textile? I haven't used it myself but it's been mentioned here a couple times. For my own BBcode type of thing that uses SA style tags I use htmlspecialchars, then str_replace to replace "[b]" with the appropriate html, then I load that string into a DOMDocument and extract the <body>.
|
|
# ? Jan 19, 2010 01:07 |
|
Hammerite posted:I have a function I use for cleaning user input that's expected to be a string. One of the things it does (dependent on an argument) is to run htmlspecialchars() on the string. I want it to just convert >, <, " and & into entities, which is what it says on the php site it should do. But I find that when users enter some non-western characters, like eastern european accented letters or Chinese characters, they get turned into character entities too, which I wasn't expecting. I would add the optional argument specifying a character set, but I don't know what to try, since it claims that specifying UTF-8 will have the same effect as leaving it at the default latin1. Why is it behaving like this? Quick question but why wouldn't you want stuff converted to entities? Also, perhaps: http://daringfireball.net/projects/markdown/ http://michelf.com/projects/php-markdown/ Would be a better option? It sounds like you're setting yourself up for an XSS attack. Rat Supremacy fucked around with this message at 11:43 on Jan 19, 2010 |
# ? Jan 19, 2010 11:41 |
|
haywire posted:Would be a better option? It sounds like you're setting yourself up for an XSS attack. Am I? I'm working quite hard to try and make sure that users can't get through any bad HTML (indeed any HTML apart from <b> and <i> tags without attributes), but I'm concerned that you think they might get something through. Could you give me more of an idea what you think I might be leaving myself open to?
|
# ? Jan 19, 2010 18:10 |
|
Hammerite posted:Am I? I'm working quite hard to try and make sure that users can't get through any bad HTML (indeed any HTML apart from <b> and <i> tags without attributes), but I'm concerned that you think they might get something through. Could you give me more of an idea what you think I might be leaving myself open to? If you really are just after bold and italic, just use simple BB code like fletcher pointed out: [b ]/[i ]. Remove all tags in your sanitation routine, leaving in your custom tags then process them after the sanitation. If your users don't read the instructions on how to emphasize their text properly, then gently caress 'em. edit: A question of my own: how would I go about implementing a tag system? For categories, I have a table with a list of categories and a tiny int id; I put the id in a category field with my article. Should I take the same approach and comma separate the tag ids? How do I search based upon tag? Where tagid like '%id%';? gwar3k1 fucked around with this message at 21:38 on Jan 19, 2010 |
# ? Jan 19, 2010 19:55 |
|
gwar3k1 posted:A question of my own: how would I go about implementing a tag system? For categories, I have a table with a list of categories and a tiny int id; I put the id in a category field with my article. Should I take the same approach and comma separate the tag ids? How do I search based upon tag? Where tagid like '%id%';? No, never every store keys in a comma delimited list, its a disaster. Use an intermediate table with 2 fields in it, one is the tag ID, the other is the article ID. Then select all the article IDs that match a specific tag, or vice versa. Its a lot easier update or remove tags using this approach as well.
|
# ? Jan 20, 2010 18:50 |
|
Begby posted:No, never every store keys in a comma delimited list, its a disaster. Of course, you're right, and I should have thought of that immediately.
|
# ? Jan 20, 2010 18:54 |
|
I have script that receives a request from a user, validates the request against an external server and then sends back a relatively large file. Right now we are using readfile to return the file in the response, but that is using a large amount of memory and causing the server to choke in load tests. Is there a way to return a large file from PHP that is more efficient on memory? Ideally, I'd like to have the file resident in memory no more than once, but I'm not sure if that's possible. Edit: I tried using readfile_chunked as described in the comments here http://cn2.php.net/manual/en/function.readfile.php#48683 and it actually made the memory usage much, much worse. The1ManMoshPit fucked around with this message at 22:38 on Jan 20, 2010 |
# ? Jan 20, 2010 21:10 |
|
The1ManMoshPit posted:I have script that receives a request from a user, validates the request against an external server and then sends back a relatively large file. Right now we are using readfile to return the file in the response, but that is using a large amount of memory and causing the server to choke in load tests. I would need to see more details to tell you exactly what to do but have you ever looked into using curl?
|
# ? Jan 21, 2010 00:44 |
|
I probably should have specified that the file I'm returning is local to my server running the PHP script, and there are only a few (right now actually only one) unique files that are ever returned. So the requests go like:
|
# ? Jan 21, 2010 04:26 |
|
The1ManMoshPit posted:Is there a way to return a large file from PHP that is more efficient on memory? Ideally, I'd like to have the file resident in memory no more than once, but I'm not sure if that's possible. Read chunking is the compatibility method of using fpassthru(): http://hk2.php.net/manual/en/function.fpassthru.php
|
# ? Jan 21, 2010 04:34 |
|
The1ManMoshPit posted:PHP loads a copy of the file into memory once for every request coming in even though I am returning the exact same file for each of these requests. This quickly overwhelms the server if a large number of requests come in simultaneously. Ideally there would be some way for me to just have the file loaded into memory already and have each instance of the script just read and output straight out of that memory. Are you running against Apache? If so, do you have the ability to install new Apache modules? If so, give mod_xsendfile a try. This module offloads the actual serving of the file to the web server. Instead of reading the file and and outputting its contents, all your script has to do is emit a few headers.
|
# ? Jan 21, 2010 04:47 |
|
Quick, likely not very bright question: I have a table full of records that are all Timestamped, and I'd like to echo all the records currently in the table that have today's date on them. What's the php SELECT whatnot to grab that? I have the rest of it crudely written out, I just can't seem to figure out how to write the select statement. Thanks.
|
# ? Jan 21, 2010 07:47 |
|
apekillape posted:Quick, likely not very bright question: Use a date function to get todays date in the format yyyy-mm-dd and do a SELECT on the table where the timestamp field is like your date variable.
|
# ? Jan 21, 2010 09:05 |
|
gwar3k1 posted:Use a date function to get todays date in the format yyyy-mm-dd and do a SELECT on the table where the timestamp field is like your date variable. So something like $today = date("Y-m-d"); $query = "SELECT message FROM gimme WHERE timestamp LIKE '$today%'"; Is that right? I think I'm screwing up the variable in the query, at the very least.
|
# ? Jan 21, 2010 09:12 |
|
apekillape posted:So something like $query = "SELECT message FROM gimme WHERE `timestamp` LIKE '".$today."%'"; I've always preferred concatenation to putting variables right into the string. The characters around 'timestamp' are the key next to the 1 on most keyboards, with ~
|
# ? Jan 21, 2010 10:50 |
|
Xenos posted:Are you running against Apache? If so, do you have the ability to install new Apache modules? If so, give mod_xsendfile a try. This module offloads the actual serving of the file to the web server. Instead of reading the file and and outputting its contents, all your script has to do is emit a few headers. This looks perfect I'll give it a try. Thanks!
|
# ? Jan 21, 2010 19:28 |
|
Ugh, ok, one more. I'm trying to do some html scraping, which I'm kind of 50-50 on. If the code isn't too complicated, I can usually whip something out. But I've never really understood what the hell I'm doing, I just kind of Frankenstein something together. So, for example, I want to grab this page: http://www.songmeanings.net/artist/directory/a/ And insert all of the artist names and urls to their pages into a database. What's the best way to do this? I've tried looking at simplexml/xpath stuff, dom parsing, all kinds of things. I mostly get caught up on trying to grab the artist name from between the tags, I just don't understand how that works. Is there a recommended tutorial for this somewhere or anything? Sorry to be a bother, I've been up all night trying to make sense of how this works and it's just starting to weigh on me.
|
# ? Jan 21, 2010 23:23 |
apekillape posted:I'm trying to do some html scraping...I just kind of Frankenstein something together. That sounds pretty normal when you're scraping HTML. I'd use DOMDocument to grab that information. Do a getElementById() on the "listing_artists" div and grab the first child (the <table>). Then do a getElementsByTagName() and get all the <tr> elements in that table. From there you can get down into the <a> that links to the artist page, using the textContent of that anchor for the artist name. If you get hung up just look for tutorials on using DOMDocument and if you get hung up after that, ask your question here!
|
|
# ? Jan 21, 2010 23:34 |
|
fletcher posted:Do a getElementById() on the "listing_artists" div and grab the first child (the <table>). Then do a getElementsByTagName() and get all the <tr> elements in that table. From there you can get down into the <a> that links to the artist page, using the textContent of that anchor for the artist name. Haha, I have almost no idea what that means, but I've at least got some decent keywords to google up. Thanks a lot man, I'm sure I'll be back shortly.
|
# ? Jan 22, 2010 00:01 |
|
apekillape posted:Haha, I have almost no idea what that means, but I've at least got some decent keywords to google up. Thanks a lot man, I'm sure I'll be back shortly. Whatever tool you use (I usually brute force it with regex/strpos) you're really only (manually, visually) looking for a pattern in the html they use which you can latch onto and then repeatedly strip out. I usually look for <tag class="SOMETHING" or something similar that will identify the beginning of each 'record'. Scraping html is, in general, in every way a dirty job; but sometimes you have to do it.
|
# ? Jan 22, 2010 00:06 |
|
Lemme throw up an example.code:
I just want to grab the Artist Name and URL and throw them into a sql database, nothing really that fancy. I just... can't seem to wrap my head around how that's done. I can steal code here and there to loop through the links, but the main issue I'm having is figuring out which bit to modify so that it grabs the name cleanly and adds it with the proper associated link to drop into the DB. If it's not asking way too much, could pretty much anyone throw up some sample code for at least the bit where that one part is done? I'm pretty good at catching on when the individual parts are explained, for some reason this one thing is just blowing my mind up though.
|
# ? Jan 22, 2010 00:15 |
|
apekillape posted:If it's not asking way too much, could pretty much anyone throw up some sample code for at least the bit where that one part is done? I'm pretty good at catching on when the individual parts are explained, for some reason this one thing is just blowing my mind up though. This assumes the page is saved in the same directory as 'subject.htm' and cheats by using the fact that the ungodly huge single line is where all the actual target data is. It makes a nice neat associative array, with keys f the artists and the urls and counts inside it. Its quick, brute force, and ugly. But it (seems to) work on that page. Its basically just a case of trimming the data to the target section, splitting it into unparsed records with preg_split and then parsing each chunk with preg_match. php:<?php $parsed_records = array(); $ifh = fopen('subject.htm', 'r'); if($ifh) { while(!feof($ifh)) { $line = rtrim(fgets($ifh)); if(strlen($line) > 5000) // its the huge line of all data! { $records = preg_split("/<tr class=.*>/U", $line); foreach($records as $record) { $matches = array(); $result = preg_match('/href=\"(.*)\">(.*)<.*alignCenter.*>([0-9]+)</U', $record, $matches); if($result > 0) { $parsed_records[$matches[2]] = array('href'=>$matches[1], 'count'=>(int)$matches[3]); } } } } fclose($ifh); } // $parsed_records is an array("artist"=>array("href"=>url, "count"=>integer)) foreach($parsed_records as $k=>$v) { echo $k.': '.print_r($v, TRUE).'<br>'; } code:
KuruMonkey fucked around with this message at 01:15 on Jan 22, 2010 |
# ? Jan 22, 2010 01:09 |
|
KuruMonkey posted:This assumes the page is saved in the same directory as 'subject.htm' and cheats by using the fact that the ungodly huge single line is where all the actual target data is. Owe you huge, man. I'll be back with victory or at least a lot of questionably hacked together data. Update: OH MY GOD I THINK I CAN MAKE THIS WORK. I had to refer back to a bunch of random tutorials to look up how to manipulate arrays properly, and I thought I might still be sunk because frankly I'm kind of a moron. Then I referred back to your post briefly so I could put the code back after I screwed it up, and out of the corner of my eye I saw where it said associative arrays. Suddenly it all made sense. Thank you so very much. apekillape fucked around with this message at 01:49 on Jan 22, 2010 |
# ? Jan 22, 2010 01:24 |
KuruMonkey posted:Its quick, brute force, and ugly. But it (seems to) work on that page. Its basically just a case of trimming the data to the target section, splitting it into unparsed records with preg_split and then parsing each chunk with preg_match. Ummm really? The DOMDocument solution is so much cleaner... Does that really look like code this guy can modify/learn from to you?
|
|
# ? Jan 22, 2010 02:31 |
Here you go apekillape:php:<?php error_reporting(1); $url = "http://www.songmeanings.net/artist/directory/a/"; //download the page first $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); $result = curl_exec($ch); curl_close($ch); //now process it $doc = new DOMDocument(); $doc->loadHTML($result); $mainDiv = $doc->getElementById("listing_artists"); $anchors = $mainDiv->getElementsByTagName("a"); foreach ($anchors as $anchor) { $artist = $anchor->textContent; $artistUrl = $anchor->getAttribute("href"); echo $artist." - ".$artistUrl."\n"; } ?>
|
|
# ? Jan 22, 2010 02:44 |
|
Terrific example, fletcher, I've been wondering how to do that.
|
# ? Jan 22, 2010 02:59 |
|
You're both beautiful people and have completely saved my bacon. Looping and getting it into the DB and all was never a problem, I just learned everything way out of order. I think this might fix a lot of my mental scraping issues though, shazam this thread is the best.
|
# ? Jan 22, 2010 03:13 |
|
apekillape posted:You're both beautiful people and have completely saved my bacon. This will get A through Z php:<?php foreach(range('a','z') as $i) { $url = "http://www.songmeanings.net/artist/directory/$i/"; $artistHTML = artistcURL($url); $artists_to_insert = artistList($artistHTML); $insert_sql = join(", ", $artists_to_insert); // Simple Database insert mysql_query("INSERT INTO table (name, url) VALUES $insert_sql"); } function artistList($html) { $doc = new DOMDocument(); $doc->loadHTML($html); $mainDiv = $doc->getElementById("listing_artists"); $anchors = $mainDiv->getElementsByTagName("a"); foreach ($anchors as $anchor) { $artist = mysql_real_escape_string($anchor->textContent); $artistUrl = mysql_real_escape_string("http://www.songmeanings.net".$anchor->getAttribute("href")); $artist_to_insert[] = "('$artist', '$artistUrl')"; } return $artist_to_insert; } function artistcURL($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); curl_setopt($curl, CURLOPT_URL, $url); $result = curl_exec($curl); curl_close($curl); return $result; } ?>
|
# ? Jan 22, 2010 05:11 |
DarkLotus posted:This will get A through Z Why are you telling somebody new to PHP to use mysql_real_escape_string prepared statements! PDO! fletcher fucked around with this message at 05:46 on Jan 22, 2010 |
|
# ? Jan 22, 2010 05:41 |
|
fletcher posted:Why are you telling somebody new to PHP to use mysql_real_escape_string prepared statements! PDO! He already had the database side down. Quote: "Looping and getting it into the DB and all was never a problem" You might as well ask why I would tell somebody to use mysql_real_escape_string or mysql_insert without first opening the database connection
|
# ? Jan 22, 2010 06:32 |
|
I'm building a small program for work which will be used when we check in/out laptops and other electronics equipment for student use. As of now, I've coded a form with HTML and PHP that will check out an item, but I'm having difficulty creating a form that will check in/renew items. What I had in mind was a query that would check to see which items were checked out by a user and then, based on the number of items checked out, a dynamic list would populate with checkboxes next to them indicating either a renew option or a checkin option. So, if there were two items checked out, a list of two items would show up with checkboxes next to each item. Unfortunately, I'm not sure how to code a dynamically generated list like that. I can only code a list of a static size. How would I code a dynamic list of this sort?
|
# ? Jan 22, 2010 08:41 |
|
|
# ? May 16, 2024 10:16 |
|
armed2010 posted:How would I code a dynamic list of this sort? Which bit is the problem? I hope you have saved the items checked out with some unique id somewhere. Then to check-in you pull that list of items, iterate over them generating the HTML as necessary for your check boxes, etc. php:<? foreach ($items as &$item) { ?> <input type="checkbox" name="<?=$item->name;?>"> <? } ?>
|
# ? Jan 22, 2010 09:02 |