Yii: Customize the results of CAutoComplete - php

I need to make a dropdown list using CAutoComplete. Everything is set and works fine, here is my code of the action:
<?php
public function actionSuggestCharacter() {
if(Yii::app()->request->isAjaxRequest && isset($_GET['q'])) {
$name = $_GET['q'];
$criteria = new CDbCriteria;
$criteria->condition='`Character` LIKE :keyword';
$criteria->params=array(':keyword'=>"$name%");
$criteria->limit = 5;
$suggestions = zCharacter::model()->findAll($criteria);
$returnVal = '';
foreach($suggestions as $suggestion) {
$returnVal .= $suggestion->Character."\n";
}
if (isset($suggestion)) {
echo $returnVal;
}
$criteria->condition='`Character` LIKE :keyword';
$criteria->params=array(':keyword'=>"%$name%");
$criteria->limit = 5;
$suggestions = zCharacter::model()->findAll($criteria);
$returnVal = '';
foreach($suggestions as $suggestion) {
$returnVal .= $suggestion->Character."\n";
}
if (isset($suggestion)) {
echo $returnVal;
}
}
}
?>
What this code does is that it shows the first 5 matches with the keyword at the beginning and the next 5 matches are with the keyword in any place.
Example. Let's say a user types in the input field "pdd" (doesn't really matter, could be any text), so the results returned by autocomplete will look like:
1. pddtext...
2. pddtext...
3. pdd_some_other_text
4. pdd_text
5. pdd_text
1. text_text_pdd
2. text_pdd_text
3. etc...
The problem is I need to separate these two blocks by some kind of line (<hr> or <div> with the border). How can I do this?
Thank you.

Can't you do something like this?
<?php
public function actionSuggestCharacter() {
if(Yii::app()->request->isAjaxRequest && isset($_GET['q'])) {
...
if (isset($suggestion)) {
echo $returnVal;
}
echo "Hey this is the delimiter\n";
$criteria->condition='`Character` LIKE :keyword';
....
}
}
?>
And then on the client side check for this string and when you encounter ""Hey this is the delimiter" replace it with your separator.

Related

Comparison of words from the database and output of the result

