Skip to content

Instantly share code, notes, and snippets.

@sunlee-newyork
Created November 16, 2013 21:30
Show Gist options
  • Save sunlee-newyork/7505604 to your computer and use it in GitHub Desktop.
Save sunlee-newyork/7505604 to your computer and use it in GitHub Desktop.
Website NONAME / SEARCH
<html>
<head>
<title>SEARCH</title>
<?php require $_SERVER["DOCUMENT_ROOT"]."/IDEA/header/index.php"; ?>
<style>
#title {
font-family:HelveticaNeue-Light;
font-size:35px;
text-align:center;
margin-top:100px;
}
#form {
width:600px;
height:24px;
margin-top:50px;
margin-left:auto;
margin-right:auto;
}
#search {
width:87%;
margin-left:auto;
margin-right:auto;
font-family:HelveticaNeue-Light;
font-size:14px;
}
#submit_button {
font-size:20px;
margin-left:10px;
}
#signup {
margin-left:245px;
font-family:HelveticaNeue-Light;
font-size:16px;
}
#login {
margin-left:5px;
font-family:HelveticaNeue-Light;
font-size:16px;
}
</style>
</head>
<body>
<div id="title">SEARCH</div>
<div id="form">
<form action="index.php" method="post">
<p>
<input id="search" name="search" type="text" value="" />
<input id="submit_button" type="submit" value="Search" />
<br><br><br>
<a href="/IDEA/signup/index.php" id="signup">Sign Up</a>
<a href="/IDEA/login/index.php" id="login">Log In</a>
</p>
</form>
</div>
<?php
// === RANKING FORMULA === \\
// Assign points (Default, Title, Description, Tag)
$value_default = 1;
$value_title = 6;
$value_descript = 2;
$value_tag = 4;
// Recent upload rank value formula [For each 24hrs of $current_time - $row["timestamp"], divide value by 1.5]
function count24hrCycles($logged_time) {
// Set current time
$current_time = strtotime(date("Y-m-d H:i:s"));
// Calculate difference in seconds between current time and "logged time"
$time_diff = $current_time - $logged_time;
// How many 24hr cycles does result have?
$value_recent = round($time_diff / 86400);
return $value_recent;
}
// === SEARCH AND RANK === \\
if ($_POST) {
$search = $_POST["search"];
$query = mysql_query (
"SELECT * FROM `idea_cards` WHERE
`owner` LIKE '%$search%' OR
`title` LIKE '%$search%' OR
`description` LIKE '%$search%' OR
`tag` LIKE '%$search%';"
);
if (!$query) {
echo "Query error: " .mysql_error(). "</br>";
}
echo "</br>";
// Make your "points" variable local in this while loop so that each "whiling" overwrites the variable
while ($row = mysql_fetch_assoc($query)) {
$points = 0;
// Calculate number of 24hr
$unix_loggedTime = strtotime($row["timestamp"]);
$no_24hrs_since_log = count24hrCycles($unix_loggedTime);
// Add points for hits. For multiple hits, calculate whether it is very relevant or stuffed
// Title points
if (strpos($row["title"], $search) !== false) {
$points = $points + $value_title;
// Stuffing filter
if (substr_count($row["title"], $search) > 1 && substr_count($row["title"], $search) < 3) {
$points = $points + 2;
}
elseif (substr_count($row["title"], $search) > 2) {
$points = $points - 1000000;
}
}
// Description points
if (strpos($row["description"], $search) !== false) {
$points = $points + $value_descript;
// Stuffing filter
if (substr_count($row["description"], $search) > 1 && substr_count($row["description"], $search) < 4) {
$points = $points + 5;
}
elseif (substr_count($row["description"], $search) >= 4) {
$points = $points - 1000000;
}
}
// Tag points
if (strpos($row["tag"], $search) !== false) {
$points = $points + $value_tag;
// Stuffing filter
if (substr_count($row["tag"], $search) > 1) {
$points = $points - 1000000;
} else {
$points = $points + 3;
}
}
// Calculate rank value (including number of 24hrs)
$points = $points + $value_default - $no_24hrs_since_log;
// Make array to hold results (placed here to make local to while loop?)
$array_row = [
"owner" => strval($row["owner" ]),
"title" => strval($row["title" ]),
"description" => strval($row["description"]),
"tag" => strval($row["tag" ]),
"id" => strval($row["id" ]),
"timestamp" => strval($row["timestamp" ]),
"24hrs" => strval($no_24hrs_since_log)
];
// Push to array, to hold both points and corresponding results
$array_rank[$points] = $array_row;
}
// Sort & rank results
krsort($array_rank);
// Display matching results
foreach ($array_rank as $points => $array_row) {
echo "<h3>Matching result:</h3>";
echo "User: " .$array_row["owner" ]. "</br>";
echo "Title: " .$array_row["title" ]. "</br>";
echo "Description: " .$array_row["description"]. "</br>";
echo "Tag: " .$array_row["tag" ]. "</br>";
echo "ID: " .$array_row["id" ]. "</br>";
echo "Timestamp: " .$array_row["timestamp" ]. "</br>";
echo "-------------------------------------------" . "</br>";
echo "Number of 24hr cycles: " .$array_row["24hrs" ]. "</br>";
echo "<b>Total points: </b>" .$points. "</br></br>";
echo "================================================================";
}
if (mysql_num_rows($query) === 0) {
echo "</br></br>No results found for that search term.";
}
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment