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
supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
Does everyone else find themselves constantly typing ampersands in every function definition and assignment when dealing with (potentially) large data structures (usually database result sets)?

It's so annoying :(. I wish we could either get actual pointers or real references.

Adbot
ADBOT LOVES YOU

cannibustacap
Jul 7, 2003

Brrrruuuuuiinnssss
Thanks a lot and Merry Christmas

Zorilla
Mar 23, 2005

GOING APE SPIT

IndieRockLance posted:

I've been trial-and-erroring my way though modifying a Wordpress template and I've run into a problem that I can't figure out. On the archives page, I'm having it generate thumbnails from the posts, overlay a transparent PNG and overlay the post titles over those matte images. Here's my code from the archives.php page (breaks added for clarity):

...

Must....clean up...code....

archive.php
php:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php $thumbnail get_post_meta($post->ID'thumbnail'true); ?>

<div class="post" id="post-<?php the_ID(); ?>">
    <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <div class="entry">
        <div class="thumbnail_container">
            <img src="<?php echo $thumbnail?>" alt="<?php the_title(); ?>" />
            <span class="gloss"></span>
        </div>
<?php the_post('Read the rest of this entry...'); ?>
    </div>
    <p class="postmetadata"><?php the_time('M d, Y'); ?><?php if (function_exists('the_ratings')) { ?> | <?php the_ratings(); } ?></p>
</div>

<?php endwhile; endif;?>



style.css
code:
.thumbnail_container {
	overflow: hidden;
	position: relative;
	float: left;
	width: 310px;
	height: 150px;
	margin: 0 10px 5px 0;
}
.gloss {
	position: absolute;
	display: block;
	top: 0;
	left: 0;
	width: 310px;
	height: 150px;
	background: transparent url(images/gloss.png) no-repeat scroll top left;
}


The idea is that you should display an image like normal (no tricky, dynamically-generated CSS background tricks), then use a <span> tag to absolutely position the partially-transparent PNG overlay right on top of it.

Zorilla fucked around with this message at 19:55 on Dec 25, 2008

Emo.fm
Jan 2, 2007
Do you guys know the text field on the upper right of the main page on Facebook, the friend search? It auto suggests names from your own friends as you type. Is there some way to do this with PHP/MySQL for my own site? I'm making a site for inputting songs as they're played (college radio station, yay) and would like some way to suggest the correct spelling/formatting of songs and artists (pulled from a database) so that we don't end up with 15 different entries for "Beatles" "The Beatles" "Beetles" "Beatls" etc.

Zorilla
Mar 23, 2005

GOING APE SPIT

Emo.fm posted:

Do you guys know the text field on the upper right of the main page on Facebook, the friend search? It auto suggests names from your own friends as you type. Is there some way to do this with PHP/MySQL for my own site? I'm making a site for inputting songs as they're played (college radio station, yay) and would like some way to suggest the correct spelling/formatting of songs and artists (pulled from a database) so that we don't end up with 15 different entries for "Beatles" "The Beatles" "Beetles" "Beatls" etc.

Not with PHP alone, but with AJAX. I would probably do something with jQuery that ties an AJAX request to an onkeyup event for that input field. That AJAX request would run a PHP script that returns suggestions in JSON format.

You would definitely have to optimize it so that it's not running a MySQL query every single time somebody presses a key. (Maybe once every 3-5 seconds while typing is better)

Zorilla fucked around with this message at 18:44 on Dec 27, 2008

Emo.fm
Jan 2, 2007

Zorilla posted:

Not with PHP alone, but with AJAX. I would probably do something with jQuery that ties an AJAX request to an onkeyup event for that input field. That AJAX request would run a PHP script that returns suggestions in JSON format.

You would definitely have to optimize it so that it's not running a MySQL query every single time somebody presses a key. (Maybe once every 3-5 seconds while typing is better)

Thanks, I will look into this.