I need to check the words received from the database with the user's entered word and if there is a match, then output its value from the database, and if not, then output what the user entered.
The code below works fine if there is a match.
function d_typeplace_morf($d_typeplace)
{
global $wpdb;
$typeplace_results = $wpdb->get_results('SELECT vozmozhnyi_variant_mesta, ego_slovoforma_v_predlozhnom_padezhe FROM dEzpra_jet_cct_tip_mest_obrabotki');
if ($typeplace_results) {
foreach ($typeplace_results as $typeplace_result) {
$d_typeplace_raw = mb_strtolower($typeplace_result->vozmozhnyi_variant_mesta);
$d_typeplace_morf = mb_strtolower($typeplace_result->ego_slovoforma_v_predlozhnom_padezhe);
$d_typeplace = mb_strtolower($d_typeplace);
if (stripos($d_typeplace, $d_typeplace_raw) !== false) {
echo $d_typeplace_morf;
}
}
}
}
I'm an amateur in PHP, just learning. And I can't figure out how to output $d_typeplace if no match is found.
I tried to add
else {
echo $d_typeplace;
}
, but I get an array of words from the user entered.
I will be grateful for any help. Also for any suggestions for improving this code.
---Addition---
I apologize for my English. This is a problem in the Russian language, I need to take into account the morphology. To do this, the database has a list of words and their analog, for example, X = Y. I get these words and compare what the user entered. If he entered X, then we output Y. If he led Z, which is not in the database, then we output Z.
Thus, we check $d_typeplace with $d_typeplace_raw and if there is a match, we output $d_typeplace_morf, which is equal to $d_typeplace_raw. And if not, then $d_typeplace (it contains the value that the user entered).
Oh, I'm sorry, I understand myself that I'm explaining stupidly)
I cannot quite understand what you are asking: you need to output the string entered by the user, but you can only print an array?
If this is the case, I think you parsed the string before, in order to therefore you need to do join again the values contained in the array.
Try with:
else {
echo implode(" ", $d_typeplace);
}
--- EDITED ---
Try with:
function d_typeplace_morf($d_typeplace)
{
global $wpdb;
$typeplace_results = $wpdb->get_results('SELECT vozmozhnyi_variant_mesta, ego_slovoforma_v_predlozhnom_padezhe FROM dEzpra_jet_cct_tip_mest_obrabotki');
if ($typeplace_results) {
$found = false;
foreach ($typeplace_results as $typeplace_result) {
$d_typeplace_raw = mb_strtolower($typeplace_result->vozmozhnyi_variant_mesta);
$d_typeplace_morf = mb_strtolower($typeplace_result->ego_slovoforma_v_predlozhnom_padezhe);
$d_typeplace = mb_strtolower($d_typeplace);
if (stripos($d_typeplace, $d_typeplace_raw) !== false) {
echo $d_typeplace_morf;
$found = true;
break;
}
}
if (!$found) {
echo $d_typeplace;
}
}
}
But I think it would be more efficient, if you implemented the second code snippet written by #Luke.T
I'm presuming you were trying to add the else like this?
function d_typeplace_morf($d_typeplace)
{
global $wpdb;
$typeplace_results = $wpdb->get_results('SELECT vozmozhnyi_variant_mesta, ego_slovoforma_v_predlozhnom_padezhe FROM dEzpra_jet_cct_tip_mest_obrabotki');
if ($typeplace_results) {
foreach ($typeplace_results as $typeplace_result) {
$d_typeplace_raw = mb_strtolower($typeplace_result->vozmozhnyi_variant_mesta);
$d_typeplace_morf = mb_strtolower($typeplace_result->ego_slovoforma_v_predlozhnom_padezhe);
$d_typeplace = mb_strtolower($d_typeplace);
if (stripos($d_typeplace, $d_typeplace_raw) !== false) {
echo $d_typeplace_morf;
} else {
echo $d_typeplace;
}
}
}
}
Which was outputting an array because the for loop was continuing, if you add a break like so...
echo $d_typeplace;
break;
It should stop outputting an array. Depending on your use case you could however perform similar functionality directly in your sql query using LIKE ...
function d_typeplace_morf($d_typeplace)
{
global $wpdb;
$typeplace_results = $wpdb->get_results('
SELECT ego_slovoforma_v_predlozhnom_padezhe
FROM dEzpra_jet_cct_tip_mest_obrabotki
WHERE vozmozhnyi_variant_mesta LIKE %' . $d_typeplace . '%');
if ($typeplace_results) {
//Echo result
} else {
echo $d_typeplace;
}
}

PHP if condition: Logical operators for filter from a form

I'm working with a form using the <datalist> tag, I have two input tags getting information from two different arrays. Below the form there is a Kanban board, the objective of the form is to serve as a filter, so when a user fills any of the inputs (or both) the board with fill accordingly.
Here is the problem: I had made two if staments (one for input) that work great when I use them alone but that I don't know how to put together because I don't know how to check which of the inputs has been filled (for that I believe I may have to use the issset() function), then if only one of them is used I need to use the || logical operator between them but if both are filled I need to use the && operator.
Using this condition (|| logical operator):
if (($tasksArray[$i]["responsible-party-names"] == $_POST['members'] && isset($_POST['members'])) ||
(($tasksArray[$i]['project-name'] == $_POST['projects'] && isset($_POST['projects']))))
If I fill only the members input it returns what it should.
If I fill only the projects input it returns all the projects in the array (although when using the if condition without the left part it works well).
Finally, if I fill both inputs I get the right project but all the team members.
What would be the simplest way to get it right?
Update:
This if condition it's inside the moveArray function, what comes after the if block looks like this:
{
$task = '<div id="item'.$i.'"'.'draggable="true" class="c-drag">';
$task .= '<div class="card cardTitle">'.$tasksArray[$i]['content'].'</div>';
$task .= '<div class="card cardDescription">'.$tasksArray[$i]['description'].'</div>';
$task .= '<div class="card cardProjectName">'.$tasksArray[$i]['project-name'].'</div>';
if (isset($tasksArray[$i]["responsible-party-names"])) {
$task .= '<div class="card cardProjectResponsibleName">'.$tasksArray[$i]['responsible-party-names'].'</div>';
} else {
$task .= '<div class="card cardProjectResponsibleName">'."Anyone".'</div>';
}
if ($tasksArray[$i]["due-date"] != "") {
$task .= '<div class="card cardDueDate">'.date("d/m/Y", strtotime($tasksArray[$i]["due-date"])).'</div>';
}
$task .= '</div>';
return $task;
}
Then I call the function on every part of the board (unassigned, to do, in progress, finished)
echo '<div id="board">';
echo '<div id="unassing">';
echo '<div id="unassing-bg" class="title">Unassigned</div>';
for ($i=0; $i < $tasksLenght; $i++) {
if (isset($tasksArray[$i]['boardColumn']) === false) {
echo(moveArray($i, $tasksArray));
}
}
echo '</div>';
echo '<div id="todo">';
echo '<div id="todo-bg" class="title">To Do</div>';
for ($i=0; $i < $tasksLenght; $i++) {
if (isset($tasksArray[$i]['boardColumn']) && $tasksArray[$i]['boardColumn']['id'] == "9805") {
echo(moveArray($i, $tasksArray));
}
}
echo '</div>';
echo '</div>';
And that produce sort of "a card" with data about a task in the corresponding place of the board.
You actually need the isset to come before trying to access the key on $_POST to prevent the "undefined" error.
I am assuming you have some code that looks like this:
$matchingTasks = [];
for ($i=0; $i <= count($tasksArray); $i++) {
...
}
return $matchingTasks; // or something
If that is the case, what you want is more like this:
matchingTasks = [];
foreach ($tasksArray as $task) {
// Store your search values
$members = isset($_POST['members']) ? $_POST['members'] : false;
$projects = isset($_POST['projects']) ? $_POST['projects'] : false;
// Store the search matches, only if you are searching by that key
$members_match = $members && $task["responsible-party-names"] == $members;
$projects_match = $projects && $task['project-name'] == $projects;
// if you are searching for BOTH must match BOTH
if ($members && $projects)
if ($members_match && $projects_match) {
$matchingTasks[] = $task;
}
continue;
}
// if you are searching for EITHER can match EITHER
if ($members XOR $projects) {
if ($members_match || $projects_match) {
$matchingTasks[] = $task;
}
continue;
}
// if you are not searching match ALL
$matchingTasks[] = $task;
}
return $matchingTasks;
More advanced, and would allow for more customization, is to stack these in a filter pattern like this:
$members = isset($_POST['members']) ? $_POST['members'] : false;
$projects = isset($_POST['projects']) ? $_POST['projects'] : false;
if ($members)
$tasksArray = filter_by_members($tasksArray, $members);
if ($projects)
$tasksArray = filter_by_project($tasksArray, $projects);
return $tasksArray;
//... elsewhere
function filter_by_members($tasks, $members) {
$result = [];
foreach ($tasks as $t) {
if ($task["responsible-party-names"] == $members) {
$result[] = $task;
}
}
return $tasks;
}
function filter_by_project($tasks, $project) {
$result = [];
foreach ($tasks as $t) {
if ($task["project-name"] == $project) {
$result[] = $task;
}
}
return $tasks;
}
This would let you stack new search filters, like filter_by_id or filter_by_date and by defining filter functions you can stack as many as you like without getting crazy with the logic.

