-4

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.

  • Have you checked the [sort functions for arrays](https://www.w3schools.com/php/php_arrays_sort.asp)? asort might help. – OldPadawan May 20 '23 at 10:08
  • Thank you, I've tried, but it's not working. – Richard Zmelik May 20 '23 at 11:48
  • Can you just put a dump of raw $pole in you question? – OldPadawan May 20 '23 at 14:03
  • Yeas, here is a part: string(1) "a" float(20.68) string(2) "za" float(18.68) string(9) "pátravý" float(14.01) – Richard Zmelik May 20 '23 at 14:30
  • In variable $kolokace are lemmas as string, in $logDice_int are numbers as float. – Richard Zmelik May 20 '23 at 14:32
  • I mean: in your image we have some raw data but not formatted. It's useful to have a version of the array we can copy/paste. i.e. $pole = array( abc => 19.2, bcd => 3.1 ...) and so on – OldPadawan May 20 '23 at 15:00
  • Ok, I try this: $pole = array(); array_push($pole, $kolokace, $logDice_float); asort($pole); echo $pole[0].$pole[1]."
    "; But the result is same.
    – Richard Zmelik May 20 '23 at 17:15
  • If we don't have the array **with data** and **properly formatted**, and the result of print_r($pole) then it's going to be hard to test and help. Instead of an image, please give us *the printed formatted array* – OldPadawan May 20 '23 at 17:27
  • OK, here is the copy of first items: { 0: "a", 1: "20.68" }, { 0: "za", 1: "18.68" }, { 0: "pátravý", 1: "14.01" }, { 0: "";"", 1: "19.06" }, { 0: ",", 1: "21.71" }, – Richard Zmelik May 20 '23 at 17:38
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman May 20 '23 at 19:09

1 Answers1

1

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" }
OldPadawan
  • 1,247
  • 3
  • 16
  • 25