0

I have an table that looks like this:

<table class="tableclass">
    <tr>
        <th></th>
        <th> Totals/Avg </th>
        <th><a href='https://url/id=10607' > Column 1 </a></th>
        <th><a href='https://url/id=11658' > Column 2 </a></th>
        <th><a href='https://url/id=12562' > Column 3 </a></th>
        <th><a href='https://url/id=13278' > Column 4 </a></th>
        <th><a href='https://url/id=14696' > Column 5 </a></th>
        <th><a href='https://url/id=16255' > Column 6 </a></th>
    </tr>
    <tr>
        <td><b> Row Info 1 </b></td>
        <td> 5,400.00 </td>
        <td> 900.00 </td>
        <td> 900.00 </td>
        <td> 900.00 </td>
        <td> 900.00 </td>
        <td> 900.00 </td>
        <td> 900.00 </td>
    </tr>
    <tr>
        <td><b> Row Info 2 </b></td>
        <td> 6,000.00 </td>
        <td> 1,000.00 </td>
        <td> 1,000.00 </td>
        <td> 1,000.00 </td>
        <td> 1,000.00 </td>
        <td> 1,000.00 </td>
        <td> 1,000.00 </td>
    </tr>
    <tr>
        <td><b> Row Info 3 </b></td>
        <td> 535,547 </td>
        <td> 91,186 </td>
        <td> 90,148 </td>
        <td> 89,694 </td>
        <td> 90,900 </td>
        <td> 90,662 </td>
        <td> 82,957 </td>
    </tr>
</table>

Using the information I found here: How to parse this table and extract data from it?

I was able to get the data into arrays:

Array
(
    [0] => 
    [1] => Array
        (
            [] => Row Info 1
            [Totals/Avg] => 5,400.00
            [Column 1] => 900.00
            [Column 2] => 900.00
            [Column 3] => 900.00
            [Column 4] => 900.00
            [Column 5] => 900.00
            [Column 6] => 900.00
        )

    [2] => Array
        (
            [] => Row Info 2
            [Totals/Avg] => 6,000.00
            [Column 1] => 1,000.00
            [Column 2] => 1,000.00
            [Column 3] => 1,000.00
            [Column 4] => 1,000.00
            [Column 5] => 1,000.00
            [Column 6] => 1,000.00
        )

    [3] => Array
        (
            [] => Row Info 3
            [Totals/Avg] => 535,547
            [Column 1] => 91,186
            [Column 2] => 90,148
            [Column 3] => 89,694
            [Column 4] => 90,900
            [Column 5] => 90,662
            [Column 6] => 82,957
        )

)

What I need to do, is store the data into mysql, laid out like with the actual data in place of the Row Info:

Column 1 - Row Info 1 - Row Info 2 - Row Info 3

I've been banging my head on this all night soo here Iam.

Edit: What I need to do is basically arrange the array so I can store it as such

Array
(
    [0] => 
    [1] => Array
        (
            [] => Column 1
            [Row Info 1] => data
            [Row Info 2] => data
            [Row Info 3] => data
        )

    [2] => Array
        (
            [] => Column 2
            [Row Info 1] => data
            [Row Info 2] => data
            [Row Info 3] => data
        )

    [3] => Array
        (
            [] => Column 3
            [Row Info 1] => data
            [Row Info 2] => data
            [Row Info 3] => data
        )
        ...etc

)
Community
  • 1
  • 1
Micheal Luttrull
  • 145
  • 1
  • 1
  • 8
  • 2
    Which database library are you using? PDO, MYSQLI, MYSQL? And what have you tried so far? – Jordy Dec 04 '14 at 12:24
  • 1
    not overly sure that Im on the right track with what you want, but this might help http://stackoverflow.com/questions/9299658/best-way-to-store-an-array-in-mysql-database – DevDonkey Dec 04 '14 at 12:25
  • @Jordy I'm using Mysqli for the library. I haven't attempted to store it yet because I've got no clue how to re-arrange the data the way I'd like. – Micheal Luttrull Dec 04 '14 at 12:31
  • @MattHolbrook-Bull I thought about using serialize, but what I need is to be able to pull each field of data based on the Columns. Updating the post for clarification. – Micheal Luttrull Dec 04 '14 at 12:32

1 Answers1

3

You can use regex like this:

simply store your HTML tags in PHP variable as:

    $details = '<table class="tableclass"> <tr> <th> </th> <th> Totals/Avg </th> <th> <a href='https://url/id=10607' > Column 1 </a> </th> <th> <a href='https://url/id=11658' > Column 2 </a> </th> <th> <a href='https://url/id=12562' > Column 3 </a> </th> <th> <a href='https://url/id=13278' > Column 4 </a> </th> <th> <a href='https://url/id=14696' > Column 5 </a> </th> <th> <a href='https://url/id=16255' > Column 6 </a> </th> </tr><tr> <td> <b> Row Info 1 </b> </td><td> 5,400.00 </td><td> 900.00 </td><td> 900.00 </td><td> 900.00 </td><td> 900.00 </td><td> 900.00 </td><td> 900.00 </td></tr><tr> <td> <b> Row Info 2 </b> </td><td> 6,000.00 </td><td> 1,000.00 </td><td> 1,000.00 </td><td> 1,000.00 </td><td> 1,000.00 </td><td> 1,000.00 </td><td> 1,000.00 </td></tr><tr> <td> <b> Row Info 3 </b> </td><td> 535,547 </td><td> 91,186 </td><td> 90,148 </td><td> 89,694 </td><td> 90,900 </td><td> 90,662 </td><td> 82,957 </td></tr>';

after that use preg_match_all() as:

preg_match_all('/info.*?<td>(.*?)<\/td><td>(.*?)<\/td><td>(.*?)<\/td><td>(.*?)<\/td><td>(.*?)<\/td><td>(.*?)<\/td><td>(.*?)<\/td>.*?<\/tr>/is',$details,$matches);

In result you will found array which contain your row info 1,2 & 3 value respectively.

you can check your result by using

print_r($matches);

now you can store your value into database.

Thanks.

Yash
  • 1,446
  • 1
  • 13
  • 26
  • I've read multiple times and places on stack not to parse HTML with regex though. Also the "Row Info #" and "Column #" are just placeholders I used when posting question, they usually are various words like city names etc. – Micheal Luttrull Dec 04 '14 at 13:43
  • 1
    replace '/info.*?' to '.*?' in previous preg_match_all(). you can take city,name etc values as well. – Yash Dec 04 '14 at 13:50