Hi, after 2 days of hard working i finally made my very first search script for my site. Im sure that it could be better, but to be my first one, i think its ok.

This scripts searches my MYSQL database (using Fulltext Search), it searches the fields 'item' (Item Name) and 'config_field_1' (Alternative names for the items). The problem is that in EVERY item the alternate name contains Japanese text (Kanji), and i cant make it to show the kanji propertly.

You can see it for your self here: http://www.animerate.net/buscador.php?cmd=search&mode=normal&words=seiya

As you can see, i get only HTML for that kanji (like 聖闘士星&#30690)

I changed the charset (from charset=iso-8859-1 to charset=utf-8) of my header file and didnts worked, i changed it also on the administration where i add the items (and write on text fields the Japanese alter names.) and didnt worked neither.

I dont know what else can i do, when i browse my items NOT USING the search script, i get the kanjis perfectly, the problems seems to be this new search script.

Here's the code:


// Create the search function:

function searchForm()
{
  // Re-usable form

  // variable setup for the form.
  $searchwords = (isset($_GET['words']) ? htmlspecialchars(stripslashes($_REQUEST['words'])) : '');
  $normal = (($_GET['mode'] == 'normal') ? ' selected="selected"' : '' );
  $boolean = (($_GET['mode'] == 'boolean') ? ' selected="selected"' : '' );

  echo '<center><form method="get" action="'.$_SERVER['PHP_SELF'].'">';
  echo '<input type="hidden" name="cmd" value="search" />';
    echo '<input type="hidden" name="mode" value="normal" />';
  echo '<span class=tbody> Buscar: <input type="text" name="words" value="'.$searchwords.'" /> </span>';

  echo '<input type="submit" value="Buscar" />';
  echo '</form></center>';
}

// Create the navigation switch
$cmd = (isset($_GET['cmd']) ? $_GET['cmd'] : '');

switch($cmd)
{
  default:
    echo '<h1>Search Database!</h1>';
    searchForm();

  break; 

  case "search":
    searchForm();
    echo '<center><h3 class=tbody>Resultados de tu busqueda:</h3></center>';


$searchstring = mysql_escape_string($_GET['words']);
switch($_GET['mode'])
{
  case "normal":
    $sql = "SELECT *,
           MATCH(item, config_field_1)
           AGAINST ('$searchstring') AS score FROM mnl_items
           WHERE MATCH(item, config_field_1)
           AGAINST ('$searchstring') ORDER BY item DESC";

  break;

  case "boolean":
    $sql = "SELECT item, 
           MATCH(item, config_field_1)
           AGAINST ('$searchstring' IN BOOLEAN MODE) AS score FROM mnl_items
           WHERE MATCH(item, config_field_1)
           AGAINST ('$searchstring' IN BOOLEAN MODE) ORDER BY score DESC";
  break;

}
// Header Table
echo "<table class=\"txtwhite\" bgcolor=\"white\" width=\"94%\"  height=\"20\" align=\"center\"  cellpadding=\"0\" cellspacing=\"1\" border=\"0\" class=\"tbodysmall\">\n";
	echo "<tr>\n";
	        echo "<td width=\"70%\" class=\"txtwhite\" bgcolor=\"061376\"> Titulo</td>\n";
	        echo "<td align=\"center\" width=\"10%\" class=\"txtwhite\" bgcolor=\"061376\">Formato</td>\n";
			echo "<td align=\"center\" width=\"10%\" class=\"txtwhite\" bgcolor=\"061376\">Episodios</td>\n";
			echo "<td align=\"center\" width=\"10%\" class=\"txtwhite\" bgcolor=\"061376\">AƱo</td>\n";
	echo "</tR>\n";
	echo "</table>\n";
echo '<table border=0 align=center width=94% cellspacing=1 cellpadding=0>';   
// echo $sql; $result = mysql_query($sql) or die (mysql_error()); while($row = mysql_fetch_object($result)) { $tempo ++; if ($tempo / 2 == intval($tempo / 2 )) { $background = "#FFFFFF";} else { $background = "#ECECEC";} echo ' <tr bgcolor='.$background.' height=30>'; echo ' <td width=70% >'; echo ' <table width 100%>'; echo ' <tr><td class=biglinktextlist><img src=http://www.animerate.net/images/vineta3.gif align=\'absmiddle\'> <a href=review_list.php?item_id='.stripslashes(htmlspecialchars($row->item_id)).'&cat_id='.stripslashes(htmlspecialchars($row->cat_id)).'&sub_cat_id='.stripslashes(htmlspecialchars($row->sub_cat_id)).'><strong> '.stripslashes(htmlspecialchars($row->item)).'</strong></a></span></td></tr>'; echo ' <tr><td class=smalllinktextlist> <img src=http://www.animerate.net/images/vineta4.gif align=\'texttop\' width=17 height=9> <span class=smalllinktextlist>Titulos Alterno: <a href=review_list.php?item_id='.stripslashes(htmlspecialchars($row->item_id)).'&cat_id='.stripslashes(htmlspecialchars($row->cat_id)).'&sub_cat_id='.stripslashes(htmlspecialchars($row->sub_cat_id)).'>'.stripslashes(htmlspecialchars($row->config_field_1)).'</a></span></td></tr>'; echo ' </table>'; echo ' </td>'; echo ' <td width=10% align=center class=top100>'.stripslashes(htmlspecialchars($row->config_field_5)).'</td>'; echo ' <td width=10% align=center class=top100>'.stripslashes(htmlspecialchars($row->config_field_6)).'</td>'; echo ' <td width=10% align=center class=top100>'.stripslashes(htmlspecialchars($row->config_field_7)).'</td>'; echo ' </tr>';
} break; } echo ' </table>';

