开发者

PHP/MYSQL fetch results

I can populate results in PHP/MYSQL without problems and the sample code below can confirm that:

$query = "SELECT id, firstname, lastname, nums_ty FROM
       table_name
       WHERE nums_ty ='5' 
       ORDER BY id Desc";

I can view all records in the DB without hassle. What am trying to achieve is this: I want to create a form on the same page whereby a user can either select ALL to display all results or choose result based on 开发者_开发百科date (WHERE date BETWEEN '$startDate' AND '$endDate'. But the default should be ALL.


Is this what you're trying to do?

// check if user has selected a date range
// If yes, add the where condition    
$query  = "SELECT id, firstname, lastname, nums_ty ";
$query .= "FROM table_name ";
$query .= "WHERE nums_ty ='5' ";

// Make sure both start and end dates are selected
if(!empty($startDate) && !empty($endDate)) {
    $query .= "AND date BETWEEN '$startDate' AND '$endDate' ";
}

$query .= "ORDER BY id DESC";

echo $query;


ok by default you query all the rows and show it in your table, i think you should use the $_POST or $_GET params with the function in php isset( ..param ) to evaluate if the dates in the form have been send, if that occurs you should use the mysql query with the dates criterias and after that just dispplay the results in your table or whatever

I hope this help u a little bit

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