I have a problem, I did a search system by tags, I calculate the percentage of the search by how many tags have the result by how many equal tags (example: 10 tags of the result, 5 equal, or 50%). However I need to sort the results and it is not possible with ORDER BY (since it is not in a column), so I came across a system to sort via array, but since there are multiple results generated inside a foreach, several arrays are created and not a single one to be able to sort. If someone can select or give an easier way I am grateful.
* NOTE: I would use "sort" to organize * OBS2: I'm using PDO
<?php
$palavra_buscada = $_GET['search'];
$palavra1 = array(', ');
$palavra2 = array(',');
$palavra1 = str_replace($palavra1, $palavra2, $palavra_buscada);
$palavras = explode(",", $palavra_buscada);
for ($i = 0; $i < count($palavras); $i++) {
$queryLike[] = "name LIKE '%" . $palavras[$i] . "%' OR tags LIKE '%" . $palavras[$i] . "%' ";
$where = "" . implode(" OR ", $queryLike) . "";
$total = "SELECT * FROM videos WHERE " . $where. "";
$totalizando = $conn->prepare($total);
$totalizando->execute();
}
$count_pesquisa = $totalizando->rowCount();
?>
<div class="listTop"><span>Videos encontrados<span class="videos-f"><?php echo $count_pesquisa; ?></span></span></div>
<?php
if($count_pesquisa > 0) {
$result_pesquisa = $totalizando->fetchAll();
foreach($result_pesquisa as $row_pesquisa) {
$video_name = $row_pesquisa['name'];
$tag_name = $row_pesquisa['tags'];
$tag_explode = explode(",", $tag_name);
$tag_count = count($tag_explode) - 1;
$result = array_diff( $tag_explode, $palavras );
$tag_equal = count($result) - 1;
$total2 = $tag_count - $tag_equal;
$percent = round($total2 * 100 / $tag_count, 1);
$percent_a = array(''.$percent.'' );
print_r($percent_a);
rsort($percent_a);
foreach ($percent_a as $tchau) {
echo '
<h1>'.$video_name.'</h1>
'.$total2.'/'.$tag_cont.'<br>
Porcentagem'.$tchau.'
';
}
echo '<br>';
echo '
';
}
} else {
}
?>