I'v done this query to look for opportunities based on account name and name:
public function getOpportunity($session_id, $client, $email, $name){
$acc = $this->getAccount($session_id, $client, $email);
$acc_id = $acc->entry_list[0]->id;
$query2 = "
opportunities.id in
(select accounts_opportunities.opportunity_id from
accounts_opportunities inner join
accounts where
accounts_opportunities.account_id = accounts.id and
accounts_opportunities.deleted = 0 and
accounts.deleted = 0 and
accounts.id = '{$acc_id}')
and opportunities.deleted=0
and opportunities.name = '{$name}'";
$op = $client->get_entry_list($session_id, 'Opportunities', $query2);
return $op;
}
For some reason, it is returning me an error with erro 40 access denied. I don't know why this query is not working. I did a similar one, to find Accounts by email, and it successfully return me the results i want without any error. That query is the following:
public function getAccount($session_id, $client, $email){
$query = "accounts.id in
( select bean_id from
email_addr_bean_rel inner join
email_addresses where
email_addr_bean_rel.email_address_id = email_addresses.id
and email_addr_bean_rel.deleted = 0 and
email_addresses.deleted = 0 and
bean_module = 'Accounts' and
email_addresses.email_address = '{$email}' )";
$acc = $client->get_entry_list($session_id, 'Accounts', $query);
return $acc;
}
Any help would be appreciated. Thank you.
EDITED: I'm using SOAP API and PHP.
See this blog post for more details...
http://developers.sugarcrm.com/wordpress/2012/03/19/howto-avoiding-subqueries-with-our-web-services/
Related
Looking for download (PDF, CSV) the data from moodle custompage. Are there API function, need to call for downloading the data from custompage. The custompage has a table data:
custompage
$table = new html_table();
$table->head = array('course', 'users');
$table->attributes['class'] = 'table';
$sql = "SELECT c.fullname, count(cc.userid) AS 'completed'
FROM {course_completions} cc JOIN {course} ON c.id = cc.course WHERE cc.timestarted > 0
GROUP BY c.fullname ";
$mds = $DB->get_records_sql($sql);
foreach ($mds as $m) {
$table->data[] = new html_table_row(array(implode(array($m->fullname. $m->lastname)),
$m->completed ));
}
echo html_writer::table($table);
For a basic PDF
require_once($CFG->libdir . '/pdflib.php');
$doc = new pdf();
$doc->AddPage();
$output = html_writer::table($table);
$doc->writeHTML($output);
$doc->Output('filename.pdf');
There is a better example in the Moodle source here - /lib/tests/other/pdflibtestpage.php
For CSV
require_once($CFG->libdir . '/csvlib.class.php');
csv_export_writer::download_array('filename.csv, $mds);
Both of the above should be before printing anything to the screen so they are downloaded.
Also in your SQL, you should use GROUP BY c.id because the course fullname isn't unique.
How can i get rank or position base highest ' gpa' and 'total' into database rows data in codeigniter?
public function fetchData() {
$data = array();
$data['fetch_data'] = $this->excel_data_insert_model->fetch_data();
$result = $data['fetch_data']->result();
foreach ($result as $row) {
//echo $row.'<br/>';
$id = $row->id;
$p_student_id = $row->stu_id;
$p_section = $row->section;
$p_gpa = $row->gpa;
$p_total = $row->total;
$x = 0;
}
}
My Answer to your raw query could be this:
Select column_name1, column_name2, MAX(gpa),MAX(total) from tbl_v1 ORDER by gpa, total desc
Also for CI
$query=$this->db->select('column_name1, column_name2, MAX(gpa),MAX(total)')->order_by('gpa,total','desc')->get('tbl_v1');
return $query;
I am working on a component where I want to show all users of a specific usergroup. Right now I found two solutions for this but I'm not feeling comfortable with both of them.
Solution 1
$usersID = JAccess::getUsersByGroup(3);
$users = array();
foreach($usersID as $cUserID)
{
$users[] = JFactory::getUser($cUserID);
}
This one seems to produce two database queries every time JFactory::getUser($cUserID) is called. I really don't want this.
Solution 2
function inside model
function getUsers()
{
if(!isset($this->users))
{
$groupID = 3;
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$select = array( 'users.id', 'users.name');
$where = $db->quoteName('map.group_id') . ' = ' . $groupID;
$query
->select($select)
->from( $db->quoteName('#__user_usergroup_map', 'map') )
->leftJoin( $db->quoteName('#__users', 'users') . ' ON (map.user_id = users.id)' )
->where($where);
$db->setQuery($query);
$this->users = $db->loadObjectList();
}
return $this->users;
}
This one works like a charm but I feel there should be a "more Joomla! way" of doing this. I don't like working on their tables.
Right now I'm going with solution 2 but i really wonder if there is some better way to do it.
$condgrade = $DB->get_recordset_sql('SELECT c.fullname, gi.itemtype, gi.itemname,
gg.userid, gg.finalgrade FROM
'.$CFG->prefix.'grade_items gi
JOIN '.$CFG->prefix.'grade_grades gg ON gi.id =
gg.itemid JOIN '.$CFG->prefix.'course c ON c.id =
gi.courseid WHERE
gi.itemtype IN ("course") AND
gg.userid = '.$USER->id.'');
foreach($condgrade as $cgg)
{
$this->content->text .= html_writer::div($cgg->fullname.' : '.round($cgg->finalgrade).'%', array('class' => 'cours'));
}
Is there any other way of retrieving data directly from the query, rather using foreach loop everytime something like in mysql : mysql_fetch_object($users);
$records = $DB->get_records_sql('SELECT c.fullname, gi.itemtype, gi.itemname,
gg.userid, gg.finalgrade FROM
{grade_items} gi
JOIN {grade_grades} gg ON gi.id =
gg.itemid JOIN {course} c ON c.id =
gi.courseid WHERE
gi.itemtype IN ("course") AND
gg.userid = :userid', array('userid' => $USER->id));
This will return an array of records.
Note also, the use of {tablename} notation, rather than manually inserting the $CFG->prefix and the use of ':userid' bound parameter + parameter list passed as "array('userid' => $USER->id)", which helps to protect you from SQL injection attacks.
Get a single record?
$user = $DB->get_record('user', array('id' => 1));
http://docs.moodle.org/dev/Data_manipulation_API#Getting_a_single_record
EDIT : As well as the recommendations from davosmith, you also need to have a unique id in the first column. Also you can just use $DB->get_records_sql() - use $DB->get_recordset_sql() if you expect a large amount of data. If you do use get_recordset_sql() then you will also need to check the query is valid with $condgrade->valid() and close it after $condgrade->close();
$sql = "SELECT gg.id,
c.fullname,
gi.itemtype,
gi.itemname,
gg.userid,
gg.finalgrade
FROM {grade_items} gi
JOIN {grade_grades} gg ON gi.id = gg.itemid
JOIN {course} c ON c.id = gi.courseid
WHERE gi.itemtype = :itemtype
AND gg.userid = :userid";
$params = array('itemtype' => 'course', 'userid' => $USER->id);
$condgrade = $DB->get_records_sql($sql, $params);
This will return an array of record objects. The array key will be whatever is in the first column. eg:
$condgrade[1] = $recordobject;
So you can access a record object directly eg:
with $condgrade[1]->fullname
But you won't know what the id is unless you retrieve it from the database. So I'm not clear why you want to access the object directly? If you can give me an example of how you would use this directly then I can try to give an answer.
I am working on a facebook application, and some of the data that I want are the groups of that user and the other members of that group as shown in this code:
$groups = $facebook->api('/me/groups?since=0');
mysqlc();
foreach($groups['data'] as $group){
$gid=GetSQLValueString($group['id'], "text");
$name=GetSQLValueString($group['name'], "text");
//echo $gid." -> ".$name;
$query = sprintf("SELECT * FROM `group` WHERE gid = %s", $gid);
$res = mysql_query($query) or die('Query failed: ' . mysql_error() . "<br />\n$sql");
if(mysql_num_rows($res) == 0){
$iquery = sprintf("INSERT INTO `group`(gid, name) values(%s,%s)", $gid, $name);
$ires = mysql_query($iquery) or die('Query failed: ' . mysql_error() . "huhuhu<br />\n$sql");
}
$c = explode("'", $gid);
$params = "/".$c[1]."/members";
echo "'".$params."'";
$members = $facebook->api("'".$params."'");
print_r($members);
}
what I wanted to have after this code are the members of each group that the user has, but this code is not working properly.. So my main query is what is the proper way or replacement to this line:
$members = $facebook->api("'".$params."'");
where I can use the fetched group Id. thanks a lot to those who will answer my query.