I want to increase the php variable in the script

Asked 1 weeks ago, Updated 1 weeks ago, 2 views

// Arrangement for markers
        var positions = [];
        var i = 1;

        //num_rows() get as many information from each row and put it in the marker
        <?php $i = 1; ?>
        for(i = 1; i <= <?php echo $row ?>; i++){

            <?php
                $set = "set @rownum = 0;";
                mysqli_query($dbcon,$set);
                $sql = "select R.* from (select @rownum:[email protected]+1 as row, A.* from restaurants A where (@rownum:=0)=0) R where row = ".$i.";";

                $result = mysqli_query($dbcon,$sql);
                $rows = mysqli_fetch_assoc($result);
                                    $i++;
            ?>

I want to increase the value of $i in $sql by 1 as the for statement repeats the same as the value of vari, but it doesn't increase even if the kids in php write $i++ first. If $row is 3, I want the value of $i to be 1, 2, 3 when I run it, but when $sql runs, $i appears as 0,0,0.

Either way, I want to increase $i or replace $i with a variable that increases by 1.

php javascript

2022-09-20 10:56

2 Answers

So, I have to get the query result through PHP and hand it over to the JavaScript array positions, right? If that's right, you can't do it like this!!!

Basically, the code below should be enough. I haven't tested it, but please look at the basic ideas. It's not the PHP that's bad! PHP is not at fault!

<?php

// Let's write something called PDO.
$pdo = new PDO ("mysql:host=blah,port=blah", "account", "password"));

// The query you created is just the first x queries in the table. So I fix it like this.
$statement = $pdo->prepare("select R.* from restaurants R LIMIT :kkk");

// Value must not be pasted into query statements. It's a big problem because of security and bugs are frequent. Let's do something called "binding".
$statement->bindValue('kkk', $row, PDO::PARAM_INT);

// Bring the mongchang and throw it into JSON.
$statement->execute();
$rows = $statement->fetchAll();
echo 'var positions = ' . json_encode($rows) . ';';

// Up to here, PHP. From the bottom, JS. You have to separate at least this area.
// Actually, inline PHP itself is a bad idea.
?>

// From here on, JavaScript will read the positions array without a word.
positions.forEach(pos => console.log(pos));


2022-09-20 10:56

Don't mix JavaScript and code, just try PHP.

PHP works on the server and is complete with HTML. JavaScript, on the other hand, works in a client environment (browser) and is included in HTML. To sum up, create HTML with PHP and spin it in your browser... This is.

JavaScript cannot affect PHP variables. It's because the timing of the execution is different. The life cycle of the PHP variable is valid only when it is on the server.

So imagine that by the time JavaScript runs, PHP is already out there. If you don't understand this, look at the script section in the source view in your browser.


2022-09-20 10:56

If you have any answers or tips


© 2022 pinfo. All rights reserved.