使用php分隔字符串

时间:2012-06-26 18:41:22

标签: php string variables explode

我有类似于此的刺痛

$_POST["ids"]='ABC,DEF,GHI'

我想分离三个变量中的每一个,然后通过while循环运行分离的结果。

foreach( explode( ',', $_POST["ids"]) as $Client_ID)
{
  $sql_qry="select *
            from   ca_client_statement
            where  client_id='".$Client_ID."' and trading_period_month like '".$TP_Month."'";
  $sql_err_no=sql_select($sql_qry,$sql_res,$sql_row_count,$sql_err,$sql_uerr);

  $row = mysql_fetch_assoc($sql_res);
  $bytes = $row['pdf_statement'];
  header("Content-type: application/pdf");
  header('Content-disposition: attachment; filename="'.$Client_ID.'statement'.$TP_format.'.pdf"');
  print $bytes;
}  

这只会为ABC生成一个声明。不是三个

非常感谢

1 个答案:

答案 0 :(得分:1)

是的,但我建议您在致电foreach后使用while循环代替explode()循环,如下所示:

foreach( explode( ',', $string) as $value)
    echo $value . ' ';

这将打印ABC DEF GHI

要将其应用于示例代码,除非启用了输出缓冲,否则在输出内容后无法调用header()

试试这个:

header("Content-type: application/pdf");
header('Content-disposition: attachment; filename="'.$Client_ID.'statement'.$TP_format.'.pdf"');
foreach( explode( ',', $_POST["ids"]) as $Client_ID)
{
  $sql_qry="select *
            from   ca_client_statement
            where  client_id='".$Client_ID."' and trading_period_month like '".$TP_Month."'";
  $sql_err_no=sql_select($sql_qry,$sql_res,$sql_row_count,$sql_err,$sql_uerr);

  $row = mysql_fetch_assoc($sql_res);
  $bytes = $row['pdf_statement'];
  print $bytes;
}