[thelist] functionitis

Emma Jane Hogbin emmajane at xtrinsic.com
Thu Apr 3 10:17:32 CST 2003


Hey all

I think this is more of a theoretical "how do you do it" than a "I need
help" question.

I'm currently working on my first ever object oriented PHP web
application. *insert line about things not killing you making you
stronger* Sometimes I have a hard time deciding when a series of tasks
deserves its own function. Is it when the thing occurs more than once,
more than twice, (etc to n+1).

For example: Today's project is a calendar/job jar/task tracking tool. I'm
working on the form where you'll enter (and edit) the entries. Scheduled
and actual time both have the exact same layout. But I can't think of
where I'll need the layout anywhere else in the site. Should I make a
function to create these two pieces, or should I copy and paste?

How do you make these kinds of decisions on whether or not to build a
new function?

emma

<tip author="emmajane" type="printing PHP arrays">
Not entirely sure what you spit out of your database? Simply print your
array of results to the page:
	print "<pre>";
	print_r($arrayName) 
	print "</pre>";
Using the <pre>formatted element will allow you to see the structure of
your array by preserving tabbed indents.
</tip>

<tip author="emmajane" type="storing PHP database output">
If you want to access the database field names in your array use an
associative array to store rows from your database:
	while ($rows = mysql_fetch_array($mysql_query)) {
		$results[$rows["id"]]["column1"] = $rows["column1"];
		$results[$rows["id"]]["column2"] = $rows["column2"];
	}
To access a single item (for a unique $id) use $results[$id]["column1"]. 
Because this method is foreach friendly you can save yourself a lot of
time dealing with the results. If you array_push the rows into an array of
results you end up with both [0] and ["named"] keys in your array, which 
makes life infinitely more complicated. 
</tip>

PS If anyone knows a more effecient way of doing the above, I'd love to
know. Right now I have to set each column individually (i.e. repeat the
$results["values"] = $rows["values] line). At least what I'm doing now is
about 100 times more useful than having array_pushed rows.

-- 
Emma Jane Hogbin
[[ 416 417 2868 ][ www.xtrinsic.com ]]


More information about the thelist mailing list