Skip to main content

ID Generator

0 likes • Nov 18, 2022 • 0 views
PHP
Loading...

More PHP Posts

Generate 64K edges

0 likes • Nov 18, 2022 • 2 views
PHP
<?php
header('Content-Type:text/html');
$jsonData = json_decode(file_get_contents("names.json"));
$namesArray = array();
foreach($jsonData as $data) {
$namesArray[] = $data;
}
for($i = 0; $i < 64000; $i++) {
echo 'D.add_edge("'. $namesArray[array_rand($namesArray)] . '", "' . $namesArray[array_rand($namesArray)]. '")';
echo "<br>";
}
?>

Search Function Display

0 likes • Nov 18, 2022 • 0 views
PHP
<?php
session_start();
if(isset($_POST['search-submit'])) {
$currentFilter;
if(isset($_POST['search-filter']) && $_POST['search-filter'] == "search-all") {
$currentFilter = "All";
} else if(isset($_POST['search-filter']) && $_POST['search-filter'] == "search-by-title") {
$currentFilter = "Title";
} else if(isset($_POST['search-filter']) && $_POST['search-filter'] == "search-by-language") {
$currentFilter = "Language";
} else {
$currentFilter = "Username";
}
}
if(isset($_POST['search-submit']) && $_POST['search'] !== "") {
require_once 'includes/config.php';
$searchQuery = '%' . htmlspecialchars($_POST['search']) . '%';
$statement;
if(htmlspecialchars($_POST['search-filter']) == "search-all") {
$statement = $conn->prepare("SELECT * FROM user_posts WHERE privacy = 0 AND title LIKE ? OR username LIKE ? OR language LIKE ?");
$statement->bind_param("sss", $searchQuery, $searchQuery, $searchQuery);
} else if(htmlspecialchars($_POST['search-filter']) == "search-by-title") {
$statement = $conn->prepare("SELECT * FROM user_posts WHERE privacy = 0 AND title LIKE ?");
$statement->bind_param("s", $searchQuery);
} else if(htmlspecialchars($_POST['search-filter']) == "search-by-language") {
$statement = $conn->prepare("SELECT * FROM user_posts WHERE privacy = 0 AND language LIKE ?");
$statement->bind_param("s", $searchQuery);
} else {
$statement = $conn->prepare("SELECT * FROM user_posts WHERE privacy = 0 AND username LIKE ?");
$statement->bind_param("s", $searchQuery);
}
$statement->execute();
$result = $statement->get_result();
}
?>
<?php
if(isset($_POST['search-submit']) && $_POST['search'] !== "") {
if($result->num_rows > 0) {
$i = 1;
echo '<ul class="list-group mt-5">';
while($row = $result->fetch_assoc()) {
$linkName = "post.php?postID=" . $row['postID'];
$privacy = ($row['privacy'] == 0) ? "Public" : "Private";
if($privacy == "Public" || ($privacy == "Private" && (isset($_SESSION['username']) && ($row['username'] == $_SESSION["username"])))) {
echo '
<a class="post-box-link" href="' . $linkName . '">
<li class="list-group-item mb-2 border rounded post-box">
<h4><span style="color: #007bff;">' . $row['title'] . '</span> | ' . $row['language'] . '</h4>
<h5>' . $row['username'] . ' - ' . '<small>' . $row['uploadDate'] . '</small></h5>
</li>
</a>
';
$i++;
}
}
echo '</ul>';
}
}
?>

Get File Extension

0 likes • Nov 19, 2022 • 0 views
PHP
<?php
function getFileExtension($file) {
return end(explode(".", $file));
}
?>

Access Associative Array

0 likes • Nov 18, 2022 • 1 view
PHP
<?php
$array = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'apple',
'fruit5' => 'apple');
// this cycle echoes all associative array
// key where value equals "apple"
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple') {
echo key($array), "\n";
}
next($array);
}
?>

Parse JSON

0 likes • Nov 19, 2022 • 0 views
PHP
<?php
$json ='{"id":1,"name":"foo","interest":["wordpress","php"]}';
$obj=json_decode($json);
echo $obj->interest[1]; //prints php
?>

Make Random Number

0 likes • Nov 19, 2022 • 0 views
PHP
<?php
function getRandomId($min = NULL, $max = NULL) {
if (is_numeric($min) && is_numeric($max)) {
return mt_rand($min, $max);
}
else {
return mt_rand();
}
}
?>