|
Tots posted:... Ah well, first thing is that opendir() returns a "directory handle" that you need to keep track of in order to use readdir(). Second thing is that PHP, like a lot of other languages, has assignments that return a value, so $a in "$a=($b=4);" has the value 4. So to make the first example more readable, php:<? $dh=opendir('/whatever'); //$dh is directory handle if($dh){ // if $dh has a value /* process directory */ closedir($dh); } ?> readdir() lets you walk through a directory or reset the cursor or whatnot. 99% of the time you just use it like: php:<? while(($entry=readdir($dh)) !== false){ /* do something with $entry */ } ?> It's sort of like PET BASIC, if you're old like me. A floppy's directory was stored in a special file called "$", so if you wanted to list the contents of the disk programatically you'd open the file "$", read in each line, then close "$". In PHP, they just built a special command around "$", so opendir() ~= fopen(), readdir() ~= fgets(), and closedir() ~= fclose(). Edit: moved closedir to inside of if Tad Naff fucked around with this message at 20:32 on Feb 23, 2009 |
# ¿ Feb 23, 2009 20:10 |
|
|
# ¿ May 17, 2024 16:03 |
|
VerySolidSnake posted:Need some help on figuring this out. I'm trying to simply split up my code into functions, to easily come back and edit them. Here is the code I'm trying to use: "global $foo;" means you're using the $foo in the global context. Once you enter a function you lose $foo unless you have a "global $foo;" in there. So you'd need to say "global $test;" in the test() function and "global $link;" in simplequery(). But this is all avoiding the fact that you should avoid global variables if at all possible. Better to return $link in db_connect and use it as a parameter in simple_query, so you can: php:<? function test(){ $link=db_connect(); if($link){ simplequery($link); }else{ echo 'DB connect failed.'; } } ?>
|
# ¿ Feb 23, 2009 22:27 |
|
VerySolidSnake posted:Changed the code to: Still need to "return $link;" in db_connect(). If you want to re-run queries, you don't have to create a new link each time, just reuse $link. For example: php:<? function db_connect() { $link = mysqli_connect('localhost', 'user', 'pass', 'db'); if (!$link) { printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error()); exit; } return $link; } function simplequery($link, $field) { if ($result = mysqli_query($link, 'SELECT '.$field.' FROM edu_users')) { echo "Values are: "; while( $row = mysqli_fetch_assoc($result) ){ echo $row[$field]; } mysqli_free_result($result); } } $myLink=db_connect(); echo("First Names:\n"); simplequery($myLink,'firstname'); echo("Last Names:\n"); simplequery($myLink,'lastname'); mysqli_close($myLink); ?>
|
# ¿ Feb 23, 2009 23:00 |
|
niralisse posted:Not if you use exec() with an &$output argument. Where the heck did DirectoryIterator come from? PHP is such a mindfuck sometimes. [looks it up] oh, 5.0. I was held back for way too long by sysadmins who wouldn't trust that new PHP 5 (probably with good reason) so I used 4.3.x long past its expire-by date.
|
# ¿ Feb 24, 2009 07:48 |
|
Vedder posted:[...]The only trouble is I receive some Word documents with all sorts of formatting, bold text here, underlining here etc and was wondering whats the best way to insert that data? It was a neverending pain in the rear end for me in that situation. Generally I'd use Word's Export to HTML (different from "Save as HTML"), then HTML Tidy, then manually fix whatever goofiness remained, then grab the body of the page and throw it into the CMS. There's also "antiword" for Linux which extracts text from Word docs (I don't think anything post-Word 97), but it also strips formatting so you'd have to reapply it all over again. Making an input page is a good idea in theory, but if people are using Word to write for the web, it's guaranteed that they will just copy from Word and then paste into the form, which leads to all sorts of misery with the curly quotes and formatting. Things like FLK and TinyMCE just exacerbate the problem, because they look like Word so people expect them to actually be Word. I hate to say it, but if you have the power to make suggestions you might want to get contributors to use a WYSIWYG HTML editor rather than Word. You'll still have to clean it up, but at least it'll arrive with the entities already done and no wacky "mso:normal" sort of tags in it.
|
# ¿ Mar 12, 2009 17:31 |
|
duck monster posted:The questions is, how does one write a regex that preg_match_all can read so that a url like Not a guru, but something like this: php:<? function route() { foreach ($this->table as $k=>$v) { $urlsplit = explode(URL_SLICE,$_SERVER['REQUEST_URI']); $url = $urlsplit[1]; if (preg_match('!^/'.$k.'/(.*)$!',$url,$matches)) { global $_URL; $_URL = $matches[1]; $obj = new $v[0](); print $obj->$v[1]($_SESSION,$_REQUEST,$_URL); } } } } ?>
|
# ¿ Mar 14, 2009 08:09 |
|
Sylink posted:EDIT: NEVERMIND I AM RETARDED PLEASE REMEMBER THAT = IS NOT THE SAME AS == I HAVE HIGHLIGHTED THE STUPID ERROR FOR OTHER MORONS LIKE ME TO LEARN FROM ...and you'll probably want session_start() at the beginning of login.php and logout.php.
|
# ¿ Mar 25, 2009 22:25 |
|
Uncle Marx posted:How do I round up a number if a division doesn't have an integer as the result? ceil()
|
# ¿ Apr 8, 2009 03:24 |
|
supster posted:I need to do a database export from within a PHP application (without using mysqldump) similar to how phpMyAdmin does it. I've searched around a bit and surprisingly did not find anything that did this - does anyone havea any suggestions? Well probably since phpmyadmin is written in PHP, you can find out how it does a dump, and just copy/adapt that. On the copy I'm looking at (version 2.9.2), it's in export.php around line 461, the line code:
|
# ¿ Apr 13, 2009 03:59 |
|
Aredna posted:This is probably just going to show how bad I am at google today... Here I thought I was the only one perverse enough to think of writing a regex to validate regexes. I gave up after a bit (checking for matching brackets is really hard). The cheap-rear end PHP way to do it might be something like: code:
or, maybe you can get away with just code:
|
# ¿ Apr 25, 2009 00:55 |
|
ABombInABowl posted:
I think that Location: might be just sending you directly to a nonexistent file, try taking it out. Also, because this bit me once, it's best to put the filename in quotes in that Content-disposition, i.e. code:
|
# ¿ Apr 28, 2009 00:41 |
|
Before I kill myself any further, is there something in PHP already out there that functions as a proxy? I'm working on a mobile (i.e. PDA) interface to a highly organic (since 1996 if not before) site that has all manner of crap in it, from PDFs to JSP to Perl CGI to straight (if very nonstandard) HTML. The basic operation is:code:
code:
|
# ¿ Jun 4, 2009 04:19 |
|
Lumpy posted:I have successfully done this, so I know it can be done... I'd have to dig through my work machine's backup HD to look for my stuff, but this page here might give you an "ah-hah!" until I can find my stuff or somebody else can help you out: Gah, I did it. I was totally on the wrong track, of course. For anyone else doing this sort of thing, don't keep POSTing info when you're following redirects.
|
# ¿ Jun 4, 2009 18:16 |
|
Bitruder posted:I need to parse some LaTeX code into human readable code (just a small subset). However, I'm not sure how to use preg_replace (or another suitable tool) to parse something like the following: I just noticed the other day that PHP's PCRE has this new "?R" thing which supposedly solves the parenthesis-matching problem, see http://ca2.php.net/manual/en/regexp.reference.php under "recursive patterns". PHP Manual posted:Consider the problem of matching a string in parentheses, allowing for unlimited nested parentheses. Without the use of recursion, the best that can be done is to use a pattern that matches up to some fixed depth of nesting. It is not possible to handle an arbitrary nesting depth. Perl 5.6 has provided an experimental facility that allows regular expressions to recurse (among other things). The special item (?R) is provided for the specific case of recursion. This PCRE pattern solves the parentheses problem (assume the PCRE_EXTENDED option is set so that white space is ignored): \( ( (?>[^()]+) | (?R) )* \) Haven't tried it yet myself but it sure sounds better than some of the hacks I've done.
|
# ¿ Jun 9, 2009 17:52 |
|
Agrikk posted:I need help breaking up lines of data in a flat file into variable to be imported into a database, and explode() doesn't seem to cut it. I'd suggest unpack(), which seems to be tailor made for splitting out fixed-length fields, and as a bonus you get to specify the packed type. That or a bunch of messy substr()'s...
|
# ¿ Jun 15, 2009 20:23 |
|
Jreedy88 posted:I'm trying to pass some PHP strings to JavaScript. I'm doing this for an AJAX request on the server. The problem comes when the string contains either a " or '. Is there a function I can use to put escape characters in there so that when I call the echo, it echoes the entire string and doesn't get cut off in the wrong spot? I made this thing after getting too insane with escaping HTML for exporting to JS: php:<? function jsescape($str){ $str=str_replace( array('"',"\n"), array('"+String.fromCharCode(34)+"','\\n'), $str); return '"'.$str.'"'; } ?> code:
|
# ¿ Jun 22, 2009 20:03 |
|
Edit: PDO is nice, I'm sure. I just don't like the verbosity. And 90% of the servers I work on are on RHEL<=5.
Tad Naff fucked around with this message at 05:57 on Jul 1, 2009 |
# ¿ Jul 1, 2009 05:44 |
|
Safety Shaun posted:I tried but this is regexing the source of a requested URL which contains HTML and Javascript. Because the matched part is really returned elsewhere... like so: php:<? $matches=array(); $number_of_matches=preg_match_all($regex,$source,$matches); var_export($matches); ?>
|
# ¿ Jul 22, 2009 05:11 |
|
Pimpsolo posted:Basically you can see that I would like to automate filling out this form so that with a single click I get the results of my query. So if either of my methods are impossible and there's an alternate, I'm open to another method. Thanks in advance for any help.
|
# ¿ Aug 20, 2009 05:02 |
|
xezton posted:I do that, too. I agree it's cleaner and it's easier to pick out variables in strings when you're scanning through code (in my opinion at least). I'm so very, very ashamed... php:<? foreach(array('Type','Null','Key','Default','Extra') as $v){ if(${"c$v"}!=${"n$v"}){ echo("***\t ojs222.$table.$cField $v '".${"c$v"}."' => ojs.$table.$nField $v '".${"n$v"}."'\n"); switch($v){ ... } } } ?>
|
# ¿ Sep 2, 2009 21:43 |
|
Agrikk posted:Two things: php:<? $Size=0; $Test=($Size===0)?'S':$Size; if(!preg_match('~^(S|LGG|SGG|[0-9]|A)$~',$Size) unset($Test); // this line might be unneccessary, depending echo $Test; ?>
|
# ¿ Sep 18, 2009 07:23 |
|
I am getting annoyed with charsets. I've been working on an email archiver, but it seems like there are more encodings than there are email clients. So far I've got a bunch of heuristics about uppercasing, making "utf8" into "UTF-8" etc., falling back to whatever mb_detect_encoding says and then iconv, but it's still missing a lot and/or getting it wrong. Here's a link to a log. Anyone out there have experience with this sort of thing? Some sort of PEAR module maybe?
|
# ¿ Sep 29, 2009 05:48 |
|
MrMoo posted:How can you mess up gb2312? iconv has oodles of aliases already setup for each encoding, e.g. utf8, UTF8, UTF-8, etc. Are you suggesting using iconv before mbstring? Because I could do that too; mbstring just seemed more 'mature' at first glance, mostly because I could actually get a list of its supported encodings. ... unless you're joking about gb2312.
|
# ¿ Sep 29, 2009 06:13 |
|
MrMoo posted:Actually I'd try recode first considering the source material. The other modules are more strict on character set names. PHP is like a series of tunnels, endlessly twisting. Thanks, I'll definitely be giving that a try tomorrow. E: Any tips on getting the PHP extension installed on RHEL5? Not seeing it in any of my current repos. E2: Scratch that, had PHP excluded from yum (5.3 breaks every drat thing). Tad Naff fucked around with this message at 06:44 on Sep 29, 2009 |
# ¿ Sep 29, 2009 06:29 |
|
Plorkyeran posted:Don't use regexps to parse xml. Aw, c'mon. I do it all the time. Granted, I'm usually checking for the existence of "AuthSuccess" in the document and I don't care about the rest of it. But yeah, if you want to keep the structure and all you might need to use an XML parser.
|
# ¿ Oct 3, 2009 04:36 |
|
TreFitty posted:I have a question about PHP/MySQL and checkboxes. I want to store boolean values in a database table and when I submit a form with an unchecked box, it stores the value as '0'. That's great. When I submit a form with a checked box, it tries to store the value as 'on'. NOT great. Try setting value="1" in the checkbox <input>s? I think you end up with "on" if you don't specify the value.
|
# ¿ Oct 20, 2009 06:30 |
|
fletcher posted:Never even heard of this. I currently use Notepad++ as well (and sftp-drive) to write all my code. All I want is an editor that can do key based authentication to edit remotely hosted code, sftp-drive is the biggest piece of poo poo. 90% of the time I use SSH and nano, but occasionally if I can be bothered to set up a share drive I'll use Notepad++. Just recently I finally got around to writing a half-decent PHP syntax highlighter for nano, up until then I was doing everything in glorious monochrome. Some day I'll learn something proper like vi or emacs.
|
# ¿ Oct 30, 2009 18:35 |
|
OK, I'm avoiding work so here's a possible rewrite.code:
Tad Naff fucked around with this message at 16:51 on Dec 8, 2009 |
# ¿ Dec 7, 2009 19:52 |
|
Yeah. As far as I can tell it's trying to do an AJAXy autosuggest, but without the AJAX. Without the AJAX you're stuck with 20000 lines of PHP-generated Javascript, so page load times will suck no matter what. The JS hunting through each element doing an indexOf is just icing on the cake. A more intelligent method would be to make a simple PHP script accepting a parameter (call it $fragment after sanitizing etc) to output the contents of the <select> based on a database query like "SELECT location_name, location_code FROM locations WHERE location_name LIKE '%$fragment%' LIMIT 100". And only have it kick in after three or so keystrokes. The the onkeyup event can just call a function that will replace the content of the <select> with whatever the server says: onkeyup="populate_select_with_suggestions(selectId,this.value)" or something like that.
|
# ¿ Dec 30, 2009 01:38 |
|
a foolish pianist posted:I've got some php code that produces a table, and it leaves a large quantity of whitespace at the top of the page, such that I have to scroll down to see the table. That's a plain old HTML question. ed darn,b
|
# ¿ Feb 5, 2010 04:02 |
|
Just to get this out of the way, I am an infra-gifted street-coder who has only held onto his job because of his looks. I am in the process of writing a CMS. Yes, this is a dumb thing to do, reinventing the reinvented wheel etc etc, but really this problem domain is pretty underrepresented out there. Anyhow, my recurring issue is that I often code myself into a corner where I'm, say, writing PHP that generates javascript that outputs HTML. Something like:php:<? $html='<a href="foo">foo\'s link</a>'; echo('<script type="text/javascript">'); if($some_condition){ echo('$("#foo").html("<?php echo $html ?>");'); // of course this fails later on }else{ echo('$("#bar") .html("<?php echo magicalescapingfunction($html) ?>" .'<scr'.'ipt type=javascript">alert(\'BAR\');</scr'.'ipt>");'); // ditto, probably } echo('</script>'); ?>
|
# ¿ Mar 17, 2010 01:34 |
|
supster posted:I know. Help. Tad Naff fucked around with this message at 05:30 on Mar 17, 2010 |
# ¿ Mar 17, 2010 04:52 |
|
Just checking in to say my mood has improved considerably since I discovered encode_json(). My snippet up there was intentionally awful btw; even I know that writing javascript with javascript is not a sane thing, and writing PHP that writes javascript that writes javascript is (not sane)^2. I was 80% done and then my client aka coworker got impressed with a similar tool's interface, so I'm trying to salvage the backend while adding all this AJAX crap to the front, which involves lots of HTML snippets being requested from the server via JSON rather than full page loads. I was not having much success writing my own escaper -- still don't know what I was doing wrong, but that's moot now.
|
# ¿ Mar 18, 2010 20:34 |
|
So, uh, not that anyone should ever do this, but why doesphp:<? $foo="bar"; $foo(); ?> php:<? ${"bar"}(); ?>
|
# ¿ Mar 25, 2010 06:16 |
|
Thirteenth Step posted:I'm trying to attach some basic Javascript validation using this code: You need <form onsubmit="return isAlphanumeric(...);" (returning false stops the submit from proceeding). Edit: change onclick to onsubmit and add it to the form tag. I always forget that. Edit2: \/\/ It's been a long day. Also I think this is the first time I had to edit my "Edit:". Tad Naff fucked around with this message at 03:56 on Apr 1, 2010 |
# ¿ Apr 1, 2010 02:02 |
|
Thirteenth Step posted:like: Looks reasonable, except for that crazy <form> and <p> intermingling. <form> and <p> are both blocks by default so probably you don't need the <p>.
|
# ¿ Apr 1, 2010 20:59 |
|
duz posted:That's presentational, which is what CSS is for: To continue derailing, I did this in a recent project but was never satisfied about making labels fixed-width, especially if they run over the allotted width. Also you can't do this trick which lets you have clickable labels without assigning everything an id: code:
code:
Tad Naff fucked around with this message at 06:11 on Apr 2, 2010 |
# ¿ Apr 2, 2010 04:12 |
|
Well I don't know about you guys, but I'm stuck with some pre-PDO-capable systems and I find ADOdb to be pretty capable and programmer-friendly. And I tend to avoid OO in Web situations, what with all that construction on each page load.
|
# ¿ Apr 10, 2010 04:26 |
|
N.Z.'s Champion posted:They want to display them in a nice gallery with hierarchy, metadata, thumbnails derived from RAW images, etc. Just to anticontribute, if they suggest or mention "contentDM", run away shooting. Unfortunately I haven't found much else out there that has a lot of traction in the digital image repository world. Omeka perhaps, in a couple more point releases...
|
# ¿ Apr 20, 2010 03:43 |
|
|
# ¿ May 17, 2024 16:03 |
|
ShoulderDaemon posted:If a user sends the request script.php?foo=a&foo=b then $_GET["foo"] will be an honest-to-god array. This is trivial for a user to do, and doesn't require any script or programming or anything on the user's end. If the script is expecting $_GET["foo"] to be a string, then bad things could happen because various operations will not perform as the programmer expected. Explicitly casting it to a string removes this possibility. What, I always had to do script.php?foo[]=a&foo[]=b to get $_GET['foo']=array('a','b')... did things change? I know I've seen Perl work like the above...
|
# ¿ Jun 15, 2010 05:36 |