Another dumb PHP question -- is there a function for finding out the current page being displayed? As in, let's say I want to do a standard "footer.php" and include it with each page, but not include a link to the current page. Is there a simple way to find that out?

Zorilla
Mar 23, 2005

GOING APE SPIT
edit: durr, I thought you were talking about WordPress for some dumb reason. Just listen to the guy below me.

Zorilla fucked around with this message at 06:14 on Dec 28, 2008

Supervillin
Feb 6, 2005

Pillbug

Emo.fm posted:

Thanks, I will look into this.

Another dumb PHP question -- is there a function for finding out the current page being displayed? As in, let's say I want to do a standard "footer.php" and include it with each page, but not include a link to the current page. Is there a simple way to find that out?

$_SERVER['REQUEST_URI'] is usually what was typed in the address bar to get to the page, and $_SERVER['PHP_SELF'] is usually the main or "parent" page (meaning, if index.php includes footer.php, PHP_SELF on either page will be index.php).

Server configuration changes the values and formats of those variables, so you might just want to make a page that spits out phpinfo() and look at the variables yourself. Some servers give multiple formats for the same information, so it might be easier to match one or the other to the link hrefs as you echo them.

MonkeyMaker
May 22, 2006

What's your poison, sir?

cannibustacap posted:

Hey I want to make a list of links where only the selected link is bold, the rest are normal.

Just like here http://www.amazon.com/gp/bestsellers/electronics/1036922/ref=pd_ts_e_nav Notice how DVD Players is bold and the rest are normal?


How would I, either in PHP or CSS, make that happen?

Make the bold style the default for the list (e.g. non-linked text). Make your links non-bold. Remove the link on the selected item. Done.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Let's say you have a User class and an Article class and you want a function that returns all the articles a user has written. Does it go in the Article class as something like Article::getArticlesByUser($user) or in the User class like $user->getArticles()? What makes more sense?

Roctor
Aug 23, 2005

The doctor of rock.

fletcher posted:

Let's say you have a User class and an Article class and you want a function that returns all the articles a user has written. Does it go in the Article class as something like Article::getArticlesByUser($user) or in the User class like $user->getArticles()? What makes more sense?

Gonna go out on a limb and say this is up to taste. Maybe there's an official way and I'm full of poo poo, though. I could easily imagine doing it either way depending on how I've chosen to design an application.

Atom
Apr 6, 2005

by Y Kant Ozma Post

fletcher posted:

Let's say you have a User class and an Article class and you want a function that returns all the articles a user has written. Does it go in the Article class as something like Article::getArticlesByUser($user) or in the User class like $user->getArticles()? What makes more sense?

I would do it the latter way. No reason to do a static function when you can have one less parameter.

KuruMonkey
Jul 23, 2004

fletcher posted:

Let's say you have a User class and an Article class and you want a function that returns all the articles a user has written. Does it go in the Article class as something like Article::getArticlesByUser($user) or in the User class like $user->getArticles()? What makes more sense?

I was about to say "depends", but in this case it really doesn't. $user->articles(); is fine. Article::articles_by_a_user(); makes little sense.

Think of it like this; once you "have" a user, it makes sense (in the vague context of a system that stores articles by users) to find all the articles by a user. I can think of a strong user case where I'd want to do that, and I can imagine I might have an instance of a user then too. (I can think of 2 clear cases; show more articles by current article's author, or when author logged in, show my articles)

On the other hand (and I'll be honest we've only got the name of the class Article, singular, to go on) it seems you've conceived of an abstraction for A USER and one for AN ARTICLE,singular in both cases.

A function belonging to a singular abstraction that returns a set of (somethings) which is related to that instance; this makes sense.

A static function of a singular abrstraction that produces a set of that type of object, related to an instance of a different type? Nonsensical.

In fact I feel I've kind of aswered this before?

I think you would be well served by a book on OO, preferably one well separated from any specifics of a language you are interested in programming in.

