Jump to content

Quantity 'echo' problem


richard_PHP

Recommended Posts

Hello all,

 

After putting in a bit of short integer coding into what is an invoice page, the original string is echoed above where it should appear (see image).

 

probs.jpg

 

As indicated by the green arrow, the 6.00 is what showed up before the code was put in, the code addition now has the 'Qty' column displaying how I want, but has the '6.00' showing.

 

I've had a look at the code and cannot find anything in the code which is causing this.

 

Help much appreciated!

 

Code:

  echo '		   </td>' . "\n" .
	   '	  <td class="dataTableContent" align="right" valign="top">' . $currencies->format(tep_add_tax($order->products[$i]['final_price'], $order->products[$i]['tax']), true, $order->info['currency'], $order->info['currency_value']) . '</td>' . "\n" .
 $num = $order->products[$i]["qty"];
 if(intval($num) == $num){
  $num = intval($num);
  }
 echo '<td class="dataTableContent" valign="top" align="right">' . $num . '</td>' . "\n" .
	   '				    <td class="dataTableContent" align="right" valign="top">' . $currencies->format(tep_add_tax($order->products[$i]['final_price'], $order->products[$i]['tax']) * $order->products[$i]['qty'], true, $order->info['currency'], $order->info['currency_value']) . '</td>' . "\n";
  echo '		  </tr>' . "\n";
  }

Link to comment
Share on other sites

Have a look at the source code, that'll tell you exactly where that number was echo'd out. Then you can match that against the PHP source, to see what statement is the likely culprit.

The reason the number shows on top, is that the browser automatically displays anything that's not inside table cells on top of the table. Even if it's right in the middle of the HTML code for said table. That's also why you have to check the page source.

Link to comment
Share on other sites

Thanks for the reply. I've looked at the source code and compared it with the PHP code and the only thing that is standing out which could be the cause is the IF statement itself. With a limited amount of knowledge in PHP I've tried to change it around as far as I can but nothing is working.

 

Any further help/hints?

 

Thanks

 

EDIT: This is the source code around the problem area:

 

<td class="dataTableContent" align="right" valign="top">£1.75</td>
6.00<td class="dataTableContent" valign="top" align="right">6</td>
                   <td class="dataTableContent" align="right" valign="top">£10.50</td>
         </tr>
<tr><td colspan="8"><hr /></td></tr>

Edited by richard_PHP
Link to comment
Share on other sites

Afraid not without being able to see the relevant HTML source myself, I'm afraid.

 

That said, your if-test is quite nonsensical, int that it will always trigger. PHP uses the same rules to autocast a string to an int, as it uses when using intval (), and it will always autocast an string to an in when comparing them against each other.

So you can just remove the whole IF-test, and just leave $num = intval ($num); in your code: Will do exactly the same.

Link to comment
Share on other sites

The problem is caused by how you're concatenating multiple lines of code (see comment in the code below).

 

<?php
echo ' </td>' . "\n" .
' <td class="dataTableContent" align="right" valign="top">' . $currencies->format(tep_add_tax($order->products[$i]['final_price'], $order->products[$i]['tax']), true, $order->info['currency'], $order->info['currency_value']) . '</td>' . "\n" . //<-- the concatenation character here causes $num to be included in the echo
$num = $order->products[$i]["qty"];

 

This type of problem is easier to avoid if you the usage of the concatenation character across multiple lines of code. My preference would be to do something like the following:

 

<?php
echo '</td>' . "\n";
echo '<td class="dataTableContent" align="right" valign="top">' . $currencies->format(tep_add_tax($order->products[$i]['final_price'], $order->products[$i]['tax']), true, $order->info['currency'], $order->info['currency_value']) . '</td>' . "\n";
$num = $order->products[$i]["qty"];
?>

Edited by cyberRobot
Link to comment
Share on other sites

Ah, nicely spotted, cyberRobot. Thought it was just one (of the many) spots on this crappy notebooks screen.

 

Another way, or rather ways, of doing it is either by using a template engine, or sprintf (). The latter would look something like this:

<?php

// TODO: Finish this template.
$itemLineTemplate = <<<OutHTML
<tr>
	<!-- Add the rest of your HTML code for the table row. -->
	<td class="dataTableContent" align="right" valign="top">%1\$s</td>
        <td class="dataTableContent" valign="top" align="right">%2\$d</td>
	<td class="dataTableContent" align="right" valign="top">%3\$s</td>
</tr>
OutHTML;

// I'm just assuming this is how the loop is started.
foreach ($orders as $order) {
/*
 * TODO: Add the rest of the code.
 */

// Store the data in shorter variables to declutter the code.
$price = $order->products[$i]['final_price'];
$tax = $order->products[$i]['tax']);
$currency = $order->info['currency'];
$currencyValue = $order->info['currency_value'];
$quantity = $order->products[$i]['qty'];

// Retrieve and validate the number of items
$num = intval ($order->products[$i]["qty"]);

// Calculate the prices in advance, to declutter to code.
$unitPrice = $currencies->format(tep_add_tax (tep_add_tax ($price, $tax), true, $currency, $currencyValue);
$sum = $currencies->format(tep_add_tax (tep_add_tax ($price, $tax) * $quantity, true, $currency, $currencyValue);

// Add the item detail to the output, using the previously defined template.
// TODO: Add any other variables you might have here.
$output .= sprintf ($itemLineTemplate, $unitPrice, $num, $sum);
}

 

Notice how much easier it is to read this? That is a great help, not only to other people, but for your as well. After all, the easier it is to read code, the smaller the likelihood of you missing a bug. ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.