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");
Related
What am I missing?
I have a drop down with values. I think everything is working fine, except when I post the results to another table I can't seem to get the gameID from the first table to post, it just goes through as blank. Am I missing an association with the team and the gameID?
Query to get data for drop down:
$sql = "select s.*, (DATE_ADD(NOW(), INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR) > gameTimeEastern or DATE_ADD(NOW(), INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR) > '" . $cutoffDateTime . "') as expired ";
$sql .= "from " . DB_PREFIX . "schedule s ";
$sql .= "inner join " . DB_PREFIX . "teams ht on s.homeID = ht.teamID ";
$sql .= "inner join " . DB_PREFIX . "teams vt on s.visitorID = vt.teamID ";
$sql .= "where s.weekNum = " . $week . " ";
$sql .= "order by s.gameTimeEastern, s.gameID";
//echo $sql;
$query = $mysqli->query($sql) or die($mysqli->error);
if ($query->num_rows > 0) {
$i = 0;
while ($row = $query->fetch_assoc()) {
$sGameID = (int)$row['gameID'];
$homeTeam = new team($row['homeID']);
$visitorTeam = new team($row['visitorID']);
$surv_pick_options_h[$i]=$row['homeID'];
$surv_pick_options_v[$i]=$row['visitorID'];
if ($row['expired']){
$surv_pick_expired_h[$i]=$row['homeID'];
$surv_pick_expired_v[$i]=$row['visitorID'];
}
$surv_gameID[$i] = (int)$row['gameID'];
$i++;
$rowclass = (($i % 2 == 0) ? ' class="altrow"' : '');
}
};
Drop down select option:
$sWeek = (int)getCurrentWeek()+1;
if ($week < $sWeek){
$survpicks_to_disable = array_slice($survpicks,0,$week-1);
$disabled = array_merge($survpicks_to_disable,$surv_pick_expired_h,$surv_pick_expired_v);
echo '<select name="survpick" id="survpick">
<option default>Select</option>';
foreach($surv_pick_options_h as $home){
echo '<option value="'.$home.'"'.(in_array($home,$disabled)?' style="background-color:pink" disabled':'').($home == $currentID?' selected':'').'>'.$home.'</option>';
}
foreach($surv_pick_options_v as $visitor){
echo '<option value="'.$visitor.'"'.(in_array($visitor,$disabled)?' style="background-color:pink" disabled':'').($visitor == $currentID?' selected':'').'>'.$visitor.'</option>';
}
}
echo '</select>';
}
And finally update a table with results:
$sql = "insert into " . DB_PREFIX . "picksurvivor (weekNum, userID, user, gameID, picksurv, showPicks) values (" . $_POST['week'] . ", " . $user->userID . ",'" . $user->userName . "', '" . $surv_gameID . "', '" . $_POST['survpick'] . "', ". (int)$_POST['showPicks'] . ");";
$mysqli->query($sql) or die('Error deleting survivor pick: ' . $mysqli->error);
I found a different approach, doing an update after the insert to basically brute force the gameID in there.
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;
}
I have a two piece of code for Regions and Categories. They are exactly the same. The code for Category works, but "Region" returns the following error:
Fatal error: Call to a member function getRegion() on null in C:\wamp64\www\site\catalog\controller\module\latest.php on line 81
// Region
$product_region = array();
$regions = $this->model_profile_profile->getProductRegions($result['product_id']);
foreach ($regions as $region_id) {
print_r($region_id); // !!! ($region_id = 2) !!! $region_id is not empty !!!
$region_info = $this->model_account_region->getRegion($region_id); // LINE 81
if ($region_info) {
$product_region[] = array(
'name' => ($region_info['path']) ? $region_info['path'] . ' > ' . $region_info['name'] : $category_info['name'],
'href' => $this->url->link('account/profile', '&path=' . $region_info['region_id'])
);
}
}
And here is the code which probably causes the error:
public function getRegion($region_id) {
$query = $this->db->query("SELECT DISTINCT *, (SELECT GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR ' > ') FROM " . DB_PREFIX . "region_path cp LEFT JOIN " . DB_PREFIX . "region_description cd1 ON (cp.path_id = cd1.region_id AND cp.region_id != cp.path_id) WHERE cp.region_id = c.region_id AND cd1.language_id = '" . (int)$this->config->get('config_language_id') . "' GROUP BY cp.region_id) AS path, (SELECT DISTINCT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'region_id=" . (int)$region_id . "') AS keyword FROM " . DB_PREFIX . "region c LEFT JOIN " . DB_PREFIX . "region_description cd2 ON (c.region_id = cd2.region_id) WHERE c.region_id = '" . (int)$region_id . "' AND cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'");
return $query->row;
}
I'd appreciate if you help me by giving a piece of code, because I'm dummy in PHP.
$this->model_account_region //This property is null
Check
echo 'FILE : '.__FILE__ .'<br/>';
echo 'LINE : '.__LINE__ .'<br/>';
echo '<pre>';
var_dump( empty( $this->model_account_region ) );
echo '</pre>';
exit;
model_account_region model class is messed up. Since you are running out of time. I suggest you copy the function
public function getRegion($region_id) {
$query = $this->db->query("SELECT DISTINCT *, (SELECT GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR ' > ') FROM " . DB_PREFIX . "region_path cp LEFT JOIN " . DB_PREFIX . "region_description cd1 ON (cp.path_id = cd1.region_id AND cp.region_id != cp.path_id) WHERE cp.region_id = c.region_id AND cd1.language_id = '" . (int)$this->config->get('config_language_id') . "' GROUP BY cp.region_id) AS path, (SELECT DISTINCT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'region_id=" . (int)$region_id . "') AS keyword FROM " . DB_PREFIX . "region c LEFT JOIN " . DB_PREFIX . "region_description cd2 ON (c.region_id = cd2.region_id) WHERE c.region_id = '" . (int)$region_id . "' AND cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'");
return $query->row;
}
into model_profile_profile then call it from there ie
$region_info = $this->model_profile_profile->getRegion($region_id); // LINE 81
if that fails its the db class failing to load properly so you add this line before calling your function on line 81
$this->load->database();
let me know if you are facing more challenges. I am a bit bored over here.
I am trying to make ONE dynamic function for count in mysql:
functions.php:
function countEntries($table, $where = '', $what = '')
{
if (!empty($where) && isset($what)) {
$q = "SELECT COUNT(*) FROM " . $table . " WHERE " . $where . " = '" . $what . "' LIMIT 1";
} else{
$q = "SELECT COUNT(*) FROM " . $table . " LIMIT 1";
}
$record = query($q);
$total = fetchrow($record);
return $total[0];
}
HTML Code:
<?php echo countEntries("news", "category", "1"); ?>
<?php echo countEntries("post", "type", "Sports"); ?>
But still got blank page without any error!!!
You can try this out.
function countEntries($table, $where = '', $what = '')
{
if (!empty($where) && isset($what)) {
$q = "SELECT COUNT(*) AS count FROM " . $table . " WHERE " . $where . " = '" . $what . "' LIMIT 1";
} else{
$q = "SELECT COUNT(*) AS count FROM " . $table . " LIMIT 1";
}
$record = query($q);
$total = fetchrow($record);
return $total['count'];
}
Here you give an alias to the count(*) and use that to access the returned result as $total['count'].
Hope it helps.
First things you forgot to close else past,second just add this line "ini_set("display_errors", 1);" at the top of your php.this will shows the error in your php.
Your code:
function countEntries($table, $where = '', $what = '')
{
if (!empty($where) && isset($what)) {
$q = "SELECT COUNT(*) FROM " . $table . " WHERE " . $where . " = '" . $what . "' LIMIT 1";
} else
$q = "SELECT COUNT(*) FROM " . $table . " LIMIT 1";
}
$record = query($q);
$total = fetchrow($record);
return $total[0];
}
my code:
function countEntries($table, $where = '', $what = '')
{
if (!empty($where) && isset($what)) {
$q = "SELECT COUNT(*) AS count FROM " . $table . " WHERE " . $where . " = '" . $what . "' LIMIT 1";
} else{
$q = "SELECT COUNT(*) AS count FROM " . $table . " LIMIT 1";
}
$record = query($q);
$total = fetchrow($record);
return $total['count'];
}
Thanks guys, Its working well now:
function countEntries($table, $where, $what)
{
if (!empty($where) && isset($what)) {
$q = "SELECT COUNT(*) FROM " . $table . " WHERE " . $where . " = '" . $what . "' LIMIT 1";
} else
$q = "SELECT COUNT(*) FROM " . $table . " LIMIT 1";
$record = mysql_query($q);
$total = mysql_fetch_array($record);
return $total[0];
}
echo countEntries('news', "type", "sport");
We have a script that generate invoice once a month (cron). But we would like to add feature, that we would be able to select date range "from - to" and then generate invoice only for the date selected.
I guess making input fields with calendar pop-up isn't hard, but filtering with PHP is a bit bigger challenge, so if anyone want to take a look at my code and give me some tips, I would be grateful.
function genInvoice($clientID, $orderID=0, $paid=false)
{
if($orderID == 0)
$sql = "select tblorders.* from tblorders,tblusers where invoiceid=0 and tblorders.userid=tblusers.id " .
"and status='closed' and tblusers.clientid=" . $clientID;
else
$sql = "select tblorders.* from tblorders,tblusers where invoiceid=0 and tblorders.userid=tblusers.id " .
"and tblusers.clientid=" . $clientID . " and tblorders.id=" . $orderID;
$res = full_query($sql) or die(mysql_error());
// If no closed orders uninvoiced, just return
if(!mysql_num_rows($res))
return 0;
$amount = 0;
$orders = array();
while($row = mysql_fetch_array($res, MYSQL_ASSOC))
{
// print_r($row);
// print "<br><hr>";
$amount += $row['amount'];
$orders[] = $row['id'];
}
$date = date("Y-m-d");
$status = $paid ?'Paid' : 'Unpaid';
$sql = "insert into tblinvoices (clientid, date, duedate, subtotal, total, status) values (" . $clientID . ",'" . $date .
"','" . $date . "'," . $amount . "," . $amount . ",'" . $status . "')";
$res = full_query($sql) or die(mysql_error());
$invoiceid = mysql_insert_id();
$sql = "update tblorders set invoiceid=" . $invoiceid . " where id in (" . implode(",", $orders) . ")";
$res = full_query($sql) or die(mysql_error());
$sql = "select tblorders.id as ReportID, FirstName, LastName, SearchName, CountyID, StateID, bl_orderitems.userid, bl_orderitems.amount, " .
"bl_orderitems.notes from tblorders, bl_orderitems left join bl_search on bl_search.id=packageid where tblorders.id in (" .
implode(",", $orders) . ") and bl_orderitems.orderid=tblorders.id order by tblorders.id,bl_orderitems.id";
$res = full_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($res, MYSQL_ASSOC))
{
if($row['CountyID'] != 0)
$locale = getCounty($row['CountyID']);
else if($row['StateID'] != 0)
$locale = getState($row['StateID']);
if($row['SearchName'] != "")
$description = mysql_real_escape_string($row['FirstName'] . " " . $row['LastName'] . " " .
$row['SearchName'] . " " . $locale . " (Order #" . $row['ReportID'] . ")");
else
$description = "Search Package: " . mysql_real_escape_string($row['notes'] . " (Order #" . $row['ReportID'] . ")");
$sql = "insert into tblinvoiceitems (invoiceid, userid, type, description, amount, duedate) values " .
"(" . $invoiceid . "," . $row['userid'] . ",'search','" . $description . "','" .
$row['amount'] . "','" . $date . "')";
// print $sql . "<br>";
full_query($sql) or die(mysql_error());
}
sendmessage ('Invoice Created', $invoiceid);
return $invoiceid;
}
not going to look through all that code, but filtering results by a date range is easy.
SELECT id FROM some_table WHERE some_date_field BETWEEN $first_date AND $second_date