Hi
I want to display the retrieved datas from database by using the search conditions.My search conditions are Age,gender .if user clicks the search button. the data should be display that satisfy both the conditions i need a mysql query and php code to retrieve the data

Thank you

Recommended Answers

All 18 Replies

We cannot help you unless you post the code and database tables you have written to achieve this so far.

<?php
// Make a MySQL Connection
require_once('conn.php');
$result = mysql_query("SELECT * FROM personal") 
or die(mysql_error());  
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="layout.css" rel="stylesheet" style type="text/css"/>
</head>

<body>
<div id="header">

</div>
<div id="nav">
<ul>

<li><a href="home.php" id="n1" target="_self">Home</a></li>
<li><a href="Register.php" id="n2" target="_self">Register</a></li>
<li><a href="wedding.php" id="n3" target="_self">Wedding</a></li>
<li><a href="contact.php" id="n4" target="_self">Contact</a></li>
</ul>
</div>
<div id="main">
<div id="left">

<?php
echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>username</th> <th>resitown</th><th>nativetown</th><th>qualify</th><th>jobdetail</th><th>income</th><th>disability</th><th>maritalstatus</th><th>emailid</th><th>phoneno</th><th>mobile</th><th>address</th></tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
	// Print out the contents of each row into a table
	echo "<tr><td>"; 
	echo $row['name'];
	echo "</td><td>"; 
	echo $row['username'];
	echo "</td><td>";
	
	echo $row['resitown'] ;
	echo "</td><td>";
	echo $row['nativetown'];
	echo "</td><td>";
	
	echo $row['qualify'] ;
	echo "</td><td>";

	
	echo $row['jobdetails'] ;
	echo "</td><td>";
	
	
	echo $row['income'] ;
	echo "</td><td>";
	echo $row['disability'] ;
	echo "</td><td>";
	echo $row['maritalstatus'] ;
	echo "</td><td>";
	echo $row['emailid'] ;
	echo "</td><td>";
	echo $row['phoneno'] ;
	echo "</td><td>";
	echo $row['mobile'] ;
	echo "</td><td>";
	echo $row['address'] ;
	echo "</td></tr>";
	
	
} 

echo "</table>";
?>
</div>
</div>
</body>
</html>

Thanks blocblue
This is the code iam having. i need to change the code using search conditions like Gender[radio button],Age[dropdownlist].for example: if the user clicks male and age[18 to 24] the datas which satisfy these conditions should be displayed in a webpage

Hello please help me its urgent

commented: Its not urgent to me, or anyone but you, posts like this will only have negative responses -3

You could add a form similar to the one below to your page. When the form is submitted, use PHP to check which inputs were set and use their values to construct your where clause.

<form method="GET" action=".">
    <p>
        <label>Gender</label>
        <input type="radio" name="gender" value="M" /> Male
        <input type="radio" name="gender" value="F" /> Female
    </p>

    <p>
        <label>Age</label>
        <select name="age">
            <option value="< 18">Under 18</option>
            <option value="18|24">18 to 24</option>
            <option value="25|40">25 to 40</option>
            <option value="> 40">Over 40</option>
        </select>
    </p>

    <p>
        <input type="submit" name="filter" value="Filter" />
    </p>
</form>

<?php 
if($_GET) {
    $sql = "SELECT * FROM personal ";
    $where = array();

    // Check whether gender selected
    if(isset($_GET['gender'] && in_array($_GET['gender'], array('M', 'F'))) {
        $where[] = sprintf("`gender` = '%s'", $_GET['gender']);
    }

    // Check whether age range selected
    if(isset($_GET['age']) {
        if(strpos($_GET['age'], '|') {
            $delimiters = split('|', $_GET['age']);
            $which[] = sprintf("`age` BETWEEN %d AND %d", (int)$age[0], (int)$age[1]);

        } else {
            $which[] = sprintf("`age` %s", $_GET['age']);
        }
    }

    // Check whether one or more where clauses exist
    if($where) {
        $sql .= 'WHERE ' . implode('AND ', $where)'
    }

    $result = mysql_query("$sql) 
}
?>

Does this help?

R.

Thank you sir
this is helpful but i got a error in the line

if(isset($_GET['gender'] && in_array($_GET['gender'])), array('M ', 'F'))

As a novice i can't rectify it

Please help me

Member Avatar for diafol

With repect, if you are a novice how can this be urgent? Is this homework?

In the line above, you seem to have a lot of ))))))))) in the wrong place.

Apologies. I wrote my reply quickly, so some of the parentheses were in the incorrect place. See below:

