+ 11

How would you translate this from JavaScript to PHP?

JS: 1. element.length; - Count the similar HTML elements 2. element[0] - Get the specific element with array 3. for loop - I want to execute INSERT query based on the table row's length. I have multiple table row to INSERT into the SQL. And I want to insert them all at once with different IDs. I can imagine doing it with JavaScript but I only have basic knowledge with PHP. Here how I should do it in JavaScript: for(var i = 0; i < element.length; i++){ // The insert query to store data } Now, that for loop, how could I translate that to PHP?

16th Oct 2018, 11:43 PM
ᴰᴼᴹᴵᴺᴼ
ᴰᴼᴹᴵᴺᴼ - avatar
7 Antworten
+ 9
LONGTIE👔 What's the length? I want to get the length of the table's row but I don't know the syntax in PHP.
17th Oct 2018, 2:14 AM
ᴰᴼᴹᴵᴺᴼ
ᴰᴼᴹᴵᴺᴼ - avatar
+ 8
Domino For your first and second questions. I afraid you are working the way the other way round, which is not common on php. Php is server script, server response with html code when getting php request from client. Html are running on client devices by their browser. For getting HTML element count, JavaScript is needed on client side. But PHP does not need to process any code to get HTML element count, since PHP is the code that produces HTML elements in the first place.
18th Oct 2018, 2:40 PM
Calviղ
Calviղ - avatar
+ 3
<?php for ($a = 0; $a < length ; $a++) { // the insert query to store data } ?>
17th Oct 2018, 1:54 AM
LONGTIE👔
LONGTIE👔 - avatar
+ 3
<? $file=file_get_contents('htmlwithtable.html'); $tofind ='<tr>'; $count=substr_count($file, $tofind); $i=0; for(..){ ... }
17th Oct 2018, 11:14 AM
Hos Sein
Hos Sein - avatar
+ 3
Domino Are you attempting to make an HTTP request via PHP to dynamically load and parse HTML from a webpage and capture that data to store in a database? Or... are you working on a web page from which you want to post data to a PHP page to be stored in a database? Each scenario requires a different approach. The first scenario involves parsing the HTML from within PHP and the other involves working with the $_POST collection on PHP. Building on what Calviղ stated, the web browser loads the HTML into a document object model (DOM) which makes it very easy to navigate via a Javascript API. References to these DOM elements can be collected into Javascript arrays, which is how the Javascript in questions #1 and #2 are supported within the browser. Therefore, you'll need an HTML Parser for PHP to replicate how the browser loads HTML into a DOM, which is a data object containing a hierarchical structure of HTML element nodes that can be collected into PHP arrays. https://stackoverflow.com/q/3577641
18th Oct 2018, 4:00 PM
David Carroll
David Carroll - avatar
+ 1
Domino i dont know the extent of the element.length function, so i cant of a similar function in php. sorry i cant help you more.
17th Oct 2018, 2:35 AM
LONGTIE👔
LONGTIE👔 - avatar
+ 1
for ($i=0; $I<length;$I++){ //The insert query to store data }
17th Oct 2018, 3:20 PM
DONALSOFT
DONALSOFT - avatar