skip to Main Content

I am solving a problem with php. I have bigrams in a table on localhost. I enter a lemma into the page and search for bigrams, or just collocations + logDice association measure. Finally i want to dump it. I wrote:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <form action="kolokace.php" method="post" style="float: left;">
        <input type="text" name="xaverius" placeholder="Svatý Xaverius" style="margin-bottom: 7px;"><br>
        
        <input type="submit" name="logDice" value="logDice" style="padding: 2px 3px; color: green; font-weight: bold;">
</form>


<div style="float: left; margin-left: 5rem;">
<!--xaverius-->
<?php
    if (isset($_POST["logDice"])){
        if ($_POST["xaverius"]){
            $lemma = $_POST["xaverius"];
            $conection = mysqli_connect("localhost", "root", "", "asociation");
            mysqli_set_charset($conection, "utf8mb4");
            $sql = "SELECT DISTINCT LOWER (word2) AS word2 FROM xaveriusbigramy WHERE word1='".$lemma."'";
            $result_kolokace = mysqli_query($conection, $sql);  
            if (mysqli_num_rows($result_kolokace) > 0){
                while ($radek_kolokace = mysqli_fetch_assoc($result_kolokace)){
                    
                    $kolokace = $radek_kolokace["word2"]; // pravé kolokace - seznam
                    
                    $mysql1 = "SELECT COUNT(word1) as w1 FROM xaveriusbigramy WHERE word2='".$kolokace."'";
                    mysqli_set_charset($conection, "utf8mb4");
                    $result = mysqli_query($conection, $mysql1);  
                    if (mysqli_num_rows($result) > 0){
                        while ($radek = mysqli_fetch_assoc($result)){
                            $freq_w1 = $radek["w1"]; // frequency of collocations

                        }
                    }

                    $mysql2 = "SELECT COUNT(word2) as w2 FROM xaveriusbigramy WHERE word1='".$lemma."'";
                    mysqli_set_charset($conection, "utf8mb4");
                    $result2 = mysqli_query($conection, $mysql2);  
                    if (mysqli_num_rows($result2) > 0){
                        while ($radek2 = mysqli_fetch_assoc($result2)){
                            $freq_w2 = $radek2["w2"]; // frekvence lemmatu, který zadávám 
                        }

                    $mysql3 = "SELECT COUNT(id) as id FROM xaveriusbigramy WHERE word1='".$lemma."' AND word2='".$kolokace."'";
                    mysqli_set_charset($conection, "utf8mb4");
                    $result3 = mysqli_query($conection, $mysql3);  
                    if (mysqli_num_rows($result3) > 0){
                        while ($radek3 = mysqli_fetch_assoc($result3)){
                            $freq_w3 = $radek3["id"]; // frequency of bigrams 
                        }
                    
                    // COUNTING logDice
                    $logDice = 14+log(2*$freq_w3/$freq_w2+$freq_w1); // logDice value
                    $logDice_round = round($logDice, 2);

                    $pole = array($kolokace=>$logDice_round); 
                     
                    foreach($pole as $key => $val){
                        $pole[$key] = floatval($val);
                        }
                    
                    arsort($pole);

                    echo $kolokace.$logDice_round."<br>";

                        }
                    }
                } 
            }
        }    
    }

?>
</div>


    
</body>
</html>

It does print what I want, but the problem is that the table is not sorted by numeric values. See:

enter image description here

Thank you for any help.

2

Answers


  1. Chosen as BEST ANSWER

    Thank you very much for your help. The problem is currently solved.


  2. If your array is properly formatted, then using PHP arrays sort functions works, as in this 3v4l.

    <?php
    
    $pole = array("a" => "20.68", "za" => "18.68", "pátravý" => "14.01", ";" => "19.06", "," => "21.71");
    
    asort($pole);
    
    foreach($pole as $x => $x_value) {
      echo "key = " . $x . ", value = " . floatval($x_value);
      echo "<br />";
    }
    
    ?>
    

    I see at least 3 issues in your code:

    1. You are wide open to SQL injections and should use parameterized prepared statements
    2. using arsort($pole); after the loop won’t help, sorting is done before you loop/print.
    3. the sample of your array doesn’t fit the requirements for sorting functions : { 0: "a", 1: "20.68" }, { 0: "za", 1: "18.68" }, { 0: "pátravý", 1: "14.01" }, { 0: "";"", 1: "19.06" }, { 0: ",", 1: "21.71" }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search