0

This is current formula for calculating a value

<div class="common-box">
<div class="common-box-left">Approx Value </div>
<div class="common-box-right"><input name="idv" type="text" class="text-box" 
    value="'.(($rows->v_price*3.41)/100) .'" readonly=""/></div>

Now when we change it to a very basic calculation purpose with if syntax based on price range, it does not work in

<div class="common-box">
<div class="common-box-left">Approx Value </div>
<div class="common-box-right"><input name="idv" type="text" class="text-box" value="'.($a = $this->prodDet->v_price);
  $b=.02889;
  $z=.02307;
  if ($a > 500000){
    $c=$a*$z;
  } else {
    $c=$a*$b;
  }
echo(round($c) . "<br>");.'" readonly=""/>
</div>

Could someone help in - what is wrong. This may be basic for you - but am learning php - Appreciate your help !!

Edit Below is the complete code

     <?php
     define( '_JEXEC', 1);
     $database = &JFactory::getDBO();

  <2 different tables been called>
  after that below:-

      if(isset($_GET["ncvd"])){  
$NewToyVariantDetail=$_GET["ncvd"];
$sql = "SELECT * from toy_newtoy_variants where v_status='1' and v_id='".$NewToyVariantDetail."'";
$database->setQuery($sql);
$rows = $database->loadObject();
$list=' <div class="common-box">
      <div class="common-box-left">Approx Value </div>
<div class="common-box-right"><input name="idv" type="text" class="text-box" 
    value="'.(($rows->v_price*3.41)/100) .'" readonly=""/></div>
      die($list);
     }
   ?>

may advise on !!

5
  • Is this part of an echo statement? Please show the full context. Commented Jan 9, 2014 at 4:05
  • You are mixing PHP and HTML and you have syntax errors all over the place. You would really be better to just pick up a PHP for beginners tutorial somewhere and go through it. Commented Jan 9, 2014 at 4:06
  • Where is the php? Is everything echoed and surrounded by single quotes? Commented Jan 9, 2014 at 4:09
  • Hello Barmarm, Newin - thanks for replying. Complete code added. May advise on it. Sure, mark am just on to pho !! Commented Jan 9, 2014 at 4:37
  • @Ruchika $list is missing closing div and '; at the end. I've posted suggestion on coding based on your previous code and complete code. Commented Jan 9, 2014 at 5:17

3 Answers 3

1

Your code has problem at :

echo(round($c) . "<br>");

Here is the working one:

<?php
$v_price = 500;
echo '
<div class="common-box">
<div class="common-box-left">Approx Value </div>
<div class="common-box-right"><input name="idv" type="text" class="text-box" value="'.($a = $v_price);
  $b=.02889;
  $z=.02307;
  if ($a > 500000){
    $c=$a*$z;
  } else {
    $c=$a*$b;
  }
echo round($c).'" readonly=""/>
</div>';
?>

My suggestion would be to write PHP code (as you did calculation) separately , not inside html input code.

For example:

<?php
$v_price = 500;
$a = $v_price;
$b=.02889;
$z=.02307;
  if ($a > 500000){
    $c=$a*$z;
  } else {
    $c=$a*$b;
  }
$val = round($c);
?>

<div class="common-box">
<div class="common-box-left">Approx Value </div>
<div class="common-box-right"><input name="idv" type="text" class="text-box" value="<?php echo $val;?>" readonly=""/>
</div>  

Issues in your updated complete code:

- <div class="common-box">  is not closed
- <div class="common-box-right">  is missing '; at the end


<?php
define( '_JEXEC', 1);
$database = &JFactory::getDBO();

