spreadsheet as text

Walter,

Trying to sort through this code. I don’t need to update the database-don’t want anyone to do that in fact! So, I think I don’t need to fuss with the tablekit[1] JavaScript library or Prototype.js.

I looked through the readme.mdown and can’t figure out what’s what. What is php, what is myactiverecord code, etc. I also see a folder named templates, but layouts_index.html just gives me a blank screen. Not sure what the other .php files in this folder are used for. I can’t seem to get a handle on the syntax.

If I want to sum the 7th column in the data base, how do I reference it? I need to total certain rows and also sub total based upon a value in a row cell value.

If I only want to read from a database, is ‘generate’ really necessary? It seems to be making an entire copy of the database for the MyActiveRecord code. Not sure I understand the benefits or when/why I would use all this vs php and mysql code. I want to be clear because my client will be making future updates to the database on a regular basis, so the simpler, the better.

Thanks for your help. Jan


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

The Gist that I posted last week works with just the
MyActiveRecord.php library, not any of the other stuff that Generate
makes.

The JavaScript was a separate thing if you needed the page to be
“live” like a real spreadsheet, and since you don’t, you can safely
ignore it.

If you wanted to sum a column in PHP, you could do that inside the
loop that builds the table. So say you had a price column, and you
were looping through all of your widgets as described in the example
Gist, you would just add a new variable to hold the incrementing
total, and then add to that inside the loop.

$widgets = MyActiveRecord::FindAll('Widgets');
$total = 0;
foreach($widgets as $widget){
	$total += $widget->price; //increment running total
	$out .= "n<tr id="row_" . $widget->id . "">";
	foreach($visible_columns as $col){
		$out .= '<td>' . $widget->h($col) . '</td>';
	}
	$out .= '</tr>';
}

Then further down the page, you would just output that $total variable
to show the sum of all prices. You could also ask the database server
for the total with a SQL query, but that steps outside of the main of
how MAR works.

Walter

On Mar 14, 2011, at 6:02 PM, jan smoot wrote:

Walter,

Trying to sort through this code. I don’t need to update the
database-don’t want anyone to do that in fact! So, I think I don’t
need to fuss with the tablekit[1] JavaScript library or Prototype.js.

I looked through the readme.mdown and can’t figure out what’s what.
What is php, what is myactiverecord code, etc. I also see a folder
named templates, but layouts_index.html just gives me a blank
screen. Not sure what the other .php files in this folder are used
for. I can’t seem to get a handle on the syntax.

If I want to sum the 7th column in the data base, how do I reference
it? I need to total certain rows and also sub total based upon a
value in a row cell value.

If I only want to read from a database, is ‘generate’ really
necessary? It seems to be making an entire copy of the database for
the MyActiveRecord code. Not sure I understand the benefits or when/
why I would use all this vs php and mysql code. I want to be clear
because my client will be making future updates to the database on a
regular basis, so the simpler, the better.

Thanks for your help. Jan


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Walter,

Thanks for your help on this and I hope it helps other with mysql database features. I have slowly figured out everything I need. Had to learn a bit of php, so that’s good.

Am still not clear on the advantages of MAR vs using mysql and interfacing directly with the database. Is MAR more secure in someway?

Thanks again for all you do for this community.
Jan


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

MAR keeps the plumbing out of your way, I used to write PHP for MySQL
long-hand, and it is so easy to get a little mistake somewhere and
have the whole thing fall down. Plus, writing long-hand encourages you
to write the same code over and over, or to mix it into your HTML to a
large degree. It’s not maintainable. Compare and contrast:

require_once('config.inc.php');
$widgets = MyActiveRecord::FindAll('widgets');

with

