Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML with PHP - <script>-section code suddenly ends - bug?

Tags:

I encountered a very strange behaviour in php-html-mixed code. I'm using XAMPP 3.2.1 (PHP 5.2.0) and IntelliJ IDEA 14.1.

This is what my code looks like (scrubbed for readability, if you need more let me know):

<?php for($i=0; $i<count($stringArray); $i++) {     $pieces = explode($GLOBALS['delimiter'], $lineData[$i]); ?>     <div>         ...         <input id="<?php echo $pieces[$someValidNumber]; ?>_identifier" ...>         ...         <script>             // some javascript with <?php echo $variable; ?>         </script>        ...     </div> <?php } ?> 

What happens when that loop runs n times, that for n-1 everything looks fine, but in the n-th run, within the <script>-section the code suddenly stops. The HTML-File ends properly with all tags closing.

This looks as following (n=4):

$('input[id$="MegaSteel_tons"]').val(output2); $('#MegaSteel_cart').prop( 

Or (n=2):

$('input[id$="BarZwo_meters"]').val(output2); $('#BarZwo_cart').prop('type', 'button').change 

Note that with an increasing n, the stop does not occur later in a deterministic way. That means when I tried n=3, the below was the result:

$('input[id$="Bar_meters"]').val(output2); $('#Bar_cart').prop('type', 'button').change(); var price 

I'm at the end of my knowledge. What causes this?


As requested more code:

$lineData = array(); $f = fopen('products.csv', 'r'); while (($line = fgetcsv($f)) !== false) {     if (strpos($line[0], $productLine) !== false) {         // the above produces single value arrays, thus we access them with [0]         $pieces = explode($GLOBALS['delimiter'], $line[0]);         $index = (int)$pieces[2];         // todo: input must check that index is not already taken         $lineData[$index-1] = $line[0];     } } fclose($f);  ksort($lineData);  for ($i = 0; $i < count($lineData); $i++) {      $pieces = explode($GLOBALS['delimiter'], $lineData[$i]);      $prod_name = $pieces[0];     $prod_lineNumber = $pieces[2];     $prod_quantity = $pieces[3];     $prod_tons = $pieces[4];     $prod_meters = $pieces[5];     $prod_pricePerTon = $pieces[6];          ?>         <p>             <!-- User-Input-->             <b> <?php echo $pieces[0]; ?></b> - <?php echo $prod_lineNumber; ?><br/>             Units: <input id="<?php echo $prod_name; ?>_quantity" type="text">             Tons: <input id="<?php echo $prod_name; ?>_tons" type="text">             Meters: <input id="<?php echo $prod_name; ?>_meters" type="text">             Price per ton: <?php echo $prod_pricePerTon; ?>             Calculated price: <span id="<?php echo $prod_name; ?>_price">0</span>             <input id="<?php echo $prod_name; ?>_cart"                    type="hidden" value="Add to shopping cart!"                    onclick="addToCart('<?php echo $prod_name; ?>')">             <!-- Auto-Update-->             <script>                 // first field - quantity                 $('input[id$="<?php echo $prod_name; ?>_quantity"]').on('keyup', function () {                     var value = parseFloat($(this).val());                     var output1 = value * <?php echo $prod_tons . " / " . $prod_quantity; ?>;                     var output2 = value * <?php echo $prod_meters . " / " . $prod_quantity; ?>;                     $('input[id$="<?php echo $prod_name; ?>_tons"]').val(output1);                     $('input[id$="<?php echo $prod_name; ?>_meters"]').val(output2);                     $('#<?php echo $prod_name; ?>_cart').prop('type', 'button').change();                     var price = output1 * <?php echo $prod_pricePerTon; ?>;                     $('#<?php echo $prod_name; ?>_price').text(price);                 });                 // second field - tons                 $('input[id$="<?php echo $pieces[0]; ?>_tons"]').on('keyup', function () {                     var value = parseFloat($(this).val());                     var output1 = value * <?php echo $prod_quantity . " / " . $prod_tons; ?>;                     var output2 = value * <?php echo $prod_meters . " / " . $prod_tons; ?>;                     $('input[id$="<?php echo $prod_name; ?>_quantity"]').val(output1);                     $('input[id$="<?php echo $prod_name; ?>_meters"]').val(output2);                     $('#<?php echo $prod_name; ?>_cart').prop('type', 'button').change();                     var price = value * <?php echo $prod_pricePerTon; ?>;                     $('#<?php echo $prod_name; ?>_price').text(price);                 });                 // third field - meters                 $('input[id$="<?php echo $pieces[0]; ?>_meters"]').on('keyup', function () {                     var value = parseFloat($(this).val());                     var output1 = value * <?php echo $prod_quantity . " / " . $prod_meters; ?>;                     var output2 = value * <?php echo $prod_tons . " / " . $prod_meters; ?>;                     $('input[id$="<?php echo $prod_name; ?>_quantity"]').val(output1);                     $('input[id$="<?php echo $prod_name; ?>_tons"]').val(output2);                     $('#<?php echo $prod_name; ?>_cart').prop('type', 'button').change();                     var price = output2 * <?php echo $prod_pricePerTon; ?>;                     $('#<?php echo $prod_name; ?>_price').text(price);                 });             </script>         </p>     <?php } ?> 

The delimiter accessed through the global variable is ;. It is defined in a file called functions.php, that is included through require_once("functions.php); in the index.php (code above).

The following shows the text file being parsed (note that this is not the best solution, but is the first incremental step towards a full blown database).

Foo;Steel;1;20;30;40;4500.3 Bar;Copper;2;20;30;40;4500.3 BarFoo;Steel;3;20;30;40;4500.3 FooBar;Steel;2;20;30;40;4500.3 FooBear;Steel;4;20;30;40;4500.3 

Note that the products (Foo, Bar, ...) are grouped by their product lines (Steel, Copper, ...) and then sorted by the numbers in column 3 (third value in the ;-seperated rows).

Accessing the steel-group echo $lineData[$i] shows the following:

Foo;Steel;1;20;30;40;4500.3 FooBar;Steel;2;20;30;40;4500.3 BarFoo;Steel;3;20;30;40;4500.3 

This is as expected exactly the same as in the file being parsed.


Update: Changing to another php version (5.4, 5.6) does not resolve the issue.


Update: In Powershell "C:\xampp\php\php.exe index.php | Out-File test.html" produced an html file, that did NOT have the issue described above. So there is a workaround. I will digest further into IntelliJ IDEA.

In the meantime I also removed the <p>...</p> tags which did not fix the issue.

like image 239
michaelbahr Avatar asked Jul 15 '15 18:07

michaelbahr


People also ask

How to error check PHP?

The quickest way to display all php errors and warnings is to add these lines to your PHP code file: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); The ini_set function will try to override the configuration found in your php. ini file.

Where to put PHP script in HTML?

A PHP script can be placed anywhere in the document. The default file extension for PHP files is " .php ". A PHP file normally contains HTML tags, and some PHP scripting code.

Is it possible to change the error handler to apply for only some errors?

It is possible to change the error handler to apply for only some errors, that way the script can handle different errors in different ways.

How to write Hello World in PHP?

Here is an example of how to print "Hello World!" in PHP. Code: <? php echo "Hello World!"; ?>


1 Answers

Do you want a fish or to learn to fish ?

"Make it simple, as simple as possible. No more." A.Einstein

Okay, it is maybe a simple answer, but true. You cannot maintain/debug such a spaghetti code in a reasonnable amount of time. Look at the time you loose about this issue. Even with a bounty, you get few luck to solve it, cause talented people dont pay time on such a code.

Your code is clean. No critics. But your method is not. I experienced such cases very often in 10 years of coding. Hours lost, energy expenses. Clarify it all, simplify, synthetize, the failure will appear. Your code will be stronger.

That's my genuine answer.

like image 132
3pic Avatar answered Sep 21 '22 10:09

3pic