//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);
}
<? instock(1); ?>
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
id int 255 non_null auto_increment primary_key Name Text 255 null Description Text 255 null InStock boolean non_null
// 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;
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...
...it should give you a starting point perhaps...
<?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;
?>