iT邦幫忙

1

PHP值帶入另一個PHP內

  • 分享至 

  • xImage

請問一下
我的CSV檔(ABC_200522.CSV)

time,DEF,ABC
01:01:00,-0.0153,0.0156
00:00:01,-0.0153,-0.0457
00:00:02,-0.0353,-0.0158
00:00:03,-0.0353,-0.1944
00:00:04,-0.0353,-0.5211
00:00:05,-0.0353,-0.4318
00:00:06,-0.0353,-0.0251
00:00:07,-0.0353,-0.1195
00:00:08,-0.0353,-0.6572
00:00:09,-0.0353,-0.2845
00:00:10,-0.0353,-0.5612
00:00:11,-0.0353,-0.8812

第一個PHP(value.php)

<?php
$dirname="pchart/csv/";
$dh=opendir($dirname);
$keyword = ABC;
$date = 200522;
$rows = 2;
while ($dave=readdir($dh))
{
if ($dave != "." && $dave != "..") {
	if (preg_match("/$keyword/i",$dave)){
		if (preg_match("/$date/i",$dave)){
			$file = fopen("$dirname"."$dave","r");
			fgets($file);
			while($row = fgetcsv($file)){
			$value = $row[$rows].',';
			echo $value;
}
fclose($file);
}}}}
?>

第二個PHP(chart.php)

<?
// 引用相關類別檔
include("pchart/class/pDraw.class.php");
include("pchart/class/pImage.class.php");
include("pchart/class/pData.class.php");
include("value.php");

$myData = new pData();
$myData->addPoints(array($value));
$myPicture = new pImage(800,300,$myData);
$myPicture->setFontProperties(array("FontName"=>"pchart/fonts/calibri.ttf","FontSize"=>10));
$myPicture->setGraphArea(60,40,760,280);
$myPicture->drawScale();
$myPicture->drawlineChart();
$myPicture->render('example_01.png');
$myPicture->autoOutput();
?>

單獨執行value.php可以秀出csv的值(0.0156,-0.0457,-0.0158,-0.1944,-0.5211,-0.4318,-0.0251,-0.1195,-0.6572,-0.2845,-0.5612,-0.8812,)
若將value.php的值帶入chart.php,$value卻僅剩-0.8812
該怎麼那value.php的值,全部帶入chart.php內呢

看更多先前的討論...收起先前的討論...
特倫斯 iT邦新手 5 級 ‧ 2020-05-28 23:26:54 檢舉
補充
我在B.PHP內include(a.php)
仍只會顯示4
player iT邦大師 1 級 ‧ 2020-05-28 23:54:43 檢舉
誰知道你的php是怎麼寫的? 這要觀落陰玩猜猜看嗎?
Franky Chen iT邦研究生 3 級 ‧ 2020-05-29 08:50:51 檢舉
Code 勒
特倫斯 iT邦新手 5 級 ‧ 2020-05-29 09:11:52 檢舉
抱歉!!我回家後在將程式碼PO上~感謝!!
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2020-05-29 09:12:16
最佳解答

1,2,3,4 --> 用陣列

特倫斯 iT邦新手 5 級 ‧ 2020-05-29 22:57:19 檢舉

我將value.php $value1字串存成陣列

$value1 = $row[$rows];
$value = explode(',',$value1);

仍在chart.php無法正常帶入$value值

    while($row = fgetcsv($file)){
        $value = $row[$rows].',';
        echo $value;
    }

把實際的值代入
你寫的實際上是這樣

    $value = 0.0156,
    echo $value;
    $value = -0.0457,
    echo $value;
    $value = -0.0158,
    echo $value;
    $value = -0.1944,
    echo $value;
    $value = -0.5211,
    echo $value;
    $value = -0.4318,
    echo $value;
    $value = -0.0251,
    echo $value;
    $value = -0.1195,
    echo $value;
    $value = -0.6572,
    echo $value;
    $value = -0.2845,
    echo $value;
    $value = -0.5612,
    echo $value;
    $value = -0.8812,
    echo $value;