Account Search Wildcard - SugarCRM

I'm looking for a way to do a wildcard search when searching for accounts in SugarCRM but I'm having trouble getting the queries to work properly.
Here's the build_generic_where_clause() function:
function build_generic_where_clause ($the_query_string) {
$where_clauses = Array();
$the_query_string = $this->db->quote($the_query_string);
array_push($where_clauses, "accounts.name like '%$the_query_string%'");
if (is_numeric($the_query_string)) {
array_push($where_clauses, "accounts.phone_alternate like '%$the_query_string%'");
array_push($where_clauses, "accounts.phone_fax like '%$the_query_string%'");
array_push($where_clauses, "accounts.phone_office like '%$the_query_string%'");
}
$the_where = "";
foreach($where_clauses as $clause)
{
if(!empty($the_where)) $the_where .= " or ";
$the_where .= $clause;
}
$log = fopen('1.txt', "a");
fwrite($log, $the_where . "\n");
return $the_where;
}
I only changed array_push($where_clauses, "accounts.name like '%$the_query_string%'"); to include the percentage signs on either side of the_query_string.
Here's processSearchForm() from view.list.php:
function processSearchForm(){
if(isset($_REQUEST['query']))
{
// we have a query
if(!empty($_SERVER['HTTP_REFERER']) && preg_match('/action=EditView/', $_SERVER['HTTP_REFERER'])) { // from EditView cancel
$this->searchForm->populateFromArray($this->storeQuery->query);
}
else {
$this->searchForm->populateFromRequest();
}
$where_clauses = $this->searchForm->generateSearchWhere(true, $this->seed->module_dir);
if (count($where_clauses) > 0 )$this->where = '('. implode(' ) AND ( ', $where_clauses) . ')';
$GLOBALS['log']->info("List View Where Clause: $this->where");
$log = fopen('1.txt', "a");
fwrite($log, $this->where . "\n");
}
if($this->use_old_search){
switch($view) {
case 'basic_search':
$this->searchForm->setup();
$this->searchForm->displayBasic($this->headers);
break;
case 'advanced_search':
$this->searchForm->setup();
$this->searchForm->displayAdvanced($this->headers);
break;
case 'saved_views':
echo $this->searchForm->displaySavedViews($this->listViewDefs, $this->lv, $this->headers);
break;
}
}else{
echo $this->searchForm->display($this->headers);
}
}
Note that I only added the log file write to catch the $this->where. If I use the searchbox to find an account such as "Newbold" as well as "New York Design", I only get "Newbold" as a result and my log file reads (accounts.name like 'new%'). So the first percentage sign is being removed somehow or another, I believe in the processSearchForm() somewhere. It's tough to figure out if that's the case or if the culprit lies elsewhere. I find this code to be a bit convoluted and all over the place, but this is the only customization I need done. Any help would be immensely appreciated.
You should be able to do this without changing any code. When you're searching from the Accounts list view, simply add the wildcards to your search in the form. Put '%$the_query_string%' in the search form.
If you're wanting to programmatically use a wildcard to search for records, you can do something like the following.
<?php
$filter = 'ABC%'; // account names that start with 'ABC'
$account = BeanFactory::getBean('Accounts');
$accounts = $account->get_list("", "accounts.name like '".$filter."'");
foreach ($accounts['list'] as $account)
{
// do something with $account
}
get_list() will pull what you need. One thing to note, get_list() will only return the numbers of records that you have your list views to return. So if your list views only show 20 records, get_list() will return up to 20 records. If you want to get all results, use get_full_list().