require_once('config.inc.php'); //just to be fair, put all the login  
stuff here
$link = mysql_connect($host,$user,$password);
if (!$link) {
	die('Could not connect: ' . mysql_error());
}
$result = mysql_query('SELECT * FROM `widgets` WHERE 1 ORDER BY id  
ASC');
if (!$result) {
	die('Invalid query: ' . mysql_error());
}
$widgets = array();
while ($obj = mysql_fetch_object($result)){
	$widgets[$obj->id] = $obj;
}
mysql_close($link);

That’s a lot of places where I have to type the same thing over and
over, and stand a very good chance of getting it wrong.

If I’m updating the database from a POST, the difference becomes even
more extreme:

if(isset($_POST['id'])){
	if($widget = MyActiveRecord::FindById('widgets',$_POST['id']){
		$widget->populate($_POST);
		$widget->save();
		header('Location: ' . $_SERVER['REQUEST_URI'];
		exit;
	}
}

To do the same in long-hand PHP, you would have to do all of the setup
from the previous example, then rip through the POST and sanitize all
of the variables (Google SQL Injection for a very scary look at how
easy it is to take down a database server), then build a query based
on your intimate knowledge of the data structure, with hard-coded
variable names, and then update the database. And each time your
database changes, you’d have to go through all of your code –
everywhere you touch the database – and make sure that you had the
correct set of variable names to match the columns. It’s so 1997. It’s
so easy to get wrong, and so hard to debug.

The ActiveRecord pattern uses the database itself as the foundation
for all the application code that touches it. Using a principle known
as “reflection”, it asks the database for a current list of columns,
then uses that to sanitize the inputs or to create the outputs. If I
change the database – at any point – the only code I have to update
is that which references a column value directly. So if I have a page
where I am showing the color of a widget, and I have $widget-

h(‘color’); in the HTML builder code, then if I change the database
to be British, I have only to change color to colour – right there,
where I’m using it. I don’t have to go through the rest of the
database access code and do the same there.

It’s a lot like setting out to build a skyscraper. If you have to keep
building the bottom 30 stories over and over each time you make a
change to the top 20, you’ll never get done.

Walter

On Mar 16, 2011, at 12:00 PM, jan smoot wrote:

Am still not clear on the advantages of MAR vs using mysql and
interfacing directly with the database. Is MAR more secure in someway?


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Walter,

Thanks for the clarification. The newbies of the world appreciate the shared experience and all that saves us from ourselves!

Until next time. Jan


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Walter,

It’s been over a year since I got this all setup and running with your help and am now having problems. I updated the data (incorrectly first time) and ran scaffold.php-which ran but not correctly. I then, thought I should make a backup of the database and accidentaly renamed it. When I ran scaffold.php it gave me an error. I changed the name back and continue to get the following error:

Fatal error: MyActiveRecord::Connection() - could not select database: /bankcom1_boli in /home6/bankcom1/public_html/generator/templates/MyActiveRecord.php on line 168

I tried copying the database and changing scaffold.php header info, but get the same error. Not sure what to do from here. Are there permissions I should check? Could really use your help since I seem to have “broken” my client’s site for now.

Thanks a lot.


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Check to see if any of the details in generate.php have changed at all. In particular, make sure that the database server name hasn’t changed from localhost to something else – maybe a dotted IP address, or a different hostname. If you look at your database server in phpMyAdmin (on your host’s cPanel) you should see the actual hostname at the top level (the home page) in the right pane, upper left corner.

Next, triple-check to be sure that your database exists, and that you can edit it as your user. Each table in each database can have different credentials – this is not common, but it is possible. Rule this out by adding a table to the database, renaming an existing table, naming it back. Make sure you can do those things in phpMyAdmin, and that you are logged in to that tool with the same user/password combination that you have entered in generate.php’s configuration block.

What you describe doesn’t sound like anything besides a configuration or permissions problem in the database.

Walter

On Mar 16, 2011, at 12:39 PM, Jan Smoot wrote:

Walter,

Thanks for the clarification. The newbies of the world appreciate the shared experience and all that saves us from ourselves!

Until next time. Jan


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

One last thing to rule out – Unix is case-sensitive. Go through all of the details you’ve entered and confirm that not only the letters and numbers are correct, but so are the case of the letters.

Walter

On May 7, 2012, at 3:22 PM, Walter Lee Davis wrote:

What you describe doesn’t sound like anything besides a configuration or permissions problem in the database.


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Walter,

Thanks for your suggestions. I did all of them. Found that the database “user” needed to be readded (with accompanying permissions) to the database. Must have become uncoupled when I accidentally renamed the database. Goofy mistake on my part.

Thanks for your direction Walter. This was painful, but fixable.


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options