你只是把不同的值塞入 $value 這個變數
所以 $value 只會保留最後一個值 -0.8812

有兩個改法:
1.用陣列的寫法 array_push($arr, $row[$rows])
2.改成 $value = $value . $row[$rows].',';
傳過去之後再 $arr = explode(",", $value);

$arr 就會是你要的結果

特倫斯 iT邦新手 5 級 ‧ 2020-05-30 07:33:29 檢舉

感謝!我使用了第二種方式

1

沒有代碼不知道你發生什麼事。
一般將兩個php共用的寫法如下
a.php

do{
include(b.php);
}while(1);

以上的寫法就是將b.php做為a.php的一段程式碼。
其運行其實都在a。b變成單純的程式碼共用。

至於你說只有4的問題。推測變數問題了吧。等你有放程式碼再查了。
現在沒辦法。

看更多先前的回應...收起先前的回應...
特倫斯 iT邦新手 5 級 ‧ 2020-05-29 09:11:33 檢舉

抱歉!!我回家後在將程式碼PO上~感謝!!

淺水員 iT邦大師 6 級 ‧ 2020-05-29 09:37:33 檢舉

應該是 while(0); ?

0不是只會跑一次。我是要讓他無限。
哇哈哈
不過好像會被擋下來的樣子。

淺水員 iT邦大師 6 級 ‧ 2020-05-29 13:05:07 檢舉

那來個薛丁格的 while?
while(random_int(0, 1));

特倫斯 iT邦新手 5 級 ‧ 2020-05-29 21:00:44 檢舉

我的CSV檔(ABC_200522.CSV)

time,DEF,ABC
01:01:00,-0.0153,0.0156
00:00:01,-0.0153,-0.0457
00:00:02,-0.0353,-0.0158
00:00:03,-0.0353,-0.1944
00:00:04,-0.0353,-0.5211
00:00:05,-0.0353,-0.4318
00:00:06,-0.0353,-0.0251
00:00:07,-0.0353,-0.1195
00:00:08,-0.0353,-0.6572
00:00:09,-0.0353,-0.2845
00:00:10,-0.0353,-0.5612
00:00:11,-0.0353,-0.8812

第一個PHP(value.php)

<?php
$dirname="pchart/csv/";
$dh=opendir($dirname);
$keyword = ABC;
$date = 200522;
$rows = 2;
while ($dave=readdir($dh))
{
if ($dave != "." && $dave != "..") {
	if (preg_match("/$keyword/i",$dave)){
		if (preg_match("/$date/i",$dave)){
			$file = fopen("$dirname"."$dave","r");
			fgets($file);
			while($row = fgetcsv($file)){
			$value = $row[$rows].',';
			echo $value;
}
fclose($file);
}}}}
?>

第二個PHP(chart.php)

<?
// 引用相關類別檔
include("pchart/class/pDraw.class.php");
include("pchart/class/pImage.class.php");
include("pchart/class/pData.class.php");
include("value.php");

$myData = new pData();
$myData->addPoints(array($value));
$myPicture = new pImage(800,300,$myData);
$myPicture->setFontProperties(array("FontName"=>"pchart/fonts/calibri.ttf","FontSize"=>10));
$myPicture->setGraphArea(60,40,760,280);
$myPicture->drawScale();
$myPicture->drawlineChart();
$myPicture->render('example_01.png');
$myPicture->autoOutput();
?>

單獨執行value.php可以秀出csv的值(0.0156,-0.0457,-0.0158,-0.1944,-0.5211,-0.4318,-0.0251,-0.1195,-0.6572,-0.2845,-0.5612,-0.8812,)
若將value.php的值帶入chart.php,$value卻僅剩-0.8812
該怎麼那value.php的值,全部帶入chart.php內呢

我要發表回答

立即登入回答