How to avoid re-comparison of strings in following scenario? - php

My PHP code snippet is as follows:
$sql = " SELECT * FROM ".TBL_QUESTIONS." WHERE question_subject_id=".$subject_id;
$sql .= " AND question_topic_id=".$topic_id;
$this->mDb->Query($sql);
$questions_data = $this->mDb->FetchArray();
$questions = $questions_data;
$exclude_words = array('which','who','what','how','when','whom','wherever','the');
/*This loop removes all the words of $exclude_words array from all questions*/
foreach($questions as $index=>$arr) {
$questions_array = explode(' ',$arr['question_text']);
$clean_questions = array_diff($questions_array, $exclude_words);
$questions[$index]['question_text'] = implode(' ',$clean_questions );
}
/*Now the actual comparison of each question with every other question stats here*/
foreach ($questions as $index=>$outer_data) {
$questions_data[$index]['similar_questions_ids_and_percentage'] = Array();
$outer_question = $outer_data['question_text'];
$qpcnt = 0;
foreach ($questions as $inner_data) {
/*This is to avoid comparing the question with itself*/
if ($outer_data['question_id'] != $inner_data['question_id']) {
$inner_question = $inner_data['question_text'];
/*This is to calculate percentage of match between each question with every other question*/
/*In this loop I want single time comparison of each question with every other question Now it's getting repeated, please help me here*/
$same_chars = similar_text($outer_question, $inner_question, $percent);
$percentage = number_format((float)$percent, 2, '.', '');
/*If $percentage is >= $percent_match only then push the respective question_id into an array*/
if($percentage >= 50) {
$questions_data[$index]['similar_questions_ids_and_percentage'][$qpcnt]['question_id'] = $inner_data['question_id'];
$questions_data[$index]['similar_questions_ids_and_percentage'][$qpcnt]['percentage'] = $percentage;
$qpcnt++;
}
}
}
}
Actually I want to avoid the re-comparison in a inner foreach loop from above code.
For example suppose there are ten questions and each one is comparing with other all remaining questions. If Q. No.1 is compared with Q. No. 8 then again when the time comes for Q.8, it's getting compared with Q.1 again. I want to avoid this. I want that if Q.1 is compared with Q.8 then again it should not be compared with Q. No.1 when the turn for Q. 8 comes.
Can anyone please help me out in this regard? any kind of help would be hoghly appreciated.