It also occurs that you might want to look into the concepts of factories, or at least become open to the idea of using multiple abstractions for the same "thing".

i.e. how about 2 classes; Article and Articles; the former to abstract an individual and specific article, the second to abstract the colection of all articles, and provide functions that produce sub-collections (as, say, arrays of Article)

So you could have Articles::by_author($user_id) which returns an array[Article]...

This would then also be the logical place to put Articles::most_recent(10); and Articles::awaiting_publishing(); etc etc

(note these may well NOT actually want to be static functions, and in terms of MVC Article and Articles are both models, which give different interfaces to the same resource...)

Atom
Apr 6, 2005

by Y Kant Ozma Post

KuruMonkey posted:

i.e. how about 2 classes; Article and Articles; the former to abstract an individual and specific article, the second to abstract the colection of all articles, and provide functions that produce sub-collections (as, say, arrays of Article)

So you could have Articles::by_author($user_id) which returns an array[Article]...

This would then also be the logical place to put Articles::most_recent(10); and Articles::awaiting_publishing(); etc etc

(note these may well NOT actually want to be static functions, and in terms of MVC Article and Articles are both models, which give different interfaces to the same resource...)

What's the point of having a seperate class for "Articles?" I can't see what data this object would contain except that of an array of objects of type "Article"...

I fail to see the necessity of creating something like Articles::most_recent(int) when Article::most_recent(int) would make just as much sense. It seems like OOP dogma more than anything practical imho.

Khorne
May 1, 2002
How am I supposed to handle timezones in php? The DateTime object seems bloated and unportable.

Edit: What I meant to ask is, what method do you personally prefer for handling timezones in php and other web languages?

Khorne fucked around with this message at 23:35 on Dec 29, 2008

KuruMonkey
Jul 23, 2004

Atom posted:

I fail to see the necessity of creating something like Articles::most_recent(int) when Article::most_recent(int) would make just as much sense. It seems like OOP dogma more than anything practical imho.

