Page 1 of 1

Convert ASP-VBScript Snippit to PHP (Newb)

Posted: Wed Sep 02, 2009 11:41 am
by adhdjeff
Hi Gang, I'm new to PHP and I'm moving some code from ASP-VBScript to PHP (4.3.9) I've been using Haneng Charts for years with ASP and need to convert the following snippit to PHP, but my PHP-Kung-Fu is weak.

The following code uses a recordset named rsChartData with 2 fields (LogDate & LogValue). Any direction you can provide would be appreciated:

<%

'We need to label each Parameter with it's own number, this variable will start at 1 and increase by 1 for each parameter pair we output:

ParameterCounter = 1

'Now it is time to loop through the database results and output them in a format that HanengCharts understands:

WHILE NOT rsChartData.EOF

%>

<param name="Text_<%=ParameterCounter%>" value="<%=rsChartData("LogDate")%>" />
<param name="Value_<%=ParameterCounter%>" value="<%=rsChartData("LogValue")%>" />
<param name="Color_Bar" value="#FF9900" />

<%
ParameterCounter = ParameterCounter + 1
rsChartData.MoveNext
WEND
%>



Re: Convert ASP-VBScript Snippit to PHP (Newb)

Posted: Wed Sep 02, 2009 12:57 pm
by flying_circus
adhdjeff wrote:Hi Gang, I'm new to PHP and I'm moving some code from ASP-VBScript to PHP (4.3.9) I've been using Haneng Charts for years with ASP and need to convert the following snippit to PHP, but my PHP-Kung-Fu is weak.

The following code uses a recordset named rsChartData with 2 fields (LogDate & LogValue). Any direction you can provide would be appreciated:

<%

'We need to label each Parameter with it's own number, this variable will start at 1 and increase by 1 for each parameter pair we output:

ParameterCounter = 1

'Now it is time to loop through the database results and output them in a format that HanengCharts understands:

WHILE NOT rsChartData.EOF

%>

<param name="Text_<%=ParameterCounter%>" value="<%=rsChartData("LogDate")%>" />
<param name="Value_<%=ParameterCounter%>" value="<%=rsChartData("LogValue")%>" />
<param name="Color_Bar" value="#FF9900" />

<%
ParameterCounter = ParameterCounter + 1
rsChartData.MoveNext
WEND
%>



Sure, I'm sort of bored, so why not, I'll convert code

Code: Select all

<?php
  # Vars
    $rsChartData = Array(); // Some database Results as Array
 
  # We need to label each Parameter with it's own number, this variable will start at 1 and increase by 1 for each parameter pair we output.
    $ParameterCounter = 1;
 
  # Now it is time to loop through the database results and output them in a format that HanengCharts understands.
    foreach($rsChartData as $record) {
      print "<param name=\"Text_{$ParameterCounter}\" value=\"{$record["LogDate"]}\" />";
      print "<param name=\"Value_{$ParameterCounter}\" value=\"{$record["LogValue"]}\" />";
      print "<param name=\"Color_Bar\" value=\"#FF9900\" />";
      $ParameterCounter++;
    }
?>
If this is actual database data, like from a MySQL database, you'll replace the foreach statement at line 9 with a while statement like:
(assuming that $rsChartData is a valid MySQL query result set)

Code: Select all