if(isset($_GET["ncvd"])){  
    $NewToyVariantDetail=$_GET["ncvd"];
    $sql = "SELECT * from toy_newtoy_variants where v_status='1' and v_id='".$NewToyVariantDetail."'";
    $database->setQuery($sql);
    $rows = $database->loadObject();
    $list=' <div class="common-box">
    <div class="common-box-left">Approx Value </div>
    <div class="common-box-right"><input name="idv" type="text" class="text-box" 
    value="'.(($rows->v_price*3.41)/100) .'" readonly=""/></div></div>';
    echo $list;
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, your code seems correct. but somehow its not working in for me. its fetching - ="<?php echo $val;?>" in the resultant output. Am checking at my end - if you have suggestion may let know . thnx
@Ruchika I think somewhere concatenation going wrong with your php and html code. just debug line by line, you will get it.
0

Try this

value = "<?php echo ($rows->v_price*3.41/100) ?>"

2 Comments

I have a feeling that whole html block at the beginning is actually part of an echo. The OP says the first block of code works.
@Mike, yeah, looking over the code some more, that makes much more sense.
0

Inline PHP works better for this ... Although, you might want to make a method a part of the class, that will calculate this for you, so you don't have so much logic put into such a small space.

<div class="common-box">
<div class="common-box-left">Approx Value</div>
<div class="common-box-right"><input name="idv" type="text" class="text-box" value="<?=round(($this->prodDet->v_price > 500000) ? $this->prodDet->v_price * 0.02307 : $this->prodDet->v_price * 0.02889));?>" readonly=""/></div>

This is the direct fix for your problem, but really there are many many issues that are being compounded into this. It's style that we need to talk about.

<?php
define('_JEXEC', 1);

$database = &JFactory::getDBO();

if(isset($_GET["ncvd"]))
{
    $NewToyVariantDetail=$_GET["ncvd"];
    $sql = "SELECT * from toy_newtoy_variants where v_status='1' and v_id='" . $NewToyVariantDetail . "'";
    $database->setQuery($sql);
    $rows = $database->loadObject();
    $list ='
        <div class="common-box">
        <div class="common-box-left">Approx Value </div>
        <div class="common-box-right"><input name="idv" type="text" class="text-box" value="'.(($rows->v_price*3.41)/100) .'"readonly=""/></div>';
        die($list);
}
?>

<?php
define('_JEXEC', 1);

$database = &JFactory::getDBO();

if(isset($_GET['ncvd']))
{
    $database->setQuery("SELECT * from toy_newtoy_variants where v_status='1' and v_id='{$_GET['ncvd']}'");
    $rows = $database->loadObject();
    echo '<div class="common-box">';
    echo '  <div class="common-box-left">Approx Value</div>';
    echo '  <div class="common-box-right"><input name="idv" type="text" class="text-box" value="' . ($rows->v_price * 3.41) / 100 . '"readonly=""/></div>';
    echo '</div>';
}
?>

This is better, however, I can't even begin to tell you how bad it is to not filter your inputs. $_GET['ncvd'] is a ticking timebomb. We need to fix that ...


I'm going to take the guess that v_id in the database is an int. As such, we use the built in filter functions to clean the input.

<?php
define('_JEXEC', 1);

$database = &JFactory::getDBO();

if(isset($_GET['ncvd']))
{
    $nvcd = filter_input(INPUT_GET, 'ncvd', FILTER_SANITIZE_NUMBER_INT);
    $sql = "SELECT * from toy_newtoy_variants where v_status='1' and v_id='{$ncvd}'";
    $database->setQuery($sql);
    $rows = $database->loadObject();
    echo '<div class="common-box">';
    echo '  <div class="common-box-left">Approx Value</div>';
    echo '  <div class="common-box-right"><input name="idv" type="text" class="text-box" value="' . ($rows->v_price * 3.41) / 100 . '"readonly=""/></div>';
    echo '</div>';
}
?>

This is the version that you should use. I don't understand why you are using die(), so you might want end the code execution there. For that, you can simply call exit if you must.

3 Comments

You missed a $c = after your ?.
I missed removing the $c as in this code example, I'm rounding the mathematical expression wrapped in the ternary operator.
Hello Mark, Mike - thanks for taking time to debug it. I have added the complete code. Could you see where am i missing bcoz by adding above code it just shows the code in the front end and not the mathematical value out of it. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.