isset $_post issue with mysql query

I'm just working on the Backend of a project an have a small problem with this snippet
if (isset($_POST['id'])) {
$cat_delete = "DELETE FROM category WHERE categoryid='".$_POST['id']."' ";
$cat_delete_ex = mysql_query($cat_delete);}`
But if the id is set with post, nothing happens.
The mysql query is working when I delete the
if (isset($_POST['id']))
anyone have an idea ?
Well I am not sure if your method is safe or not, but I would do it like this, might even throw in a regex to check for just numbers if the id is numeric:
EDIT: I made a revision, since you are dealing with an ID, I will assume the ID is numeric only, so instead of escaping it, I just will strip out everything but numbers. This may be a better fit for your situation. I also converted the function to a class so you will be able to reuse the script for several types of sanitizing strings. Maybe its because I am an overachiever too, I don't know. ADD, OCD, etc. Blame it on that :)
$postID = isset($_POST['id']) ? sanitize::ID($_POST['id']) : '';
if (sanitize::email("test#example.com")){
echo "Real email";
} else {
echo "Fake email";
}
if ($postID != ''){
$cat_delete = "DELETE FROM category WHERE categoryid='".$postID."' ";
$cat_delete_ex = mysql_query($cat_delete);
}
class sanitize{
function ID($string){
$string = preg_replace('/[^0-9,]|,[0-9]*$/','',$string);
return $string;
}
# I added another sanitize function so you can see what you can do
# with it. Add phone numbers, domain names, etc... Each one could
# be called with sanitize::{FUNCTION}
function email($string){
if (!ereg("^[^#]{1,64}#[^#]{1,255}$", $string)) {
return false;
}
$email_array = explode("#", $string);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",$local_array[$i])) return false;
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) return false;
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) return false;
}
}
return true;
}
}
are you sure you are using post for the id?(asking because is the right way, but i have one too many times
<form action="action.php?id=hereistheid"
which will bring the id in the $_GET not $_POST.
next the checking
$id=(int)$_POST['id'];
if($id)
{
//do smth
}

How to convert Meters to Foot using PHP

I have used this code which shows an error:ie if my meters is 175m but when i convert it does'nt show that thing
<?php
function metersToFeetInches($meters, $echo = true)
{
$m = $meters;
$valInFeet = $m*3.2808399;
$valFeet = (int)$valInFeet;
$valInches = round(($valInFeet-$valFeet)*12);
$data = $valFeet."′".$valInches."″";
if($echo == true)
{
echo $data;
} else {
return $data;
}
}
?>
<?php
$feetInches = metersToFeetInches(1.75,false);
echo $feetInches;
?>
This is one of those things you can easily find on Google, but I guess you didn't look further than the first link.
Anyways, you should do it like this.
<?php
function metersToFeet($meters) {
return floatval($meters) * 3.2808399;
}
?>
But why is this better than the code you posted? Well, functions are not supposed to do everything. You should write a function for a certain action, not one big function with everything you can every need. Because if you need to add something to that function or change the order of actions, it's a hell of a lot of work.
Furthermore, your function has an option $echo. But why would you need that? You can add such an option to every function, but PHP has a nice commando for that: echo. So instead, it's way better to write echo metersToFeet(10). Or $value = metersToFeet(10) if you need to save the result in a variable.
<?php
function metersToFeetInches($meters, $echo = true)
{
$m = $meters;
$valInFeet = $m*3.2808399;
$valFeet = (int)$valInFeet;
$valInches = round(($valInFeet-$valFeet)*12);
$data = $valFeet."′".$valInches."″";
if($echo == true)
{
echo $data;
} else {
return $data;
}
}
?>

Categories