Web Analytics Made Easy -
StatCounter Need help with simple function parameter problem - CodingForum

Announcement

Collapse
No announcement yet.

Need help with simple function parameter problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help with simple function parameter problem

    So Im new to php, have minimal sql experience but am trying to create something to make some kml placemarks using both. Basically, Im just trying to pull some info from sql and pass it into a function, depending on whether $Category matches the functions parameter for its value, which then echos the kml placemark. The problem is that when I run the script, four placemarks are created for each $job array, instead of just one, as if each function is taking each array that is passed to it instead of for only those matching the Category parameters of the function....I really hope Im explaining this right. Im sure its simple so dont laugh...Im just not able to see it yet. Any info is much appreciated.

    PHP Code:
    <?php

      $dbh
    =mssql_connect('server','username','password');
      
    mssql_query('database');

      function 
    getjobsoftype($Category) {
        
    $query="select pr.WBS1 as Project, cf.cust4ulservice as Service, cf.cust3ultype as Category, pr.LongName,
                              pr.Principal as PIC, pr.ProjMgr as PM, cf.cust3ulsitedev as Type, cf.custLatitude, cf.custLongitude, cl.Name as Company
                            from pr
                                join cl on pr.ClientID=cl.ClientID
                                join projectcustomtabfields cf on pr.wbs1=cf.wbs1 and pr.wbs2=cf.wbs2
                            where
                               pr.wbs2='' AND cf.cust3ultype IS NOT NULL "
    ;
        
    $results=mssql_query($query);

        while (
    $row=mssql_fetch_array($results)) {
          
    $job[]=$row;
        }

        return 
    $job;
      }

      function 
    echojobsoftype($Category,$label,$icon) {
        
    $job=getjobsoftype($Category);

        echo 
    "  <Folder>\n    <name>$label</name>\n    <open>0</open>\n";

        foreach (
    $job as $current_job) {
          
    $category=htmlentities($current_job['Category'],ENT_QUOTES);
          
    $longname=htmlentities($current_job['LongName'],ENT_QUOTES);
          
    $project=htmlentities($current_job['Project'],ENT_QUOTES);
          
    $company=htmlentities($current_job['Company'],ENT_QUOTES);
          
    $type=htmlentities($current_job['Type'],ENT_QUOTES);
          
    $pic=htmlentities($current_job['PIC'],ENT_QUOTES);
          
    $pm=htmlentities($current_job['PM'],ENT_QUOTES);
          
    $service=htmlentities($current_job['Service'],ENT_QUOTES);
          
    $lat=$current_job['custLatitude'];
          
    $lon=$current_job['custLongitude'];

          echo <<<ENDKML

        <Placemark>
          <name></name>
          <description>
          <![CDATA[
          <img src="http://intranet/images/Web-Logo.gif"><br>
          <p>
          <h2>
    $project<hr></h2>
          </p>
          <p>
          
    $longname<hr>
          </p>
          <p>
          Company: 
    $company<hr>
          Category:
    $category<hr>
          PIC: 
    $pic<br>
          PM: 
    $pm<hr>
          Service: 
    $service<br>
          </p>
          <p>
          Latitude: 
    $lat<br>
          Longitude: 
    $lon<br>
          ]]>
          </description>
          <LookAt>
        <longitude>
    $lon</longitude>
        <latitude>
    $lat</latitude>
        <range>900</range>
        <tilt>25</tilt>
        <heading>0</heading>
          </LookAt>
          <visibility>1</visibility>
          <Style>
        <labelScale>0.66</labelScale>
        <IconStyle>
          <scale>0.50</scale>
          <Icon>
            <href>
    $icon</href>
            <w>10</w>
            <h>10</h>
          </Icon>
        </IconStyle>
          </Style>
          <Point>
        <altitudeMode>relativeToGround</altitudeMode>
        <coordinates>
    $lon,$lat</coordinates>
          </Point>
        </Placemark>
    ENDKML;
        }
        echo 
    "  </Folder>\n\n";

        
    ob_flush();

      }

    function 
    ob_file_callback($buffer)
    {
      global 
    $ob_file;
      
    fwrite($ob_file,$buffer);
    }

    $ob_file fopen('test.kml','w');

    ob_start('ob_file_callback');

      echo <<<ENDKML
    <?xml version="1.0" encoding="UTF-8"?>
    <kml xmlns="http://www.opengis.net/kml/2.2">
    <Document>
    <Folder>
      <name>GeoDesign Projects</name>
      <open>0</open>
    ENDKML;

      
    echojobsoftype("(Category like '%Outside' or Category like '%SVM')",'Outside Reports','http://intranet/images/orange_point.png');

      
    echojobsoftype("Category='Geo/Env'",'Combo','http://intranet/images/red_point.png');

      
    echojobsoftype("Category='Environmental'",'Environmental','http://intranet/images/green_point.png');

      
    echojobsoftype("Category='Geotechnical'",'Geotech','http://intranet/images/blue_point.png');

      echo 
    "</Folder>\n</Document>\n</kml>";

    ob_end_flush();


    ?>

  • #2
    I see you passing variable $Category to your getjobsoftype() function, but I don't see you using that variable in the query. I think you just need to add WHERE $Category to your query. And be sure to check your query for errors!

    Comment


    • #3
      Thanks for the reply Fumigator, I appreciate the help. I had cf.cust3ultype as category in the first line of the query, it may have just been cut off a bit. I added $category to the where clause as suggested, as well, but am now getting an error stating category as an invalid column name when the script is run. The updated query is below, any ideas?
      PHP Code:
      function getjobsoftype($Category) {
          
      $query="select pr.WBS1 as Project, cf.cust4ulservice as Service, 
          cf.cust3ultype as Category, pr.LongName, pr.Principal as PIC, 
          pr.ProjMgr as PM, cf.cust3ulsitedev as Type, cf.custLatitude, cf.custLongitude, 
          cl.Name as Company
            from pr
              join cl on pr.ClientID=cl.ClientID
              join projectcustomtabfields cf on pr.wbs1=cf.wbs1 and pr.wbs2=cf.wbs2
            where
              pr.wbs2='' AND 
              cf.cust3ultype IS NOT NULL AND
              
      $Category"

      Comment


      • #4
        Yeah you can't refer to an alias name in your WHERE clause. The reason is, in the order of processing the query, MySQL goes out and gets the results based on the WHERE clause first, then assembles the content based on stuff in the SELECT area. In other words, the alias doesn't exist at the time the WHERE clause is processed.

        Comment

        Working...
        X