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>';
kenorb
  • 155,785
  • 88
  • 678
  • 743
ziad.ali
  • 321
  • 4
  • 20

2 Answers2

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;
Pierre-Luc Bolduc
  • 485
  • 1
  • 5
  • 18
  • @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', ..) ? – ziad.ali Jul 27 '15 at 12:37
  • 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? – Pierre-Luc Bolduc Jul 27 '15 at 13:33
  • @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. – ziad.ali Jul 27 '15 at 13:56
  • 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] ... – Pierre-Luc Bolduc Jul 27 '15 at 14:03
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.

CodingInTheUK
  • 930
  • 7
  • 16