Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

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:

code:
$str = "";
foreach($values as $key=>$value)
{
   $str.= $key.'=\''.$value.'\',';
}
Then remove the last comma using substr. It's really messy, and I am sure there is a nicer way of doing it?

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:
$myarray = array();
foreach($values as $key=>$value)
{
   $myarray[] = $key.'=\''.$value.'\'';
}
$str = implode(',',$myarray);

Adbot
ADBOT LOVES YOU

Hanpan
Dec 5, 2004

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.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

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:

code:
$str = "";
foreach($values as $key=>$value)
{
   $str.= $key.'=\''.$value.'\',';
}
Then remove the last comma using substr. It's really messy, and I am sure there is a nicer way of doing it?

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:
$count = 0;
foreach ($items as $item) {
    if ($count++ > 0)
        $sql .= ", ";
    $sql .= "'".$item."'";
}

Hanpan
Dec 5, 2004

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.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

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.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

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:

code:
$str = "";
foreach($values as $key=>$value)
{
   $str.= $key.'=\''.$value.'\',';
}
Then remove the last comma using substr. It's really messy, and I am sure there is a nicer way of doing it?

Use a SQL library or framework that accepts your array?

code:
$this->db->update('mytable',$array);
EDIT: fletcher!!! :argh:

Agrikk
Oct 17, 2003

Take care with that! We have not fully ascertained its function, and the ticking is accelerating.
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.

spiritual bypass
Feb 19, 2008

Grimey Drawer
You could load them into an array and then use array_search.

Agrikk
Oct 17, 2003

Take care with that! We have not fully ascertained its function, and the ticking is accelerating.
Good idea. Thanks.

The Polish Pirate
Apr 4, 2005

How many Polacks does it take to captain a pirate ship? One.
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.

Rat Supremacy
Jul 15, 2007

The custom title is an image and/or line of text that appears below your name in the forums
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.

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.

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?

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Agrikk posted:

Good idea. Thanks.

By loading them in an array, he means use list().
php:
<?
array_search('value', list($variableone, $variabletwo));
?>

Elected by Dogs
Apr 20, 2006
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.

geeves
Sep 16, 2004

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
  
}
}

?>

Now that you've instantiated the class, you can get the values of str and str2 with the getters.

php:
<?php
  $obj = new CrudeExample($args);
?>
<a href="<?= $obj->callToMethod() ?>"><?= $obj->callToOtherMethod() ?></a>

edit: yeah i use <?= because it's awesome - I think everyone should enable asp brackets (or PHP should switch its default to them) to avoid any XML issues. (<%) sue me.

geeves fucked around with this message at 02:36 on Sep 22, 2009

geeves
Sep 16, 2004

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?

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.


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

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

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
}
?>

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

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.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
How about a way to use prepared statements that doesn't use any object-oriented notation? i.e. no -> and no ::

spiritual bypass
Feb 19, 2008

Grimey Drawer

Hammerite posted:

How about a way to use prepared statements that doesn't use any object-oriented notation? i.e. no -> and no ::

Why?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

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...

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

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.

spiritual bypass
Feb 19, 2008

Grimey Drawer

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?

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

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.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

royallthefourth posted:

Why?


Hammerite posted:

I find object oriented programming style difficult to understand...

To which I ask: why?

EDIT: damnit! :argh: (I seem to be doing that a lot lately)

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

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.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



code:
people some;
like this;
code(people, like);
code:
some = new people();
like = new style(this);
some.code(like);
or something, but it's apparently a shocking revelation to most of you?

Rat Supremacy
Jul 15, 2007

The custom title is an image and/or line of text that appears below your name in the forums

fletcher posted:

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
}
?>

Okay gently caress it, I am ditching mysqli and using PDO.

DaTroof
Nov 16, 2000

CC LIMERICK CONTEST GRAND CHAMPION
There once was a poster named Troof
Who was getting quite long in the toof

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 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.

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.

Bhaal
Jul 13, 2001
I ain't going down alone
Dr. Infant, MD

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.
OO can have more overhead in the immediate scope of code, but in the long run the complexity of OO code grows at a slower rate. It's really something you should familiarize yourself with if this is something you plan to have a career on.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

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

v1nce
Sep 19, 2004

Plant your brassicas in may and cover them in mulch.

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 IT programming professionals, so that might give you some insight into why they recoil in horror at your inexperienced ways.

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.

J. Elliot Razorledgeball
Jan 28, 2005
How can I convert a number like:

code:
4.7573500000e+03
To a legitimate float?

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



floatval http://us2.php.net/manual/en/function.floatval.php

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

J. Elliot Razorledgeball posted:

How can I convert a number like:

code:
4.7573500000e+03
To a legitimate float?

$afloat = printf('%f',$yourVar);

Or that better way above this post. :v:

J. Elliot Razorledgeball
Jan 28, 2005

Perfect, thanks!

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



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);

Or that better way above this post. :v:

You're having an off week, man :\ Hope it gets better.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Munkeymon posted:

You're having an off week, man :\ Hope it gets better.

That's what I get for trying to help people.

KarmaticStylee
Apr 21, 2007

Aaaaaughibbrgubugbugrguburgle!

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)

Hanpan
Dec 5, 2004

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:
$array = new array()
foreach($item as $key=>$value)
    $array[]= array(":id"=>$key, ":weight"=>$value);

$sth = $db->prepare('UPDATE %snews SET weight = :weight WHERE id = :id');

$sth->execute($array);
-or-
$sth->execute(array_values($array));
But it doesn't seem to work? Any help would be awesome.

Adbot
ADBOT LOVES YOU

Rat Supremacy
Jul 15, 2007

The custom title is an image and/or line of text that appears below your name in the forums

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?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply