Using a custom template (Journal 2) for a PHP-based E-commerce system.
In the control panel, there's a section that loads custom modules.
Problem is, the modules are presented in the order of creation, which makes it hard to find the right module.
I would like to sort them alphabetically.
How & where can I add a sort-by-name feature to this function?
public function all() {
if (isset($this->get_data['module_type'])) {
$module_type = $this->db->escape('journal2_' . $this->get_data['module_type']);
$query = $this->db->query('SELECT * FROM ' . DB_PREFIX . 'journal2_modules WHERE module_type = "' . $module_type . '"');
} else {
$query = $this->db->query('SELECT * FROM ' . DB_PREFIX . 'journal2_modules');
}
foreach ($query->rows as &$row) {
$row['module_data'] = json_decode($row['module_data'], true);
if (is_array($row['module_data'])) {
foreach($row['module_data'] as $key => &$value) {
if (!in_array($key, array('module_name', 'module_type'))) {
unset($row['module_data'][$key]);
}
}
}
}
return $query->rows;
}
Thanks in advance!
If you need an specific order, in sql you can use the order by clause. In your case could be useful order by module_name
$query = $this->db->query('SELECT * FROM ' . DB_PREFIX .
'journal2_modules WHERE module_type = "' .
$module_type . '" order by module_name ');
Try altering your query:
if (isset($this->get_data['module_type'])) {
$module_type = $this->db->escape('journal2_' . $this->get_data['module_type']);
$query = $this->db->query('SELECT * FROM ' . DB_PREFIX . 'journal2_modules WHERE module_type = "' . $module_type . '" ORDER BY module_data ASC');
} else {
$query = $this->db->query('SELECT * FROM ' . DB_PREFIX . 'journal2_modules ORDER BY module_data ASC');
}
At the end of your queries insert an ORDER BY condition like ORDER BY columnName to sort by that column.
public function all() {
if (isset($this->get_data['module_type'])) {
$module_type = $this->db->escape('journal2_' . $this->get_data['module_type']);
$query = $this->db->query('SELECT * FROM ' . DB_PREFIX . 'journal2_modules WHERE module_type = "' . $module_type . '"' . ' ORDER BY ' . $this->get_data['module_order_by']);
} else {
$query = $this->db->query('SELECT * FROM ' . DB_PREFIX . 'journal2_modules ORDER BY ' . $this->get_data['module_order_by']);
}
foreach ($query->rows as &$row) {
$row['module_data'] = json_decode($row['module_data'], true);
if (is_array($row['module_data'])) {
foreach($row['module_data'] as $key => &$value) {
if (!in_array($key, array('module_name', 'module_type'))) {
unset($row['module_data'][$key]);
}
}
}
}
return $query->rows;
}
Related
I have 2 functions which work together. But I can't add parameters on the second one, I don't understand how it works.
1st function
if(!function_exists('pixiehuge_select_all')) {
function pixiehuge_select_all($table, $where = null, $order = null, $limit = null, $andwhere = null, $noteq = false, $or = false) {
global $wpdb;
$q = "SELECT * FROM `{$table}`";
$signWhere = ($or) ? 'OR' : 'AND';
// Select where
if(empty($noteq)) {
if(!empty($where)) {
$q .= " WHERE `" . esc_sql($where[0]) . "`='" . esc_sql($where[1]) . "'";
}
if(!empty($where) && !empty($andwhere)) {
$q .= " " . esc_sql($signWhere) . " `" . esc_sql($where[0]) . "`='" . esc_sql($where[1]) . "'";
}
} else {
if(!empty($where)) {
$q .= " WHERE `" . esc_sql($where[0]) . "`!='" . esc_sql($where[1]) . "'";
}
if(!empty($where) && !empty($andwhere)) {
$q .= " " . esc_sql($signWhere) . " `" . esc_sql($where[0]) . "`!='" . esc_sql($where[1]) . "'";
}
}
if(!empty($order)) {
$q .= " ORDER BY `" . esc_sql($order[0]) . "` " . esc_sql($order[1]);
}
if(!empty($limit)) {
$q .= " LIMIT 0, {$limit}";
}
// Check if plugin exists
if(!function_exists('huge_app')) {
return false;
}
$result = $wpdb->get_results($q, ARRAY_A);
return $result;
}
}
2nd function The one I'm trying to edit
if(!function_exists('pixiehuge_streams')) {
function pixiehuge_streams($id = false, $streamCat = false, $slug = false) {
global $tables;
// Get Stream(s)
if($id) {
$streams = pixiehuge_select_all($tables['streams'], ['id', esc_sql($id)]);
} elseif($streamCat) {
$streams = pixiehuge_select_all($tables['streams'], ['category', esc_sql($streamCat)]);
} elseif($slug) {
$streams = pixiehuge_select_all($tables['streams'], ['slug', esc_sql($slug)]);
} else {
$streams = pixiehuge_select_all( $tables['streams'] );
}
return $streams;
}
}
What I'm trying to get $streams = "SELECT * FROM streams ORDER BY id DESC LIMIT 4"; (but if I do this I get error 500)
So how can I rewrite $streams by adding parameters to pixiehuge_select_all() in the second function to get the order by id desc and limit 4 ?
Just look at your function parameters and complete it.
$streams = pixiehuge_select_all($tables['streams'], [], ['id','DESC'] ,4);
ORDER BY is not working. I've tried ways I know but it won't order by, why?
returns : Call to a member function result_array() on boolean
public function getDetailbyClsandSection($class_id, $section_id, $exam_id) {
$query = $this->db->query("SELECT exam_schedules.*,subjects.name,subjects.type FROM exam_schedules,teacher_subjects,exams,class_sections,subjects WHERE exam_schedules.teacher_subject_id = teacher_subjects.id and exam_schedules.exam_id =exams.id and class_sections.id =teacher_subjects.class_section_id and teacher_subjects.subject_id=subjects.id and class_sections.class_id =" . $this->db->escape($class_id) . " and class_sections.section_id=" . $this->db->escape($section_id) . " and exam_id =" . $this->db->escape($exam_id) . " and exam_schedules.session_id=" . $this->db->escape($this->current_session));
return $query->result_array();
}
$query = $this->db->query("SELECT exam_schedules.*,subjects.name,subjects.type FROM exam_schedules,teacher_subjects,exams,class_sections,subjects WHERE exam_schedules.hide = 'N' and exam_schedules.teacher_subject_id = teacher_subjects.id and exam_schedules.exam_id =exams.id and class_sections.id =teacher_subjects.class_section_id and teacher_subjects.subject_id=subjects.id and class_sections.class_id =" . $this->db->escape($class_id) . " and class_sections.section_id=" . $this->db->escape($section_id) . " and exam_id =" . $this->db->escape($exam_id) . " and exam_schedules.session_id=" . $this->db->escape($this->current_session) . " ORDER BY exam_schedules.date_of_exam ASC");
I'm trying to get "product_id" by "customer_id", but don't know how to do it:
public function getProductId($customer_id) {
$query = $this->db->query("SELECT DISTINCT product_id FROM " . DB_PREFIX . "product p WHERE p.customer_id = '" . (int)$customer_id . "'");
if ($query->num_rows) {
$data = $query->row;
return $product_id;
}
}
I want to display a list of all attributes that are added into database but every time I try something it doesn't work. I want to show this inside a div from the from the front page. I tried to insert this into featured.tpl:
<?php
foreach ($attribute_groups as $attribute_group) {
echo $attribute_group['name'];
print_r($attribute_group);
echo '<select name="listaGrupe">';
foreach ($attribute_groups['attribute'] as $attribute) {
echo '<option value="'.$attribute.'">'.$attribute.'</option>';
}
echo '</select>';
}
?>
You ned to use this model
public function getAttributes($data = array()) {
$sql = "SELECT *, (SELECT agd.name FROM " . DB_PREFIX . "attribute_group_description agd WHERE agd.attribute_group_id = a.attribute_group_id AND agd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS attribute_group FROM " . DB_PREFIX . "attribute a LEFT JOIN " . DB_PREFIX . "attribute_description ad ON (a.attribute_id = ad.attribute_id) WHERE ad.language_id = '" . (int)$this->config->get('config_language_id') . "'";
if (!empty($data['filter_name'])) {
$sql .= " AND LCASE(ad.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
}
if (!empty($data['filter_attribute_group_id'])) {
$sql .= " AND a.attribute_group_id = '" . $this->db->escape($data['filter_attribute_group_id']) . "'";
}
$sort_data = array(
'ad.name',
'attribute_group',
'a.sort_order'
);
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
$sql .= " ORDER BY " . $data['sort'];
} else {
$sql .= " ORDER BY attribute_group, ad.name";
}
if (isset($data['order']) && ($data['order'] == 'DESC')) {
$sql .= " DESC";
} else {
$sql .= " ASC";
}
if (isset($data['start']) || isset($data['limit'])) {
if ($data['start'] < 0) {
$data['start'] = 0;
}
if ($data['limit'] < 1) {
$data['limit'] = 20;
}
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
}
$query = $this->db->query($sql);
return $query->rows;
}
The in your controller simply load this model and manipulate with the data it returns. By default it returns all the attributes in database
You may be looking for a function format closer to this:
foreach ($attribute_groups as $attribute_group) {
$options = '';
$name = $attribute_group['name'];
$output = '<select name="$name">';
foreach ($attribute_groups['attribute'] as $attribute) {
$options .= '<option value="'.$attribute.'">'.$attribute.'</option>';
}
echo $output.$options.'</select>';
}
check in your controller .. if $attribute_groups have datas in it....
print_r($attribute_groups);
and see.. make sure your $attribute_groups array is not empty... i think u getting that error since your $attribute_groups array is empty...
if incase you are sure, $attribute_groups can be empty then u can check ...
if(!empty($attribute_groups)){
foreach ($attribute_groups as $attribute_group) {
$options = '';
$name = $attribute_group['name'];
$output = '<select name="$name">';
foreach ($attribute_groups['attribute'] as $attribute) {
$options .= '<option value="'.$attribute.'">'.$attribute.'</option>';
}
echo $output.$options.'</select>';
}
}
this will see if your array is empty or not.. if not then foreach()... else do nothing
I'm doing a MySQL query to search for items in a database. I've pulled variables out of the search form but I'm having some problems with my WHERE clause. As I don't want to search on fields that haven't been input in the form. The code I have at the minute is:
$query = " SELECT RequestID, clients.ClientName, clients.Username, RequestAssignee, requests.StatusID, requests.PriorityID, StatusName, PriorityName
FROM requests
INNER JOIN clients ON requests.ClientID = clients.ClientID
INNER JOIN statuses ON requests.StatusID = statuses.StatusID
INNER JOIN priorities ON requests.PriorityID = priorities.PriorityID
WHERE ";
if(!empty($RequestID))
{
$query2 .= "RequestID = '" . $RequestID . "' OR ";
}
if(!empty($ClientName))
{
$query2 .= "clients.ClientName = '" . $ClientName ."' OR ";
}
if(!empty($Username))
{
$query2 .= "clients.Username = '" . $Username . "' OR ";
}
if(!empty($RequestAssignee))
{
$query2 .= "RequestAssignee = '" . $RequestAssignee . "' OR ";
}
if(!empty($Status))
{
$query2 .= "statuses.StatusName = '" . $Status ."' OR ";
}
if(!empty($Priority))
{
$query2 .= "priorities.PriorityName = '" . $Priority ."'";
}
However you can see an issue whereby if someone only searches one field, the query adds an 'OR' to the end, resulting in an error:
SELECT RequestID, clients.ClientName, clients.Username, RequestAssignee, requests.StatusID, requests.PriorityID, StatusName, PriorityName FROM requests INNER JOIN clients ON requests.ClientID = clients.ClientID INNER JOIN statuses ON requests.StatusID = statuses.StatusID INNER JOIN priorities ON requests.PriorityID = priorities.PriorityID WHERE RequestID = '3' OR
Im guessing I'm going to have to put some sort of loop or counter in but unsure how to approach it. Any ideas?
Thanks, Matt.
$parts = array();
if(!empty($RequestID))
{
$parts[] = "RequestID = '" . $RequestID . "' ";
}
if(!empty($ClientName))
{
$parts[] = "clients.ClientName = '" . $ClientName ."' ";
}
if(!empty($Username))
{
$parts[] = "clients.Username = '" . $Username . "' ";
}
if(!empty($RequestAssignee))
{
$parts[] = "RequestAssignee = '" . $RequestAssignee . "' ";
}
if(!empty($Status))
{
$parts[] = "statuses.StatusName = '" . $Status ."' ";
}
if(!empty($Priority))
{
$parts[] = "priorities.PriorityName = '" . $Priority ."' ";
}
$query2 .= implode(' OR ', $parts);
Ok so the way i would approach this is to build the variables that will form your where clause separatley:
This could be done in an array:
FIELD1=>'Value1';
FIELD2=>'Value2';
I would then loop over this array, for the first element, i=1 i would build in a WHERE, for i+n -> i+(n-1) i would pre-pend an OR, for the last array value i wouldn't do anything.
I can then use the string i build in this loop - to stick into my query string.
Have a go at something like this and give us a shout if you need more help. Doing it this way is slightly more maintainable also.