Home » Php » Generate word combination with php mysql

Generate word combination with php mysql

Posted by: admin February 25, 2020 Leave a comment

Questions:

Please help me. I got word combination code from here. It’s work perfectly.

The problem is, i want to change the $alphabet = array('a','b','c') to my mysql. So, I modified the code. But when I run it, I got error Undefined offset: 1. This is the code:

test.php

include "combine.php";

$db = new mysqli("localhost", "root", "", "db_book");

//the result of my sql is word from tb_all_word who appeared more than 2 times

$sql = "SELECT c.word
        FROM tb_all_word c, tb_book_data a
        WHERE c.book_id = a.book_id
        GROUP BY c.word
        HAVING COUNT(c.word) >= 2";

$query = mysqli_query($db, $sql);
$alphabet = mysqli_fetch_array($query);
$result = array();
$arrResult = array();

combine($alphabet, $result, $arrResult);
print_r($result);

combine.php (code from here, i didn’t change anything)

function combine($shiftedAlphabet, &$result, &$arrResult) {
    global $alphabet;

    $currentElementStr = '';
    $currentElementArr = array();

    for($i = 0; $i < count($shiftedAlphabet); ++$i) {
        $newElement = $shiftedAlphabet[$i];
        $currentElementStr .= $newElement;
        $currentElementArr[] = $newElement;

        if(!in_array($currentElementStr, $result)) { // if not duplicated => push it to result
           // find right position
           $thisCount = count($currentElementArr);
           $indexFrom = 0;
           $indexToInsert = 0;

           // find range of indexes with current count of elements {
           foreach ($arrResult as $arrResultKey => $arrResultValue) {
               $indexToInsert = $arrResultKey + 1;
               if ($thisCount > count($arrResultValue)) {
                   $indexFrom = $indexToInsert;
               }
              if ($thisCount < count($arrResultValue)) {
                  --$indexToInsert;
                  break;
            }
        }
       // find range of indexes with current count of elements }

       // find true index inside true range {
        $trueIndex = $indexToInsert;
        $break = false;
        for($j = $indexFrom; $j < $indexToInsert; ++$j) {
            $trueIndex = $j + 1;
            foreach($arrResult[$j] as $key => $value) {
                if (array_search($value, $alphabet) > array_search($currentElementArr[$key], $alphabet)) {
                    $break = true;
                    break;
                }
            }
            if($break) {
                --$trueIndex;
                break;
            }
        }
        // find true index inside true range }

        // find true index inside true range {
        $trueIndex = $indexToInsert;
        $break = false;
        for($j = $indexFrom; $j < $indexToInsert; ++$j) {
            $trueIndex = $j + 1;
            foreach($arrResult[$j] as $key => $value) {
                if (array_search($value, $alphabet) > array_search($currentElementArr[$key], $alphabet)) {
                    $break = true;
                    break;
                }
            }
            if($break) {
                --$trueIndex;
                break;
            }
        }
        // find true index inside true range }

        array_splice($result, $trueIndex, 0, $currentElementStr);
        array_splice($arrResult, $trueIndex, 0, array($currentElementArr));
    }
}

for($i = 0; $i < count($shiftedAlphabet) - 1; ++$i) {
    $tmpShiftedAlphabet = $shiftedAlphabet; // for combining all possible subcombinations
    array_splice($tmpShiftedAlphabet, $i, 1);
    combine($tmpShiftedAlphabet, $result, $arrResult);
}
}

I’m sure the problem is in the test.php code, but I don’t know how to fix it. Can someone help me? I’m a newbie in programming.

How to&Answers: