views:

45

answers:

2

hello im using this code to do search

    <form action="arama.php" method="get">
<input type="text" name="lol">
<select name='kategori'>
<option value="tum">Tum kategoriler</option>
<? 
while ($kat = mysql_fetch_array($kategori_isim)) {
echo "
<option value=".$kat[kategori_isim].">".$kat[kategori_isim]."</option>";
} 
?>
</select>
<input type="submit" value="ara">
</form>

<?
    $lol = mysql_real_escape_string($_GET['lol']);
    $kategori = mysql_real_escape_string($_GET['kategori']);

    if ($kategori == "tum") {
    $ara = mysql_query("select * from dosyalar where baslik like '%$lol%'");
    }
    else {
    $ara = mysql_query("select * from dosyalar where baslik like '%$lol%' order by kategori = '%$kategori%'");
    }
    ?>

search by term works but not listing by kategori.. what can i do?

+1  A: 

I'm not sure I understand the question (I can't really figure out what those fields mean), but I think that your second query should be more like:

$ara = mysql_query("SELECT * FROM dosyalar WHERE kategori LIKE '%$kategori%'");

ORDER BY only specifies how to sort the results, you can only use a column name, not a check as in your code.

Extending my answer: the ORDER BY kategori = '%$kategori%' isn't a syntax error but I don't think it does anything useful. The check kategori = '%$kategori%' will always be false (unless you have a value with percent signs both at start and at end) so the ORDER BY clause will be useless and you will just do the same select you're doing in the other branch of the if block.

Specifying ORDER BY kategori = '$kategori' will return 0 for all the records where kategori is not equal to $kategori, 1 for the ones where it matches. This will basically sort all the matching rows at the end of your query.

kemp
+1  A: 

Maybe the query failed. In that case mysql_query() returns FALSE and mysql_error() returns a description of the error.
Try

<form action="arama.php" method="get">
  <input type="text" name="lol" />
  <select name='kategori'>
    <option value="tum">Tum kategoriler</option>
<?php 
while ( false!==($kat=mysql_fetch_array($kategori_isim)) ) {
  $htmlIsim = htmlspecialchars($kat['kategori_isim']);
  echo '    <option value="', $htmlIsim, '">', $htmlIsim, "</option>\n";
}
?>
  </select>
  <input type="submit" value="ara" />
</form>

<?php
$lol = isset($_GET['lol']) ? mysql_real_escape_string($_GET['lol']) : '';
$kategori = isset($_GET['kategori']) ? mysql_real_escape_string($_GET['kategori']) : '';
$query = "select * from dosyalar where baslik like '%$lol%'";
if ( 'tum'!==$kategori ) {
  $query .= "order by kategori = '%$kategori%'";
}
$ara = mysql_query($query) or die( htmlspecialchars(mysql_error().': '.$query) );
echo '<pre>Debug: ', mysql_num_rows($ara) , ' records in the result set for ', htmlspecialchars($query), "</pre>\n";
?>
VolkerK
no way dont work
Ronnie Chester Lynwood
And what's the output of the `echo '<pre>Debug: '....` line?
VolkerK