Display data with same table but different condition sql - sql

I have this kind of result, absent to Current wolcha was ok but i want to get the previous status of wolcha by getting or displaying only the total previous wolcha no need for the absent, lates etc. just the total.
I have this kind of result, absent to Current wolcha was ok but i want to get the previous status of wolcha by getting or displaying only the total previous wolcha no need for the absent, lates etc. just the total.
function getwolchastatus($salon_id){
global $db;
$query = "SELECT employee_attendance.emp_id,
employee_attendance.date,
employee_attendance.branch_id,
employee_attendance.user_id,
employee_profile.emp_no,
COUNT(IF(employee_attendance.status='A',1, NULL)) 'absent',
COUNT(IF(employee_attendance.status='L',1, NULL)) 'late',
COUNT(IF(employee_attendance.status='SL',1, NULL)) 'sick_leave',
COUNT(IF(employee_attendance.status='LWP',1, NULL)) 'leave_w_pay',
COUNT(IF(employee_attendance.status='LWOP',1, NULL)) 'leave_wo_pay',
COUNT(IF(employee_attendance.status='HD',1, NULL)) 'halfday'
FROM employee_attendance
INNER JOIN employee_profile ON employee_profile.emp_id = employee_attendance.emp_id
INNER JOIN users ON users.user_id = employee_profile.user_id WHERE MONTH(employee_attendance.date) = MONTH(CURRENT_DATE) AND YEAR(employee_attendance.date) = YEAR(CURRENT_DATE) AND users.status = 'activate' AND users.salon_id = 2 GROUP BY employee_attendance.emp_id";
$result = $db->query($query) or die($db->error);
$content = '';
while($row = $result->fetch_array()) {
extract($row);
setlocale(LC_MONETARY, 'en_PH');
$query2 = "SELECT employee_attendance.emp_id,
employee_attendance.date,
employee_attendance.branch_id,
employee_attendance.user_id,
employee_profile.emp_no,
COUNT(IF(employee_attendance.status='A',1, NULL)) 'absent2',
COUNT(IF(employee_attendance.status='L',1, NULL)) 'late2',
COUNT(IF(employee_attendance.status='SL',1, NULL)) 'sick_leave2',
COUNT(IF(employee_attendance.status='LWP',1, NULL)) 'leave_w_pay2',
COUNT(IF(employee_attendance.status='LWOP',1, NULL)) 'leave_wo_pay2',
COUNT(IF(employee_attendance.status='HD',1, NULL)) 'halfday2'
FROM employee_attendance
INNER JOIN employee_profile ON employee_profile.emp_id = employee_attendance.emp_id
INNER JOIN users ON users.user_id = employee_profile.user_id WHERE employee_attendance.date between '2017-12-01' and '2017-12-31' AND users.status = 'activate' AND users.salon_id = 2 GROUP BY employee_attendance.emp_id";
$result2 = $db->query($query2) or die($db->error);
$row2 = $result2->fetch_array();
$new_branch = new Branch;
$employee = new Employee;
$new_user = new Users;
$branch_name = $new_branch->get_branch_info($row['branch_id'], 'branch_name');
$first_name = $employee->get_employee_info($row['emp_id'], 'first_name');
$last_name = $employee->get_employee_info($row['emp_id'], 'last_name');
$nickname = $employee->get_employee_info($row['emp_id'], 'nickname');
$employee_name = $nickname;
//$today = date('MONTH');
// $date1 = DATEADD('DAY', -30, GETDATE());
if(($row['absent'] <3) && ($row['late'] < 2) &&($row['sick_leave'] < 1) && ($row['leave_w_pay'] < 1) && ($row['leave_wo_pay'] < 1) && ($row['halfday'] <2)){
$row['total'] = 1;
}else{
$row['total'] = 0;
}
if(($row2['absent2'] <3) && ($row2['late2'] < 2) &&($row2['sick_leave2'] < 1) && ($row2['leave_w_pay2'] < 1) && ($row2['leave_wo_pay2'] < 1) && ($row2['halfday2'] <2)){
$row2['total2'] = 1;
}else{
$row2['total2'] = 0;
}
$total3 = $row['total'] + $row2['total2'];
$content .= "<tr>";
// $content .= "<td>";
// $content .= $branch_name;
// $content .= "</td>";
$content .= "<td>";
$content .= $last_name.' '.$first_name;
$content .= "</td>";
$content .= "<td>";
$content .= $row['absent'];
$content .= "</td>";
$content .= "<td>";
$content .= $row['late'];
$content .= "</td>";
$content .= "<td>";
$content .= $row['sick_leave'];
$content .= "</td>";
$content .= "<td>";
$content .= $row['leave_w_pay'];
$content .= "</td>";
$content .= "<td>";
$content .= $row['leave_wo_pay'];
$content .= "</td>";
$content .= "<td>";
$content .= $row['halfday'];
$content .= "</td>";
$content .= "<td>";
$content .= $row['total'];
$content .= "</td>";
$content .= "<td>";
$content .= $row2['total2'];
$content .= "</td>";
$content .= "<td>";
$content .= $total3;
$content .= "</td>";
$content .= "</tr>";
}
echo $content;
}

