2012-05-15 82 views
-3

我想創建一個使用平面文件的評分系統(我知道,我更喜歡使用mysql)。我已經設法創建了足夠的變量,以便將變量從遊戲中移出(用戶名和分數)並放入所述文本文件中。我也可以用格式化的表格輸出這個文本文件。不過,我無法按照任何順序將分數排序 - 理想情況下,我需要降序排列。在搜索完網頁後,我能找到的最好的代碼是在附加的代碼中,但目前似乎並沒有做任何事情。我知道它在某個地方可能是一個愚蠢的錯誤,但我可以用一雙新鮮的眼睛來查看它。 非常感謝,哈利數字排序一個php .txt文件

<?php 
echo '<table width="5%" border="5"><tr><th>Your Score</th><th>Your Name</th></tr>'; 
echo '<tr><td>'.$_POST["m_score"].'</td><td>'.$_POST["name"].'</td></tr>'; 
echo '</table>'; 
echo "<br />"; 


$data=$_POST["m_score"].",".$_POST["name"].","; 
     $filename = "./highscores.txt"; 
     $handle = fopen($filename, "a"); 

     if (!$handle) 
     { 
      print "There were problems opening the file"; 
      exit(); 
     } 

     fwrite($handle, $data); 
     fclose($handle); 

     $handle = fopen($filename, "r"); 
     $datain = fread($handle, filesize($filename)); 
     $value = explode(",",$datain);   
     $row = floor(count($value)/2); 
     $count = 0; 


     $filename = file("./highscores.txt"); 
     sort($filename); 
     file_put_contents("./highscores.txt", implode($filename)); 


    echo '<table width="5%" border="1"><tr><th>Top Scores</th><th>Top Users</th></tr>'; 
    for ($i=0;$i<$row;$i++) 
    { 
     echo '<tr>'; 
     for($x=0;$x<2;$x++){ 
      echo "<td>$value[$count]</td>"; 
      $count++; 
     } 
     echo "</tr>"; 
    } 
    echo '</table>'; 

    fclose($handle); 

?> 
+0

你的代碼是開放的XSS以各種方式+您的使用只有1需要2 PARAM破滅,再加上通知不確定的變量,使'的error_reporting(E_ALL)'!爲什麼你不使用數據庫,即使只是sqlite? –

+0

Webbiedave,我已經複製的唯一代碼是排序($文件名)段,我從這個網站得到,但對態度的讚譽!真的讓我覺得值得註冊。 Lawrence,XSS在這段代碼中並不是一個大問題,它的意思是非常低的水平(因爲我敢肯定你從我的糟糕嘗試中猜到了)這件作品。我會很高興地使用另一個數據庫,事實上(令人討厭)最初使用mysql編寫它。但是,它必須使用平面文件方式編寫 - 這對我來說是首先。 – user1396515

回答

0

我疲憊不堪了一個例子,你可以看一下從,學習,如果你真的不爲具有一個完全成熟的MySQL服務器處理您的DATAS那麼這裏就是你可以用內置的做sqlite2函數,對於小數據非常簡單和有用,因爲任何類型的sql總是會更快地查詢並且更容易插入平面文件。希望它能幫助:

<?php 
//Create db or return connection 
$db = setup('scores.db'); 

//form so you can enter data 
echo '<h1>Put Score demo:</h1> 
<form method="POST" action=""> 
    <p>Name: <input type="text" name="name" size="20"></p> 
    <p>Score: <input type="text" name="m_score" size="20"><input type="submit" value="Submit"></p> 
</form>'; 
//Validate score as a number and the name is set 
if(isset($_POST['m_score']) && is_numeric($_POST['m_score']) && !empty($_POST['name'])){ 

    $name = $_POST['name']; 
    $m_score = $_POST['m_score']; 

    //Put result into database 
    database("INSERT INTO scores (id,name , m_score) VALUES (NULL,'".sqlite_escape($name)."',".(int)$m_score.")",'put',$db); 

    //Show users score 
    echo '<table width="5%" border="5"><tr><th>Your Score</th><th>Your Name</th></tr>'; 
    echo '<tr><td>'.htmlentities($m_score).'</td><td>'.htmlentities($name).'</td></tr>'; 
    echo '</table>'; 
    echo "<br />"; 

}else{ 
    //Get Results from database 
    $result = database("SELECT DISTINCT name, m_score from scores ORDER BY m_score DESC",'get',$db); 

    //Show global score 
    echo '<table width="5%" border="1"><tr><th>Top Scores</th><th>Top Users</th></tr>'; 
    foreach ($result as $row){ 
     echo '<tr>'; 
     echo "<td>".htmlentities($row['m_score'])."</td>"; 
     echo "<td>".htmlentities($row['name'])."</td>"; 
     echo "</tr>"; 
    } 
    echo '</table>'; 
} 

/** 
* Simple function to setup the sqlite database or return a connection to that db 
* 
* @param string $db_name 
* @return mixed (Connection or setup success) 
*/ 
function setup($db_name){ 
    global $db; 
    if(file_exists($db_name)){ 
     if(isset($db)){return $db;} 
     return sqlite_open($db_name, 0666, $error); 
    } 
    $db = sqlite_open($db_name, 0666, $error); 
    (!$db)?die($error):null; 
    //Create new db 
    $query ="CREATE TABLE scores (id INTEGER PRIMARY KEY, name TEXT(255), m_score INTEGER)"; 
    $ok = sqlite_exec($db, $query, $error); 
    (!$ok)?die("Cannot execute query. $error"):null; 

    echo 'Setup completed, Refresh page.'; 
    die; 
} 

/** 
* A simple sqlite CRUD with U & D missing ;p 
* 
* @param (sql query) $query 
* @param (put|get) $do 
* @param (Connection resource) $db 
* @return result 
*/ 
function database($query,$do,$db){ 
    $error=null; 
    $return=null; 
    switch($do){ 
     case "get": 
      $result = sqlite_query($db, $query, $error); 
      if (!$result) die("Cannot execute query {$query} ". $error); 
      $i=0; 
      while ($entry = sqlite_fetch_array($result, SQLITE_ASSOC)) { 
       foreach($entry as $key=>$value){ 
        $return[$i][$key]=$value; 
       } 
       $i++; 
      } 
      return $return; 
      break; 
     case "put": 
      $result = sqlite_exec($db, $query, $error); 
      if ($result==false){ die("Cannot execute statement {$query} ". $error);}else{ return sqlite_changes($db);} 
      break; 
     default: 
      die("Missing 2nd argument for database function."); 
      break; 
    } 
    return true; 
} 

/** 
* Function to escape user input for db 
* 
* @param unknown_type $data 
* @return unknown 
*/ 
function sqlite_escape($data) { 
    if(is_array($data)){return array_map("sqlite_escape_string", $data);} 
    return sqlite_escape_string($data); 
} 
?> 
+0

非常感謝,我會仔細查看該代碼以充分了解它:) – user1396515