There's no 'necessity' its a nicety of abstraction. In the cold light of pure practicality there's no need to seperate the abstraction of the collection of items from the abstraction of the items themselves. (just as there's no 'necessity' to write in an OO style in PHP at all, you could even still implement MVC in a purely procedural programming method, probably clumsy, but you could do it if it suited your purpose)

On the other hand, it can serve a purpose to seperate into 2 abstractions the functionality that applies to the group from that which applies to the individual.

Example; changing the headline of an article probably 'belongs' in an abstraction for the article as an individual item, but finding all articles with a given feature is (potentially) a feature of the group of articles as a whole.

Further example, you could possibly write this pair of classes so that modifying the implementation of one did not imply a need to change the implementation of the other - and in some cases that alone might be reason to split into 2 classes.

Conversely, considerations of further (as in to come at a later date) extensions to Article might mean that having it all wrapped up in one class to inherit from was helpful.

It really come down to; there needs to be an underlying understanding of the flow of your data, the structure of your data and the intended (and expected) use cases for the classes and objects your making. This all gels together to give you a 'feel' of what is the correct abstraction for your application.

The question (certainly when taken in combination with the poster's previous one) was taken in a sense of 'what would you do?' Hence my comment about the OO book; not meaning to imply 'and then you too would understand the ONE TRUE ABSTRACTION for users and articles in all systems' but that this would be the place to look for a wider view of how to go about crafting the abstractions that will work for the specific app at hand; because there are many perfectly valid ways to abstract authors and articles (I mean; is this a 1-1, 1-M, M-1 or M-M relationship in this application? Is this even decided yet? Because that would change EVERYTHING!)

I've certainly never considered myself dogmatic about OO (slack about it, yes, dogmatic - no) I gave an answer of how I would do it, why I would do it that way, and why I wouldn't do it the other way - and a suggestion to go look at other ways - where the dogma at?

Edit; and my god I can witter on about this stuff :(

KuruMonkey
Jul 23, 2004

Khorne posted:

Edit: What I meant to ask is, what method do you personally prefer for handling timezones in php and other web languages?

I don't do a lot with timezones, honestly (its a perk of living in GMT - just make everything Zulu and forget about it)

But; when I do, I simply...

1-store all times as Zulu
2-manually offset by any required timezone offset using the magic of addition and subtraction, at the point the offset is required (i.e. I brute force it)

I'm probably anomalous though, because I tend not to use stuff thats only in PHP 5, because of ending up wanting to get my code running on clients' lovely shared hosting that they will not change.

So I tend to use time() and microtime() and strftime() more than maybe is normal/sane.

p.s. I use the same methods for currencies (store all as GBP and convert as required) and measures (SI units and convert).

Atom
Apr 6, 2005

by Y Kant Ozma Post

KuruMonkey posted:

I've certainly never considered myself dogmatic about OO (slack about it, yes, dogmatic - no) I gave an answer of how I would do it, why I would do it that way, and why I wouldn't do it the other way - and a suggestion to go look at other ways - where the dogma at?

Edit; and my god I can witter on about this stuff :(

Forgive me perhaps dogma was not the correct term. I just can't see a reason for doing it that way other than for adherence to a self-imposed standard.

KuruMonkey
Jul 23, 2004

Atom posted:

adherence to a self-imposed standard.

Yeah; thats pretty much accurate.

To the extent that I'd not use a singular name (article) for anything that included functionality for a collection (articles). I mean, I frequently write code like this;

php:
<?
foreach($cats as $cat)
{
$cat->stuff();
}
?>
and expect the plurality/singularity of the names to convey the one/set nature of things, and its kind of second nature now.

But thats off in the lands of 'how do you name YOUR classes?!?' Which is just faffery :)

Edit; I will however put on my software QA hat, and defend to the hilt that having a standard (self selected or official) and sticking to it is Good Practice. Oh yes.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Lots of interesting comments! Thanks guys. I figured it had more of a clear cut answer but it seems that is not the case! I think it's definitely time to pick up a book about OOP.

Yaksha
Jun 28, 2005

Demilich
I'm looking for something to help with my daily work tasks, as well as recording the information I find while doing these tasks. I need to check server hard drive usages, modify certain profile information, clean out workstations at specific time intervals, etc.

I'm looking for some kind of task or to do site I can set up with PHP and MySQL where I can automate things a bit. I need tasks that repeat daily (but not on weekends, more configuration options would be nice), the ability to add one time tasks, and be able to go back to show that I did what was supposed to be done.

This will be for my use only, others won't be able to log in and create their own tasks. Anyone know if such a thing exists?

Supervillin
Feb 6, 2005

Pillbug

Yaksha posted:

I'm looking for something to help with my daily work tasks, as well as recording the information I find while doing these tasks. I need to check server hard drive usages, modify certain profile information, clean out workstations at specific time intervals, etc.

I'm looking for some kind of task or to do site I can set up with PHP and MySQL where I can automate things a bit. I need tasks that repeat daily (but not on weekends, more configuration options would be nice), the ability to add one time tasks, and be able to go back to show that I did what was supposed to be done.

This will be for my use only, others won't be able to log in and create their own tasks. Anyone know if such a thing exists?

If it's for your own use only, is there a reason you want to host it yourself? Cause Remember The Milk does all that stuff you said and more, and is free.

You can specify tasks to repeat "every day", "every weekday", or even "every month on the 2nd last Friday". Tasks can have subtasks (or not), and you can keep finished tasks to take a look at past progress.

Envy Insanity
Oct 2, 2003

Have gone to commit suicide. Intend to return from grave Friday. Feed cat.
I'm in no way a coder, I just play with Joomla and edit other people's hard work. I'm having an issue with a template I've been working with and I was hoping someone could give me a hand.

The site is here: (alttabbed.net is not my site)

http://www.alttabbed.net/revdemo

This is a Joomla site with the "Simplix" template installed from Rockettheme. I've been tweeking it for the last couple days and it's almost looking exactly how I want.

The image is the article and the text and the menu are in modules. My hope was to be able to get them all to line up, top and bottom, however if I extend the image to it's full height of 450 (the height of the two modules), the middle module extends down further for no reason whatsoever. I think this may be a padding/margin thing but I'm an idiot and can't find anything related to that in the CSS file. The image also seems to start a couple pixels below the modules.

Also, in Firefox and a few other browsers the site looks fine, but in IE7/8 it only takes up 3/4 of the screen and the rest is white.

I know I can't explain poo poo very well, but if someone is familiar with Joomla and could help me out it would be greatly appreciated.

edit: Well, I fixed the to alignment, but the middle module still extends when the image is set to 500px. IE/Chrome issue, I still have no idea.

edit2: I think I fixed everything.

Envy Insanity fucked around with this message at 17:28 on Jan 1, 2009

Standish
May 21, 2001

KuruMonkey posted:

I don't do a lot with timezones, honestly (its a perk of living in GMT - just make everything Zulu and forget about it)
...until March when all your times will be 1 hour off for six months.

quote:

But; when I do, I simply...

1-store all times as Zulu
2-manually offset by any required timezone offset using the magic of addition and subtraction, at the point the offset is required (i.e. I brute force it)
Again, this won't work because of daylight saving time. (Even if you're not currently under daylight saving time, the time you're converting could need to be adjusted for DST, for example my offset from GMT is currently zero, but if I want to format the UTC time "2009-06-01 12:00Z" then that needs to be offset by 1 hour even though DST is not currently in effect in my timezone.).


The right way to do it is to store the symbolic zoneinfo timezone name e.g. "Europe/Berlin" and call
php:
<?
$timezone = $user->getPreferredTimezone(); // or whatever
date_default_timezone_set($timezone); 
?>
somewhere near the start of your script.

Stephen
Feb 6, 2004

Stoned
I've been trying to create a multipart text/html email and for some reason I can't get it to read properly. Here's what I've been doing, following examples of mime types on Google:
code:
$headers = 'From: E-mail <email@myaddress.com>';
$headers .= "MIME-Version: 1.0\r\n";
$headers .= 'Content-type: multipart/mixed; boundary="boundary123"'."\r\n";

$body = "--boundary123\r\n";
$body.= "Content-type: text/plain\r\n";
$body.= "Here's some content";
$body.= "--boundary123";
$body.= "Content-type: text/html\r\n";
$body.= "<html><Here's some HTML content</html>";
$body.="--boundary123--";

mail($to, $subject, $body, $headers )
The email will show up in my inbox, however it doesn't properly split the boundaries, it just prints them on the page with all the HTML as text.

Can anyone point out what the issue with my code is? Thanks

Standish
May 21, 2001

Stephen posted:

I've been trying to create a multipart text/html email and for some reason I can't get it to read properly. Here's what I've been doing, following examples of mime types on Google:
code:
$headers = 'From: E-mail <email@myaddress.com>';
$headers .= "MIME-Version: 1.0\r\n";
$headers .= 'Content-type: multipart/mixed; boundary="boundary123"'."\r\n";

$body = "--boundary123\r\n";
$body.= "Content-type: text/plain\r\n";
$body.= "Here's some content";
$body.= "--boundary123";
$body.= "Content-type: text/html\r\n";
$body.= "<html><Here's some HTML content</html>";
$body.="--boundary123--";

mail($to, $subject, $body, $headers )
The email will show up in my inbox, however it doesn't properly split the boundaries, it just prints them on the page with all the HTML as text.

Can anyone point out what the issue with my code is? Thanks
You need two "\r\n"s between the last header and the body, not one. Also it's "Content-Type", not "Content-type". Also you have no \r\n after the From: header, or before the "--boundary123"s.

Stephen
Feb 6, 2004

Stoned
Awesome, thanks. I fixed those and noticed I had the semicolon inside the boundary definition which was obviously breaking it as well.

Now I've got it working. Both the HTML and text parts of the mail message are showing up, however I was under the impression that only one part should appear, depending on if the user has HTML message enabled or not. As it is, in my HTML enabled gmail window, both parts of the message are showing, and in a client with HTML disabled, both parts are also showing and all the HTML code is appearing as text.

Edit: Nevermind, changed the MIME type to multipart/alternative and it worked great!

Thanks again for the help

Stephen fucked around with this message at 17:33 on Jan 2, 2009

Zorilla
Mar 23, 2005

GOING APE SPIT

Stephen posted:

Multipart email stuff

PHP also supports heredoc statements, which is good for large blocks of text you don't want to use PHP's templating system for:

code:
$headers = <<<EOF
From: E-mail <email@myaddress.com>
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="boundary123"

EOF;

$body = <<<EOF
--boundary123
Content-Type: text/plain
Here's some content
--boundary123
Content-Type: text/html
<html>
Here's some HTML content
</html>
--boundary123--
EOF;

mail($to, $subject, $body, $headers);
Personally, I'd still use output buffering + PHP templating because it highlights and you can plug in variables when needed:

php:
<?php
$email["friendly_name"] = "E-mail";
$email["address"] = "email@myaddress.com";

ob_start();
?>
From: <?php echo $email["friendly_name"]; ?> <<?php echo $email["address"]; ?>>
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="boundary123"

<?php
$headers ob_get_contents();
ob_end_clean();

ob_start();
?>
--boundary123
Content-Type: text/plain
Here's some content
--boundary123
Content-Type: text/html
<html>
Here's some HTML content
</html>
--boundary123--
<?php
$body ob_get_contents();
ob_end_clean();

mail($to$subject$body$headers);
?>

Zorilla fucked around with this message at 18:09 on Jan 2, 2009

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
I've been a big fan of EditPlus for awhile now for all my PHP needs. I really find myself wanting an object outline and some basic auto-completion sometimes. Are there any lightweight editors that do that for PHP on windows? I like Eclipse for Java but didn't really like it for PHP.

SuckerPunched
Dec 20, 2006

You could try Aptana with the PHP plugin. It's based on Eclipse and has a lot of neat built-in stuff specifically for web development.

https://www.aptana.com

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

SuckerPunched posted:

You could try Aptana with the PHP plugin. It's based on Eclipse and has a lot of neat built-in stuff specifically for web development.

https://www.aptana.com

Looks neat but this is the exact opposite of "lightweight"

SuckerPunched
Dec 20, 2006

Well yeah but I was going off your "I like Eclipse" statement.

DaTroof
Nov 16, 2000

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

fletcher posted:

I've been a big fan of EditPlus for awhile now for all my PHP needs. I really find myself wanting an object outline and some basic auto-completion sometimes. Are there any lightweight editors that do that for PHP on windows? I like Eclipse for Java but didn't really like it for PHP.

I'm in the middle of evaluating NuSphere PhpEd. It's not as lightweight as EditPlus, but I imagine that would be true of any IDE that can outline objects and complete code. It's nowhere near as heavyweight as Eclipse or Visual Studio, either.

So far I'm really impressed. I downloaded a massive project (around 10MB of source code) for local development. Frankly, I expected PhpEd to get a little confused about the class hierarchy. I opened a script and instantiated a derived class that depends on an autoload function for inclusion. The editor gave me all of its public members without a hiccup. It includes doc comments in the code completion if they exist (method and argument descriptions, etc.). And this is on an antiquated Thinkpad running XP Pro with 512MB of RAM.

Unfortunately, it's also not as cheap as EditPlus, but a free trial is available.

DaTroof fucked around with this message at 00:56 on Jan 5, 2009

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Thanks guys I'll give both a shot.

Here's a general question, let's say I have a function like before like $user->getArticles() but I want to limit my results to certain articles, like ones filed under $category about $topic written on $date with $coAuthor, etc etc with all these different things and before you know it I'm passing in 15 arguments to getArticles(...), what's a better way of doing that?

Are you supposed to make some kind other ArticlesFilter class where I can specify $articleFilter->setCategory('Technology') and then I just pass in my filter object: $user->getArticles($articlesFilter) ?

fletcher fucked around with this message at 09:05 on Jan 5, 2009

Murodese
Mar 6, 2007

Think you've got what it takes?
We're looking for fine Men & Women to help Protect the Australian Way of Life.

Become part of the Legend. Defence Jobs.
You could always just accept arrays as input.

ie.

php:
<?
$where = array(
    'category' => $category,
    'topic' => $topic
);

$user->getArticles($where);
?>

hexadecimal
Nov 23, 2008

by Fragmaster
Let's say I have a list of variables I would like to pass to my php script, such as http://someurl?a=2&b=3. What if I want to pass a string "a=2&b=3" as a single variable? like http://someurl?args="a=2&b=3". How can I do this? I tried quotes already and they don't work. Is there some particular escape character I can use or something?

MononcQc
May 29, 2007

hexadecimal posted:

Let's say I have a list of variables I would like to pass to my php script, such as http://someurl?a=2&b=3. What if I want to pass a string "a=2&b=3" as a single variable? like http://someurl?args="a=2&b=3". How can I do this? I tried quotes already and they don't work. Is there some particular escape character I can use or something?
use %26 for &: http://someurl?a=2%26b=3
see http://ca3.php.net/manual/en/function.urlencode.php

Atom
Apr 6, 2005

by Y Kant Ozma Post

Murodese posted:

You could always just accept arrays as input.

Yeah but if you're going to use an associative array you might as well use an object imho. I hate looking at code that takes associative arrays as input because an associative array doesn't have an explicit structure where an object does.

sonic bed head
Dec 18, 2003

this is naturual, baby!

Atom posted:

Yeah but if you're going to use an associative array you might as well use an object imho. I hate looking at code that takes associative arrays as input because an associative array doesn't have an explicit structure where an object does.

As I'm nowhere near a classically trained programmer, can you explain what the difference is to me? What do you mean by an explicit structure? To me they are pretty much the same thing because a class with public variables still has to have it's variables set in much the same way as an associative array. Is the benefit just better design or somehow optimized code?

Adbot
ADBOT LOVES YOU

Internet Headache
May 14, 2007

sonic bed head posted:

As I'm nowhere near a classically trained programmer, can you explain what the difference is to me? What do you mean by an explicit structure? To me they are pretty much the same thing because a class with public variables still has to have it's variables set in much the same way as an associative array. Is the benefit just better design or somehow optimized code?
Design, if anything. IDEs can look up an object's class definition to give you an idea of the parameters. With an array, you're forced to dig up comments for valid keys.

In any case I don't like passing either arrays or objects as input unless necessary, so this is what I would do. It's the $where array approach with methods.
php:
<?
$articledb = new ArticleDatabase($db);
$articledb->filterCategory('balls');
$articledb->filterTopic(1);
$articledb->filterDate(false, '2009-01-01');
$user->articles = $articledb->fetch();



class ArticleDatabase {
  // Store filters to be returned to buildQuery later
  protected $filters = array();

  public function filterCategory($cat) {}
  public function filterTopic($topicId) {}
  public function filterDate($dateBegin, $dateEnd=false) {}

  public function fetch() {
    $this->buildQuery();
    // run query and initialize ArticleResult with the database result
    return new ArticleResult($result);
  }
  protected function buildQuery() {}
}

class ArticleResult implements Iterator {
  public $title;
  public $category;
  public $date;
  protected $result;

  // Check php manual for the native Iterator class
  // to give array-like access to the articles result
  public function current() {
    return $this->result->fetch_object();
  }
  // etc
}

?>

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