Related

display dynamic data in multiple columns

I use an html string in php to display sql query results in a single column:
How can I use multiple columns to lay it out like this:
while($row = mysqli_fetch_array($q)){
$id = $row['id'];
$title = $row['title'];
$desc = $row['description'];
$link = $row['link'];
$output .= '<a href="' . $link . '">
<h3>' . $title . '</h3>
<p>' . $desc . '</p>
</a>';
}
Credit to hungrykoala for help on this one
$i = 0 ;
$resultsPerRow = 2 ;
$output = '<tr>';
while($row = mysqli_fetch_array($q)){
$i++;
$id = $row['id'];
$title = $row['title'];
$desc = $row['description'];
$link = $row['link'];
$output .= '<td>' .$title. '<br>' .$desc. '</td>';
if ($i % $resultsPerRow == 0) {
$output .= '</tr><td><br></td><tr>';
}
}
echo($output);
Then output to a table tag

Open Cart SEO URL for category/manufacturer

OC 1.5.6
I've installed a module to filter products in categories based on manufacturer.
I'm able to reach every "search result page" at an url like:
example.com/index.php?route=product/category&path=X&manufacturer=Y
I'd like to create a SEO url like:
example.com/alias-for-category/alias-for-manufacturer
(ex. /bikes/honda or /cars/bmw)
but I can't figure out how to do it.
I've tried for testing and simplifying to convert just a single url:
product/category&path=1&manufacturer=1
so I added to url_alias a record with this query and keyword
cat1-man1
then I modified catalog/common/seo_url.php
function Index
if (isset($this->request->get['product_id'])) {
$this->request->get['route'] = 'product/product';
} elseif (isset($this->request->get['path'])) {
$this->request->get['route'] = 'product/category';
} elseif (isset($this->request->get['manufacturer_id'])) {
$this->request->get['route'] = 'product/manufacturer/info';
} elseif (isset($this->request->get['information_id'])) {
$this->request->get['route'] = 'information/information';
}
//ADDED BELOW CODE TO ROUTE ALL URL
else {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($this->request->get['_route_']) . "'");
if ($query->num_rows) {
$this->request->get['route'] = $query->row['query'];
}
}
and function rewrite
if (($data['route'] == 'product/product' && $key == 'product_id') || (($data['route'] == 'product/manufacturer/info' || $data['route'] == 'product/product') && $key == 'manufacturer_id') || ($data['route'] == 'information/information' && $key == 'information_id')) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "'");
if ($query->num_rows) {
$url .= '/' . $query->row['keyword'];
unset($data[$key]);
}
} elseif ($key == 'path') {
$categories = explode('_', $value);
foreach ($categories as $category) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'category_id=" . (int)$category . "'");
if ($query->num_rows) {
$url .= '/' . $query->row['keyword'];
}
}
unset($data[$key]);
}
//ADDED CODE BELOW TO CONVERT ALL URL
else {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($data['route']) . "'");
if ($query->num_rows) {
$url .= '/' . $query->row['keyword'];
unset($data[$key]);
}
}
but also this simplified version doesn't work at all, page not found is shown.
What I'm doing wrong? Any tip will be really appreciated
First of all the custom module I've installed expects the parameter for manufacturer as "manufacturer" instead of "manufacturer_id" so a converted URL like :
example.com/index.php?route=product/category&path=X&manufacturer_id=Y
isn't handled correctly by the module.
So the answer to my question is : OC does already convert alias-cat/alias-manufacturer to a correct internal URL
On the other hand OC does not convert URL of the type
example.com/index.php?route=product/category&path=X&manufacturer_id=Y
into SEO friendly URL like:
example.com/alias-for-category/alias-for-manufacturer
in the menus or links, to do this, modify catalog/common/seo_url.php like this:
public function rewrite($link) {
$url_info = parse_url(str_replace('&', '&', $link));
$url = '';
$data = array();
parse_str($url_info['query'], $data);
foreach ($data as $key => $value) {
if (isset($data['route'])) {
if (($data['route'] == 'product/product' && $key == 'product_id') || (($data['route'] == 'product/manufacturer/info' || $data['route'] == 'product/product') && $key == 'manufacturer_id') || ($data['route'] == 'information/information' && $key == 'information_id')) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "'");
if ($query->num_rows) {
$url .= '/' . $query->row['keyword'];
unset($data[$key]);
}
} elseif ($key == 'path') {
$categories = explode('_', $value);
foreach ($categories as $category) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'category_id=" . (int)$category . "'");
if ($query->num_rows) {
$url .= '/' . $query->row['keyword'];
}
}
unset($data[$key]);
}
// ADD THIS CODE
elseif ($key == 'manufacturer_id') {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'manufacturer_id=" . (int)$value . "'");
if ($query->num_rows) {
$url .= '/' . $query->row['keyword'];
}
unset($data[$key]);
}
// END ADDED CODE----------------
else {
.....

The entry on SQL it's being recognized as column

I'm trying to use the extension RelatedContentByTags, but the entry on bolt_taxonomy column it's being recognized as column.
I have this error when I put {{ relatedcontentbytags(record) }} on index.
'Twig_Error_Runtime thrown with message
"An exception has been thrown during the rendering of a template
("An exception occurred while executing ' SELECT bolt_entries.id FROM bolt_entries LEFT JOIN
bolt_taxonomy ON bolt_entries.id = bolt_taxonomy.content_id WHERE
bolt_entries.status = "published"AND bolt_entries.id != 3 AND
bolt_taxonomy.contenttype = "entries" AND (bolt_taxonomy.taxonomytype = "tags" AND
(bolt_taxonomy.slug = "teste2"))':
SQLSTATE[42703]: Undefined column: 7 ERROR: column "published" does not exist
LINE 1: ...t_taxonomy.content_id WHERE bolt_entries.status = "published...^") in "entry.twig" at line 65."
And the extension.php is that:
$results = array();
foreach ($tables as $name) {
$table = sprintf('%s%s', $tablePrefix, $name);
$querySelect = '';
$querySelect .= sprintf(' SELECT %s.id FROM %s', $table, $table);
$querySelect .= sprintf(' LEFT JOIN %s', $taxonomyTable);
$querySelect .= sprintf(' ON %s.id = %s.content_id', $table, $taxonomyTable);
$querySelect .= sprintf(' WHERE %s.status = "published"', $table);
if ($name == $record->contenttype['slug']) {
$querySelect .= sprintf('AND %s.id != '. $record->id, $table);
}
$querySelect .= sprintf(' AND %s.contenttype = "%s"', $taxonomyTable, $name);
$querySelect .= sprintf(' AND (%s)', $queryWhere);
$queryResults = $app['db']->fetchAll( $querySelect );
if (!empty($queryResults)) {
$ids = implode(' || ', \utilphp\util::array_pluck($queryResults, 'id'));
$contents = $app['storage']->getContent($name, array('id' => $ids, 'returnsingle' => false));
$results = array_merge( $results, $contents );
}
}
Depending on what database you are using (I'm guessing Postgresql based on the error code) text enclosed in double-quotes "" can be used to denote a table name; single-quotes '' are normally used to denote character literals, so try changing "published" to 'published' (the same goes for entries, tags, and teste2 too of course).

Export sql to xml with utf8 encoding

I try to find a solution by extracting sql table values in utf8 encoding but no luck.
The bellow code exports a right XML file named "test.xml" but without encoding.
Any suggestions?
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "admin";
$dbname = "My_DB";
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);
$sql = "SELECT product_id, model, image FROM product";
$q = mysql_query($sql)or die(mysql_error());
$xml = "<products>";
while($r = mysql_fetch_array($q)){
$xml .= "<product>";
$xml .= "<id>";
$xml .= "<![CDATA[".$r["product_id"]."]]>";
$xml .= "</id>";
$xml .= "<name><![CDATA[" . $r["model"] . "]]></name>";
$xml .= "<image>".$r['image']."</image>";
$xml .= "</product>";
}
$xml .= "</products>";
$sxe = new SimpleXMLElement($xml);
$sxe->asXML("test.xml");
?>
What you are doing here is adding all data in a variable and then just outputting it to a file. Why don't you try formatting your XML file as you want it (instead of using SimpleXMLElement only in the end? What I mean is something like this:
[...]
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$xml .= "<products>";
while($r = mysql_fetch_array($q)){
$xml .= "<product>";
$xml .= "<id>";
$xml .= "<![CDATA[".$r["product_id"]."]]>";
$xml .= "</id>";
$xml .= "<name><![CDATA[" . $r["model"] . "]]></name>";
$xml .= "<image>".$r['image']."</image>";
$xml .= "</product>";
}
$xml .= "</products>";
echo $xml;
This will result to an XML file created on your own. Just saying because you are not using the SimpleXMLElement all the way, instead you are adding the XML elements and attributes manually. You can take a look at here to see how else you could have built your file!
Hope it helps!

FatSecret API - food.get method invalid signature

Hi I am new to FatSecret Platform API and i developed a PHP script to access food details using foods.search method Here is the foods.search method Api call example,used Curl and works perfect. <?php $consumer_key = "xxxxxxx"; $secret_key = "xxxxxxx"; //Signature Base String //<HTTP Method>&<Request URL>&<Normalized Parameters> $base = rawurlencode("GET")."&"; $base .= "http%3A%2F%2Fplatform.fatsecret.com%2Frest%2Fserver. api&"; //sort params by abc....necessary to build a correct unique signature $params = "method=foods.search&"; $params .= "oauth_consumer_key=$consumer_key&"; // ur consumer key $params .= "oauth_nonce=123&"; $params .= "oauth_signature_method=HMAC-SHA1&"; $params .= "oauth_timestamp=".time()."&"; $params .= "oauth_version=1.0&"; $params .= "search_expression=".urlencode($_GET['pasVar']); $params2 = rawurlencode($params); $base .= $params2; //encrypt it! $sig= base64_encode(hash_hmac('sha1', $base, "$secret_key&", true)); // replace xxx with Consumer Secret //now get the search results and write them down $url = "http://platform.fatsecret.com/rest/server.api?". $params."&oauth_signature=".rawurlencode($sig); //$food_feed = file_get_contents($url); list($output,$error,$info) = loadFoods($url); echo '<pre>'; if($error == 0){ if($info['http_code'] == '200') echo $output; else die('Status INFO : '.$info['http_code']); } else die('Status ERROR : '.$error); function loadFoods($url) { // create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, $url); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string $output = curl_exec($ch); $error = curl_error($ch); $info = curl_getinfo($ch); // close curl resource to free up system resources curl_close($ch); return array($output,$error,$info); } ?> the above script working perfect and retrieves food details using search method.But when i use food.get method to retrieve data using food_id it says invalid signature error,Here's the food.get method api call example,I have given correct keys and passed food_id parameter.Can someone help me to solve the issue,I am really struck on this code.It says invalid signature error. <?php $consumer_key = "xxxxxxxxx"; $secret_key = "xxxxxxxxxx"; //Signature Base String //<HTTP Method>&<Request URL>&<Normalized Parameters> $base = rawurlencode("GET")."&"; $base .= "http%3A%2F%2Fplatform.fatsecret.com%2Frest%2Fserver.api&"; //sort params by abc....necessary to build a correct unique signature $params = "method=food.get&"; $params .= "oauth_consumer_key=$consumer_key&"; // ur consumer key $params .= "oauth_nonce=".rand()."&"; $params .= "oauth_signature_method=HMAC-SHA1&"; $params .= "oauth_timestamp=".time()."&"; $params .= "oauth_version=1.0&"; $params .= "food_id=".urlencode($_GET['pasVar']); $params2 = rawurlencode($params); $base .= $params2; //encrypt it! $sig= base64_encode(hash_hmac('sha1', $base, "$secret_key&", true)); // replace xxx with Consumer Secret //now get the search results and write them down $url = "http://platform.fatsecret.com/rest/server.api?". $params."&oauth_signature=".rawurlencode($sig); //$food_feed = file_get_contents($url); list($output,$error,$info) = loadFoods($url); echo '<pre>'; if($error == 0){ if($info['http_code'] == '200') echo $output; else die('Status INFO : '.$info['http_code']); } else die('Status ERROR : '.$error); function loadFoods($url) { // create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, $url); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string $output = curl_exec($ch); $error = curl_error($ch); $info = curl_getinfo($ch); // close curl resource to free up system resources curl_close($ch); return array($output,$error,$info); } ?> Help me what's the error in the code and help me to solve the issue as i have to use this API in my project Thanks waiting for your replies....
You should order you params by alpha.
So you should do it in this order:
$params = "food_id=".urlencode($_GET['pasVar'])."&";
$params .= "method=food.get&";
$params .= "oauth_consumer_key=$consumer_key&"; // ur consumer key
$params .= "oauth_nonce=".rand()."&";
$params .= "oauth_signature_method=HMAC-SHA1&";
$params .= "oauth_timestamp=".time()."&";
$params .= "oauth_version=1.0";