I am trying to figure out how to do a simple database query and loop over the result, normally I would do something like below, how do I do it in Revo? I have spent a ton of time going over the docs and I am lost. I have a template variable that puts a 0 or 1 into its value.
I just want to do something like this:
$output = ’’;
@ $db = new mysqli(’localhost’,’root’,’root’, ’db’);
if (mysqli_connect_errno()) {
echo ’Could not connect to database’;
exit;
}
$query = "SELECT site_tmplvar_contentvalues.tmplvarid, site_tmplvar_contentvalues.contentid, site_tmplvar_contentvalues.value
FROM site_tmplvar_contentvalues
WHERE value = 1
AND tmplvarid = 19";
$result = $db->query($query);
$numrow = $result->num_rows;
if($numrow) {
for($i=0;$i<$numrow;$i++) {
$row = $result->fetch_assoc();
$ouput = $row[’contentid’];
}
}
echo $output;
Are you connecting to the MODx DB or another one?
The MODx DB is always connected, so that’s unnecessary.
For another one, the best way is to create xPDO classes for it, instantiate a new xPDO object, and use xPDO for the queries.
See this:
http://bobsguides.com/custom-db-tables.html
and this:
http://bobsguides.com/revolution-objects.html
What information, exactly, do you want?
I have a TV to decide if that resource should be summarized and listed out on another page (featured yes or no), it stores a 1 or 0. So I need to look in the tmplvar_contentvalues table look for all the tmplvarid’s that = 19, see if it has a value of 1, loop through the results to get its contentid so I can add it to the summary page.
Doing that query outside of MODX is pretty straight forward. Just don’t know how to accomplish it here.
I went and looked at the links you sent and I am guessing I do something like
this
$tvs = $modx->newQuery(’modTemplateVar’);
$tvs->where(array(’tmplvarid’ => 19, ’value’ => 1));
or this?
$tv = $modx->getObject(’modTemplateVar’,array(’tmplvarid’ => 19, ’value’ => 1));
$id = $tv->getValue(’contentid’);
thanks for your help.
The easiest way is:
$val = $modx->resource->getTVValue(19);
Or using the name of the TV:
$val = $modx->resource->getTVValue('MyTv');
This would work also,
$tv = $modx->getObject('modTemplateVar',19);
$val = $tv->getValue($modx->resource->get('id'));
If you want the value of the TV in another resource:
$tv = $modx->getObject('modTemplateVar',19);
$val = $tv->getValue($resourceId);
Thanks Bob,
I will give this a go. Thanks again for the help.
So I thought I would report back that when I was looking in the getResources source to find out how to do a few things for this snippet, I realized I was building something already there all along in getResources. I just needed to use the tvFilters.
One strange thing about this is that when I tried to use a drop down that was a yes or no storing a 0 or 1 it didn’t work. If I use a text field that yes or no was typed in it did work. Odd, not sure why thats the case.
Either way, this is the solution I needed. Would be nice if I could use that drop down though.
can you show us your complete code what you did try:
- input-option-values for dropdown-tv
- chunk for getResources-tpl
- getResources snippet-call