Question

im writing a script to download a csv,

the form i using to send data server like below,

(the value of hidden field has domain name,namerserver1,namerserver2,namerserver3,namerserver4) some have domain name and not have ns data

<form class="dmns2" method="post">
<input type="hidden" value="wiseowldating.co.uk,ns.nothard.net,ns2.nothard.net,ns3.nothard.net" name="nsv[]" />
<input type="hidden" value="willow.nothard.net.zz" name="nsv[]" />
<input type="hidden" value="welditz.com,ns.nothard.net,ns2.nothard.net,ns3.nothard.net," name="nsv[]" />
<input type="submit" id="btnsubmit"  value="CSV Export"/>
</form>

I'm getting those values on php like below

if(isset($_POST['nsv'])){
foreach($_POST['nsv'] as $val){
    echo $val.'<br/>';
    }
exit(0);
}

the records are showing correctly as below

wiseowldating.co.uk,ns.nothard.net,ns2.nothard.net,ns3.nothard.net,
willow.nothard.net.zz,
welditz.com,ns.nothard.net,ns2.nothard.net,ns3.nothard.net,

but i want export this output to a csv files as this format

Doman                 nameserver1    nameserver2     nameserver3     nameserver4 nameserver5
wiseowldating.co.uk   ns.nothard.net ns2.nothard.net ns3.nothard.net
willow.nothard.net.zz
welditz.com           ns.nothard.net ns2.nothard.net ns3.nothard.net

this code is working, thanks for help

$fp = fopen("nsdata.csv", "w");
$row=array('Domain','NS1','NS2','NS3','NS4');
fputcsv($fp, $row);
foreach($_POST['nsv'] as $val){
    $ar=explode(',',$val);
    fputcsv($fp,$ar);
}
fclose($fp);

can anyone help me to do this using php please, i appreciate your help. thank you

Was it helpful?

Solution

Your order of arguments to the explode() function is wrong

And '\t' is not the same as "\t"

And you're missing the "\t" when you write your headers as well

$fp = fopen("nsdata.csv", "w");
$row = array('Domain','NS1','NS2','NS3','NS4');
fputcsv($fp, $row, "\t");
foreach($_POST['nsv'] as $val){
    $ar=explode(',', $val);
    fputcsv($fp, $ar, "\t");
}
fclose($fp);

OTHER TIPS

you should try tabulator as separator

fputcsv($fp, $foo, chr(9));  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top