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:
Thank you for any help.
2
Answers
Thank you very much for your help. The problem is currently solved.
If your array is properly formatted, then using PHP arrays sort functions works, as in this 3v4l.
I see at least 3 issues in your code:
{ 0: "a", 1: "20.68" }, { 0: "za", 1: "18.68" }, { 0: "pátravý", 1: "14.01" }, { 0: "";"", 1: "19.06" }, { 0: ",", 1: "21.71" }