0

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

6
  • Explode on , to convert to an array, then use PHP's fputcsv() function with a "\t" separator Commented Apr 6, 2014 at 10:15
  • thanks do you have any example for sample codes Commented Apr 6, 2014 at 10:16
  • There's examples of using fputcsv() in the PHP documentation Commented Apr 6, 2014 at 10:16
  • i used below codes,but the created csv file not have datas $fp = fopen("nsdata.csv", "w"); $row=array('Domain','NS1','NS2','NS3','NS4'); fputcsv($fp, $row); foreach($_POST['nsv'] as $val){ fputcsv($fp, $val, chr(9)); } Commented Apr 6, 2014 at 10:31
  • also tried by exploading, it only shows headers Commented Apr 6, 2014 at 10:35

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

thanks i found it, no need \t, when i put that it not separating columns :)
1

you should try tabulator as separator

fputcsv($fp, $foo, chr(9));  

3 Comments

i used below codes,but the created csv file not have datas $fp = fopen("nsdata.csv", "w"); $row=array('Domain','NS1','NS2','NS3','NS4'); fputcsv($fp, $row); foreach($_POST['nsv'] as $val){ fputcsv($fp, $val, chr(9)); }
also tried using explode, it shows only headers $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,'\t'); } fclose($fp);
@IreshaKumari Please don't post code as comments. It's unreadable. Edit your question instead. Also, any clarification should be done within / below the question, not below one of the answers.

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.