Joomla 3 JDatabase how to echo rows - database

I can't find valid statement for fetching rows and displaying items in JDatabase.
My code looks like this:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array('item_id, item_name'));
$query->from('#__items');
How to print out these items in table?

The docs page is here: http://docs.joomla.org/Accessing_the_database_using_JDatabase/3.0
you need to add something like this:
$db->setQuery($query);
$results = $db->loadObjectList();
This will give you an array of objects where each object is a row.
This page: http://docs.joomla.org/Accessing_the_database_using_JDatabase/1.5 is for Joomla! 1.5, but still has (imho) the best list of the possible functions to get your data. Most are still valid I think.
To output the $results array you use something like this:
foreach ($results as $row) :
echo $row->item_id;
echo $row->item_name;
endforeach;

Try like this:
// Get a database object
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('item_id, item_name');
$query->from('#__items');
// sets up a database query for later execution
$db->setQuery($query);
// fetch result as an object list
$result = $db->loadObjectList();
For more detail see the link Accessing the database using JDatabase/3.0 . For more methods of how to fetch result you can use loadResult(),loadRow(),loadAssoc(),loadObject(),loadResultArray(),loadRowList(),loadAssocList().
You can also refer this Accessing the database using JDatabase/1.5 .
You can also refer this link for how to
Developing a Model-View-Controller Component/3.0/Introduction
Developing a Model-View-Controller Component/2.5
Hope it help you.

You don't add the array to your select query, you simply add the values, separated by a comma, like so:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('item_id, item_name')
$query->from('#__items');
$db->setQuery($query);
$results = $db->loadObjectList();

Related

Joomla article 3.X - Hot to define a img article for a external run

I am using (improperly) a call to an external db where, based on the title of the article (which is empty) it gives me back some values ​​which I then expose.
The problem is that being the article composed only by the title and when I go to use the Joomla modules, they don't see the values ​​that I extract from db.
In practice, at each external call, I would like to re-define the parameters of that view:
introtext
text
img
PHp code:
<?php
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
$ids = explode(':',JRequest::getString('id'));
$article_id = $ids[0];
$article =& JTable::getInstance("content");
$article->load($article_id);
$nome_micio = $article->get("title");
}
echo $nome_micio;
$db = JFactory::getDbo();
$gatto = $db->setQuery("SELECT * FROM #__gatti WHERE nome = '$nome_micio' and deceduto = 'No'")->loadObject();
?>
I need something like
define ($article->introtext) = $gatto->textonquerydb
Any ideas?

I want to show some values stored in database in a Joomla article

Hello everyone I want to show some values from the database in a joomla article in the following manner how can I do it ???
I've created the database in the following manner
One way to include php code in your article is to use an extension like
http://extensions.joomla.org/extension/sourcerer
This will allow you to put some php code that will make a query. Doing this in a Joomla way it would look like:
$db = JFactory::getDbo();
$query = $db->getQuery(true)
$query
->select($db->quoteName(array('ques', 'options', 'ans')))
->from($db->quoteName('#__yourtablename'))
->where($db->quoteName('id') . ' = ' . $idOfQuestionYouWantToGrab);
$db->setQuery($query);
$result = $db->loadAssoc();
Now you can show your question:
<?php echo $result['ques']; ?>
then somehow you will need to visualize questions separating them them with some regular expression, since your fields aren't structured well :/
Same with the answers. Then you will need to make some validation with JavaScript to see if answer is the correct one.

How to use findAll() in yii2?

I want to know how can i get all data of user with array id for where condition
In yii you could do something like this
$students = Student::model()->findAll("id IN ({$_POST['studentIds']})");
or
$userDtls = Student::model ()->findAllByAttributes ( array (
'id' => explode ( ",", $_POST ['studentIds'] )
) );
Now in yii2 CDbCriteria is not there, so which approach should i use to achieve same thing??
I have tried this but it only returns data for first id in the array
$result = Users::findAll([ 'id'=> $_POST ['keylist']]);
In documentation it is written that we can use this
$result = Users::findAll([1,488,489]);
But my array $_POST['keylist'] is something like this
keylist{
0='1'
1='5'
2='8'
}
I have also tried this
$ids = \Yii::$app->request->post('keylist', []);
$result = Users::findAll($ids);
And still returns data for first id in the array here is the screenshot
Thats why it doesnt work i guess
thank you
$users = Users::findAll($ids); is a correct approach.
See what you can pass in $ids in official docs here.
As I explained you here, you should never trust data from $_POST and check it for existence and validate before using.
Example of getting and check for existence with Yii2:
$ids = \Yii::$app->request->post('ids');
Or just:
$ids = isset($_POST['ids']) ? $_POST['ids'] : null;
For more complex cases I'd recommend to create separate search model and use it with validation, see Gii's CRUD for example.
UPDATE: Pay attention to what you actually pass as $ids.
$students_ids = Yii::$app->request->post('studentIds');
if(isset($students_ids)) {
$result = Users::find()->where(['in','id',$students_ids])->all();
}
var_dump($result)
Try like this

Trying to Select Single Value from Joomla 2.5 Database

I am trying to determine if a certain module is published or not. Here is the script I am using to query the database:
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('published');
$query->from('#__modules');
$query->where('module = mod_modulename');
$options = $db->loadObjectList();
When I try to return $options; I am getting a blank screen. Why won’t this just tell me if it is a 1 or 0 value and how can I fix it? Thanks.
Well I have it cleared up that my query was incomplete, so that you all for those notes. I now am having trouble getting the values that are loaded in the object list to print on the screen. I have tried return print_r and a foreach loop but nothing is showing up. Is there a way to test and find out if the object list is empty? It should not be as I see the value in the database table...
You forgot adding the following
$db->setQuery($query);
So completed your query would be
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('published');
$query->from('#__modules');
$query->where('module = mod_modulename');
$db->setQuery($query); //this is what you forgot
$options = $db->loadObjectList();
Edit: You also don't need to set $db as reference anymore (&)

CakePHP - how to handle double quote in conditions array

See the following example code:
$conditions = array("Post.title" => 'This is a "Book"');
// Example usage with a model:
$this->Post->find('first', array('conditions' => $conditions));
Because find() actually looks for title = 'This is a \"Book\"', no result returned. I am wondering how to prevent find() from adding backslashes. Or is there any other solution?
==fixed==
*Actually the error occurred when I used updateAll($field, $conditions), not find(). I did not put the quote around literal values. For example, $field = array('title' => $some_title) should be $field = array('title' => "'" . Sanitize::escape($some_title) . "'") . Don't like the way CakePHP handles this though.*
You must be mistaken. The error must be somewhere else.
The resulting SQL query does contain
LIKE 'foo \"bar\"'
But that escaping is actually intentional.
I will still turn up the DB entry with foo "bar" - I just tried it myself with cake2.3/2.4.
So CakePHP is working correctly.
Just checked with cakePhp version 2.3.5, The double quotes are working fine, Please check below code for a Profile controller.
$data = $this->Profile->find('all',array('conditions'=>array('Profile.type'=>'user "one"')));
pr($data);

Resources