Codeigniter - prevent added space on the select statement - codeigniter

How to prevent auto fix in the select statement using CodeIgniter 3?
Query that I want: SELECT REPLACE(item, ',', '') AS item (without space)
I tried : $this->db->select("replace(item, ',', '') AS item", FALSE);
Result : SELECT REPLACE(item, ', ', '') AS item (CI added a space in my query)
I read this documentation but it didn't work.
This my real query:
$this->db->select("a.unit_id");
$this->db->select("replace(c.topik, ',', '') AS topik", FALSE); //this my problem (added space which shouldnt)
$this->db->select("COUNT(*) AS jumlah");
$this->db->from('opub_kliping a');
$this->db->join('opub_media b','a.media_id = b.organisasi_id');
$this->db->join('opub_tag c','a.id = c.kliping_id');
if(!empty($data['keyword']))$this->db->where("(a.judul LIKE '%".$data['keyword']."%' OR a.text LIKE '%".$data['keyword']."')");
if(!empty($data['tgl1']))$this->db->where("a.tanggal >=",$data['tgl1']);
if(!empty($data['tgl2']))$this->db->where("a.tanggal <=",$data['tgl2']);
if(!empty($data['unit']))$this->db->where("a.unit_id ",$data['unit']);
if(!empty($data['media']))$this->db->where("a.media_id ",$data['media']);
if(!empty($data['jenis']))$this->db->where("a.jenis_id ",$data['jenis']);
if(!empty($data['tone']))$this->db->where("a.karakter ",$data['tone']);
if(!empty($data['topik']))
{
$qTopik = array();
$expTopik = explode(',', $data['topik']);
foreach($expTopik as $t)
{
if(!empty($t))
{
$qTopik[] = "c.topik LIKE '%$t%'";
}
}
if(!empty($qTopik))
{
$qTopiks = implode(",", $qTopik);
$this->db->where($qTopiks);
}
}
$this->db->where('a.unit_id !=', 0);
$this->db->where('c.topik !=', NULL);
$this->db->where('c.topik !=', '');
$this->db->group_by("a.unit_id");
$this->db->group_by("replace(c.topik, ',', '')", FALSE); //this produce correct query (without adding space)
$this->db->order_by('a.unit_id', 'ASC');
$this->db->order_by('jumlah', 'DESC');
$q= $this->db->get();

i found this after tried some experiment
$this->db->select(array('REPLACE(c.topik', "','", "'') AS topik"), false);
seems little hack maybe. but its work for me
hope will help others

change 4 lines of your query
$this->db->select("a.unit_id,replace(c.topik, ',', '') AS topik,COUNT(*) AS jumlah")->from('opub_kliping a');
$this->db->join('opub_media b','a.media_id = b.organisasi_id');

Related

Laravel count query

Something is strange.I try to check if query gives at least one object in collection. It has always worked, but now, it does not anymore.
$myObj = User::whereRaw("lower(name) = '".strtolower(trim("toto"))."'");
if ( $myObj->count() > 0) {
\Log::info( "Good" );
}
Code stops working. but if I add:
$myObj = User::whereRaw("lower(name) = '".strtolower(trim("toto"))."'");
\Log::info( $myObj->count() );
if ( $myObj->count() > 0) {
\Log::info( "Good" );
}
it displays the number of objects returned by query, before breaking the code.
What is wrong, please?
Possibly, you omit the method get() to fetch all records, maybe it could be:
$myObj = User::whereRaw("lower(name) = '".strtolower(trim("toto"))."'")->get();

is any one good enough to help get this working?

