Search code examples
phpmysqlvirtuemart

php substr being ignored


i have a PHP/mySQL question related to virtuemart ecommerce - I've asked on the VM forum but the response is very slow.

I'm using this code to load products from the same category in a product page, its works well but;

<?php   // find all the other products in this category
$q = "SELECT p.product_id, p.product_name, c.category_name, c.category_flypage
   FROM #__{vm}_product p,#__{vm}_product_category_xref pc, #__{vm}_category c
   WHERE p.product_publish='Y' AND pc.product_id = p.product_id AND pc.category_id = c.category_id AND c.category_id = '$category_id' AND p.product_id != '$product_id'
   ORDER BY p.product_name ";
$db = new ps_DB;
$db->query( $q );
if( $db->next_record() )  {
   echo 'Other products in the category "'.$db->f('category_name').'"<br />';
   $flypage = $db->f('category_flypage');
   $db->reset();
   while( $db->next_record() ) {
      ?><a href="<?php  $sess->purl(URL . "index.php?page=shop.product_details&flypage=$flypage&product_id=" . $db->f("product_id") . "&category_id=$category_id" ) ?>"><?php $db->p("product_name"); ?></a><br />
      <?php
   }
}
?>

But, I cant substr the output of the product name, if I put <?php substr($db->p("product_name"), 0 12) ?> the substr is completely ignored and the whole name is still output - any one know why?


Solution

  • I suspect that $db->p prints the name directly, instead of returning it. You're also missing an echo before substr... The code should probably look like this:

    <?php echo substr($db->f('product_name'), 0, 12) ?>
    

    Here I assumed that $db->f returns the value of the field, given how it's used in the rest of the code.