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
Mackerel, the Thief
Sep 24, 2003

Plorkyeran posted:

Oh no! Storing a bunch of copies of a few paragraphs! No one could possibly afford to store an extra megabyte for a large site with many translations!

I work for a large site with many translations. I'm assuming you don't.

Adbot
ADBOT LOVES YOU

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
If you are trying to suggest that you have enough translations and text to translate that the storage space is in any way worth caring about, I'm going to call bullshit.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Plorkyeran posted:

If you are trying to suggest that you have enough translations and text to translate that the storage space is in any way worth caring about, I'm going to call bullshit.

As was said before, storage space doesn't really have anything to do with it.

In the past I have stored translations in a table that contains: name, category, language, and the text. Primary key can be name,category,language. User goes to a certain part of the app, you can grab all the messages for that category. Make a utility class that is easy to access that ideally pulls them from memcache: Messages.get("SOME_LABEL", "Calendar", "en_US").

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Well yes, and I already agreed that there are potential performance issues and that you should hash them if it does turn out to be a problem, and I would rather hope that anyone working on a site large enough that the performance impacts are foregone would have to ask basic questions about how to handle translations. Is there some problem caused by very long keys other than potential slow performance and the extra storage space?

DaTroof
Nov 16, 2000

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

Plorkyeran posted:

Well yes, and I already agreed that there are potential performance issues and that you should hash them if it does turn out to be a problem, and I would rather hope that anyone working on a site large enough that the performance impacts are foregone would have to ask basic questions about how to handle translations. Is there some problem caused by very long keys other than potential slow performance and the extra storage space?

Potential slow performance is much more likely to be a problem than extra storage space. Also, 400-character keys are just a big pain in the rear end to work with. A key like "terms of service" is going to be a lot easier to remember, and a lot more contextually relevant whenever the copy needs to change, than a paragraph of legalese.

Mackerel, the Thief
Sep 24, 2003

Plorkyeran posted:

Well yes, and I already agreed that there are potential performance issues and that you should hash them if it does turn out to be a problem, and I would rather hope that anyone working on a site large enough that the performance impacts are foregone would have to ask basic questions about how to handle translations. Is there some problem caused by very long keys other than potential slow performance and the extra storage space?

There are two things I don't understand: why you're freaking out and secondly and why inefficiency is tolerable. The argument that you're making is, "The only thing wrong with storing these humongous keys is that more disk space is used, CPU load is increased, and every single request is slower!" In that case, you're absolutely right. Those things do happen if you do that. Except that I'd get fired if I had that kind of attitude.

Begby
Apr 7, 2005

Light saber? Check. Black boots? Check. Codpiece? Check. He's more machine than kid now.
A primary key rule is that it should never ever change. This is a rule that should result in a caning if it is broken. I have seen so many access databases that used a first/last name or a company name as a primary key and wanted to shoot myself each time.

Totally ignoring storage concerns, using text that may change in the future for the key in this case is pretty bad design. It might not be the primary key in this case, but its pretty close. Using a hash of that text for the key is also a bad idea. Whenever you want to change the text on the website, you will not only need to make changes to your php files, but also make changes to your database which might involve generating a new hash.

Mackerel, the Thief
Sep 24, 2003

Begby posted:

Totally ignoring storage concerns, using text that may change in the future for the key in this case is pretty bad design. It might not be the primary key in this case, but its pretty close. Using a hash of that text for the key is also a bad idea. Whenever you want to change the text on the website, you will not only need to make changes to your php files, but also make changes to your database which might involve generating a new hash.

Why would you need to modify the php or database if translations are provided by users (or google)?

Begby
Apr 7, 2005

Light saber? Check. Black boots? Check. Codpiece? Check. He's more machine than kid now.

Mackerel, the Thief posted:

Why would you need to modify the php or database if translations are provided by users (or google)?

I am talking about modifying the original text. Like if someone says to add a sentence to the paragraph, and you are using said paragraph to look up translations in a database. In that case you would have to modify the original paragraph and the DB.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Begby posted:

I am talking about modifying the original text. Like if someone says to add a sentence to the paragraph, and you are using said paragraph to look up translations in a database. In that case you would have to modify the original paragraph and the DB.

If you modify the original text and your translation framework doesn't invalidate the old translations, then your translation framework is broken. The point of keying based on the original text is specifically so that you have to at least look at all the translations when you make a change, and hopefully avoid having out-of-date translations presented to users.

