Question

I'm trying to store the values of the table in to a csv file for which I used fputcsv function. However it puts the elements into a single column. I need to store the values in three columns.

Code:

foreach ($total as $key => $value) 
{
        foreach ($final as $key1 => $value1)
        {
            if($value == $value1)
            {
            $total[$key] = null;
            break;
            }
        }
}
foreach($total as $key =>$value)
{   
    if(($total[$key] != null) && (!preg_match("/^bb_demo_/",$total[$key])))
    {
    array_push($check, $value,$ext1[$key], $ext2[$key]);
    echo "<table border='1' width='400px' cellpadding='1' cellspacing='1'>";
    echo "<tr><td width='100px' align='center'><strong>$value</strong></td>";
    echo "<td width='100px' align='center'><b>";
    echo $key+1;
    echo "</td>";
    echo "<td width='100px' align='center'>";
    echo $ext1[$key];
    echo "</td>";
    echo "<td width='100px' align='center'>";
    echo $ext2[$key];
    echo "</td>";       
    }
}

I tried to get values from a csv file perfom certain operations on it and displayed them as a table in html. Now I want these values stored in html table to be stored in a csv file So I created a new array $check and used the following code:

$file = fopen("contacts.csv","w");
foreach($check as $values)
{
    fputcsv($file,preg_split('//',$values));
}

fclose($file);

which stores values into one column

Im thinking whether is there a way that I separate the first three elements separated by commas using 'preg_split' i.e. using preg_split('/,,,/)? I know the above expression is incorrect But I don't have a clue of doing it? Could someone help me with this? Thanks in advance

Was it helpful?

Solution

I considered three different loops to store the values of $value, e used a for loop as follows

Code for($i=0; $i < sizeof($check); $i++) {

fputcsv($file, array($check[$i], $check1[$i], $check2[$i])) ; }
fclose($file) ;

It works!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top