Maybe you can update the second foreach to also get the index:
foreach ($questions as $inner_data) {
changed to:
foreach ($questions as $secondIndex=>$inner_data){
and just under it add:
if ($secondIndex <= $index)
continue;

Related

How can I get students position based on their total score

This code is just giving me 1 as the position of all my students. I would like the students to have their position based on their total. If their is a tie in total, it should be able to handle that too. Any help will be highly appreciated
Below is my code
$getStudPosition=mysqli_query($conn,"SELECT sum(sr.total) AS gtot FROM student_result sr WHERE sr.studentId='$id' ORDER BY resId DESC");
while ($r=mysqli_fetch_array($getStudPosition))
{
$students = array($gTotal=$r['gtot']);// convert to array
$pos = $real_pos = 0;
$prev_score = -1;
foreach ($students as $exam_n => $score)
{
$real_pos += 1;
$pos = ($prev_score != $score) ? $real_pos: $pos;
$prev_score = $score;
}
$average = $score/$totSubject; //getting students average
}
I'm running 10.4.18 version of MariaDB.
It looks like you are trying to get the position for the students one at a time, inside a loop? Getting the position for all of them at once is going to be much more efficient. To do this, you simply need to use https://mariadb.com/kb/en/rank/. It's hard to give a more detailed answer without the information we've asked for.

For each / query doesn't give the right value

I have a table in my database. I want to get ltotal value from leave table, and then count all of ltotal. Here are my query and code I use:
$annual_query = pg_query("select ltotal from leave where lapplicant='adam' and ltype=2");
$annual_result = pg_fetch_array($annual_query);
if (pg_num_rows($annual_query) > 0) {
foreach ($annual_result as $data) {
$total_annual = $total_annual + $data;
}
print($total_annual);
}
There are 3 records in the table leave where lapplicant='adam' and ltype=2.
Each ltotal is 1.
When I tried to runprint($total_annual) the result is 2 (it must be 3).
Then I tried to print_r($annual_result['ltotal']), the results is just 1 (it must be 1,1,1).
Can anyone help me? Thank you.
pg_fetch_array() returns just one row with numeric and associative keys (same value twice when traversed). You should use pg_fetch_all() and traverse or use while loop on consecutive rows.
$total_annual = 0;
$annual_query = pg_query("select ltotal from leave where lapplicant='adam' and ltype=2");
while ($row = pg_fetch_array($annual_query)) {
$total_annual = $total_annual + $row['ltotal'];
}
print($total_annual);
or
$total_annual = 0;
$annual_query = pg_query("select ltotal from leave where lapplicant='adam' and ltype=2");
$annual_result = pg_fetch_all($annual_query);
foreach ($annual_result as $row) {
$total_annual = $total_annual + $row['ltotal'];
}
print($total_annual);
Tamil pointed out the immediate problem. However - why don't you just do this in SQL?
select sum(ltotal)
from leave
where lapplicant='adam' and ltype=2

Data insertion using array in PHP

I have a php script for conducting online test. while taking test, I am using post method to get the answers in the action page.The answers are passed as an array with question id and the selected option value(true or false/the answers the candidate entered like that).Then I have to insert the answers marked by the candidate in the database. The code is as shown below:
$student_id=$_POST['name'];
$survey_id=$_POST['survey_id'];
$store = array();
if (isset($_POST['question_id'])) {
foreach ($_POST['question_id'] as $key => $option) {
$option1 = array_filter($option);
print_r($option1);
if (count($option1) > 1) {
$option2 = implode("^", $option1);
$store[] = $option2;
} else {
$value = $option1;
$i = implode(null, $option1);
$store[] = $i;
}
}
print_r($store);
}
$t = new DateTime();
$t->setTimestamp($time = time());
$t->setTimeZone(new DateTimeZone("Asia/Singapore"));
$date = $t->format(DateTime::RFC850);
$SQL = "INSERT INTO answer_table(student_id ,survey_id, ans_1, ans_2, ans_3, ans_4, ans_5, ans_6, ans_7, ans_8, ans_9, ans_10, timestamp) VALUES ('$student_id','$survey_id', '$store[0]', '$store[1]', '$store[2]', '$store[3]', '$store[4]', '$store[5]', '$store[6]', '$store[7]', '$store[8]', '$store[9]', '$date')";
$result = mysql_query($SQL);
If the student answers the test in sequential order(from question number 1 to 10) the code works fine. But when the candidate answers in random manner(first 10 then 5 like that),the table field named,ans_1 will get inserted with answer of question number 10. I need to insert fields with corresponding answers ,(ans_1 with answer of question 1 like that) what ever pattern,the candidate takes test.
Can anyone help me to solve this issue. Thanks in advance.
For achieving you goal.you should store question_id with answers or create another table which store question_id and corresponding answers, with id of answer table.
If you have the question id in the $key variable, you could use
$store[$key] = $option2;
instead of
$store[] = $option2;
With that the first question is the first entry in the array regardless of the order of the answers in the post array.
You can replace $store[] = $option2; with:
$store[$_POST['question_id']] = $option2;

Break a special character in MySQL

I have a requirement where I need to check a pipe | in the database. If found I need to play around differently.
Here how my db table looks like //Please check the | character in row 11
And if I run a group by sql command myresult will be
Which is correct.
But my requirement is to break the | in any cell and give the count accordingly. The expected result as
Can this be done using MySQL commands alone or do I need to use some php script as well?
Any snippet will be helpful.
Hope this script might help u
$frt =array();
$stmt = $mysqli->prepare("select `fruits` from `meva`") or $mysqli->error ;
$stmt->execute();
$stmt->bind_result($fruits);
while ($stmt->fetch()) {
$frt[]=$fruits;
}
// var_dump($frt); //check all the fruits is in array
$res = array();
$tot = count($frt);
for($i=0;$i<=$tot;$i++)
{
if(preg_match("/\|/", $frt[$i]))
{
$res[] =explode( '|', $frt[$i]);
}else
{
$res[] = $frt[$i];
}
}
// var_dump($res);
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($res));
foreach($it as $v) {
$ary[]=$v;
}
$all_fruits = array();
$tot_ary = count($ary);
for($io=0;$io<=$tot_ary;$io++)
{
if(isset($ary[$io])!='')
{
$all_fruits[] = trim($ary[$io]);
}else
{
continue;
}
}
// var_dump($all_fruits);
$newArray = array_count_values($all_fruits);
foreach ($newArray as $key => $value) {
echo "$key - <strong>$value</strong> <br />";
}
I think you should fix your data. You can run these two statements in a row until all the data is fixed:
INSERT INTO meva (fruits)
SELECT SUBSTR(fruits, LOCATE('|', fruits) - 1) FROM meva
WHERE LOCATE('|', fruits) > 0;
UPDATE meva
SET fruits = SUBSTR(fruits, LOCATE('|', fruits) + 1)
WHERE LOCATE('|', fruits) > 0;
This will fix the table.
However, if it is your interview question (or a school assignment) just to count from the table as it is, then you can only do it if you know the maximum number of pipes in a given row.
So, if the maximum number of pipes in a row is 1, then your select statement would be:
SELECT count(*),
CASE WHEN LOCATE('|', fruits) > 0 THEN SUBSTR(fruits, LOCATE('|', fruits) - 1) ELSE fruits END
FROM meva
GROUP BY CASE WHEN LOCATE('|', fruits) > 0 THEN SUBSTR(fruits, LOCATE('|', fruits) - 1) ELSE fruits END
If you can have more than one pipe in a row, then your CASE statement will be more complex
Actually the best solution is to change your data structure. This current structure is not recommended. each 'cell' has to contain only one value. If you need to store several fuirts for a specific ID, use
id fruit
11 Apple
11 Mango
this might require some adjustments to your code / tables, but it will prevent the need for more future hacks.
You can use php and do it like below
$query = mysql_query("SELECT fruit FROM meva");
$cnt_array = array();
while($row = mysql_fetch_assoc($query)){
$fruits = $row["fruit"];
$fruit = explode("|", $fruits);
foreach($fruit as $fru){
if(array_key_exists($fru,$cnt_array)){
$cnt_array[$fru] = $cnt_array[$fru]+1;
}
else{
$cnt_array[$fru] = 1;
}
}
}
print_r($cnt_array);
NOTE : This code is not tested,please try it and edit accordingly

How to do something different for the last item in an array (from mysql_fetch_array)? [duplicate]

This question already has answers here:
Echo a comma on all but the last value? (result from mysql_fetch_array)
(11 answers)
Why doesn't it prevent further looping?
(2 answers)
Closed 8 years ago.
I am trying to make sure that commas go after all of the items EXCEPT for the last one - I am trying to figure out how to calculate out what the last item is for variable lists. I decided to try to use count($variablename) but it doesn't seem to work.
Is there something wrong with my syntax?
$servicearray = mysql_query("select serv_id from org_serv_xref where org_id='".$org_id."'");
$servi = 0;
while ($servicearrayrow = mysql_fetch_array($servicearray)) {
$servdescarray = mysql_query("select serv_desc from service where serv_id='".$servicearrayrow['serv_id']."'");
while ($servdescarrayrow = mysql_fetch_array($servdescarray)) {
if ($servi < 5 OR $servi < count($servdescarrayrow)) {
ECHO $servdescarrayrow['serv_desc'].",";
$servi++;
}
else if ($servi == 5 OR $servi == count($servdescarrayrow)) {
echo $servdescarrayrow['serv_desc'];
$servi++;
}
else {
$servi = 0;
break 2;
}
}
}
Because you're starting $servi at 0. It will always be less than the count of the array. If you start it at 1 it should work the way you expect it to.
change your query to
$servicearray = mysql_query("select GROUP_CONCAT(serv_id) from org_serv_xref where org_id='".$org_id."'");
now sql builds the nice comma delimited list for you :)
As near as I can figure, your trying to do something like this (although I could be totally wrong):
// Initialise vars
$lastid = '';
$items = array();
// Do the query
$query = "SELECT serv_desc, serv_id
FROM service
WHERE serv_id IN (
SELECT serv_id
FROM org_serv_xref
WHERE org_id='".$org_id."'
)
ORDER BY serv_id";
$servdescarray = mysql_query($query);
while ($servdescarrayrow = mysql_fetch_assoc($servdescarray)) {
// We've reached the next ID, echo the result of the last one.
if ($lastid != $servdescarrayrow['serv_id']) {
$lastid = $servdescarrayrow['serv_id'];
if (count($items)) {
echo implode(',',$items);
$items = array();
}
}
if (count($items) == 5) { // We don't want more than 5 items
continue;
} else { // Add an item to the results
$items[$servdescarrayrow['serv_desc']];
}
}
// Make sure we get the last one
if (count($items)) {
echo implode(',',$items);
}

Categories