I need to get the data out of all of the table cells in the 4th row of the 4th table on an HTML page. After researching for a while, it seems that using DOMXPath is the best way to parse the HTML file. However, no IDs or classes are used anywhere in the file. What would be the best way to get the data out of these cells?
Thanks in advance.
You can specify an index when fetching with XPath. In your case
/html/body/table[4]/tbody/tr[4]/td
Note that an XPath index is not zero-based, but one-based.
If you are familiar with jQuery syntax, have you looked into phpQuery?
Related
I have a question, I want to have a dynamic table in php without database. The number of rows and columns in the table should be easy to adjust.
I want to make a variable of the rows and columns.
How can I make this? Is here a tutorial about?
thanks in advance.
You can use a number of ways to store your data. If you choose not to use a database then saving the data to file after encoding it would probably the next most common way.
Take a look at the XML example I've included here, let me know if you have any more questions. There is definitely more than one way you can do this.
PHP XML to dynamic table
I have a html table, generated by another website that I'm trying to convert to a php array.
I can not convert it using simplexml because the code of the generated table is not valid, and cause a lot of errors, also I need to keep some attributes of the table td elements, and remove the others.
What would be the most efficient way of doing this? Or do you know any php class that could help me achieve this?
BTW: What I'm trying to do is convert an school schedule to a php array, that I will be able to exploit after.
Here is an example of the data I retrieve: http://paste2.org/p/1869193
Btw, using php strip tags, I already remove the unnecessary tags such as spans and fonts.
You can also use PHP's Tidy if installed (it is by default on some installs) - it not only cleans up the HTML, but also lets you traverse the DOM:
http://www.php.net/manual/en/book.tidy.php
You can find a list of HTML parserd in the answers of the following question on SO:
Robust and Mature HTML Parser for PHP
Does php has datagridview object or something? Or do I just make a table out of it? I need to do this format, http://replays.mineski.net/.. Do you think XML as a information storage is suitable for this?
well, html table is itself some kind of datagrid. So just use html table, tr, td to achieve what you want, additionally, use th for table headers. Good luck.
You could use something like PEAR datagrid http://pear.php.net/package/Structures_DataGrid, there are also some nice javascript based ones you can use eg: http://www.trirand.net/demophp.aspx
I've been racking my brain about an easy way to generate an HTML table with rowspans with PHP that would look like this, numbers indicating each cell's rowspan:
I noticed that there is sort of a pattern in the rowspans if you go from left to right, top to bottom: 1,3,7,1,1,1,1,1,3,1,1. I'm not sure if that matters.
Also, I wonder if it's possible to use any method for the table above for a table like this:
maybe addressing your second example first is easier, but it might be adapted to the first example.
the structure of the tables in html is obtained using the rowspan an colspan properties.
try to picture the table as a closet, the rows as drawers and cells as boxes inside those drawers.
with these properties, u can "merge" the cells throughout rows and/or columns.
my approach would be to set a minimal block with the appearance and data that you need, ie your second example, and write it down in html.
the "php-cycle-the-thing-up" part would require you to use that html "template" in the loop dropping your recordset data in the td portions of the html.
in that way, i think you might get what your looking for.
on the other side, if you need the php to create the table dynamically, and merge the cells automatically in a set pattern, or even ignoring the pattern and simply adapt the structure as the data is coming, it would probably be better to look at the structure from the end.
because of the way that tables are built with html, nesting table-tr-td, building cycles with this look is tricky. have you considered simply nesting divs?
i'm new to stackoverflow, i wanted to comment but by the time i finished this, i couldn't remove the answer nor comment the question, sorry if this didnt quite help you... :S
write down, come up with the recursion formula for the table. you need to come up with the math with the least number ideally ZERO conditional statements.
it's not that hard. for a desktop, i use three columns if it detects a mobile devices, my php automatically switches to 1 or 2 columns depending upon screen size
I am new to php and am asking for some coding help. I have little experience with php and have gone to the php.net site and read couple books to get some ideas on how to perform this task.
There seems to be many functions and I am confused on what would be the best fit. (i.e. fgetcsv, explode(), regex??) for extracting data in the file. THen I would need assistance printing/display this information in orderly fashion.
Here is what I need to do:
import, readin txt file that is
delimited (see sample)
The attributes are not always ordered and some records will have missing attributes.
Dynamically create a web table (html)
to present this data
Sample records:
attribute1=value;attribute2=value;attribute3=value;attribute4=value;
attribute1=value;attribute2=value;attribute4=value;
attribute1=value;attribute2=value;attribute3=value;
How do I go about this? What would be best practice for this? From my research it seems I would create an array? multidimensional? Thank you for your time and insight and i hope my question is clear.
Seems like homework, if so best to tag it as such.
You will want to look into file(), foreach() and explode() given that it is delimited by ;
The number of attributes should not matter if they are missing, but all depends on how you setup the display data. Given that they are missing though, you will need know what is the largest amount of attributes to setup the table correctly and not cause issues.
Best of luck!
i would first use the file() method, which will give you an array with each line as an element. Then a couple of explodes and loops to get through it all,first exploding on ';', then loop through each of these and explode on '='.