if(isset($_GET['gender']) && in_array($_GET['gender'], array('M ', 'F')))

R

Thank you very much sir.
but i got the folling error while executing

mysql_fetch_array() expects parameter 1 to be resource, null given in....

Thanks a lot for your help

I have just re-read the code I posted and corrected a couple of errors. As stated, this was written in haste, but was done so to give you a starting point for your own efforts.

What does your code look like at the moment with something similar included?

<form method="GET" action=".">
    <p>
        <label>Gender</label>
        <input type="radio" name="gender" value="M" /> Male
        <input type="radio" name="gender" value="F" /> Female
    </p>

    <p>
        <label>Age</label>
        <select name="age">
            <option value="< 18">Under 18</option>
            <option value="18|24">18 to 24</option>
            <option value="25|40">25 to 40</option>
            <option value="> 40">Over 40</option>
        </select>
    </p>

    <p>
        <input type="submit" name="filter" value="Filter" />
    </p>
</form>

<?php 
    $sql = "SELECT * FROM personal ";
    $where = array();

    // Check whether gender selected
    if(isset($_GET['gender']) && in_array($_GET['gender'], array('M', 'F'))) {
        $where[] = sprintf("`gender` = '%s'", $_GET['gender']);
    }

    // Check whether age range selected
    if(isset($_GET['age']) {
        if(strpos($_GET['age'], '|') {
            $delimiters = split('|', $_GET['age']);
            $which[] = sprintf("`age` BETWEEN %d AND %d", (int)$age[0], (int)$age[1]);

        } else {
            $which[] = sprintf("`age` %s", $_GET['age']);
        }
    }

    // Check whether one or more where clauses exist
    if($where) {
        $sql .= 'WHERE ' . implode('AND ', $where);
    }

    $result = mysql_query($sql); 
?>

Thanks sir i have used your code but i got 2 error
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in..
Warning: split() [function.split]: REG_EMPTY in

While retrieving the datas a got all the datas in the database for example:when i click female and age[18 to 24] in display i got all the datas from the database.


sir sorry for troubling you again and again but i have no other choice.
Please help me sir

Thanks a lot

And so I ask again:

What does your code look like at the moment with something similar included?

sir, i don't understand what you are saying

Your error messages are useless without source code and line numbers for reference. So, repost your code with the search form integrated and post the full error messages please.

<?php 
require_once('conn.php');
if($_GET)
 {
    $sql = "SELECT * FROM personal ";
    $where = array();
 
    // Check whether gender selected
   if(isset($_GET['gender']) && in_array($_GET['gender'], array('M ', 'F')))
	{
        $where[] = sprintf("'gender' = '%s'", $_GET['gender']);
    }
 
    // Check whether age range selected
    if(isset($_GET['age'])) {
        if(strpos($_GET['age'], '|')) {
            $delimiters = split('|', $_GET['age']);
            $which[] = sprintf("'age' BETWEEN %d AND %d", (int)$age[0], (int)$age[1]);
 
        } else {
            $which[] = sprintf("'age' %s", $_GET['age']);
        }
    }
 
    // Check whether one or more where clauses exist
    if($where) {
        $sql = 'WHERE ' . implode('AND ', $where);
    }
 
    $result = mysql_query("$sql") ;
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form method="GET" action="">
    <p>
        <label>Gender</label>
        <input type="radio" name="gender" value="M" /> Male
        <input type="radio" name="gender" value="F" /> Female
    </p>
 
    <p>
        <label>Age</label>
        <select name="age">
            <option value="< 18">Under 18</option>
            <option value="18|24">18 to 24</option>
            <option value="25|40">25 to 40</option>
            <option value="> 40">Over 40</option>
        </select>
    </p>
 
    <p>
        <input type="submit" name="filter" value="Filter" />
    </p>
</form>
 
<?php
echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>username</th> <th>Age</th><th>Gender</th><th>resitown</th><th>nativetown</th><th>qualify</th><th>jobdetail</th><th>income</th><th>disability</th><th>maritalstatus</th><th>emailid</th><th>phoneno</th><th>mobile</th><th>address</th></tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
	// Print out the contents of each row into a table
	echo "<tr><td>"; 
	echo $row['name'];
	echo "</td><td>"; 
	echo $row['username'];
	echo "</td><td>";
	echo $row['age'];
	echo "</td><td>";
	echo $row['gender'];
	echo "</td><td>";
	echo $row['resitown'] ;
	echo "</td><td>";
	echo $row['nativetown'];
	echo "</td><td>";
	
	echo $row['qualify'] ;
	echo "</td><td>";

	
	echo $row['jobdetails'] ;
	echo "</td><td>";
    echo $row['income'] ;
	echo "</td><td>";
	echo $row['disability'] ;
	echo "</td><td>";
	echo $row['maritalstatus'] ;
	echo "</td><td>";
	echo $row['emailid'] ;
	echo "</td><td>";
	echo $row['phoneno'] ;
	echo "</td><td>";
	echo $row['mobile'] ;
	echo "</td><td>";
	echo $row['address'] ;
	echo "</td></tr>";
	
	
} 

echo "</table>";
?>
 
</body>
</html>

This is the code i am using
the error messages are

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\xampp\htdocs\Vanniyar\tes.php on line 69

Warning: split() [function.split]: REG_EMPTY in C:\xampp\htdocs\Vanniyar\tes.php on line 17

Thank you

I have amended the code a little.

Firstly, I have removed the check for $_GET. This should mean the $result variable is always populated and in scope.

Secondly, I have corrected another error with my code on line 17. The variable $delimiters has been replaced with $age.

Finally, I have wrapped the while loop in an if statement to check that rows were actually returned before attempting to loop over them.

Let me know how you get one...
R

<?php 
require_once('conn.php');

$sql = "SELECT * FROM personal ";
$where = array();
 
// Check whether gender selected
if(isset($_GET['gender']) && in_array($_GET['gender'], array('M ', 'F'))) {
    $where[] = sprintf("'gender' = '%s'", $_GET['gender']);
}
 
// Check whether age range selected
if(isset($_GET['age'])) {
    if(strpos($_GET['age'], '|')) {
        $age = explode('|', $_GET['age']);
        $which[] = sprintf("'age' BETWEEN %d AND %d", (int)$age[0], (int)$age[1]);
 
    } else {
        $which[] = sprintf("'age' %s", $_GET['age']);
    }
}
 
// Check whether one or more where clauses exist
if($where) {
    $sql = 'WHERE ' . implode('AND ', $where);
}
 
$result = mysql_query($sql) ;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form method="GET" action="">
    <p>
        <label>Gender</label>
        <input type="radio" name="gender" value="M" /> Male
        <input type="radio" name="gender" value="F" /> Female
    </p>
 
    <p>
        <label>Age</label>
        <select name="age">
            <option value="< 18">Under 18</option>
            <option value="18|24">18 to 24</option>
            <option value="25|40">25 to 40</option>
            <option value="> 40">Over 40</option>
        </select>
    </p>
 
    <p>
        <input type="submit" name="filter" value="Filter" />
    </p>
</form>
 
<table border='1'>
    <tr>
        <th>Name</th>
        <th>username</th>
        <th>Age</th>
        <th>Gender</th>
        <th>resitown</th>
        <th>nativetown</th>
        <th>qualify</th>
        <th>jobdetail</th>
        <th>income</th>
        <th>disability</th>
        <th>maritalstatus</th>
        <th>emailid</th>
        <th>phoneno</th>
        <th>mobile</th>
        <th>address</th>
     </tr>

<?php if(0 < mysql_num_rows($result)): ?>
    <?php while(($row = mysql_fetch_array($result))): ?>
        <tr>
            <td><?php echo $row['name']; ?></td>
            <td><?php echo $row['username']; ?></td>
            <td><?php echo $row['age']; ?></td>
            <td><?php echo $row['gender']; ?></td>
            <td><?php echo $row['resitown']; ?></td>
            <td><?php echo $row['nativetown']; ?></td>
            <td><?php echo $row['qualify']; ?></td>
            <td><?php echo $row['jobdetails']; ?></td>
            <td><?php echo $row['income']; ?></td>
            <td><?php echo $row['disability']; ?></td>
            <td><?php echo $row['maritalstatus']; ?></td>
            <td><?php echo $row['emailid']; ?></td>
            <td><?php echo $row['phoneno']; ?></td>
            <td><?php echo $row['mobile']; ?></td>
            <td><?php echo $row['address']; ?></td>
        </tr>
    <?php endwhile; ?>
<?php else: ?>
    <tr>
        <td colspan="15">No results matched your search criteria</td>
    </tr>	
<?php endif; ?>

</table> 
</body>
</html>

Thanks sir now it retrieves all the datas from the database for example:when i click female and age[18 to 24] in display i got all the datas from the database(including male and other ages).


Thanks for your help sir

You've changed the backticks to quotes in the SQL query. Change them all back to backticks...

$where[] = sprintf("`gender` = '%s'", $_GET['gender']);

R

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.