|
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: I imagine you will be told by a variety of people to use prepared statements. An alternative, which does substantially the same thing as what you are already doing but avoids the messy "getting rid of the last comma" step, is the following code:
|
# ? Sep 21, 2009 17:45 |
|
|
# ? May 19, 2024 12:53 |
|
Hammerite posted:I imagine you will be told by a variety of people to use prepared statements. Wow, I have been coding PHP for ages and I've never come across these! Thanks for the heads up.
|
# ? Sep 21, 2009 19:05 |
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: You should be using prepared statements, that looks so ripe for some injection. Anyways, the comma thing comes up a lot when I code, I find myself using: code:
|
|
# ? Sep 21, 2009 19:13 |
|
fletcher posted:You should be using prepared statements, that looks so ripe for some injection. Obviously I have mysql_real_escape_string'd my values first... but yeah I'll definitely check out prepared statements.
|
# ? Sep 21, 2009 19:20 |
Hanpan posted:Obviously I have mysql_real_escape_string'd my values first... but yeah I'll definitely check out prepared statements. Yeah don't use that either, just use prepared statements. Let something like PDO handle escaping for you.
|
|
# ? Sep 21, 2009 19:55 |
|
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 |
|
Is there an way to search for the existence of a value in multiple variables at once? For example, if I have: $variableone = "do"; $variabletwo = "ray"; $variablethree = "mi"; $variablefour = "fa"; $variablefive = "so"; Is there a function that I can use to search for the existence of "fa" in any of the five variables? I could easily write a bunch of seperate if/else statements to so this, but I'd like to come up with something a little bit more elegant.
|
# ? Sep 21, 2009 22:34 |
|
You could load them into an array and then use array_search.
|
# ? Sep 21, 2009 23:01 |
|
Good idea. Thanks.
|
# ? Sep 21, 2009 23:05 |
|
I have a Wordpress theme that I'm modifying, and the way the menu works is if you click on one of the page tabs, it takes you to that page and changes the tab color from blue to white. The thing it, I want the tabs to link to categories instead of pages, which I know how to do, but it's a url redirection to a link that's not the page link, so the tab color doesn't turn white. A bit hard to describe, so if you go to https://www.whatyoushouldbuy.com you'll see that clicking "Under $10" works because it's going to specific page, but clicking "$10-$25" doesn't because it redirects to a category. The PHP code is here: <?php if (is_page()) { $highlight = "page_item"; } else {$highlight = "page_item current_page_item"; } ?> <li class="<?php echo $highlight; ?>"><a href="<?php bloginfo('url'); ?>">Home</a></li> <?php wp_list_pages('sort_column=menu_order&depth=1&title_li='); ?> I'm assuming the current_page_item refers to a bit of CSS: #menu li.current_page_item, #menu li.current_page_item a { background: #fff; color: #559AD3; } Because that signals that the background is white. What I want it to do is check for a certain url instead of checking that it's a page. e.g.: if (url == https://www.cool.com/awesome) then { change tab3 to white } Reading back over this, I realize it's jumbled, but I don't know PHP well and can't figure out how to change it.
|
# ? Sep 22, 2009 01:19 |
|
Is there a way to use prepared statments that isn't really long winded?The Polish Pirate posted:I have a Wordpress theme that I'm modifying, and the way the menu works is if you click on one of the page tabs, it takes you to that page and changes the tab color from blue to white. What is the source of the is_page() function? Perhaps rewriting the condition to satisfy this to make it dependent on a static array (assuming your site doesnt change much) would be easiest?
|
# ? Sep 22, 2009 01:41 |
|
Agrikk posted:Good idea. Thanks. By loading them in an array, he means use list(). php:<? array_search('value', list($variableone, $variabletwo)); ?>
|
# ? Sep 22, 2009 01:47 |
|
Is it possible to output some text, viewable on browser (echo/print), wait 10-15 seconds, then header() something completely serverside - no JS? I tried sleep() but that just makes it delay then load everything at once. I think I've seen a CGI script do this, not sure.
|
# ? Sep 22, 2009 02:09 |
|
Munkeymon posted:Does any language with OO constructs let you return something from the constructor? I can't think of one that does. It can return the object; you don't need to actually return a specific type of object (and I don't see why you would want to). This is a crude example php:<?php class CrudeExample { private $str = ""; private $str2 = ""; public function __construct($args) { $this->callToMethod($args); $this->callToOtherMethod($args); } public function getStr() { return $this->str; } public function getStr2() { return $this->str2; } private function callToMethod($args) { // some logic that sets $this->str } private function callToOtherMethod($args) { // some logic that sets $this->str2 } } ?> php:<?php $obj = new CrudeExample($args); ?> <a href="<?= $obj->callToMethod() ?>"><?= $obj->callToOtherMethod() ?></a> geeves fucked around with this message at 02:36 on Sep 22, 2009 |
# ? Sep 22, 2009 02:26 |
|
Elected by Dogs posted:Is it possible to output some text, viewable on browser (echo/print), wait 10-15 seconds, then header() something completely serverside - no JS? You cannot call to anything in header() after outputting to the browser. If you are simply wanting to output and pause (if it's an intensive / data heavy call) try flush() with sleep() to see what you can put together. http://www.php.net/flush geeves fucked around with this message at 02:36 on Sep 22, 2009 |
# ? Sep 22, 2009 02:34 |
haywire posted:Is there a way to use prepared statments that isn't really long winded? php:<? $db = Database::getConnection();//singleton pattern, returns PDO object $query = $db->prepare("select username from user where id = :id"); $query->bindParam("id", $id); if ($query->execute()){ //do stuff } ?>
|
|
# ? Sep 22, 2009 02:49 |
geeves posted:It can return the object; you don't need to actually return a specific type of object (and I don't see why you would want to). What are you talking about? Your constructor doesn't return anything in that example.
|
|
# ? Sep 22, 2009 05:44 |
|
How about a way to use prepared statements that doesn't use any object-oriented notation? i.e. no -> and no ::
|
# ? Sep 22, 2009 12:15 |
|
Hammerite posted:How about a way to use prepared statements that doesn't use any object-oriented notation? i.e. no -> and no :: Why?
|
# ? Sep 22, 2009 15:44 |
royallthefourth posted:Why? Yeah, I can't imagine why you wouldn't want to use object oriented notation. Your whole app should be using it...
|
|
# ? Sep 22, 2009 18:26 |
|
royallthefourth posted:Why? I find object oriented programming style difficult to understand, so I avoid using it in favour of what I understand to be called "procedural" programming. Plenty of things you see documented at the PHP online manual have examples of both procedural and OO style use. I figured this would hopefully be the case for prepared statements as well. fletcher posted:Yeah, I can't imagine why you wouldn't want to use object oriented notation. Your whole app should be using it... I would like to be able to say that object oriented notation appears precisely nowhere in my site's scripts, but I had to (grudgingly) use some in order to send emails using PEAR.
|
# ? Sep 22, 2009 20:58 |
|
Hammerite posted:I would like to be able to say that object oriented notation appears precisely nowhere in my site's scripts royallthefourth posted:Why?
|
# ? Sep 22, 2009 21:28 |
|
royallthefourth posted:I find OO programming ideas difficult to understand (partly because of unfamiliarity but partly because they seem to add complexity that I don't see the need for), so I dislike and avoid them. I answered this (albeit more briefly) in the post immediately above yours.
|
# ? Sep 22, 2009 21:50 |
|
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 |
Hammerite posted:I find object oriented programming style difficult to understand, so I avoid using it in favour of what I understand to be called "procedural" programming. Plenty of things you see documented at the PHP online manual have examples of both procedural and OO style use. I figured this would hopefully be the case for prepared statements as well. I can't even imagine what a nightmare your code must be. It's ok, I was in the same boat a few years ago. The tipping point was when I tried to make a change to code I hadn't touched in 6 months. Only then did I realize the importance of OOP and took the time to learn it, while I did a complete re-write of the app using it. You will be rewriting that whole thing soon enough, so go ahead and put off learning OOP a bit longer.
|
|
# ? Sep 22, 2009 22:46 |
|
code:
code:
|
# ? Sep 22, 2009 23:01 |
|
fletcher posted:
Okay gently caress it, I am ditching mysqli and using PDO.
|
# ? Sep 23, 2009 02:40 |
|
Hammerite posted:I find object oriented programming style difficult to understand, so I avoid using it in favour of what I understand to be called "procedural" programming. Plenty of things you see documented at the PHP online manual have examples of both procedural and OO style use. I figured this would hopefully be the case for prepared statements as well. Cripes. You can use, for example, mysqli with nothing but procedural functions, but it still uses objects in the background. I strongly recommend that you familiarize yourself with OO concepts unless you intend to never work on projects that are non-trivial or involve other people. There's no way in hell I would staff a programmer who held this opinion.
|
# ? Sep 23, 2009 07:06 |
|
Hammerite posted:I find OO programming ideas difficult to understand (partly because of unfamiliarity but partly because they seem to add complexity that I don't see the need for), so I dislike and avoid them. I answered this (albeit more briefly) in the post immediately above yours.
|
# ? Sep 23, 2009 07:18 |
|
DaTroof posted:There's no way in hell I would staff a programmer who held this opinion. Bhaal posted:It's really something you should familiarize yourself with if this is something you plan to have a career on. Oh dear, have I unwittingly stumbled into a part of these forums reserved for aspiring IT professionals?! If I decide to choose programming as a career path rather than a hobby, you can bet my attitude to this and other things would be different. Hammerite fucked around with this message at 09:05 on Sep 23, 2009 |
# ? Sep 23, 2009 08:55 |
|
Hammerite posted:Oh dear, have I unwittingly stumbled into a part of these forums reserved for aspiring IT professionals?! No, but the majority of people who post the solutions here are Hammerite posted:[...] but I had to (grudgingly) use some [OOP] in order to send emails using PEAR. You may only be doing PHP as a hobby, but it sounds like you're going out of your way to avoid Object Orientated methods, which is counter productive when simple OOP solutions exist. You've gone on the defensive about your procedural methods, but in reality we can clearly see you don't understand OOP and you're finding it difficult to wrap your brain around it, which is why royallthefourth and Lumpy are asking why/what you find difficult. If you can tell us what you're having trouble understanding then any number of us would gladly try to explain it. I understand the position you're coming from as for the longest time I couldn't find any good resources to learn about OOP which both explained matters but din't overcomplicate the examples. It can also be difficult to find tutorials which give you a reason for OOP being usful in a particular situation, as a lot of tutorials simply make you do bullshit code, or stuff that seems like it would be easier as procedural functions, thus not explaining the usfulness of OOP.
|
# ? Sep 23, 2009 10:47 |
|
How can I convert a number like:code:
|
# ? Sep 23, 2009 21:59 |
|
floatval http://us2.php.net/manual/en/function.floatval.php
|
# ? Sep 23, 2009 22:09 |
|
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:floatval http://us2.php.net/manual/en/function.floatval.php Perfect, thanks!
|
# ? Sep 23, 2009 22:11 |
|
Is the downloadable manual in the OP? http://www.php.net/get/php_manual_en.chm/from/a/mirror The one good thing about having one massive, bloated namespace is that you can pretty much just start typing in the index keyword search box and find whatever you need pretty quickly.Lumpy posted:$afloat = printf('%f',$yourVar); You're having an off week, man :\ Hope it gets better.
|
# ? Sep 23, 2009 22:42 |
|
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 |
|
DaTroof posted:There's no way in hell I would staff a programmer who held this opinion. Seriously. The only time I do not use OOP is when I write a quick script for myself to do something (i.e. scrape/display data)
|
# ? Sep 24, 2009 17:31 |
|
Thanks to this thread I am now aware of the awesomeness that is PDO and prepared statements. I was just wondering if someone could help me get some syntax down, as Google isn't being very helpful this evening. If I have an array, which contains a bunch of assoc arrays, is there a way to pass this array directly to the execute function without having to bind each param? I tried this: code:
|
# ? Sep 24, 2009 23:07 |
|
|
# ? May 19, 2024 12:53 |
|
Hammerite posted:Oh dear, have I unwittingly stumbled into a part of these forums reserved for aspiring IT professionals?! If I decide to choose programming as a career path rather than a hobby, you can bet my attitude to this and other things would be different. Yep, because doing things in a better way and having organised code and/or working with other people are strictly reserved for people in big enterprisey corporations and a simple hobbyist would have no intention of improving themselves ever, now would they?
|
# ? Sep 25, 2009 00:51 |