|
eHacked posted:How do I extract information from XML? Read up on this: http://us.php.net/simplexml [EDIT] Whipped up some sample code because it's so drat easy.... this uses the XPATH function that was added to simpleXML in PHP5.2, you'll have to do it the slightly more complicated way if you are using a previous version... php:<? $string = <<<XML <?xml version="1.0"?> <previewroot> <category> <item Id="6691" websiteid="0" category="2" contentgroup="1"/> <item Id="6690" websiteid="0" category="3" contentgroup="1"/> <item Id="6689" websiteid="0" category="4" contentgroup="1"/> </category> <sites> <item Id="117" websiteid="0" siteid="8" contentgroup="1"/> </sites> <content> <names> <name name="jpg" num="520" filesize="87222276"/> <name name="thumb" num="525"/> <name name="full" num="1" filesize="383874155"/> <name name="hdwmv" num="5" filesize="573953501" movie_length="2100.707"/> <name name="hswmv" num="5" filesize="381170297" movie_length="2100.815"/> <name name="lswmv" num="5" filesize="158266517" movie_length="2100.695"/> <name name="mpg" num="5" filesize="305189364" movie_length="2106.967912"/> </names> </content> </previewroot> XML; $xml = new SimpleXMLElement($string); $result = $xml->xpath('/previewroot/content/names/name[@movie_length != ""]'); echo "this many nodes have 'movie_length': " . count($result) . "<br />"; echo "the first movie_length is: " .$result[0][@movie_length]; ?> Lumpy fucked around with this message at 03:21 on Apr 1, 2009 |
# ¿ Apr 1, 2009 03:07 |
|
|
# ¿ May 15, 2024 16:27 |
|
supster posted:Being pretty nitpicky here, but I would use this xpath expression to be a tiny bit cleaner: Next time, I'll just post the link. I kid, I kid: You are correct on every point, I just "spelled it all out" for the learnin' Lumpy fucked around with this message at 06:29 on Apr 1, 2009 |
# ¿ Apr 1, 2009 06:25 |
|
booshi posted:I am having some problems with getting this code to redirect properly after properly logging in. I've been banging my head over this and I still can't figure out what I'm not doing right (certain things edited / in caps to hide specifics): If there is any whitespace before the second header() it won't work. For example: code:
|
# ¿ Apr 1, 2009 06:37 |
|
keveh posted:OK, this is just driving me crazy and I know somebody will be able to tell me why I'm just being stupid! I'm not sure I understand what you are asking, but your checkReply() function can only return one reply per message since you set $return to a new value with = instead of appending with .= Echo the length and type of $this->messages to make sure it's what you think it is. It could be you are returning the database result set, not an array of the actual results in your getMessages() method.
|
# ¿ Apr 3, 2009 06:12 |
|
Crazak P posted:this is probably a really dumb question, but I can't seem to find the answer to it. How do you make a link where when you click on it, it automatically gives the save as prompt. I'd like to do this for jpg images. I got 9 good tutorials on it on my first Google search. I'll copy and paste the code from the first one here: Some website posted:
Please note the pasted code there has a huge security hole in it, but it should give you the gist of how to do Save As.. boxes. If you don't want a new window popping up / replacing the current one, put a tiny / invisible eyeFrame on your page, and send the download link to it.
|
# ¿ Apr 3, 2009 16:20 |
|
Munkeymon posted:Am I missing something or couldn't he just use Content-disposition: inline;... to prevent the browser from leaving the page? I have no idea.
|
# ¿ Apr 3, 2009 17:00 |
|
Safety Shaun posted:I have some returned XML data in $xml using simplexml_load_file() and I want to extract $someTitle from it from $xml->a->b->c->d->Title , how would I go about doing so? http://us.php.net/manual/en/function.simplexml-element-xpath.php You are welcome.
|
# ¿ Apr 8, 2009 19:39 |
|
eHacked posted:I'm learning OOP from a pretty fantastic book (so far): PHP Object Oriented Solutions by David Powers. "The thing that most other languages use a dot for" At least that's what I call it. Was interesting to learn the actual name for it.
|
# ¿ Apr 13, 2009 18:41 |
|
Fangs404 posted:I'm writing a website that has a lot of PHP files that need access to a DB (almost all of them do). Now, in order to avoid having to type something like Put anything that stores info you don't want known outside of web root. For example, if your web site is stored at: /blah/some/path/webRoot/ Then have your files with db passwords and other stuff at: /blah/some/path/hideMe/ And the web server can't serve the files up at all to people.
|
# ¿ Apr 18, 2009 16:09 |
|
Vedder posted:Got a bit of a weird on here. I have a newsletter to do and normally it has around 5 articles, each on in a seperate coloured div. I have found a tutorial which tells you how to make alternate coloured table rows and I have adapted this and it works fine with two colours, however I have 4. The code looks like this: You are using the operator horribly wrong. The () ? : ; means "If what is in the parenthesis is true, do the thing after the question mark, if not, do the thing on the other side of the colon" You can't just add more colons You want to do something like: php:<? $count = 0; $colors = array('grey','blue','yellow','green'); while ($row = mysql_fetch_array($news)) { $count++; echo "<p>{$colors[$count % 4]}</p>"; } ?> Lumpy fucked around with this message at 16:12 on Apr 22, 2009 |
# ¿ Apr 22, 2009 16:04 |
|
Mercator posted:Thank you for your post, I welcomed the laugh that resulted. Why do you have the same avatar as Yodzilla? Every time I see one of your posts, I get all confused
|
# ¿ Apr 22, 2009 16:19 |
|
drcru posted:Thanks, I've stopped worrying about it now. I use the CodeIgniter framework (although I am starting to use Kohana). If smarty is too much, then these are probably way overkill for you, but wouldn't hurt to take a gander. Kohana: http://www.kohanaphp.com/ CodeIgniter: http://www.codeigniter.com
|
# ¿ May 22, 2009 19:58 |
|
Sylink posted:I am making a wiki like site (I'm not using Mediawiki because I want something simpler) but I want to use/retain a lot of the similar formatting features as far as editing pages. There are much simpler wikis out there. Why re-invent the wheel? For the actual answer to your question, look at the source to Mediawiki and others and see how they do it.
|
# ¿ May 24, 2009 18:30 |
|
usingpond posted:I'm trying to make a CMS for a Flash application that reads data from XML. I'm trying to use DOM PHP (it seemed a little more legit than SimpleXML when I started). Use the DOM XML Methods for editing to edit it? What exactly are you having trouble figuring out what to do?
|
# ¿ May 26, 2009 18:47 |
|
usingpond posted:Well basically, I wanted to edit a child from this: I have no idea if you are using PHP4 or 5, here's a 4 example php:<? // assume you've read in the XML to $myXML $root = $myXML->document_element(); $parentTag = $root->first_child(); // you could skip this and do it directly below, but to be clear. $childTag = $parentTag->first_child(); $childTag->replace_child("DIFFERENT INFO",$childTag->first_child()); ?> php:<? $myXML = new DOMDocument(); $myXML->loadXML('<root><parent name="parent"><child>info</child></parent></root>'); $xpath = new DOMXpath($myXML); $nodelist = $xpath->query('/root/parent/child'); $kidNode = $nodelist->item(0); /* you could also have done: $kidNode = $myXML->firstChild->firstChild->firstChild; */ $newContent = $myXML->createTextNode("DIFFERENT INFO"); $kidNode->replaceChild($newContent,$kidNode->firstChild); echo "<PRE>"; echo htmlentities($myXML->saveXML()); echo "</PRE>"; ?> Lumpy fucked around with this message at 00:46 on May 27, 2009 |
# ¿ May 27, 2009 00:26 |
|
Jreedy88 posted:I'm working on some PHP code some old guy set up here at work. I hadn't looked at anything PHP until two days ago, so I'm still fairly new to this. I came across this and it didn't look right. Also, I don't think it's working as intended. Please shed some light on this. Firstly, no. Secondly, that's how you make it so you don't have to supply all the arguments. In PHP, and many other languages, you can set arguments to default values, so taht if the argument is not supplied, it's set. Here's fun made up code to learn you: php:<? function blah($name="Dave",$msg="Sup d00d") { echo "Hey $name, $msg"; } blah(); // "Hey Dave, Sup d00d" blah("Larry"); // "Hey Larry, Sup d00d" blah("Skippy","you suck"); // "Hey Skippy, you suck" ?>
|
# ¿ May 28, 2009 15:14 |
|
Golbez posted:Out of curiosity, is there any way to skip the first argument in that? So like... I do not say this with 100% certainty (but like 99.8%), but no. You can't pass over arguments.
|
# ¿ May 28, 2009 18:18 |
|
stoops posted:i'm only storing the resume name in the db field. You can use PHPs file handling abilities to read each file in, search words, close, etc. Or you can use shell_exec() to use grep or whatever. I'd go with the latter, and make sure you are caching results or building an index of matches to avoid duplicate searches and so on.
|
# ¿ Jun 2, 2009 14:30 |
|
FeloniousDrunk posted: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: 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: http://www.weberdev.com/get_example-4555.html
|
# ¿ Jun 4, 2009 05:01 |
|
Hammerite posted:Is there a good reason to use sprintf instead of str_replace? Is it faster? You use them for very different things in general, so it's not really an "instead" thing. EDIT: as for the discussion du jour, I used to be an echo guy, but I have come to love the printf Lumpy fucked around with this message at 23:58 on Sep 2, 2009 |
# ¿ Sep 2, 2009 23:56 |
|
Hanpan posted:Does anyone have any neat methods for preparing a associative array for use in a SQL update statement? Normally I do something like this: Use a SQL library or framework that accepts your array? code:
|
# ¿ Sep 21, 2009 20:30 |
|
royallthefourth posted:Why? Hammerite posted:I find object oriented programming style difficult to understand... To which I ask: why? EDIT: damnit! (I seem to be doing that a lot lately)
|
# ¿ Sep 22, 2009 21:50 |
|
J. Elliot Razorledgeball posted:How can I convert a number like: $afloat = printf('%f',$yourVar); Or that better way above this post.
|
# ¿ Sep 23, 2009 22:10 |
|
Munkeymon posted:You're having an off week, man :\ Hope it gets better. That's what I get for trying to help people.
|
# ¿ Sep 24, 2009 01:42 |
|
WhiteHowler posted:I tried making an array of objects, but I obviously did something very wrong... You need to explicitly set the class variables, unless something has changed and there's magic assignment of arguments to class member vars based or order defined or something... which might be the case, because my PHP is rusty. Think of it this way: How does new playerrankclass('poop',1,3) know what do to with the string 'poop' and the numbers 1 and 3? You either need to make a constructor function in your class that takes arguments, or set them then add to your array: php:<? $tmp = new playerrankclass(); $tmp->rankname = 'poop'; $tmp->rankmin = 1; $tmp->rankmax = 3; $plyerrank[]= $tmp; ?>
|
# ¿ Oct 14, 2009 18:56 |
|
WhiteHowler posted:Oh, that makes sense. what does print_r($playerrank); output?
|
# ¿ Oct 14, 2009 22:12 |
|
Xarthor posted:Hopefully this doesn't need it's own thread. The fine folks in the Wordpress Thread could probably help you out better than us slobs.
|
# ¿ Nov 18, 2009 19:33 |
|
Fruit Smoothies posted:This isn't a pure PHP question per se, but I hope you guys can help. Serialize can store objects / classes in a db, but I'm not sure that's what you need, because I'm not sure I fully understand your problem. Does one user change ALL bread to toast, or just his own bread?
|
# ¿ Nov 28, 2009 01:45 |
|
Amethyst posted:Hey guys, I'm helping my GF write her honours thesis with a simple php scraper of a vBulletin board (not SA). http://php.net/manual/en/function.file-get-contents.php Crazy what they put in the documentation these days. Example 4.
|
# ¿ Dec 15, 2009 03:15 |
|
drcru posted:How would I go about making sure something happens 75% or n% of the time? Did you initialize the random number generator at some point? [EDIT] looks like you don't need to do this since 4.2, but I dunno what version you are on.... php:<? srand( time() ); $num = rand(1,100); printf("I generated %d", $num); if( $num <= 75 ) { // yippie! }else { // boo } ?> Lumpy fucked around with this message at 07:51 on Dec 17, 2009 |
# ¿ Dec 17, 2009 07:48 |
|
KuruMonkey posted:Edit: they have a variable caled $ok2...its an array! To be fair, the keystrokes you save are well worth the slight loss in clarity of having to type out $okAsWell every time.
|
# ¿ Dec 22, 2009 01:33 |
|
supster posted:Or it could just be named something that is meaningful in context of what the variable represents... You don't "get" jokes, do you.
|
# ¿ Dec 22, 2009 02:48 |
|
Hammerite posted:Oh ok. I wouldn't go to that trouble. You could instead do the following Are you really casting it to a string then checking against an array of strings that happen to also be the digits 0 - 9 to check to see if the input is a number?
|
# ¿ Jan 8, 2010 01:02 |
|
Hammerite posted:That's right. The reason I'm doing something so convoluted is because I wasn't sure what behaviour PHP exhibits if you ask it whether a non-numeric single-character string is equal in value to integer zero. i.e. I wasn't sure whether for example 'X' == 0 or '.' == 0 evaluate to true or false. I wanted to make sure only digits can get through. While the code I posted is rather stupid, I believe it's secure. I didn't want to post insecure code for the guy. As I mentioned, if I could have been bothered to look up the right way to do it with a regex I would have done that instead. php:<? if(preg_match('/^\d+$/', $_GET['id']) ) { // hooray! } ?>
|
# ¿ Jan 8, 2010 03:27 |
|
Tivac posted:Did I miss something? Why wouldn't you use PHP's built-in (since 5.2) input filtering? Because id=2132+323213 would still get through. php Manual posted:FILTER_SANITIZE_NUMBER_INT : Remove all characters except digits, plus and minus sign. EDIT: To see, I whipped this up: code:
code:
Lumpy fucked around with this message at 20:26 on Jan 8, 2010 |
# ¿ Jan 8, 2010 20:16 |
|
Firequirks posted:I just started using CodeIgniter a couple days ago, but I see that the thread has gone to archives. Seems like he's got the field names in the form lined up with the field names on the DB table, is yours set up the same way? And yes, using Active Record cleans data IIRC. Lumpy fucked around with this message at 22:31 on Jan 16, 2010 |
# ¿ Jan 16, 2010 22:26 |
|
Firequirks posted:Yes, that's all correct. The only issue is that it's not stripping out the name and value for the Submit button from the $_POST array when inserting. The error specifically mentions that I'm trying to add a row to my ingredients table with a 'submit' column that doesn't exist: Don't give your SUBMIT button a name? EDIT: Yeah, I looked at his code in the video you posted, and there's no NAME attribute on the SUBMIT button. Lumpy fucked around with this message at 22:39 on Jan 16, 2010 |
# ¿ Jan 16, 2010 22:36 |
|
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 |
|
electricHyena posted:This might be the easiest question in here, as I know nothing about PHP. I just installed ZenPhoto on my site and I'm having some issues with the code. There will be a function called "printSlideShowLink()" somewhere in ZenPhoto. It looks like it takes a string argument, which is being obtained form some sort of localization function 'gettext()' I'm guessing this function spits out a string like: code:
code:
|
# ¿ Feb 3, 2010 04:18 |
|
|
# ¿ May 15, 2024 16:27 |
|
KuruMonkey posted:Just a little aside: Ahh, cool. I use my framework's localization stuff, had no clue PHP even had that built in. One of these days I should actually just spend some time reading the manual. I picked up PHP as a "solve this specific problem" tool, so I know the parts of it I've used well, but never really looked around at the rest of it much. Then again, I don't do all that much PHP any more.
|
# ¿ Feb 3, 2010 17:28 |