We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29319
    • 37 Posts
    Hi all. This script works perfectly fine untill i run it through a snippet in modx here is what is turned out:

    echo $data['text']; ?>  



    Here is the code:

    <?php
    function getStockSite($stockLink){
       
       if ($fp = fopen($stockLink, 'r')) {
          $content = '';
            
          while ($line = fread($fp, 1024)) {
             $content .= $line;
          }
       }
    
       return $content;  
    }
    
    function processStockSite($wurl){
        
        $wrss = getStockSite($wurl);
        $name  = '-';
        $price = '';
        $diff  = '';
        
        if (strlen($wrss)>100){
            $spos = 0;
            
            // Get text
            $spos = strpos($wrss,'</span>:',$spos)+3;
            $spos = strpos($wrss,'<big>',$spos);
            $epos = strpos($wrss,'</div><h1>',$spos);
            if ($epos>$spos){
               $text = substr($wrss,$spos,$epos-$spos);
            } else {
               $text = '-';
            }
    
            $spos = $epos + 10;
            // Get company name
            $epos = strpos($wrss,'<',$spos);
            if ($epos>$spos){
                $name = substr($wrss,$spos,$epos-$spos);
            } 
    
            
            // Get actual price
            $spos = strpos($wrss,'yfs_l10')+strlen('yfs_l10');
            $spos = strpos($wrss,'>',$spos)+1;
            $epos = strpos($wrss,'<',$spos);
            if ($epos>$spos){
                $price = substr($wrss,$spos,$epos-$spos);
            } else {
                $price = '-';
            }
            
            // Get direction
            $spos = strpos($wrss,'alt',$epos)+strlen('alt')+2;
            $epos = strpos($wrss,'"',$spos);
            if ($epos>$spos){
                $dir = strtolower(substr($wrss,$spos,$epos-$spos));
            } 
            
            // Get difference
            $spos = strpos($wrss,'>',$epos+3)+1;
            $epos = strpos($wrss,'<',$spos);
            if ($epos>$spos){
                $diff = substr($wrss,$spos,$epos-$spos);
            } 
    
        }
        
        $result['name']  = $name;
        $result['value'] = $price;
        $result['diff']  = $diff;
        $result['direction'] = $dir;
        $result['text']  = $text;
        
        return $result;
        
    }
    
    
    // Get stock data
    //$data = processStockSite('c:\q.htm'); // Google
    $data = processStockSite('http://finance.yahoo.com/q?s=TLS.AX'); // Google
    //$data = processStockSite('http://finance.yahoo.com/q?s=MSFT'); // Microsoft
    //$data = processStockSite('http://finance.yahoo.com/q?s=AAPL'); // Apple
    //$data = processStockSite('http://finance.yahoo.com/q?s=GE'); // GE
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
    <html>
    <head>
       <title>Micro Stock</title>
       <link href="style/style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div id="main">
        <div id="caption"> </div>
         <div id ="icon2"></div>
          <div id="result">
    <?php 
             echo $data['text'];
             //echo $data['value'].' ';
             //if ($data['direction'] == 'up') echo '<span class="up"> +'.$data['diff'].'</span>';
             //else echo '<span class="down"> -'.$data['diff'].'</span>'; ?>
          </div>
    
    </div>
    </body>
    ?>
      • 29774
      • 386 Posts
      Frist, you shouldn’t have any html in your snippet; the snippet will only display anything returned or echo’ed from php. For good practice put the html in a chunk and load into your snippet. Second, you have two closing php tags: ?>
        Snippets: GoogleMap | FileDetails | Related Plugin: SSL
        • 29319
        • 37 Posts
        how would you suggest I would get rid of html into a chunk since it surrounds php?
          • 4310
          • 2,310 Posts
          You could try something like :
          <?php
          function getStockSite($stockLink){
             
             if ($fp = fopen($stockLink, 'r')) {
                $content = '';
                  
                while ($line = fread($fp, 1024)) {
                   $content .= $line;
                }
             }
          
             return $content;  
          }
          
          function processStockSite($wurl){
              
              $wrss = getStockSite($wurl);
              $name  = '-';
              $price = '';
              $diff  = '';
              
              if (strlen($wrss)>100){
                  $spos = 0;
                  
                  // Get text
                  $spos = strpos($wrss,'</span>:',$spos)+3;
                  $spos = strpos($wrss,'<big>',$spos);
                  $epos = strpos($wrss,'</div><h1>',$spos);
                  if ($epos>$spos){
                     $text = substr($wrss,$spos,$epos-$spos);
                  } else {
                     $text = '-';
                  }
          
                  $spos = $epos + 10;
                  // Get company name
                  $epos = strpos($wrss,'<',$spos);
                  if ($epos>$spos){
                      $name = substr($wrss,$spos,$epos-$spos);
                  } 
          
                  
                  // Get actual price
                  $spos = strpos($wrss,'yfs_l10')+strlen('yfs_l10');
                  $spos = strpos($wrss,'>',$spos)+1;
                  $epos = strpos($wrss,'<',$spos);
                  if ($epos>$spos){
                      $price = substr($wrss,$spos,$epos-$spos);
                  } else {
                      $price = '-';
                  }
                  
                  // Get direction
                  $spos = strpos($wrss,'alt',$epos)+strlen('alt')+2;
                  $epos = strpos($wrss,'"',$spos);
                  if ($epos>$spos){
                      $dir = strtolower(substr($wrss,$spos,$epos-$spos));
                  } 
                  
                  // Get difference
                  $spos = strpos($wrss,'>',$epos+3)+1;
                  $epos = strpos($wrss,'<',$spos);
                  if ($epos>$spos){
                      $diff = substr($wrss,$spos,$epos-$spos);
                  } 
          
              }
              
              $result['name']  = $name;
              $result['value'] = $price;
              $result['diff']  = $diff;
              $result['direction'] = $dir;
              $result['text']  = $text;
              
              return $result;
              
          }
          
          
          // Get stock data
          //$data = processStockSite('c:\q.htm'); // Google
          $data = processStockSite('http://finance.yahoo.com/q?s=TLS.AX'); // Google
          //$data = processStockSite('http://finance.yahoo.com/q?s=MSFT'); // Microsoft
          //$data = processStockSite('http://finance.yahoo.com/q?s=AAPL'); // Apple
          //$data = processStockSite('http://finance.yahoo.com/q?s=GE'); // GE
          $output = '';
          $output .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
          <html>
          <head>
             <title>Micro Stock</title>
             <link href="style/style.css" rel="stylesheet" type="text/css" />
          </head>
          <body>
              <div id="main">
              <div id="caption"> </div>
               <div id ="icon2"></div>
                <div id="result">
          
                  '.$data['text'].'
          
                </div>
          
          </div>
          </body>';
          return $output;
          ?>
            • 27708 MODX Staff
            • 2,502 Posts
            Update: @bunk58s method works fine too.
            Are you displaying this in a document or an iFrame? Why do you have the document head in the snippet? That typically goes into the template and then the snippet call into a document/resource.

            Your snippet should look like this:
            <?php
            function getStockSite($stockLink){
               
               if ($fp = fopen($stockLink, 'r')) {
                  $content = '';
                    
                  while ($line = fread($fp, 1024)) {
                     $content .= $line;
                  }
               }
            
               return $content;  
            }
            
            function processStockSite($wurl){
                
                $wrss = getStockSite($wurl);
                $name  = '-';
                $price = '';
                $diff  = '';
                
                if (strlen($wrss)>100){
                    $spos = 0;
                    
                    // Get text
                    $spos = strpos($wrss,'</span>:',$spos)+3;
                    $spos = strpos($wrss,'<big>',$spos);
                    $epos = strpos($wrss,'</div><h1>',$spos);
                    if ($epos>$spos){
                       $text = substr($wrss,$spos,$epos-$spos);
                    } else {
                       $text = '-';
                    }
            
                    $spos = $epos + 10;
                    // Get company name
                    $epos = strpos($wrss,'<',$spos);
                    if ($epos>$spos){
                        $name = substr($wrss,$spos,$epos-$spos);
                    } 
            
                    
                    // Get actual price
                    $spos = strpos($wrss,'yfs_l10')+strlen('yfs_l10');
                    $spos = strpos($wrss,'>',$spos)+1;
                    $epos = strpos($wrss,'<',$spos);
                    if ($epos>$spos){
                        $price = substr($wrss,$spos,$epos-$spos);
                    } else {
                        $price = '-';
                    }
                    
                    // Get direction
                    $spos = strpos($wrss,'alt',$epos)+strlen('alt')+2;
                    $epos = strpos($wrss,'"',$spos);
                    if ($epos>$spos){
                        $dir = strtolower(substr($wrss,$spos,$epos-$spos));
                    } 
                    
                    // Get difference
                    $spos = strpos($wrss,'>',$epos+3)+1;
                    $epos = strpos($wrss,'<',$spos);
                    if ($epos>$spos){
                        $diff = substr($wrss,$spos,$epos-$spos);
                    } 
            
                }
                
                $result['name']  = $name;
                $result['value'] = $price;
                $result['diff']  = $diff;
                $result['direction'] = $dir;
                $result['text']  = $text;
                
                return $result;
                
            }
            
            
            // Get stock data
            //$data = processStockSite('c:\q.htm'); // Google
            $data = processStockSite('http://finance.yahoo.com/q?s=TLS.AX'); // Google
            //$data = processStockSite('http://finance.yahoo.com/q?s=MSFT'); // Microsoft
            //$data = processStockSite('http://finance.yahoo.com/q?s=AAPL'); // Apple
            //$data = processStockSite('http://finance.yahoo.com/q?s=GE'); // GE
            
            // Return the Data to a document
            $output = $data['text'];
            return $output;
            ?>
            
            


            and your document Template should look like this (this could easily be a Chunk):
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
            <html>
            <head>
               <title>Micro Stock</title>
               <link href="style/style.css" rel="stylesheet" type="text/css" />
            </head>
            <body>
                <div id="main">
                <div id="caption"> </div>
                 <div id ="icon2"></div>
                  <div id="result">
            <!--If this is a template you call the content document variable -->
            [*content*]
            
            <!--If this is a chunk you can call the Snippet directly like so-->
            [!GetStockData!]
            
            </div>
            </body>
            ?>


            and your document content should look like this:
            [!GetStockData!]
            

              Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
              • 29319
              • 37 Posts
              Quote from: bunk58 at Dec 11, 2009, 06:39 AM

              You could try something like :
              <?php
              function getStockSite($stockLink){
                 
                 if ($fp = fopen($stockLink, 'r')) {
                    $content = '';
                      
                    while ($line = fread($fp, 1024)) {
                       $content .= $line;
                    }
                 }
              
                 return $content;  
              }
              
              function processStockSite($wurl){
                  
                  $wrss = getStockSite($wurl);
                  $name  = '-';
                  $price = '';
                  $diff  = '';
                  
                  if (strlen($wrss)>100){
                      $spos = 0;
                      
                      // Get text
                      $spos = strpos($wrss,'</span>:',$spos)+3;
                      $spos = strpos($wrss,'<big>',$spos);
                      $epos = strpos($wrss,'</div><h1>',$spos);
                      if ($epos>$spos){
                         $text = substr($wrss,$spos,$epos-$spos);
                      } else {
                         $text = '-';
                      }
              
                      $spos = $epos + 10;
                      // Get company name
                      $epos = strpos($wrss,'<',$spos);
                      if ($epos>$spos){
                          $name = substr($wrss,$spos,$epos-$spos);
                      } 
              
                      
                      // Get actual price
                      $spos = strpos($wrss,'yfs_l10')+strlen('yfs_l10');
                      $spos = strpos($wrss,'>',$spos)+1;
                      $epos = strpos($wrss,'<',$spos);
                      if ($epos>$spos){
                          $price = substr($wrss,$spos,$epos-$spos);
                      } else {
                          $price = '-';
                      }
                      
                      // Get direction
                      $spos = strpos($wrss,'alt',$epos)+strlen('alt')+2;
                      $epos = strpos($wrss,'"',$spos);
                      if ($epos>$spos){
                          $dir = strtolower(substr($wrss,$spos,$epos-$spos));
                      } 
                      
                      // Get difference
                      $spos = strpos($wrss,'>',$epos+3)+1;
                      $epos = strpos($wrss,'<',$spos);
                      if ($epos>$spos){
                          $diff = substr($wrss,$spos,$epos-$spos);
                      } 
              
                  }
                  
                  $result['name']  = $name;
                  $result['value'] = $price;
                  $result['diff']  = $diff;
                  $result['direction'] = $dir;
                  $result['text']  = $text;
                  
                  return $result;
                  
              }
              
              
              // Get stock data
              //$data = processStockSite('c:\q.htm'); // Google
              $data = processStockSite('http://finance.yahoo.com/q?s=TLS.AX'); // Google
              //$data = processStockSite('http://finance.yahoo.com/q?s=MSFT'); // Microsoft
              //$data = processStockSite('http://finance.yahoo.com/q?s=AAPL'); // Apple
              //$data = processStockSite('http://finance.yahoo.com/q?s=GE'); // GE
              $output = '';
              $output .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
              <html>
              <head>
                 <title>Micro Stock</title>
                 <link href="style/style.css" rel="stylesheet" type="text/css" />
              </head>
              <body>
                  <div id="main">
                  <div id="caption"> </div>
                   <div id ="icon2"></div>
                    <div id="result">
              
                      '.$data['text'].'
              
                    </div>
              
              </div>
              </body>';
              return $output;
              ?>


              Perfect thanks a mill.