Php data to Html table dynamically [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 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.

Related

Getting data from HTML or JavaScript object? [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 5 years ago.
Improve this question
Let's say I have a HTML table with 5k rows and 50 columns (generated from JavaScript object) and I would like to send 50 checked rows (checkbox) from client to server using HTTP (JSON). What would be more efficient? Iterating in HTML to find the checked rows or iterating trough my JavaScript object to find corresponding rows?
fields = columns (50)
values = rows (~5k)
JavaScript data object:
parent {
child: [{field1: value1, field2: value2, field3: value3, and so on...}]
}
I'm not sure what you are trying to do with this information, but interacting with the DOM is one of the slowest things you can do, so you should check the JavaScript objects.
While you generate each row, you keep a reference of the checkbox and you bind it to your data in a javascript object.
Then you add an event listener on the checkboxes: when you tick on or off a row, you push or remove the mapped data line in an array that will always be up to date and ready to be sent.

Reading HTML file 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 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");

Firebase: Convert Json string with html tags to html [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 6 years ago.
Improve this question
I have been researching for almost 2 hours now to get a simple solution of how I could easily convert a json string having html tags so that the converted string can then be rendered on a web-page without printing the tags in raw format.
This string <h1>Magazine Heading</h1>as stored in firebase in json format, displays on my web page without any change:<h1>Magazine Heading</h1>
I have tried using the jQuery Library's: JSON.parseJSON(); function, but it still doesn't work. There are so many questions and topics on this, but none are direct and effective.
NB: Not a duplicate question...other answers in related questions are too complex and vague.
First, to parse JSON you can use JSON.parse(). Second, to insert the HTML into an element you can use element.innerHTML. In your case:
element.innerHTML = '<h1>Magazine Heading</h1>';
After parseing your JSON string. You can parse this and other xml/html results using jquery's parseXML.
var xml = "<h1>Magazine Heading</h1>",
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$h1 = $xml.find( "h1" );
var text = $h1.text();
https://api.jquery.com/jQuery.parseXML/
It might seem like overkill, but you can use this to parse the simplest html to very complex ones.

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) {
...
});

Best way to store data on page for Ajax requests [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have a page been generated that comes with an array of data generated by php. I need to use some of this data for an ajax request.
I just wondered what the best way is to store the data on the page, it's not sensitive and the page doesn't involve a form. At the moment I've been using data- attributes in a hidden div but just seems a bit messey?
eg.
<div id="data" class="hidden" data-question="<?= $blah ?>" data-another="<?= $blahblah ?>"></div>
Although there is no "best" way, I would recommend JSON -- a format for storing and exchanging data, and would seem to fit your situation well.
Instead of storing arbitrary data in data-attributes on an html element, like this:
<div data-name="bob" data-age="27" data-gender="male" />
Json allows you to store them like this:
people = {
"bob" : {"age":"27", "gender":"male"},
"alice" {"age":"31", "gender":"female"}
}
You can access this data programatically through native or library calls in almost any language, especially PHP and JavaScript.
In JavaScript, JSON is accessed like this:
BobsAge = people['bob'].age;
This format allows for other programmatic access, such as looping over objects or keys, that may be difficult to perform on data-attributes of html elements.
In your case, JSON could be generated like so (although there are better ways):
<script>
var data = {
<? foreach ($data as $d){ ?>
{
"question" : "<? $d['question'] ?>",
"answer" : "<? $d['answer'] ?>"
},
<?}?>
};
</script>
There are three possibilities:
attributes: this is your approach
This is probably best for simple strings, e.g. parameters, and data that is descriptive - e.g data-popover for an element which contains popover contents. Many JS plugins are built that way.
PHP echo in <script> tag
No "additional markup" for the cost of having <script> tag in the same file (and, probably, the use of global JS variables). I wouldn't recommend that.
additional AJAX call fetching JSON with data from PHP
Best for more complicated data structures, such as nested arrays or objects.

Categories

Resources