猿问

PHP GD 从 MYSQL 表创建图形

我目前正在使用 GD 为 PHP 创建一个条形图(inb4 使用 Java 脚本,我不能。这是一个要求)就目前而言,我在 GD 中有一个正常工作的条形图,我可以显示正常数字,但现在我是试图从 MYSQL 表中检索数据并将其显示在图表中。


require "connect.php";

$sql = "SELECT 

title, searchcount

FROM movies

ORDER BY

searchcount DESC 

LIMIT 10";

$result = $conn->query($sql);


if (mysqli_num_rows($result) > 0) 

{

    while($row = mysqli_fetch_array($result))

    {

        $data = [

            $row['title'] => substr_count($row['searchcount'], "1"),

        ];

    }

}

上述代码目前可用于检索所需的 10 个结果之一。我想不通的是如何找回其他 9 个,我们将不胜感激。


https://imgur.com/m8sU8QG 这是图表当前状态的链接


图形是如何绘制的。


/*

 * Chart settings and create image

 */


// Image dimensions

$imageWidth = 700;

$imageHeight = 400;


// Grid dimensions and placement within image

$gridTop = 40;

$gridLeft = 50;

$gridBottom = 340;

$gridRight = 650;

$gridHeight = $gridBottom - $gridTop;

$gridWidth = $gridRight - $gridLeft;


// Bar and line width

$lineWidth = 1;

$barWidth = 20;


// Font settings

$font = 'OpenSans-Regular.ttf';

$fontSize = 10;


// Margin between label and axis

$labelMargin = 8;


// Max value on y-axis

$yMaxValue = 25;


// Distance between grid lines on y-axis

$yLabelSpan = 5;


// Init image

$chart = imagecreate($imageWidth, $imageHeight);


// Setup colors

$backgroundColor = imagecolorallocate($chart, 255, 255, 255);

$axisColor = imagecolorallocate($chart, 85, 85, 85);

$labelColor = $axisColor;

$gridColor = imagecolorallocate($chart, 212, 212, 212);

$barColor = imagecolorallocate($chart, 47, 133, 217);


imagefill($chart, 0, 0, $backgroundColor);


imagesetthickness($chart, $lineWidth);


/*

 * Print grid lines bottom up

 */

}

牛魔王的故事
浏览 105回答 1
1回答

忽然笑

好的,我测试了你的代码并且它有效,所以我的猜测是格式不正确或者你没有从数据库获得正确的输出。尝试更改此行:$data = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $row['title'] => substr_count($row['searchcount'], "1"),&nbsp; &nbsp; &nbsp; &nbsp; ];对此:$data[$row['title']] = substr_count($row['searchcount'], "1");正如你期望得到一$key => $value对,所以$value不是array(). 这是test我使用的数据和结果:<?php$data = array(&nbsp; &nbsp; 'test1' => 10,&nbsp; &nbsp; 'test2' => 4,&nbsp; &nbsp; 'test3' => 10,&nbsp; &nbsp; 'test4' => 4,&nbsp; &nbsp; 'test5' => 10,&nbsp; &nbsp; 'test6' => 4,&nbsp; &nbsp; 'test7' => 10,&nbsp; &nbsp; 'test8' => 4,&nbsp; &nbsp; 'test9' => 10,&nbsp; &nbsp; 'test10' => 4);/*&nbsp;* Chart settings and create image&nbsp;*/// Image dimensions$imageWidth = 700;$imageHeight = 400;// Grid dimensions and placement within image$gridTop = 40;$gridLeft = 50;$gridBottom = 340;$gridRight = 650;$gridHeight = $gridBottom - $gridTop;$gridWidth = $gridRight - $gridLeft;// Bar and line width$lineWidth = 1;$barWidth = 20;// Font settings$font = getcwd() . '/arial.ttf'; // I switched to use Arial font&nbsp;$fontSize = 10;// Margin between label and axis$labelMargin = 8;// Max value on y-axis$yMaxValue = 25;// Distance between grid lines on y-axis$yLabelSpan = 5;// Init image$chart = imagecreate($imageWidth, $imageHeight);// Setup colors$backgroundColor = imagecolorallocate($chart, 255, 255, 255);$axisColor = imagecolorallocate($chart, 85, 85, 85);$labelColor = $axisColor;$gridColor = imagecolorallocate($chart, 212, 212, 212);$barColor = imagecolorallocate($chart, 47, 133, 217);imagefill($chart, 0, 0, $backgroundColor);imagesetthickness($chart, $lineWidth);/*&nbsp;* Print grid lines bottom up&nbsp;*/for($i = 0; $i <= $yMaxValue; $i += $yLabelSpan) {&nbsp; &nbsp; $y = $gridBottom - $i * $gridHeight / $yMaxValue;&nbsp; &nbsp; // draw the line&nbsp; &nbsp; imageline($chart, $gridLeft, $y, $gridRight, $y, $gridColor);&nbsp; &nbsp; // draw right aligned label&nbsp; &nbsp; $labelBox = imagettfbbox($fontSize, 0, $font, strval($i));&nbsp; &nbsp; $labelWidth = $labelBox[4] - $labelBox[0];&nbsp; &nbsp; $labelX = $gridLeft - $labelWidth - $labelMargin;&nbsp; &nbsp; $labelY = $y + $fontSize / 2;&nbsp; &nbsp; imagettftext($chart, $fontSize, 0, $labelX, $labelY, $labelColor, $font, strval($i));}/*&nbsp;* Draw x- and y-axis&nbsp;*/imageline($chart, $gridLeft, $gridTop, $gridLeft, $gridBottom, $axisColor);imageline($chart, $gridLeft, $gridBottom, $gridRight, $gridBottom, $axisColor);/*&nbsp;* Draw the bars with labels&nbsp;*/$barSpacing = $gridWidth / count($data);$itemX = $gridLeft + $barSpacing / 2;foreach($data as $key => $value) {&nbsp; &nbsp; // Draw the bar&nbsp; &nbsp; $x1 = $itemX - $barWidth / 2;&nbsp; &nbsp; $y1 = $gridBottom - $value / $yMaxValue * $gridHeight;&nbsp; &nbsp; $x2 = $itemX + $barWidth / 2;&nbsp; &nbsp; $y2 = $gridBottom - 1;&nbsp; &nbsp; imagefilledrectangle($chart, $x1, $y1, $x2, $y2, $barColor);&nbsp; &nbsp; // Draw the label&nbsp; &nbsp; $labelBox = imagettfbbox($fontSize, 0, $font, $key);&nbsp; &nbsp; $labelWidth = $labelBox[4] - $labelBox[0];&nbsp; &nbsp; $labelX = $itemX - $labelWidth / 2;&nbsp; &nbsp; $labelY = $gridBottom + $labelMargin + $fontSize;&nbsp; &nbsp; imagettftext($chart, $fontSize, 0, $labelX, $labelY, $labelColor, $font, $key);&nbsp; &nbsp; $itemX += $barSpacing;}/*&nbsp;* Output image to browser&nbsp;*/imagepng($chart, 'chart.png');?>输出
随时随地看视频慕课网APP
我要回答