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 (&)
Related
I need your help because I can't save array from form in the pivot table. If someone can show me how because this is going to drive me crazy.
So I have a exam form with input number in foreach and which has a dynamic name.
And I have two total results to.
This is my data form :
I want save my input number question in pivot table and the results on another table.
For the result it's ok but it's for save in pivot table I have a problem.
Thanks to help me.
In my controller I make this :
$data = $request->all();
foreach($data as $question_id => $answer_id){
$result = new PratiqueReponse();
$result->question_id = $question_id;
$result->answer_id = $answer_id;
$result->save();
}
But not working.
You did not share much information, so i can only guess but i think you should save to db like this:
$data = $request->all();
foreach($data['questions'] as $question_id => $answer_id){
$result = new PratiqueReponse();
$result->question_id = $question_id;
$result->answer_id = $answer_id;
$result->save();
}
you need to loop trough questions, not the whole array.
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
In cakephp, I executed this query to find the id field value of the corresponding username
$count = $this->User->find('all',array('conditions'=>array('User.username' => $n)))
How I will get take the value of a field from this array result to a variable?
I am new to cakephp and any help will be very useful.
Well, after calling find, you will notice that $count will be populated if something was brought from the database. I would change something, though, I would use "first" instead of "all" because you are finding only one record.
You can either use this
//in your controller
$count = $this->User->find('first',
array('conditions'=>array('User.username' => $n)));
//and set the variable to be used in the view in this way
$this->set('yourId', $count['User']['id']);
Then in your view
echo $yourId;
Or, you can also do this
$yourId = $this->User->field('id', array('User.username' => $n));
$this->set(compact('yourId'));
and then in your view
echo $yourId
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);
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();