micropenis
Jul 19, 2004
An '04 Mac user from the UK. What's worse?
I'm in way over my head here. Basically I'm trying to figure out how to add/replace text before the very first <h3> of a document. I've been told to check out php preg_replace and RegEx. Will these be able to do what I would like?

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine
This could go in the SQL thread as well, but I had yet another coding theory question: How much to do you do in a MySQL query versus PHP?

The question came to me while dealing with a date format, and realizing I needed to change the spaces to non-breaking spaces. So right now I have DATE_FORMAT(time,'%r'). (note: I realize there are ways around this (like "%h:%m:%s&nbsp%p") but it brought me to the theory question, which has less to do with this specific instance)

To change the space inside %r to non-breaking spaces I have two options:

PHP: str_replace(' ','&nbsp',$result)

MySQL: REPLACE(DATE_FORMAT(time,'%r'),' ','&nbsp')

Now, the boss has said, use PHP to cut down on MySQL server load (I honestly don't know what the load is, but odds are it's not worth caring about at this point), and he says PHP can generate the date display faster that MySQL can, by throwing the timestamp into a date('format',strtotime($result)) call.

Generally, I've tried to do as much as I can in the query, including concatenations, etc., but I wanted to check in and see what y'all think.

butt dickus
Jul 7, 2007

top ten juiced up coaches
and the top ten juiced up players
A database is used for storing and retrieving data. PHP is a hypertext preprocessor. Since this is just formatting for the user, you should do it in PHP.

McGlockenshire
Dec 16, 2005

GOLLOCKS!

micropenis posted:

I'm in way over my head here. Basically I'm trying to figure out how to add/replace text before the very first <h3> of a document. I've been told to check out php preg_replace and RegEx. Will these be able to do what I would like?

Normally I'd say "don't parse HTML with regexes!", because you shouldn't parse HTML with regexes. But you aren't parsing, you just need to insert some text that is totally not markup before a tag, right?

$result = preg_replace('/<h3>/', 'Stuff before the tag and also the <h3>', $variable, 1);

The 1 at the end limits it to one replacement. This is untested, you might need to escape the brackets. I'd say you could use str_replace instead, but it doesn't seem to have a mechanism to limit the number of results.




Golbez posted:

This could go in the SQL thread as well, but I had yet another coding theory question: How much to do you do in a MySQL query versus PHP?
[...]
Now, the boss has said, use PHP to cut down on MySQL server load

That's an awful reason. But it should still be done in PHP. You're formatting it for output, right? It's not the database's job to format things for output, that's the job of the thing that will actually perform the output.

e: drat you, Doctor rear end in a top hat!

Mackerel, the Thief
Sep 24, 2003

Golbez posted:

Now, the boss has said, use PHP to cut down on MySQL server load (I honestly don't know what the load is, but odds are it's not worth caring about at this point), and he says PHP can generate the date display faster that MySQL can, by throwing the timestamp into a date('format',strtotime($result)) call.

Generally, I've tried to do as much as I can in the query, including concatenations, etc., but I wanted to check in and see what y'all think.

I'm really against doing any formatting in SQL since it violates MVC. Data should be separate from presentation. Beyond that, your boss is right, PHP should (e: most of the time) be able to handle the formatting with much less server stress than SQL.

Kidane
Dec 15, 2004

