Jump to content

Recommended Posts

(Like is the hardest MySQL syntax/code/whatever...XD)

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>RuneScape Servers - Search</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="container">
<div id="header"><h1>RuneScape Servers - Search</h1></div>
  <div id="wrapper">
    <div id="content">
      <p><strong>Search</strong></p>
     <p><?php 

   $connect = mysql_connect("$mhost","$muser","$mpassword") or die("Connection failed!");
   mysql_select_db("$mdatabase") or die("Database fail!");

    $search = $_GET['keywords'];
    $type = $_GET['category'];

    $search = mysql_real_escape_string($search);
    $type = mysql_real_escape_string($type);

    if (!$search || !$type) {
    echo "<b>(Note: enter in all information that has a * by it)</b><br>*Enter in some keywords: <form action='search.php' method='GET'><input type='text' name='keywords'><br><input type='radio' name='category' value='Non-project server' />*Non-project<br><input type='radio' name='category' value='Project server'>*Project<br><input type='submit'></form>";
    } else {
    echo "Viewing results for <b>".$search  ."</b> for a <b>". $type ."</b> server<br>";

//max displayed per page
$per_page = 10;

//get start variable
$start = $_GET['start'];

//count records
$record_count = mysql_num_rows(mysql_query("SELECT * FROM tables"));

//count max pages
$max_pages = $record_count / $per_page; //may come out as decimal

if (!$start)
   $start = 0;
   
//display data
$get = mysql_query("SELECT * FROM tables WHERE category='$type' ORDER BY likes DESC LIMIT $start, $per_page");
while ($row = mysql_fetch_assoc($get))

{
echo "<br><b>". $row['servername'] ." (". $row['category'] .") created by ". $row['serverowner'] ." (". $row[views] ." views & ". $row[likes] ." likes!)</b><br>";
}

//setup prev and next variables
$prev = $start - $per_page;
$next = $start + $per_page;

//show prev button
if (!($start<=0))
       echo "<a href='index.php?start=$prev'>Prev</a> ";

//show page numbers

//set variable for first page
$i=1;

for ($x=0;$x<$record_count;$x=$x+$per_page)
{
if ($start!=$x)
    echo " <a href='index.php?start=$x'>$i</a> ";
else
    echo " <a href='index.php?start=$x'><b>$i</b></a> ";
$i++;
}

//show next button
if (!($start>=$record_count-$per_page))
       echo " <a href='index.php?start=$next'>Next</a>";

    }

    ?></p>
    </div>
  </div>
  <div id="navigation">
    <p><strong>Menu</strong></p>
    <ul>
      <li><a href="search.php">Search</a></li>
      <li><a href="add.php">Add a project</a></li>
    </ul>
  </div>
  <div id="extra">
    <p><strong>Read me!</strong></p>
    <p>The search page only shows 10 results per page!</p>
  </div>
  <div id="footer">
    <p>Happy searching</p>
  </div>
</div>
</body>
</html>

 

In my code, how would I make it so $search could be used as keywords to search a name of a server LIKE his inputed keywords?

Link to comment
https://forums.phpfreaks.com/topic/200347-better-searching-using-like/
Share on other sites

In my code, how would I make it so $search could be used as keywords to search a name of a server

 

Insert it into your query in the right place.

 

A better question is why on earth are you querying the same database table twice in the same script?

 

What do you mean? They are both used for different things?

 

And, what you said doesn't help me.......at all. :/

LIKE in Mysql is an operator, much like =. so if you want to use it then... well... use it. just replace the = with Like, for example

SELECT column FROM table WHERE somecolumn LIKE 'someValue'

 

also, the bit about 2 queries to the same table, it would be a lot more efficient if instead of preforming 2 queries, just preform one, store the data in an array, and then use that array

Here is my code:

 

//display data
$get = mysql_query("SELECT * FROM tables WHERE category='$type' AND servername LIKE '$search' ORDER BY likes DESC LIMIT $start, $per_page");
while ($row = mysql_fetch_assoc($get))

 

It works, but you have to type in the EXACT name of the server for it to show. :/

try using wildcards with like

servername LIKE '%$search%'

 

which will match a name with $search in it. (like if you typed myserv, it would match myserver, but NOTserver.)

 

but like mchl said, it sounds like you are looking for a more comprehensive string match, like regex

try using wildcards with like

servername LIKE '%$search%'

 

which will match a name with $search in it. (like if you typed myserv, it would match myserver, but NOTserver.)

 

but like mchl said, it sounds like you are looking for a more comprehensive string match, like regex

 

THANK YOU, IT WORKED!

 

WOOO-HOO! :D

 

Thank you! And thank you everyone else. ;)

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.