Forum Moderators: coopster

Message Too Old, No Replies

Newbie php question - if statement variable

         

ronnyskog

3:20 pm on Oct 7, 2006 (gmt 0)

10+ Year Member



I have a php script creating a stock chart with buy and sell signals using data from a csv file. The csv file looks like this:

"Date MM/DD/YYYY","Open","High","Low","Close","Volume","Trade signal"
07/07/2006,18.71,18.95,18.5,18.56,57542500,"CASH"
07/06/2006,18.81,18.94,18.75,18.85,35026000,""
07/05/2006,19.2,19.26,18.74,18.75,51632300,"BUY"

The last section is the trade signal stored in: $labelData

I'm adding the trade signals to the chart with the code below:
What I'm trying to do is activating the code: "$tb->setAlignment(Center)" only if the trade signal is CASH:

if ($chartType == "CandleStick") {
$layer = $m->addCandleStick(0x33ff33, 0xff3333);
#/ Label goes here:
{
$layer->addExtraField($labelData);
$layer->setDataLabelFormat("{field0}");
$tb = $layer->setDataLabelStyle("ARIALBD.TTF", 8, 0xFFFF0002, 0);

if ($labelData == "CASH")
$tb->setAlignment(Center);
}

But this code [if ($labelData == "CASH") ] is not working as intended. How can I write an if statement that will accomplish what I'm trying to do here? I guess I have to use $labelData[something] but I'm very new to programming and haven't been able to figure this out yet.

This seees to be the code reading the csv file and defining variables including $labelData:

#process csv file
$fp = fopen($filenameCSV,"r");
$row = 0;
$totalrow=$requestedDuration+$requestedExtraPoints;
while (($data = fgetcsv($fp,$totalrow,","))!== FALSE) {
list($month, $day, $year) = split("/",$data[0]);
# Assume the first row of csv file is the end date
if ($row == 0) {
$endDate = chartTime($year, intval($month), intval($day));

# After we obtain the endDate, we get the startDate based on the user selected
# duration. It is OK to obtain more data than requested.
if ($requestedDuration >= 30) {
# More or equal to 30 days - so we use months as the unit
$YMD = getChartYMD($endDate);
$startMonth = (int)($YMD / 100) % 100 - (int)($requestedDuration / 30);
$startYear = (int)($YMD / 10000);
while ($startMonth < 1) {
$startYear = $startYear - 1;
$startMonth = $startMonth + 12;
}
$startDate = chartTime($startYear, $startMonth, 1);
} else {
# Less than 30 days - use day as the unit. Note that we use trading days
# below. For less than 30 days, the starting point of the axis is always at
# the start of the day (9:30am)
$startDate = $endDate - fmod2($endDate, 86400) + 9 * 3600 + 30 * 60;
for($i = 1; $i < $requestedDuration; ++$i) {
if (getChartWeekDay($startDate) == 1) {
$startDate = $startDate - 3 * 86400;
} else {
$startDate = $startDate - 86400;
}
}
}
}

$timeStamps1[$row] = chartTime($year, intval($month), intval($day));
$highData1[$row] = $data[2];
$lowData1[$row] = $data[3];
$openData1[$row] = $data[1];
$closeData1[$row] = $data[4];
$volData1[$row] = $data[5];
// Label Data:
$labelData1[$row] = $data[6];
//echo "Row $row: $labelData1[$row]<br>";

#Stop when all data has been acquired
if ($timeStamps1[$row] <= $startDate) {break;}

$row+=1;
}

#sort ascending. required by FinanceChart object
for ($i=count($timeStamps1)-1,$j=0;$i>=0;$i--,$j++){
$timeStamps[$j]=$timeStamps1[$i];
$highData[$j] =$highData1[$i];
$lowData[$j] =$lowData1[$i];
$openData[$j] =$openData1[$i];
$closeData[$j] =$closeData1[$i];
$volData[$j] =$volData1[$i];
$labelData[$j] =$labelData1[$i];
//echo "Reverse $j: $labelData[$i]<br>";
}

// fix labelPoints:
$labelData = array_slice($labelData, $extraPoints);

eelixduppy

4:01 am on Oct 8, 2006 (gmt 0)



Welcome to WebmasterWorld!

This syntax and logic is correct:"


if ($labelData == "CASH")
$tb->setAlignment(Center);
}

However I think you want to put Center in parenthesis otherwise PHP thinks its a constant:


$tb->setAlignment("Center");

As for finding your problem, I would start by adding

[url=http://us3.php.net/manual/en/function.error-reporting.php]error_reporting[/url](E_ALL);
to the top of the script. This will help identify the problems if there are any. Also, try to look over the code and make sure that every step is doing exactly what it should be doing (ie. echoing information to the browser)

Good luck!