I have four tables I need to join in query build
function get_debtors( ) {
$this->db->select('invoice.student_id as STUDENT_ID,student.name AS
STUDENT_NAME,section.name AS CLASS,
invoice.description AS DESCRIPTION,
invoice.amount AS TOTAL_AMOUNT,
invoice.amount_owed AS AMOUNT_OWED,
parent.name AS PARENT_NAME,
parent.email AS PARENT_EMAIL,
parent.phone AS PARENT_PHONE,
parent.address AS PARENT_ADDRESS,
invoice.status AS STATUS');
$this->db->from('invoice , student , parent , section ');
$this->db->join('student ', 'student.student_id = invoice.student_id');
$this->db->join('parent ', 'parent.parent_id = student.parent_id, 'left'');
$this->db->join('section ', 'section.section_id = student.section_id','left');
$this->db->where('invoice.status', 'debtor');
$query = $this->db->get();
Try this. If there is still any error than post the error also here.
function get_debtors( )
{
$this->db->select('invoice.student_id as STUDENT_ID,student.name AS
STUDENT_NAME,section.name AS CLASS,
invoice.description AS DESCRIPTION,
invoice.amount AS TOTAL_AMOUNT,
invoice.amount_owed AS AMOUNT_OWED,
parent.name AS PARENT_NAME,
parent.email AS PARENT_EMAIL,
parent.phone AS PARENT_PHONE,
parent.address AS PARENT_ADDRESS,
invoice.status AS STATUS');
$this->db->from('invoice');
$this->db->join('student ', 'student.student_id = invoice.student_id');
$this->db->join('parent ', 'parent.parent_id = student.parent_id', 'left');
$this->db->join('section ', 'section.section_id = student.section_id','left');
$this->db->where('invoice.status', 'debtor');
$query = $this->db->get();
}

Looking for case-insensitive in Phalcon Datatables Library (m1ome)

I use this Library :
https://github.com/m1ome/phalcon-datatables
This is how i use it :
$resultset = $this->modelsManager
->createQuery("SELECT protId, protUsmsName, protUsmsId, protComsName, protAddress, protQty, protPrice, protTotalPrice,
protShpmName, protDateOrder , protPthdId
FROM ProductOrderTransaction WHERE AuditDelete = 'N' AND protMomsId = '4'
ORDER BY protId DESC")
->execute();
$dataTables = new DataTable();
$dataTables->fromResultSet($resultset)->sendResponse();
But, while i search "company" in the Data tables, it can't be found because it has value "Company". Case sensitive case.
Anyone had the modify Lib? thx
$check = (strpos(strtoupper( $item->$column ), strtoupper( $search ) ) !== false);

Magento: how to fix customer name search

Under the Magento admin menu Customers -> Manage Customers, we're unable to search for a customer using his name if the string consists of more than one word. Where would I find the core codes that performs this task? We don't have an extension for this section.
Example of the problem:
If I want to search for a customer named John Smith, searching for "john smith" returns no results. Searching for "john" or "smith" separately works, but it will bring up any names that contain "john" or "smith" similar to the SQL queries, LIKE %john% or LIKE %smith%.
Looks like you're using Magento version 1.6
It is a bug and has been fixed in 1.7
Here's the code that need to be modified: app/code/core/Mage/Customer/Model/Resource/Customer/Collection.php
I post the code taken from version 1.7:
Mage_Customer_Model_Resource_Customer_Collection
public function addNameToSelect()
{
$fields = array();
$customerAccount = Mage::getConfig()->getFieldset('customer_account');
foreach ($customerAccount as $code => $node) {
if ($node->is('name')) {
$fields[$code] = $code;
}
}
$adapter = $this->getConnection();
$concatenate = array();
if (isset($fields['prefix'])) {
$concatenate[] = $adapter->getCheckSql(
'{{prefix}} IS NOT NULL AND {{prefix}} != \'\'',
$adapter->getConcatSql(array('LTRIM(RTRIM({{prefix}}))', '\' \'')),
'\'\'');
}
$concatenate[] = 'LTRIM(RTRIM({{firstname}}))';
$concatenate[] = '\' \'';
if (isset($fields['middlename'])) {
$concatenate[] = $adapter->getCheckSql(
'{{middlename}} IS NOT NULL AND {{middlename}} != \'\'',
$adapter->getConcatSql(array('LTRIM(RTRIM({{middlename}}))', '\' \'')),
'\'\'');
}
$concatenate[] = 'LTRIM(RTRIM({{lastname}}))';
if (isset($fields['suffix'])) {
$concatenate[] = $adapter
->getCheckSql('{{suffix}} IS NOT NULL AND {{suffix}} != \'\'',
$adapter->getConcatSql(array('\' \'', 'LTRIM(RTRIM({{suffix}}))')),
'\'\'');
}
$nameExpr = $adapter->getConcatSql($concatenate);
$this->addExpressionAttributeToSelect('name', $nameExpr, $fields);
return $this;
}

Auto refresh content in Mediawiki

I added a new tag (<news />) to my mediawiki to list the last modified pages.
Unfortunately the list is not updated unless I modify the page where the tag is.
I'm looking for a way to do it, and I think of AJAX. But I didn't manage to make AJAX refreshing my list.
Does anyone know a simple way to add an auto refresh feature on my Mediawiki ?
Here is my extension code :
$wgHooks['ParserFirstCallInit'][] = 'replaceTags';
function replaceTags( Parser $parser ) {
$parser->setHook( 'news', 'newsRender' );
return true;
}
function newsRender( $input, array $args, Parser $parser, PPFrame $frame ) {
// Titre =News=
$output = $parser->parse( "=News=", $parser->mTitle, $parser->mOptions, false, false )->getText();
$nb = 5;
$querySQL = "SELECT page_namespace, page_title, page_id, page_latest, rev_timestamp
FROM page, revision
WHERE page.page_latest = revision.rev_id
AND page_namespace = 0
ORDER BY rev_timestamp
DESC LIMIT 0,$nb";
$dbr = wfGetDB( DB_SLAVE );
$res = $dbr->query( $querySQL );
$count = $dbr->numRows( $res );
if( $count > 0 ) {
$output .= "<ul>";
while( $row = $dbr->fetchObject( $res ) )
{
$pageTitle = $row->page_title;
$nicerPageTitle = str_replace("_", " ", $pageTitle);
$pageNamespace = $row->page_namespace;
$title = Title::makeTitleSafe( $pageNamespace, $pageTitle );
$url = $title->getFullURL();
$date = $row->rev_timestamp;
$date = wfTimestamp( TS_RFC2822, $date );
$output .= "<li>$nicerPageTitle $date</li>";
}
$output .= "</ul>";
} else {
$output .= "A l'ouest rien de nouveau !!!";
}
return $output;
}
Thanks to nischayn22, I go into the cache problem in depth.
And I found that it's possible to deactivate it :
$parser->disableCache();
I tried it, and it works !!!
http://www.mediawiki.org/wiki/Extensions_FAQ#How_do_I_disable_caching_for_pages_using_my_extension.3F
This probably happens because MediaWiki uses Cache for pages. What you could rather do is make a SpecialPage for the feature needed. AFAIK Special Pages are not cached (confirm this on irc #mediawiki).
Also you might already find a similar implementation done by someone if you search the extensions that exist on Mediawiki.org .(Otherwise I would be happy to build one for you :)
Update: Extensions you could use Dynamic List(used in wikinews) and news . There could be more if you search mediawiki.org.

Resources