我想将这段代码简化为函数,也许只是在简单的情况下,它仅替换“字符串”中的单词。我不想要您可能知道如何缩短10,000行的代码。
我还想对我想在PHP
和MySQL
当然,该代码有效,但是您可以看到它占用了多少空间。
我需要缩短settings.php
中的代码
dbc.php
<?php
$DB_SERVER = 'localhost';
$DB_USERNAME = 'XXXX';
$DB_PASSWORD = 'XXXX';
$dev_db = mysqli_connect($DB_SERVER,$DB_USERNAME,$DB_PASSWORD,'sf');
$easy_db = mysqli_connect($DB_SERVER,$DB_USERNAME,$DB_PASSWORD,'easy');
$medium_db = mysqli_connect($DB_SERVER,$DB_USERNAME,$DB_PASSWORD,'medium');
$hard_db = mysqli_connect($DB_SERVER,$DB_USERNAME,$DB_PASSWORD,'hard');
settings.php
<?php
ini_set("display_errors", 0);
include'dbc.php';
//Fix Znaków
function fixSpecialChars($text)
{
$text = str_replace('/', '$s', $text);
$text = str_replace('http://', 'http:$c$s$', $text);
$text = str_replace('https://', 'https:$c$s$', $text);
$text = str_replace('%40', '@', $text);
return $text;
}
//Czas
$dt = date_timestamp_get(date_create()) - 1800;
$dt24 = date_timestamp_get(date_create()) - 86400;
//Aktywni dev
$dev_res = mysqli_query($dev_db, "SELECT ID, name, usysclass, banned FROM players WHERE poll >= $dt AND poll <= NOW() ORDER BY poll DESC");
$dev_res24 = mysqli_query($dev_db, "SELECT ID, name, usysclass, banned FROM players WHERE poll >= $dt24 AND poll <= NOW() ORDER BY poll DESC");
$dev_uno = mysqli_num_rows($dev_res);
$dev_uno24 = mysqli_num_rows($dev_res24);
$dev_user_counting = mysqli_query($dev_db, "SELECT count(*) as total_count from players");
$dev_user_count = mysqli_fetch_object($dev_user_counting);
$dev_all_acount = $dev_user_count->total_count;
$dev_all = $dev_all_acount + 0;
//Aktywni easy
$easy_res = mysqli_query($easy_db, "SELECT ID, name, usysclass, banned FROM players WHERE poll >= $dt AND poll <= NOW() ORDER BY poll DESC");
$easy_res24 = mysqli_query($easy_db, "SELECT ID, name, usysclass, banned FROM players WHERE poll >= $dt24 AND poll <= NOW() ORDER BY poll DESC");
$easy_uno = mysqli_num_rows($easy_res);
$easy_uno24 = mysqli_num_rows($easy_res24);
$easy_user_counting = mysqli_query($easy_db, "SELECT count(*) as total_count from players");
$easy_user_count = mysqli_fetch_object($easy_user_counting);
$easy_all_acount = $easy_user_count->total_count;
$easy_all = $easy_all_acount + 0;
//Aktywni medium
$medium_res = mysqli_query($medium_db, "SELECT ID, name, usysclass, banned FROM players WHERE poll >= $dt AND poll <= NOW() ORDER BY poll DESC");
$medium_res24 = mysqli_query($medium_db, "SELECT ID, name, usysclass, banned FROM players WHERE poll >= $dt24 AND poll <= NOW() ORDER BY poll DESC");
$medium_uno = mysqli_num_rows($medium_res);
$medium_uno24 = mysqli_num_rows($medium_res24);
$medium_user_counting = mysqli_query($medium_db, "SELECT count(*) as total_count from players");
$medium_user_count = mysqli_fetch_object($medium_user_counting);
$medium_all_acount = $medium_user_count->total_count;
$medium_all = $medium_all_acount + 0;
//Aktywni hard
$hard_res = mysqli_query($hard_db, "SELECT ID, name, usysclass, banned FROM players WHERE poll >= $dt AND poll <= NOW() ORDER BY poll DESC");
$hard_res24 = mysqli_query($hard_db, "SELECT ID, name, usysclass, banned FROM players WHERE poll >= $dt24 AND poll <= NOW() ORDER BY poll DESC");
$hard_uno = mysqli_num_rows($hard_res);
$hard_uno24 = mysqli_num_rows($hard_res24);
$hard_user_counting = mysqli_query($hard_db, "SELECT count(*) as total_count from players");
$hard_user_count = mysqli_fetch_object($hard_user_counting);
$hard_all_acount = $hard_user_count->total_count;
$hard_all = $hard_all_acount + 0;
home.php(我的页面)
<?php
include 'main.php';
checkLoggedIn();
head("Główna");
?>
<div class="content">
<h2>Home Page</h2>
<p>Welcome back, <?= $_SESSION['name'] ?>!</p>
</div>
<?php include 'settings.php' ?>
<table style="width: 100%;" border="0">
<tbody>
<tr>
<td style="vertical-align: top; padding-left: 0px;">
<div class="content"><h2 style="text-align: center"><span>Wybierz serwer:</span></h2></div>
<p class="news">
<div id="sfmenu">
<div class="btn2_main">
<a href="http://sf.sgsie.pl/dev/">
<div class="sfbt">
Beta
</div>
</a>
</div>
</div>
<div id="sfmenu">
<div class="btn2_main">
<a href="X.php">
<div class="sfbt">
Easy
</div>
</a>
</div>
<div class="btn2_main">
<a href="X.php">
<div class="sfbt">
Medium
</div>
</a>
</div>
<div class="btn2_main">
<a href="X.php">
<div class="sfbt">
Hard
</div>
</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
<table style="width: 100%;" border="0">
<tbody>
<tr>
<td style="vertical-align: top; padding-left: 0px;">
<div class="content"><h2 style="text-align: center"><span>Aktywni Gracze:</span></h2></div>
<br>
<p class="news">
<table align="center" border="0" cellpadding="4" cellspacing="1" class="tableinborder" summary="none"
width="600px">
<tr>
<td align="left"><b><span style="color: #33cc00; font-size: small; ">Graczy online:</span>
<br>
<span>Dev: </span><span style="color: red; font-size: small; "><?= $dev_uno ?><span
style="color: #33cc00; font-size: small; "> / </span><?= $dev_all ?></span></b>
<br>
<span>Easy: </span><span style="color: red; font-size: small; "><?= $easy_uno ?><span
style="color: #33cc00; font-size: small; "> / </span><?= $easy_all ?></span></b>
<br>
<span>Medium: </span><span style="color: red; font-size: small; "><?= $medium_uno ?><span
style="color: #33cc00; font-size: small; "> / </span><?= $medium_all ?></span></b>
<br>
<span>Hard: </span><span style="color: red; font-size: small; "><?= $hard_uno ?><span
style="color: #33cc00; font-size: small; "> / </span><?= $hard_all ?></span></b>
</td>
</tr>
</table>
<br>
<br>
<table align="center" border="0" cellpadding="4" cellspacing="1" class="tableinborder" summary="none"
width="600px">
<tr>
<td align="left"><b><span style="color: #33cc00; font-size: small; ">Graczy online (24h):</span>
<br>
<span>Dev: </span><span style="color: red; font-size: small; "><?= $dev_uno24 ?><span
style="color: #33cc00; font-size: small; "> / </span><?= $dev_all ?></span></b>
<br>
<span>Easy: </span><span style="color: red; font-size: small; "><?= $easy_uno24 ?><span
style="color: #33cc00; font-size: small; "> / </span><?= $easy_all ?></span></b>
<br>
<span>Medium: </span><span style="color: red; font-size: small; "><?= $medium_uno24 ?><span
style="color: #33cc00; font-size: small; "> / </span><?= $medium_all ?></span></b>
<br>
<span>Hard: </span><span style="color: red; font-size: small; "><?= $hard_uno24 ?><span
style="color: #33cc00; font-size: small; "> / </span><?= $hard_all ?></span></b>
</td>
</tr>
</table>
<br>
<br>
</td>
</tr>
</tbody>
</table>
<?php
foot();
?>
答案 0 :(得分:0)
您可以这样创建函数。
import { configure } from '@storybook/react';
// automatically import all files ending in *.stories.js
configure(require.context('../stories', true, /\.stories\.(js|mdx)$/), module);
这个小功能将使您获得所有row_count的代码最小化,只需添加此方法并通过为其指定两个参数import '@storybook/addon-actions/register';
import '@storybook/addon-links/register';
import '@storybook/addon-docs/register';
import '@storybook/addon-knobs/register';
和module.exports = ['@storybook/addon-docs/react/preset'];
来调用此方法,connection是您的数据库连接变量,而query是query mysql。它将返回行数。