天天看點

mysql 分類計數器_PHP MySQL映像計數器

我是PHP新手,一直在研究計數器.計數器很好用,但是現在我想将數字轉換成圖像.

我建立了12張圖檔0-9,一個空格和一個逗号圖檔.

我在上下搜尋,以擷取将數字格式轉換為圖像所需的提示,但沒有成功.到目前為止,我所發現的就是如何僅使用檔案PHP / MySQL來建立基本的計數器,以及如何使用PHP / MySQL來顯示加密的圖像.

是以問題是:

如何告訴給定的代碼以顯示圖像代替每個數字?

目前PHP結果的示例:命中:2,435

我希望我的PHP獲得總點選數(示例),然後将2435替換為以下代碼:

mysql 分類計數器_PHP MySQL映像計數器
mysql 分類計數器_PHP MySQL映像計數器
mysql 分類計數器_PHP MySQL映像計數器
mysql 分類計數器_PHP MySQL映像計數器
mysql 分類計數器_PHP MySQL映像計數器

注意:我在此處顯示的代碼中使用了很多注釋.這樣,任何新的編碼人員都可以更輕松地了解所顯示的腳本.我将在本文的末尾添加我的最終/完成代碼,以便大家在找到解決方案後都能看到最終産品.

此代碼完全是虛構的,可作為文本點選計數器

// Begin open SQL connection to database

$concount = mysqli_connect("site","username","password","database");

// End connection to database

// Begin update number of hits

mysqli_query($concount,"UPDATE counter SET hits = hits + 1");

// End update number of hits

// Begin get number of hits

$hits = ("SELECT SUM(hits) FROM counter");

// End get number of hits

// Begin show number of hits

$result = mysqli_query($concount,$hits);

while($row = mysqli_fetch_array($result)) {

echo "Hits: " . number_format((float)$row['0']) . " ";

}

// End show number of hits

// Begin close SQL connection

mysqli_close($con);

// End close SQL connection

編輯:下面是我的代碼的最終結果.

請注意,此腳本中的數組在圖像數組的開頭和結尾都放置了’. (請參見以下示例)

Array ( [0] => ' [1] => 2 [2] => 4 [3] => 3 [4] => 5 [5] => ' )

是以,除非我想在命中計數器的以太端上出現破碎的圖像,否則必須使用它們.我将已經計劃在兩端使用的透明圖像重命名為’.png(請參見以下示例)

mysql 分類計數器_PHP MySQL映像計數器
mysql 分類計數器_PHP MySQL映像計數器
mysql 分類計數器_PHP MySQL映像計數器
mysql 分類計數器_PHP MySQL映像計數器
mysql 分類計數器_PHP MySQL映像計數器
mysql 分類計數器_PHP MySQL映像計數器

最終密碼

此代碼是完全虛構的,用作圖像點選計數器

// Begin open SQL connection to database

$concount = mysqli_connect("site","username","password","database");

// End connection to database

// Begin update number of hits

mysqli_query($concount,"UPDATE counter SET hits = hits + 1");

// End update number of hits

// Begin get number of hits

$hits = ("SELECT SUM(hits) FROM counter");

// End get number of hits

// Begin assign $hits an id

$result = mysqli_query($concount,$hits);

while($row = mysqli_fetch_array($result)) {

$totalhits=("'" . $row[0] . "'");

}

// End assign $hits an id

// Begin get id for number of hits, split the string into array, and assign id to numbers

$arr = str_split($totalhits);

$numbers = $arr;

foreach ($numbers as $value) {

// End get id for number of hits, split the string into array, and assign id to numbers

// Begin show number of hits as images

echo "

mysql 分類計數器_PHP MySQL映像計數器

";

}

// End show number of hits as images

// Begin close SQL connection

mysqli_close($con);

// End close SQL connection

最後說明:

我還沒有嘗試為更大的數字添加逗号或删除數組上的撇号.如果我願意,我會回來編輯它.

解決方法:

您需要将命中計數器分成每個值都包含一位數字的數組,然後使用for循環追加圖像.

$array = str_split($your_hit_variable_from_mysql);

if(!empty($array)){

foreach($array as $single){

echo '

mysql 分類計數器_PHP MySQL映像計數器

;

}

}else{

echo '

mysql 分類計數器_PHP MySQL映像計數器

;

}

?>

確定您以整數格式存儲數字,而不是以52,200之類的字元串存儲逗号.

欲了解更多資訊,請檢查Here.

編輯:當圖像的計數器為0時,添加了異常處理.

标簽:image,mysql,php,hitcounter

來源: https://codeday.me/bug/20191028/1951824.html