简体   繁体   中英

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

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.

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.

for ($i = 0; $i < count($lineData)+1; $i++)

should give you an "Undefined offset" notice, try replacing it with

for ($i = 0; $i < count($lineData); $i++)

also, I think you have to be sure that

$lineData[$index-1] = $line[0];

produces an array with all indexes from 0 to count($lineData)-1

Try setting error_reporting(E_ALL); and ini_set('display_errors', 1); at the beggining of your script. It might be that php is throwing an error and not showing it. With these commands you'll activate error reporting and show those errors.

Maybe try make something like that:

<?php
$body = 'your javasrcipr text';
for ($i = 0; $i<count($lineData); $i++) {
    print_r($body);
}?>

and check what it makes to your website

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM