after more search on SO and google, I decided that probably I need to utilize collator_create and collator_sort functions since my site's language is Turkish and I use UTF-8 encoding.
new position where I am is:
/* fetch values */
$etiket_bulutu = '';
while ($beyan->fetch())
{
$etiket_bulutu .= $tags.', ';
}
$etiket_bulutu = substr_replace($etiket_bulutu ,'',-2); //omit last {, } chars
$etiketler = explode(", ", $etiket_bulutu); //get each tag as arr
$etiketler = array_unique($etiketler);
$etiketler = array_values($etiketler); //only unique tags without NULLs
$etadet = count($etiketler);
$coll = collator_create('tr_TR'); //from http://www.php.net/manual/en/collator.sort.php
collator_sort($coll, $etiketler);
for($x=0;$x<$etadet;$x++)
{
echo $etiketler[$x];
echo "<br />";
}
Now, I have no html output and in my error.log file I got this warning "... PHP Warning: sort() expects parameter 2 to be long, string given in ..." and this fatal error "PHP Fatal error: Class 'Collator' not found"
I also googled for these issues but couldn't solve my issue. My PHP version is 5.3.24 waiting for your valuable help. BR
in my related MySql table, my each article (each table row) has its tags in row's related cell. my tagging format has 2 rules:
tag1, tag2
)smart-phone, tag2
)as last input, different articles can have same tags of course
having a page titled "tags", I try to achieve alphabetical sort of all tags and print them with links back to the list of related articles.
I thought the procedure below:
note: if ($beyan = $db_baglanti->prepare($sorgum))
{
/* bind parameters */
$beyan -> bind_param("s", $bindparametre1); echo "\t".''."\r\n";
echo "\t\t".'convert_one_row
is a string validation function.
/* execute statement */
$beyan->execute();
/* bind result variables */
$beyan->bind_result($etiketler);
'.'Etiketler'.'
'."\r\n";
/* fetch values */
$etiket_bulutu = '';
while ($beyan->fetch())
{
$etiket_bulutu .= $etiketler.', ';
}
$etiket_bulutu = substr_replace($etiket_bulutu ,'',-2); //en son 2karakteri yani {, } attık $etiket_bulutu = explode(", ", $etiket_bulutu); //her bir etiketten array yarattık sort($etiket_bulutu, SORT_NATURAL | SORT_FLAG_CASE); $etiket_bulutu = array_unique($etiket_bulutu, SORT_STRING); //sadece tekil etiketler kaldı $bulut = ''; foreach($etiket_bulutu as $etiket) {$bulut .= ''.convert_one_row($etiket).', ';} $bulut = substr_replace($bulut ,'',-2); //en son 2karakteri yani {, } attık echo "\r\n\r\n".'
'.$bulut.'
'."\r\n\r\n";echo "\t".''."\r\n";
/* close statement */
$beyan->close();
}
?>
I achieved to print tags with related links however alphabetical sort issue has failed. Currently, 1st article's tags are written then 2nd article's tags, 3rd and so on... But there is no alphabetical sorting.
current situation example:
article 1 tags: table, apple, smart-phone
article 2 tags: smart-phone, world, soccer
current situation: table, apple, smart-phone, world, soccer
what I require: apple, smart-phone, soccer, table
Can you please correct me thank you in advance
After asking this question and learning the answer of it, I needed to find a way to sort UTF-8 (Turkish input) string array from MySql db alphabetically.
then I used Fy-'s answer in this link and it worked.
Now, my solution is:
setlocale(LC_COLLATE, 'tr_TR.utf8'); //utf-8 sorting için gerekli
$sorgum = "...";
$bindparametre1 = '...';
if ($beyan = $db_baglanti->prepare($sorgum))
{
/* bind parameters */
$beyan -> bind_param("s", $bindparametre1);
/* execute statement */
$beyan->execute();
/* bind result variables */
$beyan->bind_result($etiketler);
echo "\t".'<div class="sol-icerik-kapsar">'."\r\n";
echo "\t\t".'<h1>'.'Etiketler'.'</h1>'."\r\n";
/* fetch values */
$etiket_bulutu = '';
while ($beyan->fetch())
{
$etiket_bulutu .= $etiketler.', ';
}
$etiket_bulutu = substr_replace($etiket_bulutu ,'',-2); //en son 2karakteri yani {, } attık
$etiketler = explode(", ", $etiket_bulutu); //her bir etiketten array yarattık
$etiketler = array_unique($etiketler);
$etiketler = array_values($etiketler); //sadece tekil etiketler kaldı
$etadet = count($etiketler);
usort($etiketler, 'strcoll');
echo "\n\t\t".'<p>'."\n";
for($x=0;$x<$etadet;$x++)
{
echo "\t\t\t".'<a href="'.url_validate(sitenin_koku.'etiketler/'.$etiketler[$x]).'">'.convert_one_row($etiketler[$x]).'</a>, '."\n";
}
echo "\t\t".'</p>'."\n";
key codes for my solution here are : setlocale(LC_COLLATE, 'tr_TR.utf8');
and usort($etiketler, 'strcoll');
regards