Jump to content

can anyone help me create search script?


Go to solution Solved by QuickOldCar,

Recommended Posts

Hello,can anyone help me create members search script?

here is image example

 

g3cc6decddgqmu8f5s.png

 

here is 'users 'table


  `id` int(11) NOT NULL auto_increment,
  `age` varchar(200) collate utf8_unicode_ci NOT NULL,
  `city` varchar(200) collate utf8_unicode_ci NOT NULL,
  `username` varchar(30) collate utf8_unicode_ci NOT NULL,
  `password` varchar(150) collate utf8_unicode_ci NOT NULL,
  `salt` varchar(10) collate utf8_unicode_ci NOT NULL,
  `mgroup` int(3) NOT NULL,
  `email` varchar(100) collate utf8_unicode_ci NOT NULL,
  `ip` varchar(15) collate utf8_unicode_ci NOT NULL,
  `posts` int(11) NOT NULL,
  `date_joined` varchar(25) collate utf8_unicode_ci NOT NULL,
  `logged_in` int(1) NOT NULL default '0',
  `last_action_time` int(25) NOT NULL,
  `signature` text collate utf8_unicode_ci NOT NULL,
  `avatar` varchar(300) collate utf8_unicode_ci NOT NULL default 'default',
  `what_is_doing` varchar(30) collate utf8_unicode_ci NOT NULL,
  `what_is_doing_id` varchar(100) collate utf8_unicode_ci NOT NULL,
  `name` varchar(200) collate utf8_unicode_ci NOT NULL,
  `website` varchar(200) collate utf8_unicode_ci NOT NULL,
  `title` varchar(150) collate utf8_unicode_ci NOT NULL,
  `rank_pips` int(11) NOT NULL,
  `friends` tinytext collate utf8_unicode_ci NOT NULL,
  `blocks` tinytext collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

Please help me?  :)

Edited by sopranolv
Link to comment
https://forums.phpfreaks.com/topic/294264-can-anyone-help-me-create-search-script/
Share on other sites

The purpose of this forum is to help people with their existing code, not create it.

Do you have any code right now?

 

You need retrieve the POST or GET data from that form

connect to the database

do an sql query using WHERE and BETWEEN using the POST dynamic values

return the results in a while loop

 

http://php.net/manual/en/book.pdo.php

 

http://php.net/manual/en/book.mysqli.php

http://php.net/manual/en/mysqli-result.fetch-assoc.php

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between

  • Solution

I had a little time to spare

Edit to your database credentials and should see it working

<?php
//POST minimum age
if (isset($_POST['minage']) && ctype_digit(trim($_POST['minage']))) {
    $minage = trim($_POST['minage']);
} else {
    $minage = "1"; //default    
}

//POST maximum age
if (isset($_POST['maxage']) && ctype_digit(trim($_POST['maxage']))) {
    $maxage = trim($_POST['maxage']);
} else {
    $maxage = "120"; //default    
}

//POST city if set
if (isset($_POST['city']) && trim($_POST['city']) != '') {
    $city = trim($_POST['city']);
} else {
    $city = '';
}
?>
<form action="" method="POST">
Age from <input type="text" name="minage" value="<?php echo $minage;?>" size="3">
 to <input type="text" name="maxage" value="<?php echo $maxage;?>" size="3"><br />
City <input type="text" name="city" value="<?php echo $city;?>">
<input type="submit" name="submit" value="Find User">
</form>
<?php
if (isset($_POST['submit'])) {
    //mysql connection
    $link = mysqli_connect("localhost", "database_user", "database_password", "database_name");//edit to your information
    
    //check connection
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    
    //build query
    $query = "SELECT username FROM users WHERE (age BETWEEN $minage AND $maxage)";
    
    if ($city != '') {
        $query .= " AND city='" . mysqli_real_escape_string($link, $city) . "'";
    }
    
    //see query
    //echo $query;
    
    //fetch results
    if ($result = mysqli_query($link, $query)) {
        
        //check if is no results
        if (mysqli_num_rows($result) <= 0) {
            echo "No Results";
        } else {
            //fetch associative array
            while ($row = mysqli_fetch_assoc($result)) {
                echo $row["username"] . "<br />";
            }
        }
        //free result set
        mysqli_free_result($result);
    }
    
    //close connection
    mysqli_close($link);
}
?> 
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.