views:

79

answers:

3

Hello guys I'm stucked in a point and I need your help. I want to change SQL QUERY for each $_GET parameters. I'm using this:

<?
if (isset($_GET['page']) ) {
   $pageno = $_GET['page'];
} else {
   $pageno = 1;
} // if
$query = mysql_query("SELECT count(id) FROM m3_music_mp3");
$query_data = mysql_fetch_row($query);
$numrows = $query_data[0];
$rows_per_page = 30;
$lastpage      = ceil($numrows/$rows_per_page);
$pageno = (int)$pageno;
if ($pageno > $lastpage) {
   $pageno = $lastpage;
} // if
if ($pageno < 1) {
   $pageno = 1;
} // if
$limit = 'ORDER BY id DESC LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
$query = mysql_query("SELECT * FROM m3_music_mp3 $limit");
?>

This does IF music.php?page=1 is set $query = "SELECT * FROM m3_music_mp3 ORDER BY id DESC LIMIT X,X" but I want to do is IF music.php?word=A&page=1 set $query = "SELECT * FROM m3_music_mp3 WHERE title LIKE '$word%' ORDER BY id DESC LIMIT X,X"

I tried

if(isset($_GET['word']) && isset($_GET['page'])) {
$limit .= 'WHERE....'
}

something like but does not works. thank you. and sorry for bad english & poor php knowledge of me

A: 

It should be:

if(isset($_GET['word'])) {
  $word = mysql_real_escape_string($_GET['word']);

  $limit = " WHERE title LIKE '%$word%' ORDER BY id DESC LIMIT " .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
}
else
{
  $limit = " ORDER BY id DESC LIMIT " .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
}

$query = mysql_query("SELECT * FROM m3_music_mp3 $limit") or die(mysql_error());
Sarfraz
thank you for your comment. it works fine. but how can i increase $_GET's? is using elseif (isset) thing should work?
Ronnie Chester Lynwood
A: 

Your solution should work fine, maybe you just need a space before WHERE, like .= ' WHERE... print out your final query string to see where it is going wrong.

Orbit
A: 

basically sAc is right. but you can reduce your code. cause limit isnt optional this schould work just fine: Edit changed the code bit to be copy- and testable ;)

<?
if (isset($_GET['page']) ) {
   $pageno = $_GET['page'];
} else {
   $pageno = 1;
} // if
$limit = "";
    if(isset($_GET['word'])) {
      $word = mysql_real_escape_string($_GET['word']);

      $limit = " WHERE title LIKE '%$word%'"
    }
$query = mysql_query("SELECT count(id) FROM m3_music_mp3" . $limit);
$query_data = mysql_fetch_row($query);
$numrows = $query_data[0];
$rows_per_page = 30;
$lastpage      = ceil($numrows/$rows_per_page);
$pageno = (int)$pageno;
if ($pageno > $lastpage) {
   $pageno = $lastpage;
} // if
if ($pageno < 1) {
   $pageno = 1;
} // if
$limit .= ' ORDER BY id DESC LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
$query = mysql_query("SELECT * FROM m3_music_mp3 $limit");
?>
maggie
okay. this works too. thank you. but now pagination is not work correctly paging still works with all data not with how many title rows :S
Ronnie Chester Lynwood
get the isset word bit and place it BEFORE $query = mysql_query("SELECT count(id) FROM m3_music_mp3");change $query = mysql_query("SELECT count(id) FROM m3_music_mp3"); to $query = mysql_query("SELECT count(id) FROM m3_music_mp3" . $limit);
maggie