I hope you can help me on this.

    How about changing the charset of the actual page? You're using the HTML DTD, why not specify the character encoding:

    <meta http-equiv="Content-type" content="text/hmlt; charset=UTF-8">

    See if that works.

    I do know that PHP doesn't work with multi-byte data (and won't until PHP 6), so PHP won't be doing anything to your information. Make sure your database is set up to handle the characters (i.e. the formatting and locale is correct) and try adding the charset to the page.

      Hi, thanks for your reply...

      I did that already, but it gave me a lot of error spelling (like accents) and still dont have those Kanjis šŸ™

      The charset it seems to be allright, if you see here (http://www.animerate.net/listado_anime.php) you will see some Kanjis that are correctly displayed.

      Even if you go to the item's page (like this: http://www.animerate.net/review_list.php?item_id=71&cat_id=1&sub_cat_id=16&offset=)

      In Titulos Alternos you can see Kanjis also.. I still believe the problem is with the search script. šŸ™

      P.S In order to make the script work, i changed the field type (yn Phpmyadmin) to "TEXT" (i can use varchar or char also) but i decided to use TEXT, this type doesnt have problems with characters quantity.

      Any other ideas?

        your problem is here:

        echo '  <td width=10% align=center  class=top100>'.stripslashes(htmlspecialchars($row->config_field_5)).'</td>'; 
        echo '  <td width=10% align=center  class=top100>'.stripslashes(htmlspecialchars($row->config_field_6)).'</td>'; 
        echo '  <td width=10% align=center  class=top100>'.stripslashes(htmlspecialchars($row->config_field_7)).'</td>';

        Specifically, htmlspecialchars(). The data appears to already have HTML entities in it, so running it through htmlspecialchars() will only serve to mess up any entities already in it. For example doing that will take "&#32854;" and turn it into "&amp;#32854;" which is why you see it on the page.

          ahh crap... why couldn't I see that?

          Good catch... good catch...

            OMG! great man... that was the problem...

            Thanks a lot for the help (both of you).

              Write a Reply...