DANGER TO MANIFOLD
Trying to upgrade PHP at work (we're running 5.2.5 :X) and it seems that Zend Optimizer doesn't support PHP 5.3.x? Are they holding out for PHP 6 or what? This essentially means that no web host can run PHP >= 5.3.0, which is already pretty old.

Not sure if this is the right place for this but it looks like I have no alternatives, any suggestions?

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine
A couple of my functions lately have run a query then returned the resource directly, rather than an array of stuff, because I want to loop over it in the receiving area without having to do it twice. For example...
php:
<?
$result = do_query();
while ($row = mysql_fetch_assoc($result)) { stuff }

function do_query() {
    return mysql_query('select * from foo');
}
?>
This has one loop, but it feels weird to be passing a resource around. On the other hand...
php:
<?
$array = do_query();
foreach($array as $row) { stuff }

function do_query() {
    $result = mysql_query('select * from foo');
    $a = array();
    while ($row = mysql_fetch_assoc($result)) { $a[] = $row }
    return $a;
}
?>
More code, and two loops, but I'm passing around an array rather than a resource. And this feels like it's impure somehow, like I'm doing it wrong. Am I making a big deal out of nothing, or is passing resources somehow frowned upon?

(on a side note, I've tried just doing "while ($a[] = mysql_fetch_assoc($result)) { }" but I get a blank entry at the end of $a, which, having to remove that, negates the savings in code conciseness)

Hammerite
Mar 9, 2007

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

Golbez posted:

More code, and two loops, but I'm passing around an array rather than a resource. And this feels like it's impure somehow, like I'm doing it wrong. Am I making a big deal out of nothing, or is passing resources somehow frowned upon?

I think you are making a big deal out of nothing. I would return the resource, if doing so calls for less looping. (I wouldn't usually write a function whose sole purpose is to perform a specific query and return the result, though.)

KuruMonkey
Jul 23, 2004
I think if you answer the question: What happens to my memory footprint if the query returns 10,000 (largish) records? You'll have the answer to which is better.

Just remember to free the result outside your function.

Or use PDO.

Also 'negates the savings in code conciseness': less characters typed is not a sensible metric of code quality.

Begby
Apr 7, 2005

Light saber? Check. Black boots? Check. Codpiece? Check. He's more machine than kid now.

Kidane posted:

Trying to upgrade PHP at work (we're running 5.2.5 :X) and it seems that Zend Optimizer doesn't support PHP 5.3.x? Are they holding out for PHP 6 or what? This essentially means that no web host can run PHP >= 5.3.0, which is already pretty old.

Not sure if this is the right place for this but it looks like I have no alternatives, any suggestions?

I honestly don't think I have ever known one of our web hosts to run Zend Optimizer. Is there a particular reason why you need it? Do you have encrypted scripts from someone else you need to run? I would assume they would update it soon, or that demand for Zend Guard is so low that it doesn't make financial sense for Zend to stay on top of it.

DarkLotus
Sep 30, 2001

Lithium Hosting
Personal, Reseller & VPS Hosting
30-day no risk Free Trial &
90-days Money Back Guarantee!

Begby posted:

I honestly don't think I have ever known one of our web hosts to run Zend Optimizer. Is there a particular reason why you need it? Do you have encrypted scripts from someone else you need to run? I would assume they would update it soon, or that demand for Zend Guard is so low that it doesn't make financial sense for Zend to stay on top of it.

I've only had one user request Zend be installed on my servers. Everyone else uses ioncube.

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine
I don't think I normally post straight coding questions here, but this one...
php:
<?
$i = -1;
echo ' and in role '.($i == -1)?'not us':'poop';
?>
When I run it, "not us" is echoed. That's it. " and in role " is nowhere to be found. This is confusing the hell out of me; how can it echo something from the end of the command while ignoring one from the start of it?

In fact, I just changed $i to 1 and I still get 'not us'. So... huh? Now, if I wrap the ternary block in parentheses, the whole shebang works fine. But why? And why is it ignoring the first part of the string?

Golbez fucked around with this message at 16:00 on Aug 26, 2010

Hammerite
Mar 9, 2007

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

Golbez posted:

I don't think I normally post straight coding questions here, but this one...
php:
<?
echo ' and in role '.($i == -1)?'not us':'poop';
?>
In this case, $i is in fact equal to -1. But when I run it, "not us" is echoed. That's it. " and in role " is nowhere to be found. This is confusing the hell out of me; how can it echo something from the end of the command while ignoring one from the start of it?

In fact, I just changed $i to 1 and I still get 'not us'. So... huh? Now, if I wrap the ternary block in parentheses, the whole shebang works fine. But why? And why is it ignoring the first part of the string?

http://www.php.net/manual/en/language.operators.precedence.php

The string concatenation operator has higher precedence than the ternary operator. What is happening is that the first 'argument' to ?: is the string ' and in role ', concatenated on the right with a Boolean. It will always evaluate to true as a Boolean.

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

Hammerite posted:

http://www.php.net/manual/en/language.operators.precedence.php

The string concatenation operator has higher precedence than the ternary operator. What is happening is that the first 'argument' to ?: is the string ' and in role ', concatenated on the right with a Boolean. It will always evaluate to true as a Boolean.

Thanks. That's some craziness, I have hundreds of ternary time bombs that I now need to make sure are properly wrapped.

Edit: Turns out all the others are just fine, I guess I probably ran into this before and the body forgets pain.

Golbez fucked around with this message at 16:05 on Aug 26, 2010

Yay
Aug 4, 2007
Given the problem is the concatenation operator, would using:
code:
echo 'thing', 'thing', ternary, 'thing';
not work? Speculating about crap like this, I realise how much I miss having a repl shell.

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

Yay posted:

Given the problem is the concatenation operator, would using:
code:
echo 'thing', 'thing', ternary, 'thing';
not work? Speculating about crap like this, I realise how much I miss having a repl shell.

Using commas works, because when you're using , you're sending multiple arguments to echo, and the comma has the lowest precedence of all. (If it didn't, sending function arguments would be a nightmare)

Hammerite
Mar 9, 2007

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

Yay posted:

Given the problem is the concatenation operator, would using:
code:
echo 'thing', 'thing', ternary, 'thing';
not work? Speculating about crap like this, I realise how much I miss having a repl shell.

Yes,
code:
echo ' and in role ',
     ($i == -1) ? 'not us' : 'poop';
would work as desired.

Munkeymon
Aug 14, 2003

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



Yay posted:

Speculating about crap like this, I realise how much I miss having a repl shell.

http://www.phpsh.org/

Hmm, I could swear the regular interactive mode used to work like a really terrible REPL that crashed as soon as you did anything wrong, but I must be thinking of something else.

Mackerel, the Thief
Sep 24, 2003

Golbez posted:

php:
<?
$result = do_query();
while ($row = mysql_fetch_assoc($result)) { stuff }

function do_query() {
    return mysql_query('select * from foo');
}
?>
vs.

php:
<?
$array = do_query();
foreach($array as $row) { stuff }

function do_query() {
    $result = mysql_query('select * from foo');
    $a = array();
    while ($row = mysql_fetch_assoc($result)) { $a[] = $row }
    return $a;
}
?>

The thing that makes me a bit wobbly about this is that it doesn't keep the controlling layer agnostic of the data it receives. It must receive a mysql resource. Returning the resource does give you a bit more control and it will always be a bit faster, but what happens if you were to upgrade to PDO or install memcached? You then have to modify code in a lot of places as opposed to just in your models. That's why I personally like the latter code better, but it's really up to you and your situation.

Hammerite
Mar 9, 2007

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

Mackerel, the Thief posted:

The thing that makes me a bit wobbly about this is that it doesn't keep the controlling layer agnostic of the data it receives. It must receive a mysql resource.

There is a way to avoid this being a problem. Namely, instead of using the mysql functions for looping through resultsets in your application code, you create your own wrapper function and call that instead. That way, it doesn't matter that you are returning a mysql resource. If you decide to change to another way of communicating with your database, you can just update the code in your wrapper function. It doesn't matter that a different type of resource is being returned, because it's still being dealt with by your functions and it makes no difference as far as the code that uses those functions is concerned.

Kidane
Dec 15, 2004

DANGER TO MANIFOLD

Begby posted:

I honestly don't think I have ever known one of our web hosts to run Zend Optimizer. Is there a particular reason why you need it? Do you have encrypted scripts from someone else you need to run? I would assume they would update it soon, or that demand for Zend Guard is so low that it doesn't make financial sense for Zend to stay on top of it.
Haha, I have no idea; I'm a perl guy. The task of updating PHP on our shared hosting platform was thrown to me because my boss likes watching me squirm.

I ASSumed a lot of people used it but if that's not the case then this should be easier. I was going off some of the more vocal posts on the Zend forums where people are clamoring for 5.3.x support.

Thanks!

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine
I'm kind of between projects, having done a big one and now waiting for them to explain the newer ones, so I've been taking a lot of time to go over my code on the last project, explaining most of my recent posts. Anyway, another brainstorming/coding theory one:

Should a function hold more than one query?

I was looking through stuff like my chat archive function, where first it copies (with insert...select) lines from the temporary table to the main table, and then deletes lines from the temporary table. And I was looking at that and thinking, should this contain two queries, or should I split them out? Each function has a query and each query, a function? And, secondarily, should these functions *only* contain the query code? So that even if I have a function with a single query, if it's doing anything other than running that query, the query should be in another dedicated function?

This isn't necessarily specific to PHP, I suppose.

Edit: and, I've taken to using COUNT(*) and mysql_result($result,0,0) to easily find out how many rows there are. But when it's a simple boolean (i.e. either there's 1 row or 0 rows) I still do return mysql_num_rows($result) (and, actually, I do (bool)mysql_num_rows for some reason). It's kind of been a mental thing - a mysql_result means it's a count, a mysql_num_rows means it's a boolean. ... Does this seem stupid to anyone else, because I sometimes wonder it as I browse.

Golbez fucked around with this message at 14:39 on Aug 30, 2010

Hammerite
Mar 9, 2007

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

Golbez posted:

And I was looking at that and thinking, should this contain two queries, or should I split them out? Each function has a query and each query, a function?

I don't get why you would think this has to be the case. A function is a block of code that carries out some task you want done. That might involve performing a query, or it might not, or it might involve several queries. Why should there be limit of one query to every function?

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

Hammerite posted:

I don't get why you would think this has to be the case. A function is a block of code that carries out some task you want done. That might involve performing a query, or it might not, or it might involve several queries. Why should there be limit of one query to every function?

I dunno, I got it in my head that it could be considered cleaner. Instead of ...

php:
<?
function DoStuff($ID)
{
    mysql_query('insert ... select');
    mysql_query('delete from...');
}
?>
there'd be...

php:
<?
function DoStuff($ID)
{
    CopyChatLines($ID);
    DeleteChatLines($ID);
}
?>
and keeping the actual functions free of query code, relegating that to specific query functions. But I guess having reusable queries only really matters if they are, well, reused. This is prematurely splitting them out.

epswing
Nov 4, 2003

Soiled Meat

Golbez posted:

But I guess having reusable queries only really matters if they are, well, reused. This is prematurely splitting them out.

Agree. However, one could say that splitting code out into functions "unnecessarily" (ie. those functions are only ever called from one place) causes you to become more aware of their existence, and more likely to reuse them, than if they were buried in some function.

Begby
Apr 7, 2005

Light saber? Check. Black boots? Check. Codpiece? Check. He's more machine than kid now.

Golbez posted:

and keeping the actual functions free of query code, relegating that to specific query functions. But I guess having reusable queries only really matters if they are, well, reused. This is prematurely splitting them out.

There is nothing concrete about how to split up functions, don't overthink it, nor is there a rule saying that a function has to be used more than once.

By far the most important thing though is that your code is readable and maintainable. If a function gets to be too large, it might make sense to split it into smaller functions, then call those functions in order from some master function. I think that is a design pattern with some fancy name, but who cares.

It might make sense to create a class with private methods to accomplish this, or at least pretend private methods depending on what version of PHP you are stuck with.

Personally I prefer to work with code that is split into a lot of classes, each with a clearly defined purpose. Then within those classes have single task methods with clear self documenting names. Again, this is just my personal preference, do what works for you.

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

Begby posted:

It might make sense to create a class with private methods to accomplish this, or at least pretend private methods depending on what version of PHP you are stuck with.

Personally I prefer to work with code that is split into a lot of classes, each with a clearly defined purpose. Then within those classes have single task methods with clear self documenting names. Again, this is just my personal preference, do what works for you.

Well, if this were a class/object, then I would probably have separate functions for each query, my mind just works easier that way when dealing with a class. But, and I've said this here before with much pain, they don't use classes here so I'm dealing with 1030 lines containing fifty functions. Not massive, but among the larger projects I've done (note that the number doesn't include the 2000 lines of javascript).

I miss objects.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Hammerite posted:

There is a way to avoid this being a problem. Namely, instead of using the mysql functions for looping through resultsets in your application code, you create your own wrapper function and call that instead. That way, it doesn't matter that you are returning a mysql resource. If you decide to change to another way of communicating with your database, you can just update the code in your wrapper function. It doesn't matter that a different type of resource is being returned, because it's still being dealt with by your functions and it makes no difference as far as the code that uses those functions is concerned.

Or instead of reinventing the wheel you use PDO so other people that read your code don't think you are crazy.

Hammerite
Mar 9, 2007

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

fletcher posted:

Or instead of reinventing the wheel you use PDO so other people that read your code don't think you are crazy.

What does what we were talking about have to do with PDO versus proprietary database classes or functions? You would still have to work with resultset objects, or whatever the equivalent is when using PDO.

By the way - if your response to that is "PDO is so wonderful that you'll never need to use anything else, just explicitly use PDO instead of any kind of wrapper functions" then

a) I don't buy it,

b) I've seen example PDO code that you and others have posted and in my opinion it looks pretty ugly, and harder to understand than the code I make use of for performing queries.

Adbot
ADBOT LOVES YOU

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror

fletcher posted:

Or instead of reinventing the wheel you use PDO so other people that read your code don't think you are crazy.

PDO is nice and all, but you still need to write a half-dozen lines of code to do anything useful. I use a set of sprintf-like wrapper functions that automatically escape arguments, run the query, and return the first field of the first result, or the first result row as an array, or the result set, etc.

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