Hi, i am a ASP-guy converting to MODx/Php trying to figure things out
Im used to build db-solutions for customers on IIS/ASP.
In ASP i used a Recordset-object to execute Queries, Update records and use the result in the Recordset to present it on webbpages.
How is this done in "Best Practice" in php against custom tables in Modx?
Thx
/R Sweden
[ed. note: johnjohn22 last edited this post 14 years ago.]
____________________________________________________
Antonio Zdilar
linearvector.com
Hi BC, thanks for link, will try it out.
I managed to used plain php to INSERT and also SELECT data from db. I put the code in two snippets, one to insert and one to read.
Q1: Why should i use xPDO instead of plain php to access my db ?
Q2: In ASP i usualy mix server script with html to presernt the final result. How do i do this in MODx, in a chunk or a snippet?
READ DATA:
mysql_connect('x.x.x.x',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO custom_products VALUES ('','Saab','3')";
mysql_query($query);
READ DATA:
mysql_connect('x.x.x.x',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM custom_products";
$result = mysql_query($query);
____________________________________________________
Q1: xPDO is an ORM, so google for ORM and you will find answer to Q1
Q2: You should not mix script with html. Script (php) goes to snippets (retrieve data, do some logic..) and presents data through chunk (html + placeholders for data passed from snippet)
Antonio Zdilar
linearvector.com
Thanks BC. i will follow your lead...
____________________________________________________
Ok, so i got some xPDO database-operations to work.
Question: Now, i would love a way to write plain SQL-querys?
Have tried this but it returns 0 rows:
$query = "SELECT * FROM Products WHERE iStatus=1";
$criteria = new xPDOCriteria($modx, $query);
$products = $modx->getCollection('Products',$criteria);
$output .= '<p>Totalt: '. count($products) . '</p>';
____________________________________________________
-
MODX Staff
- 10,725 Posts
It doesn't work because your WHERE clause is empty.
Thx opengeek, it should be:
$query = "SELECT * FROM Products WHERE field1=1 OR field1=2";
$criteria = new xPDOCriteria($modx, $query);
$products = $modx->getCollection('Products',$criteria);
$output .= '<p>Totalt: '. count($products) . '</p>';
____________________________________________________
And a getChunk question:
I pass a database-row to a chunk like this:
foreach($products as $product) {
$fields = $product->toArray();
$output .= $modx->getChunk('ListProduct', $fields);
}
Question: How can i pass an additional value besides the database-values ?
I have tryed with $output .= $modx->getChunk('ListProduct ? &name=´george´', $fields); but it didnt work...
____________________________________________________
foreach($products as $product) {
$fields = $product->toArray();
$fields['besidesDBValue']='someValue';
$output .= $modx->getChunk('ListProduct', $fields);
}
Antonio Zdilar
linearvector.com