We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23922
    • 4 Posts
    I am trying to port some php code that takes a request ( instock(x) ) and then returns wether x is in stock or out of stock (stored as boolean by ID) It works great in PHP but I have no idea how to translate it into a usable snippet/chunk combo. It only needs to receive one value (from snippet call such as [[stock &id=1]] and return the value of id 1 from the table).

    Values are stored on a table in the same DB as the MODx CMS and edited by an external script

    Clear as mud?

    from the function file
    //declare function
    function instock($x){
    //connect to db
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
    mysql_select_db($dbsel);
    // query table to find row where id=$x
    $query  = "SELECT * FROM stock WHERE id='$x'";
    $result = mysql_query($query);
    //assign $num for use later in while loop
    $num=mysql_numrows($result);
    
    // check for result return
    if (!$result) {
        die('Invalid query: ' . mysql_error());
    
    }else{
    // format result to something usable
    $i=0;
    while ($i < $num) {
    $id=mysql_result($result,$i,"id");
    $name=mysql_result($result,$i,"Name");
    $Desctiption=mysql_result($result,$i,"Description");
    $InStock=mysql_result($result,$i,"InStock");
    $i++;
    }
    // check to see if part is in stock or out of stock according to boolean value of DB
    if($InStock=="1"){
    echo "In Stock";
    }else{
    
    echo "Out of Stock";
    }
    // following is debug code
    #echo "<br>";
    #echo "<!--" . $query . "-->";
    #echo "<!-- result " . $InStock . "-->";
    }
    mysql_close($conn);
    
    }
    


    called in the PHP version as such
    <? instock(1); ?>


    if you need further explination please feel free to ask
      • 23491 ☆ A M B ☆
      • 1,056 Posts
      Nice. This is a fun one. Once you get your first snippet created, you’ll see how truly easy--and Flexible--they can be!

      Check this out for inspiration: http://wiki.modxcms.com/index.php/Creating_Snippets

        Mike Reid - www.pixelchutes.com
        MODx Ambassador / Contributor
        [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
        ________________________________
        Where every pixel matters.
        • 23922
        • 4 Posts
        Quote from: pixelchutes at Jun 25, 2007, 07:21 PM

        Nice. This is a fun one. Once you get your first snippet created, you’ll see how truly easy--and Flexible--they can be!

        Check this out for inspiration: http://wiki.modxcms.com/index.php/Creating_Snippets



        I actually looked at that and after reading it was more confused than when I started. I also forgot to mention how the table is laid out
        id  int  255  non_null auto_increment  primary_key
        Name Text 255 null
        Description Text 255 null
        InStock boolean non_null
        
          • 4041
          • 788 Posts
          This is totally untested but give it a shot:

          Take this and copy/paste into a new snippet.

          // usage:
          //  [!Snippetname?itemid=XX!]
          //  [!Snippetname?itemid =XX&debug=yes!]
          
          
          // set up your messages to print
          $Is_in_stock_message ="That part is in stock";
          $Is_not_in_stock_message ="That part is not in stock";
          $stock_not_found_message ="No stock available with that item ID#";
          
          $usedebug = isset($debug) ? $debug : "no";
          
          $output ="";
          
          $tbl = $modx->dbConfig['table_prefix'];
          $stocktable = $tbl."stock";
          
          // grab the itemid, if not set it uses the document id
          $itemid = isset($itemid) ? $itemid : $modx->DocumentIdentifier;
          
          $sql  = "SELECT * FROM ".$stocktable." WHERE id='".$itemid."';
          $result = $this->dbQuery($sql);
          $limit = count($result);
          
          if(!$result) {
             $output .= $stock_not_found_message;
          }
          
          
          // print the results
          if($limit > 0) {
                for ($x = 0; $x < $limit; $x++) {
                  $part_id = $result[$x][id];
                  $part_name    = $result[$x][Name];
                  $part_desc    = $result[$x][Description];
                  $part_instock = $result[$x][InStock];
          
                   if($part_instock =="1"){
                     $debug_output .= $part_name."<br />
                                     ".$part_desc."<br />
                                     ".part_instock.";
                     
                     $output .= $Is_in_stock_message;
                   }else{
                     $output .= $Is_not_in_stock_message;
                   }
                   
                }
          } else {
             $output .="";
          }
          
           if($usedebug =="yes" && $debug_output !=""){
            $output .=$debug_output;
            }
            
          return $output;


          Like I said, this is untested and may need a few adjustments but it should give you a starting point perhaps...
            xforum
            http://frsbuilders.net (under construction) forum for evolution
            • 23922
            • 4 Posts
            Quote from: breezer at Jun 25, 2007, 11:26 PM

            This is totally untested but give it a shot:

            Take this and copy/paste into a new snippet.

            ..snipped code..

            Like I said, this is untested and may need a few adjustments but it should give you a starting point perhaps...

            I will give that a try later tonight or tomorrow and see if it works. Thanks for the starting point laugh
              • 23922
              • 4 Posts
              Quote from: breezer at Jun 25, 2007, 11:26 PM

              ...it should give you a starting point perhaps...

              Thanks again for the code. Between what you gave me and looking through various pieces of the API documentation on the wiki I tweaked the code to work.
              <?php
              // Stock Snippet v1.0
              // usage:
              //  [!Snippetname? &itemid=XX!]
              
              // set up your messages to print
              $Is_in_stock_message ="That part is in stock";
              $Is_not_in_stock_message ="That part is not in stock";
              $stock_not_found_message ="Part ID not found in DB";
              
              $output ="";
              
              $tbl = $modx->dbConfig['table_prefix'];
              $stocktable = $tbl."partstock";
              
              // grab the itemid. if not use zero
              $itemid = isset($itemid) ? $itemid : '0';
              
              // select everything from row in stocktable where it is equal to itemid
              $result = $modx->db->select( 'id, InStock', $stocktable , 'id =' . $itemid );
              $col = $modx->db->getColumn( 'InStock', $result );
              $num = $modx->db->getRecordCount( $result );
               
              if( !$result ) {
              	$output .=$stock_not_found_message;
              }else{
              if($col="1"){
              $output .= $Is_in_stock_message;
              }elseif($col="0"){
              $output .=$Is_not_in_stock_message;
              }else{
              $output .=$stock_not_found_message;
              }
              }
              return $output;
              ?>
              


              Thanks for the help everyone.