Jump to content

MySQL Search


Drezard

Recommended Posts

Hello, I am trying to create a mysql search application. Basically I have this much of it:

 

if (!isset($_POST['submit'])) {
  
   echo "<form action='' method='post'>
	 Search: <input name='search' type='text' maxvalue='32'> <br>
	 <input type='submit' name='submit'>
         </form>";
  
  }
  
  if (isset($_POST['submit'])) {
  
   if (!isset($_POST['search']) || trim($_POST['search']) == '') {
    echo "Please enter a search type <br>";
   }
   
   if (isset($_POST['search']) || trim($_POST['search'])!= '') {
    
    $search = mysql_escape_string($_POST['search']);
    $min = 0;
    $max = 20;

$query = "SELECT username FROM user_ids WHERE username LIKE '%$search%' LIMIT '$min', '$max'";	

$result = mysql_query($query);

if (mysql_num_rows($result) > 0) { 

     echo "<table border=1 width='100%'>"; 

     while(list($username)  = mysql_fetch_row($result)) { 
          echo "<tr>"; 
          echo "<td>$username</td>"; 
          echo "<td><a href='char_viewuser?username=$username'>View</a></td>";
          echo "</tr>"; 
    }
  
     echo "</table>";
     echo "<a href=' '>Next Page</a><br>";
  
    } 
    else {

     echo "No rows found!";
  
    }

   }
  
  } 

 

Now, in another part of the script i connect to the database and all of that. Now, how would I make it so that if i click the "Next Page" Link at the bottom it would change the $min and $max variable to 21 and 40 and then display results? (Not display the form again).

 

So, basically I want to create a way of searching my database for all the usernames with the search in there name and then displaying it. BUT... I also want to add a feature for the next page bit (so i can show more then 1 - 20 results on another page).

 

How could i do this?

 

- cheers, Daniel

 

 

Link to comment
https://forums.phpfreaks.com/topic/45847-mysql-search/
Share on other sites

Thanks, Okay... I got most of my application done (I look at pagination and it looked a bit advanced for me...)

 

So heres my code:

 

function user_search() {

  if (!isset($_POST['submit'])) {
  
   echo "<form action='' method='post'>
	 Search: <input name='search' type='text' maxvalue='32'> <br>
	 <input type='submit' name='submit'>
         </form>";
  
  }
  
  if (isset($_POST['submit'])) {
   
   if (!isset($_POST['search']) || trim($_POST['search']) == '') {
    echo "Please enter a search type <br>";
   }
   
   if (isset($_POST['search']) || trim($_POST['search']) != '') {
    
$search = mysql_escape_string($_POST['search']);

$_SESSION['search'] = "$search";

echo "<meta http-equiv='refresh' content=0;url='user_results.php?page=1'>";

   }
  
  }
  
}
  
function user_results() {

   
  if (!isset($_SESSION['search']) || !isset($_GET['page'])) {
   echo "<meta http-equiv='refresh' content=0;url='user_search.php'>"; 
  }
  
  if (isset($_SESSION['search']) || isset($_GET['page'])) {

   $searcharray = explode(' ', $_SESSION['search']);
   $search = $searcharray[0];
   $page = mysql_escape_string($_GET['page']);
   $page_start = $page * 20;
   $page_end = $page_start + 20;

   $query = "SELECT username FROM user_ids WHERE username LIKE '%$search%' LIMIT '$page_start', '$page_end'";	

   $result = mysql_query($query);

   if (mysql_num_rows($result) > 0) { 

    echo "<table border=1 width='100%'>"; 

    while(list($username)  = mysql_fetch_row($result)) { 
          echo "<tr>"; 
          echo "<td>$username</td>"; 
          echo "<td><a href='char_viewuser?username=$username'>View</a></td>";
          echo "</tr>"; 
    }
  
    echo "</table>";

if ($page != 1) {

 echo "<a href='user_results.php?page=$page-1'>Last page</a>";

}

echo "<a href='user_results.php?page=$page+1'>Next page</a>";
  
   } 
   else {

     echo "No rows found!";

   }

  }

}

 

Now, it connects and such in other parts of the class. So basically if you can't tell, theres two files, user_search.php is the place where the search form is keeped and then it redirects to user_results.php once it has been completed...

 

BUT... it doesn't set $_SESSION['search']. I think its because I have already declared another session ($_SESSION['userinfo']) in the class...

 

Now what other ways (I thought about $_POST but it seems risky, because of Sql insertion and such....

 

What else could i do?

 

- Cheers, Daniel

Link to comment
https://forums.phpfreaks.com/topic/45847-mysql-search/#findComment-222831
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.