richard_PHP Posted February 7, 2013 Share Posted February 7, 2013 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). 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/274157-quantity-echo-problem/ Share on other sites More sharing options...
Christian F. Posted February 7, 2013 Share Posted February 7, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274157-quantity-echo-problem/#findComment-1410755 Share on other sites More sharing options...
richard_PHP Posted February 8, 2013 Author Share Posted February 8, 2013 (edited) 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 February 8, 2013 by richard_PHP Quote Link to comment https://forums.phpfreaks.com/topic/274157-quantity-echo-problem/#findComment-1410980 Share on other sites More sharing options...
Christian F. Posted February 8, 2013 Share Posted February 8, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274157-quantity-echo-problem/#findComment-1410981 Share on other sites More sharing options...
cyberRobot Posted February 8, 2013 Share Posted February 8, 2013 (edited) 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 February 8, 2013 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/274157-quantity-echo-problem/#findComment-1410991 Share on other sites More sharing options...
Christian F. Posted February 8, 2013 Share Posted February 8, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274157-quantity-echo-problem/#findComment-1411047 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.