PHP Wizards

From: Ken (SHIELDSIT)20 Apr 2011 14:27
To: Peter (BOUGHTONP) 51 of 101
Pete are you up for a complicated question?
From: Peter (BOUGHTONP)20 Apr 2011 14:36
To: af (CAER) 52 of 101
Yes, of course, (because that's how my imaginary SQL database has been configured to react).

(It's been possible to do triggers with databases for years, long before Rails came along. Probably even before Ruby too.)


It's a function (stored procedure), just like your User.findByEmail one. It's automatically generated by another one of those triggers - every time a table is created or altered - so it's faster than doing dynamic lookups with Method Missing.
From: Peter (BOUGHTONP)20 Apr 2011 14:39
To: Ken (SHIELDSIT) 53 of 101
If it's a quick complicated one... I'm supposed to be finishing something off at the moment, but keep getting distracted by that dastardly Rails fan. :@
From: af (CAER)20 Apr 2011 14:40
To: Peter (BOUGHTONP) 54 of 101
Well now, that's interesting.

I'd best not get too involved in this as I really don't know all that much :$
From: Peter (BOUGHTONP)20 Apr 2011 14:46
To: af (CAER) 55 of 101
The difference is, nobody has actually created a popular framework for all this stuff (well, not that I'm aware of), but the ability to do it is there.

Very little (if any) of the stuff that Rails (or others) is new - it's all been possible for decades, but either computers were too slow, or it just didn't get used in mainstream until someone actually shouted about it.
From: af (CAER)20 Apr 2011 14:48
To: Peter (BOUGHTONP) 56 of 101
Hmm. Why's nobody made a popular framework for it? Is that sort of thing possible in SQL? Man I really don't know much about server things :$
From: Peter (BOUGHTONP)20 Apr 2011 14:56
To: af (CAER) 57 of 101
Well it's not pure SQL at that point, it's getting into database-specific programming languages.

And I guess, especially compared to more general programming languages, they're a bit crappy - they're static typing, still needs some form of front-end to be useful, and stuff.
From: Ken (SHIELDSIT)20 Apr 2011 15:01
To: Peter (BOUGHTONP) 58 of 101
I don't think it's going to be quick, so I'll wait to post it.
From: Ken (SHIELDSIT)20 Apr 2011 15:29
To: Peter (BOUGHTONP) 59 of 101

OK I've attached a screen shot of the current report and my code. Here is what I need to do, and fucked if I know if I can do it.

 

In the screen shot you can see that everything in the description field is Red Oak except one (which is White Oak). This can change to anything.

 

For example we could have a batch of Cherry and then one Hard Maple in the batch.

 

So what I need to somehow do is get a majority of the type of wood it is and ignore the minority. Pretty complicated....

Attachments:
From: Peter (BOUGHTONP)20 Apr 2011 17:45
To: Ken (SHIELDSIT) 60 of 101
I'm still busy, but want a quick break, so anyway...

CFML code:
<!---
	Count how many of each wood type is used...
--->
<cfset WoodTypes = [] />
<cfloop query="batch_report3">
	<cfset WoodName = extractWoodName(Description) />
 
	<cfif StructKeyExists(WoodTypes,WoodName)>
		<cfset WoodTypes[WoodName]++ />
	<cfelse>
		<cfset WoodTypes[WoodName] = 0 />
	</cfif>
</cfloop>
 
<!---
	Work out what the most common wood type is...
--->
<cfset MaxCount = 0 />
<cfset MaxType = "" />
<cfloop item="CurType" collection=#WoodTypes#>
	<cfif WoodTypes[CurType] GT MaxCount >
		<cfset MaxCount = WoodTypes[CurType] />
		<cfset MaxType = CurType />
	</cfif>
</cfloop>
 
<cfoutput>
	<p>Most common wood type is "<b>#MaxType#</b>".</p>
</cfoutput>
 
 
<!---
	Extract the name from the description.
	(May need changing/expanding depending on other description formats.)
--->
<cffunction name="extractWoodName" returntype="string" output="false" >
	<cfargument name="Text" type="string" required />
 
	<cfset var Name = rereplace(Arguments.Text,'^\d/\d x RW','') />
 
	<cfset Name = rereplace(Arguments.Text,'(?:prime|\da?)? (?:common|boards|unselected|blocking) green') />
 
	<cfreturn Trim(Name) />
</cffunction>


If one of the many crazy columns has just the wood name on its own, you don't need the extractWoodName bit.

I didn't test that, so there might be bugs in it, but hopefully not.


Oh, and that only returns one maximum, so if there's like 5 Cherry, 5 Hard Maple, 2 White Oak, it'll say "cherry" OR "hard maple", but not both.
Don't know if that's good enough, or if you do need to cater for that?



If I wasn't busy, I'd suggest a billion ways to make your code look nicer, but I will say just two things:

TABS ARE YOUR FRIEND!
Identing blocks makes things MUCH easier to read.
(Although using eight space for it, like Firefox is doing above, is a tad annoying.)

Also, you don't need lots of those #s - they basically only need to be used when outputting HTML/etc (i.e. inside cfoutput), or when using a variable in a string (i.e. inside "quotes"), but generally not in cfset tags.
EDITED: 20 Apr 2011 18:22 by BOUGHTONP
From: Ken (SHIELDSIT)20 Apr 2011 17:59
To: Peter (BOUGHTONP) 61 of 101

Yeah there is no doubt my code isn't nice.

 

I think I've found the easiest way. There is a product id table that seems to have the types in some kind of unique code. I'll take that approach.

 

Thanks man!

From: Ken (SHIELDSIT)20 Apr 2011 18:13
To: Peter (BOUGHTONP) 62 of 101
Was trying to use some of that code you made to see if I could get it to extract the most common used and I get this error.

quote:
Wrong Context, when you use attribute index you must also use attribute from and to or list or file


It's pointing to this bit:

quote:
28: <cfloop index="CurType" collection=#WoodTypes#>


Any ideas?
From: af (CAER)20 Apr 2011 18:14
To: Peter (BOUGHTONP) 63 of 101
quote:
TABS ARE YOUR FRIEND!
Identing blocks makes things MUCH easier to read.
(Although using eight space for it, like Firefox is doing above, is a tad annoying.)
WRONG :@

Spaces are your friend. Tabs* are evil.

* the ASCII #9 character
From: Peter (BOUGHTONP)20 Apr 2011 18:15
To: Ken (SHIELDSIT) 64 of 101
Oh crap, I always do that. :(

Change index="CurType" to item="CurType"
From: Peter (BOUGHTONP)20 Apr 2011 18:21
To: af (CAER) 65 of 101
Oh no, you're not one of those freaks are you!? :P

Tabs allow configurable indenting. Spaces don't.

Spaces are silly.

(Though still preferable to no indents at all.)
EDITED: 20 Apr 2011 18:22 by BOUGHTONP
From: af (CAER)20 Apr 2011 18:24
To: Peter (BOUGHTONP) 66 of 101

If you have a decent editor, spaces do allow configurable indenting. Plus they guarantee consistency of appearance, which is important when you're working with other people on a file and there is a mandated maximum line length.

 

(honestly though I don't care much - if I download some sample code that uses tab characters I just do :retab in Vim to convert them to spaces. Consistency is what's important)

EDITED: 20 Apr 2011 18:32 by CAER
From: Peter (BOUGHTONP)20 Apr 2011 18:35
To: af (CAER) 67 of 101
No they don't, they allow convertable indenting, because the editor needs to convert from actual to preferred (and probably back again when saving).

Tabs allow it to be done without touching the data.


Also, a "decent editor" could deal without any indenting (or newlines) at all - but we don't always have a decent editor to hand, so keeping the raw files with sensible whitespace makes far more sense.


quote:
they guarantee consistency of appearance, which is important when you're working with other people on a file and there is a mandated maximum line length.

WTF!? Who has maximum line lengths? This isn't 1970s any more.

Get a decent editor and you have:
1) horizontal scrollbars
2) working (soft) word wrap.

And get a decent language and structure and you'll almost never go beyond 100 characters wide anyway.
EDITED: 20 Apr 2011 18:38 by BOUGHTONP
From: Ken (SHIELDSIT)20 Apr 2011 18:35
To: Peter (BOUGHTONP) 68 of 101

Can't get it to work, but that's OK because I've confused myself to the point that I'm not sure what I'm trying to accomplish.

 

Fuck my head hurts!

From: Peter (BOUGHTONP)20 Apr 2011 18:38
To: Ken (SHIELDSIT) 69 of 101
Is it giving an error or just producing the wrong result?
From: Ken (SHIELDSIT)20 Apr 2011 18:43
To: Peter (BOUGHTONP) 70 of 101

error. but I'm stuck on trying to figure out how to approach this.

 

I have the needed info in the database. So I just need to compare or count and that's where I'm stuck atm.