douhu2370 2011-09-06 19:27
浏览 23
已采纳

显示动态变量值

I have a form that will ultimately create a pipe delimited text file. The form contains multiple rows each row has multiple drop down fields named 1_cat[], 2_cat[], 3_cat[] etc.

In the form submission script I have:

if ($submit) {
   foreach($_POST['videofile'] as $row=>$item)
   {
      $videofile = $_POST['videofile'][$row];
      $videotype = $_POST['videotype'][$row];

      for($i=0; $i<=$drop_fields; $i=$i+1) {
         $val = $i."_cat[".$row."]"; // HERE'S WHERE I'M HAVING DIFFICULTY
         $cat = $cat.$val.",";
      }
      print "$videofile|$videotype|$cat";
      $cat = "";
   }
}

The code below outputs 1_cat[0],2_cat[0],3_cat[0], 1_cat[1],2_cat[1],3_cat[1] etc. -- so it displays the names, but not values of those variables as I would like it to do.

Any help would be greatly appreciated.

  • 写回答

3条回答 默认 最新

  • doupu3211 2011-09-06 19:38
    关注

    There are several things in this code snippet that should be corrected:

    A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. 1_cat is not a valid variable name. If you wish to use your form fields as variable names, you will need to change the field names in your form.

    videofile needs a $: $videofile

    The for loop counter can be incremented like this: $i++. This is cleaner and more standard than $i=$i+1.

    The line $cat = $cat.$val.","; uses $cat before it is initialized. You should provide an initial value for $cat before the start of the loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?