0

I have one array that I am showing like this:

echo '<table>';
foreach ($rowData as $row => $tr) {
    echo '<tr>'; 
    foreach ($tr as $td)
        echo '<td>' . $td .'</td>';
    echo '</tr>';
}
echo '</table>';

The second and fourth columns are always the name of the column. The result is something like:

Name: John Locke - NickName: Locke

Age: 20 - Adress: Ok

See the pattern?

How can I put these array in my database?


As my database table structure is:

ID - Name - NickName - Age - Adress

I don't have a clue how to do that with the array i'm using..

--UPDATE

$table = $html->find('table', 0);
$rowData = array();

foreach($table->find('tr') as $row) {
    // initialize array to store the cell data from each row
    $flight = array();

    foreach($row->find('td') as $cell) {

        foreach($cell->find('span') as $e){
            $e->innertext = '';
        }

        // push the cell's text to the array
        $flight[] = $cell->plaintext;
    }
    $rowData[] = $flight;
}

echo '<table>';
foreach ($rowData as $row => $tr) {
    echo '<tr>'; 
    echo '<td>' . $row . '</td>'; 
    foreach ($tr as $td)
        echo '<td>' . $td .'</td>';
    echo '</tr>';
}
echo '</table>';

2 Answers 2

1

you can do that:

$insert = "";
foreach ($rowData as $row => $tr) {
        $insert .= "('".$tr[0]."','".$tr[0]."','".$tr[0]."','".$tr[0]."'),";

}
$insert = substr($insert, 0, -1)
$sql = "Insert into YourTable values ".$insert;
Sign up to request clarification or add additional context in comments.

4 Comments

@pbolduc The result is: Insert into myTable values ('ID:', '1', 'NAME:' ,'JOHN'), ('NICKNAME:','LOCKE','AGE:','20'); - How can i change to be Insert into myTable (ID, NAME, NICKNAME) values ('1', 'LOCKE', ..) ?
Can you show me your array i will make you a better solution thx. I would like to you see the index of your array. Does it is numbers or a string?
@pbolduc have you ever used simplehtml dom parser? i'm creating the array as i get the return of "find". I updated the post so you can se better.
No i never used it. I have updated my post, try to change the index of td by the one that you want exemple : tr[1] or tr[2] ...
0

As you already know what the array positions relate to, IE positions 0 through 3. You can simply iterate as you are doing now and compile a query instead.

Just drop this snippet into your code, ive knocked this up on the fly and its a bit hackish, probably a better way. Just look at what it does on your screen and see where if any it needs correcting.

$insert = '';
for ($i=0; $i<=3; $i++):

   $insert .= "'" . $tr[$i] . "'";

   if ($i !== 3):
       $insert .= ',';
   endif;

endfor;

echo $insert;

Once you are seeing what looks like a correct insert then you can (using safe methods) insert it into your database.

2 Comments

Don't know if am doing the right way (probably not), but i'm getting a lot of "$tr[$i],$tr[$i]". @Chris
Sorry i added single quotes around it, which makes php read it verbatim. you will need edited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.