Forum Moderators: open

Message Too Old, No Replies

sort by alphabet and by number

sort alphanumerically

         

qlooney

1:30 am on Aug 13, 2009 (gmt 0)

10+ Year Member



Hello,
I'm a newbie here and to be honest, i don't have programming background. i just learn things from asking around. ^^

I wanted to create a page which has a list of titles from a database and when people click a certain letter like "A" for example, then it will show titles started with that letter.

so far i made it, but the problem is, i wanted to show the titles started with numbers. i want to create a link where when people click "#" then only titles started with numbers show.

this is my current code:


<a href="movie.php?id=A">A</a>
<a href="movie.php?id=B">B</a>
<a href="movie.php?id=C">C</a>
<a href="movie.php?id=D">D</a>
...... (until Z)
<a href="movie.php?id=?">#</a> <---- I wanted this link only to show all titles that started with numbers

<div class="right"><div class="list">

<?php
include "global_var.php";

if (!isset($_REQUEST['id']))
{
$id = "A";
}

else
{
$id=$_REQUEST['id'];
}

$dbtabl = 'tbl_movie';
$conn = mysql_connect($db["host"], $db["user"], $db["password"]);
mysql_selectdb($db["database"]);

//query the database
$query = "SELECT id_movie,ori_title,alt_title FROM tbl_movie where ori_title like '".$id."%' or alt_title like '".$id."%' order by ori_title asc";

$result = mysql_query($query, $conn) or die("Invalid query: " . mysql_error());
$i=0;
while ($row=mysql_fetch_row($result))
{

if (strtolower($id)==substr(strtolower($row[1]),0,1))
{
//echo substr($row[1],0,1)." ¦ ".$row[1]." ¦ ".$row[2]."<br>";
$judul[$i]=$row[1].'¦'.$row[0];
$i++;
}

else
{
//echo substr($row[1],0,1)." ¦ ".$row[2]." ¦ ".$row[1]."<br>";
$judul[$i]=$row[2].'¦'.$row[0];
$i++;
}
}

sort($title);
$n=0;

//if no title found
if ($i==0)
{
echo "Data not found<p>&nbsp;</p>";
}

//end no data


foreach ($title as $key => $val)
{
list($strtitle,$strid)=split('[¦]',$val);
echo "<p><a href=\"movie_content.php?id=$strid\">";
echo htmlspecialchars($strtitle);
echo "</a></p>";
}

?>

</div></div>

can anyone help me?

thx and rgds.

maximillianos

11:50 am on Aug 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This could be done with JavaScript. You would need to reference the text value of each link and evaluate it to see if the first char is a number. If not, filter it out. (make display property none)

From the server side, you could call the same page with a new param=num and then simply check the link title for a number before displaying. Assuming your titles are in a db.

rocknbil

2:27 pm on Aug 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard qlooney, sorry for the delay in reply, was sure someone else would pick this one up.

Use a mySQL regexp to match on any title beginning with a number.

.....
else
{
$id=$_REQUEST['id'];
}

$term = (preg_match('/^\?$/',$id))?" regexp '^\d+.*'":"like '$id\%'";

...

$query = "SELECT id_movie,ori_title,alt_title FROM tbl_movie where ori_title $term or alt_title $term order by ori_title asc";

What this does:

Short circuit evaluation, basically an if/else:

$term = - store the term in $term

(preg_match('/^\?$/',$id))? - if the $id begins with (^) and ends with ($) a single ?,

" regexp '^\d+.*'" - set "$term" to the mysql regexp operator, and the expression to search for terms that begin with one or more digits (^\d+) followed by zero or more of any character (.*) This will match 012abc, 01234, etc.

: - "else"

"like \'$id\%'" - set $term to a like clause beginning with whatever's in $id followed by any character.

So what you should get, if echoed, is

// $_GET['id'] = 'A';

SELECT id_movie,ori_title,alt_title FROM tbl_movie where ori_title like 'A%' or alt_title like 'A%' order by ori_title asc

// $_GET['id'] = '?';

SELECT id_movie,ori_title,alt_title FROM tbl_movie where ori_title regexp '^\d+.*' or alt_title regexp '^\d+.*' order by ori_title asc

Although I'd recommend using some other character than ? as your numeric marker, as it may be taken out of context and interpolated in some cases, this should still work.