Reading HTML file in PHP [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am currently a Student, developing a Website for an Online Course Planning for Monash I am Stuck with a reading HTML files in my PHP code
I need to Upload a File (Unofficial Records of a Student) and once I Click Upload, the System MUST Scan the HTML file and read through the Tables from the first (of the academic Records to the Last one) where it says Incomplete and it must evaluate the Grade, if it is a P, C, D, HD, the System will automatically Cross that unit for the Student, if the Student has Failed, it will be a N and the System must automatically highlight the Unit in order to tell that the student needs to re do the Unit
when the Academic Record is saved from the official Monash website, it saves automatically as a HTML, so i have no way in changing that
(Note that it is a student who will download in future use, not programmers, so they might not know how to convert it)

Like so - to read the file?
<?php
$text= file_get_contents('yourfile.htm');
echo $text;
?>
Once you have it grabbed, you can parse the HTML - see here:
PHP Parse HTML code

You could load the HTML into a DOMDocument and then iterate over the document nodes to extract the information.
A quick example from the top of my head (did not test it thought):
$document = new DOMDocument();
$document->loadHTMLFile("path-to-your-html-file.html");
$tableElement = $document->getElementById("your-table-id");
$allTableRows = $tableElement->getElementsByTagName("tr");
foreach($allTableRows as $tableRow) {
$allTableCellsInThisRow = $tableRow->getElementsByTagName("td");
$firstCell = $allTableCellsInThisRow->item(0);
$secondCell = $allTableCellsInThisRow->item(1);
// and so on ..
// do your processing of table rows and data here
}
Links
http://php.net/manual/de/domdocument.loadhtmlfile.php
http://php.net/manual/de/class.domnodelist.php

You can try this piece of code:
https://www.w3schools.com/php/func_string_money_format.asp
Here
<?php
$file = fopen("test.html","r");
echo fgets($file);
fclose($file);
?>
OR
Try:
readfile("/path/to/file");
OR
echo file_get_contents("/path/to/file");

Related

How to get code from another file and write everything to one file? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to use htmlspecialchars but it only accepts the address of the file, can I make it accept the code and not the address?
return "<script src='" . htmlspecialchars('static/functions.js', ENT_QUOTES, 'utf-8') . "'" . nonce() . "></script>\n";
function nonce() {
return ' nonce="' . get_nonce() . '"';
}
function get_nonce() {
static $nonce;
if (!$nonce) {
$nonce = base64_encode(rand_string());
}
return $nonce;
}
The HTML <script src= is going to fetch a resource and run it, from wherever you have specified in the src attribute. It's expecting a URL.
The htmlspecialchars( function is expecting a string, and will return a string.
The way you have it right now, it will take the string "static/functions.js" and process it.
Your question suggests you want it to operate on the contenst of the file. This is techncially possible, but the file has to be fetched first.
htmlspecialchars( file_get_contents(__DIR__ . 'static/functions.js') )
But that still seems odd. You want to convert all the encoded special characters in your JavaScript file to their HTML entities? Like < to <? That's almost surely going to make the JavaScript invalid.
Even if it didn't ruin your JavaScript, you'd still be trying to take the contents of that file and putting them in the src property of the script tag, which is expecting a URL.
Overall this seems like you need to step back and think about what exactly you're trying to accomplish.
Edit for OP question in comments
<div>
<span>Some regular HTML content</span>
</div>
<script>
<?php
// we're now in PHP, about to store some JavaScript as a string
$js = <<<EOD
console.log("I am a JavaScript console message!");
console.log("Me too!");
EOD; // note this can't be indentend or spaced, it has to be flush left
// we can 'echo' the JavaScript directly into the HTML
echo $js;
?>
</script>

Php data to Html table dynamically [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a php file that set an array of values and a table into a html page. I would like to insert the php data into this table with a limit 15 of rows so when the data reaches the limit, continue on an adjoining table:
Could JQuery be a good option to get this?.
Yes, if those data change dynamically, using jQuery would be a good solution.
i believe it would be faster, if you directly divide it using php.
here is a piece of code for your problem.
//assuming this is your database retrieval
$tableArr = array();
for($i=1; $i<=50; $i++) {
$tableArr[] = '<tr><td>'.$i.'</td><td> Name '.$i.'</td><td> Type'.$i.'</td></tr>';
}
//chunked or divided data
$dividedArray = array_chunk($tableArr, 15);
$dataTable = "";
$commonHeader = '<thead><tr><th>ID</th><th>Name</th><th>Type</th></tr></thead>';
foreach($dividedArray as $indDividedTable) {
$dataTable.= '<table border="1">';
$dataTable.= $commonHeader;
$dataTable.= implode("\n", $indDividedTable);
$dataTable.= '</table>';
}
echo $dataTable;
Not sure if you mean by "adjoining" literally "next to it" but I've used jQuery Datatables for pagination of data and it has worked out well. You may want to modify your PHP to output the data into a JSON format, or perhaps even make the data-fetch a separate query back to the server.
What does adjoining means? If you mean pagination, jQuery surely can do this using AJAX. I'm using jQuery plugin datatables
to retrieve data from PHP with JSON format. It has table pagination by default.

How to create a alert message for existing child node data in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a xml document:
<root>
<info>
<name>John</name>
<address>USA</address>
</info>
<info>
<name>David</name>
<address>Australia</address>
</info>
</root>
I need to check the names; if the name John exists in the <name> node then it should show alert that the name already exists, and if not exists the data should append in the XML document.
Any help..?
$xml = simplexml_load_file($xmlUrl);
$Entry = $xml->channel->item;
foreach ($Entry as $EntryItem){
if($EntryItem->name == 'ArrayList/Whatever') {
echo "ignoring $EntryItem->name";
} else {
// Whatever code
}
}
Thats most likely a start of what you'd need to be doing. I'm not too sure where you're getting your list of names or how you want to be alerted of such, based off of that php can not give an alert box but javascript can. You can use javascript after to check for information for maybe a hidden list then alert that way. Theres far too many ways of doing so.

Filter out JSON for only columns I need using jQuery/javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm using this to parse out the JSON coming from PHP (age_get.php) and it's working great.
$.getJSON('age_get.php', function(data) {
var ticks1=[]
$.each(data, function(key, val) {
ticks1.push("["+val.index+",'"+val.value+"']");
});
var ticks6 =[(ticks1.join())]
The output is this, which as I said is fine.
["[90,'18-24'],[91,'25-29'],[92,'30-34'],[93,'35-39'…'60-64'],[99,'65-69'],[100,'70-74'],[101,'75-99']"]
In my PHP file, the mySQL query part is this:
$sql = "select * from advanced_data where category like 'age range'";
So, basically, I have the PHP already "filtering" the JSON from a much larger table (i.e., with many more columns).
There has to be a better way than creating individual PHP files for each time I need something out of this database, but I'm pretty new to this.
So, the question is, can I have a single PHP file with a query more like this:
$sql = "select * from advanced_data;
And then in my HTML file/jQuery have something that essentially filters out the JSON for what I need similar to how "where category like 'age range'" works in my PHP file.
Hope that's clear. Any thoughts would be appreciated.
Yes you can do that technically, but it is not a good idea. When using ajax, it is good to keep the data small.
Instead of get everything from php and search it with js loop, you can improve your php script to let it accept queries.
In your php:
<?php
$whereCategory = isset($_GET['category'])? "where category like '{$_GET['category']}'" : '';
$sql = "select * from advanced_data {$whereCategory};";
...
In your ajax:
$.getJSON('age_get.php?category=testCategory', function(data) {
...
});

Why is a json_encoded object ending up as an array in javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am passing some data to javascript using PHP in a project I am working on it. In my PHP I have:
<?php
$allprojects = $pages->find('projects')->children();
$transfer = array();
$i=0;
foreach ($allprojects as $p) {
$transfer[] = array(
'i' => $i,
'title' => $p->title()->value,
'url' => $p->url(),
'thumb' => thumb($p->children()->first()->images()->first(), array('width'=>170))
);
$i++;
}
?>
<script>
var projects = <?php print json_encode($transfer); ?>
</script>
If I then try to use this variable projects it is an array. I.e.:
projects instanceof Array == true
I am a bit confused as to why this is the case. Can anyone illuminate the situation?
JSON is a data interchange format for representing an object or a list of objects as a string. In your case you're using the format to interchange your list (specifically an array) $transfer[] from the PHP running server-side to the Javascript on the client side.
If you view the resulting HTML source in your broswer you will see that the line:
var projects = <?php print json_encode($transfer); ?>
has been rendered as, for example:
var projects = [{i: 1, title: "Example", url: "www.example.com", thumb: "example.jpg" }];
so as far as JavaScript is concerned this line is defining projects as an array (using []) of objects (using {}). So you have successfully used the format to exchange the array of objects you started with from the server to the client.
If you want to get the JSON format of it into a variable on the client side again, perhaps in order to pass it back to the server somehow, you can use:
var projectsJSON = JSON.stringify(projects);

Categories

Resources