while($record = $rsChartData->fetch_assoc()) {

Re: Convert ASP-VBScript Snippit to PHP (Newb)

Posted: Wed Sep 02, 2009 2:57 pm
by adhdjeff
Thanks Flying_Circus. I used your code but I think I may be missing something because the java-charting tool gives me an error that it's not receiving any params.

rsChartData is indeed a valid MySQL recordset (I can see the values when pasted into a table). I made a minor change to one of the RS field names, but other than that, everything appears identical to the sample:

Code: Select all

<?php
  # Vars
    $rsChartData = Array(); // Some database Results as Array
 
  # We need to label each Parameter with it's own number, this variable will start at 1 and increase by 1 for each parameter pair we output.
    $ParameterCounter = 1;
 
  # Now it is time to loop through the database results and output them in a format that HanengCharts understands.
    while($record = $rsChartData->fetch_assoc()) {
      print "<param name=\"Text_{$ParameterCounter}\" value=\"{$record["LogDate"]}\" />";
      print "<param name=\"Value_{$ParameterCounter}\" value=\"{$record["Duration"]}\" />";
      print "<param name=\"Color_Bar\" value=\"#FF9900\" />";
      $ParameterCounter++;
    }
?>
I even pasted this in it's place which worked so at least I know it isn't the chart tool:

Code: Select all

<PARAM NAME="Value_1" VALUE="40">
    <PARAM NAME="Value_2" VALUE="50">
    <PARAM NAME="Value_3" VALUE="60">
    <PARAM NAME="Text_1" VALUE="Jan">
    <PARAM NAME="Text_2" VALUE="Feb">
    <PARAM NAME="Text_3" VALUE="Mar">
Learning PHP has been great, but I'm just not good enough to debug yet--so thank you again for your help.

Re: Convert ASP-VBScript Snippit to PHP (Newb)

Posted: Wed Sep 02, 2009 3:21 pm
by flying_circus
Whats the rest of your code look like?

My script was meant as an example. Your original code didnt contain all the required information to do a complete conversion.

Edit:

I went ahead and populated the $rsChartData array to give you an idea of the format this code uses.

Code: Select all

<?php
  # Vars
    // Some database Results as Array
    $rsChartData = Array(Array("LogDate" => "Jan", "LogValue" => "40"),
                         Array("LogDate" => "Feb", "LogValue" => "50"),
                         Array("LogDate" => "Mar", "LogValue" => "60"));
 
  # We need to label each Parameter with it's own number, this variable will start at 1 and increase by 1 for each parameter pair we output.
    $ParameterCounter = 1;
 
  # Now it is time to loop through the database results and output them in a format that HanengCharts understands.
    foreach($rsChartData as $record) {
      print "<param name=\"Text_{$ParameterCounter}\" value=\"{$record["LogDate"]}\" />\n";
      print "<param name=\"Value_{$ParameterCounter}\" value=\"{$record["LogValue"]}\" />\n";
      print "<param name=\"Color_Bar\" value=\"#FF9900\" />\n";
      $ParameterCounter++;
    }
?>
And Output:

Code: Select all

<param name="Text_1" value="Jan" />
<param name="Value_1" value="40" />
<param name="Color_Bar" value="#FF9900" />
<param name="Text_2" value="Feb" />
<param name="Value_2" value="50" />
<param name="Color_Bar" value="#FF9900" />
<param name="Text_3" value="Mar" />
<param name="Value_3" value="60" />
<param name="Color_Bar" value="#FF9900" />

Re: Convert ASP-VBScript Snippit to PHP (Newb)

Posted: Wed Sep 02, 2009 3:41 pm
by adhdjeff
Thanks again for your help FC. Most of the PHP code below is generated by Dreamweaver, but it seems to work well. BTW, I just visited Sun River last month. Beautiful!

Code: Select all

<?php require_once('Connections/myrclog.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
 
mysql_select_db($database_myrclog, $myrclog);
$query_rsChartData = "SELECT FlightLog.LogDate, FlightLog.Duration FROM FlightLog";
$rsChartData = mysql_query($query_rsChartData, $myrclog) or die(mysql_error());
$row_rsChartData = mysql_fetch_assoc($rsChartData);
$totalRows_rsChartData = mysql_num_rows($rsChartData);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test 3</title>
</head>
 
<body>
<p>
  <applet code="HanengCharts.class" archive="HanengCharts2.jar" width="500" height="350">
    <PARAM NAME="LicenseKey" VALUE="GGBMXW2S9MSzWt" />
    <PARAM NAME="ChartType" VALUE="5">
    <PARAM NAME="VerticalXAxisLabels" VALUE="true" />
<?php
  # Vars
    $rsChartData = Array(); // Some database Results as Array
 
  # We need to label each Parameter with it's own number, this variable will start at 1 and increase by 1 for each parameter pair we output.
    $ParameterCounter = 1;
 
  # Now it is time to loop through the database results and output them in a format that HanengCharts understands.
    while($record = $rsChartData->fetch_assoc()) {
      print "<PARAM NAME=\"Text_{$ParameterCounter}\" VALUE=\"{$record["LogDate"]}\" />";
      print "<PARAM NAME=\"Value_{$ParameterCounter}\" VALUE=\"{$record["Duration"]}\" />";
      print "<PARAM NAME=\"Color_Bar\" VALUE=\"#FF9900\" />";
      $ParameterCounter++;
    }
?>
  </applet>
</p>
</body>
</html>
<?php
mysql_free_result($rsChartData);
?>
 

Re: Convert ASP-VBScript Snippit to PHP (Newb)

Posted: Wed Sep 02, 2009 5:59 pm
by flying_circus
Give this a shot. As long as the query suceed's, it *should* work. Re-declaring the $rsChartData down below was over writing the SQL results.

Now, being as curious as I am, and also into aviation... what are you building that needs a flight log? 8)

What did you think of Sunriver and are you local?

Code: Select all

<?php
  # Includes
    require_once('Connections/myrclog.php');
  
  # Functions
    if (!function_exists("GetSQLValueString")) {
      function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {
        if (PHP_VERSION < 6) {
          $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
        }
       
        $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
       
        switch ($theType) {
          case "text":
            $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
            break;    
          case "long":
          case "int":
            $theValue = ($theValue != "") ? intval($theValue) : "NULL";
            break;
          case "double":
            $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
            break;
          case "date":
            $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
            break;
          case "defined":
            $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
            break;
        }
        return $theValue;
      }
    }
    
  # Select Database
    mysql_select_db($database_myrclog, $myrclog);
    
  # Build Query String
    $query_rsChartData = "SELECT `FlightLog`.`LogDate`, `FlightLog`.`Duration` FROM `FlightLog`;";
    
  # Execute Query
    $rsChartData = mysql_query($query_rsChartData, $myrclog) or die(mysql_error());
    
  # Why is this here other that to fetch only the first record?
    //$row_rsChartData = mysql_fetch_assoc($rsChartData);
    
  # Count Records
    $totalRows_rsChartData = mysql_num_rows($rsChartData);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>test 3</title>
  </head>
 
  <body>
    <p>
      <applet code="HanengCharts.class" archive="HanengCharts2.jar" width="500" height="350">
        <PARAM NAME="LicenseKey" VALUE="GGBMXW2S9MSzWt" />
        <PARAM NAME="ChartType" VALUE="5">
        <PARAM NAME="VerticalXAxisLabels" VALUE="true" />
        <?php
          # Vars
            /* We dont want to set $rsChartData to an empty array
              $rsChartData = Array(); // Some database Results as Array
            */
         
          # We need to label each Parameter with it's own number, this variable will start at 1 and increase by 1 for each parameter pair we output.
            $ParameterCounter = 1;
         
          # Now it is time to loop through the database results and output them in a format that HanengCharts understands.
            while($record = $rsChartData->fetch_assoc()) {
              print "<PARAM NAME=\"Text_{$ParameterCounter}\" VALUE=\"{$record["LogDate"]}\" />";
              print "<PARAM NAME=\"Value_{$ParameterCounter}\" VALUE=\"{$record["Duration"]}\" />";
              print "<PARAM NAME=\"Color_Bar\" VALUE=\"#FF9900\" />";
              $ParameterCounter++;
            }
        ?>
      </applet>
    </p>
  </body>
</html>
<?php
  mysql_free_result($rsChartData);
?>

Re: Convert ASP-VBScript Snippit to PHP (Newb)

Posted: Wed Sep 02, 2009 10:45 pm
by adhdjeff
Thanks FC. As a private pilot (in Seattle), I wrote a web app in ASP to log my flights. With gas prices going through the roof I didn't feel like spending $250/hr to fly anymore and I've been passing the time with radio controlled helicopters instead--which have been harder to learn to fly than real aircraft. I've been logging my flight and battery data (batteries cost more than $200 ea. so it's important to log their performance).

A number of other pilots wanted access to my app, so I'm opening it up to them (myrclog.com) and using this as an opportunity to learn PHP. I've used the Haneng Charts tool to graph data in many of my ASP apps, but I'm new to PHP and simply can't seen to figure out the syntax (I'm using Dreamweaver as a crutch to build the recordsets and do other PHP things since I'm not PHP literate enough to code by hand).

I tried your revised code but got the same "no param's" error. I would be willing to pay you to help me figure it out though (because it helps me learn in the process) so PM me and we can connect offline if you'd like.