Forum Moderators: coopster

Message Too Old, No Replies

alphabetical list now working

         

dgnzcn

7:31 pm on Nov 25, 2009 (gmt 0)

10+ Year Member



hi, this alphabetical order from database does not working.

can you help me ? thanks.

<!--mysql-->
<?php
if(empty($_POST)) {
$harf = $_GET['harf'];
$harf .= "%";
$search = $harf;
$sdesc = "*";
$classquery = "";
} else {
$search = $_POST['search'];
$search = "%" . $search . "%";
$sdesc = "%" . $search . "%";
$sclass = $_POST['harf'];
$classquery = "AND 'harf' = CONVERT( _iso-8859-9 '" . $sclass . "' USING latin5 )";
}
mysql_connect("localhost", "adminpa", "Lw");
mysql_select_db("adminpaneli");
// If current page number, use it
// if not, set one!
$query = mysql_query("SELECT * FROM `urunler` WHERE `ad` LIKE '.$alphabet.%' ORDER BY `ad` DESC");

if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}

// Define the number of results per page
$max_results = 5;

// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);

// Perform mysql query on only the current page number's results
$result = mysql_query("SELECT * FROM urunler ORDER BY ad DESC LIMIT $from, $max_results")
or die(mysql_error());

//TO PRINT OUT THE DATA
echo "<table border='1'>";
echo "<tr> <th>ad</th></tr>";
// keeps getting the next row until there are no more to get
while($urunler = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $urunler['ad'];
echo "</td><td>";
}

echo "</table>";
//STOP PRINTING OUT THE DATA

// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM urunler ORDER BY ad DESC"),0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);

// Build Page Number Hyperlinks
echo "<p class=\"center\">Sayfa: ";

// Build Previous Link
if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['php_SELF']."?page=$prev\">&laquo;</a> ";
}

for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['php_SELF']."?page=$i &harf=$harf\">$i</a> ";
}
}

// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['php_SELF']."?page=$next\">&raquo;</a>";
}
echo "</p>";

mysql_close();
?>
<?php

$alphabet = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
foreach ($alphabet as $harf) {
echo "<a href=\"?harf=" . $harf . "\">" . $harf . "</a>&nbsp;¦&nbsp;";
}
echo "<a href=\"?\">Show All</a></p><br />
<form method=\"post\" action=\"?\">
<input type=\"text\" name=\"search\" /> <select name=\"class\">
<option>Select Category</option>
<option>---</option>
<option value=\"restaurant\">restaurant</option>
</select>
<input type=\"submit\" name=\"submit\" value=\"Search\" class=\"submit\" />
</form>";
?>

dgnzcn

7:52 pm on Nov 25, 2009 (gmt 0)

10+ Year Member



I am sorry. my codes is NOT working. :) anybody help me?

rocknbil

8:58 pm on Nov 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right off I can see,

AND 'harf'

Do not quote field names.

AND harf =

Second, you didn't really say what the problem is. :-) I see you have this,

or die(mysql_error());

on the second query, but not on the first. Attach it to your first query (and all others,) see if it tells you anything (but fix the item above first, that will cause an error.)

Be sure to remove the mysql_error traps before live deployment, this reveals info about your database.

dgnzcn

9:05 pm on Nov 25, 2009 (gmt 0)

10+ Year Member



hi ok. :)

the problem is, if I press any alphabetical letter notting happen.
alphabetical letter pagination does not work.

help please.

rocknbil

5:51 pm on Nov 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, that is an error.

Read your script from the top down, and note the first instance of "$alphabet."

$query = mysql_query("SELECT * FROM `urunler` WHERE `ad` LIKE '.$alphabet.%' ORDER BY `ad` DESC");

This is not defined until later in the script.

When you create your links, you're using harf:

echo "<a href=\"?harf=" . $harf . "\">" . $harf . "</a>&nbsp;¦&nbsp;";

So that's what you want. I'm not sure what you're doing here, or why:

$harf = $_GET['harf'];
//$harf .= "%";
//$search = $harf;

You have the % in the select, and don't even use $search. At any rate, I'd remove those like above, then do

$query = mysql_query("SELECT * FROM `urunler` WHERE `ad` LIKE '" . $harf . "%' ORDER BY `ad` DESC");

However, you're not even using $query anywhere, so what the heck? :-)

I'd probably use regexp instead, because not all your fields may begin with capitals.

$query = mysql_query("SELECT * FROM `urunler` WHERE `ad` regexp '" . $harf . ".*' ORDER BY `ad` DESC");

You have a lot of things astray there, looks like an edited copy of something free. Start at the top and work down, echo out select statements and values, this will help.