douhuan1905 2015-01-04 19:26
浏览 31
已采纳

如何使用php重复添加按钮

I am trying to add an "Add to wishlist" button next to every record from a database. If I add the form tags inside the loop it comes up with an error asking to place ";" inside action. If I add the button outside the loop it is shown only once. What am I doing wrong ?

EDIT: the error I get is Parse error: syntax error, unexpected ''>' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' in /home/sta402/PHP_stuff/CarTemplate/Views/CarList.phtml on line 16 and the underlined part of the code is between the quotation marks after "action" in the form for the button.

    <?php require('template/header.phtml') ?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

    <br>
    <table class="table table-hover">
       <thead>
        <tr><th>Type</th><th>Make</th><th>Model</th><th>Colour</th><th>Price</th><th>Year</th><th>Picture</th></tr>


       </thead>   
    <tbody> 
        <?php foreach ($view->carDataSet as $carData) {
              echo '<tr> <td>' . $carData->getType() . '</td> <td>' . $carData->getMake() . '</td> <td>' . $carData->getModel() . '</td> <td>' . $carData->getColour() .'</td> <td>' . $carData->getPrice() . '</td> <td>' . $carData->getYearOfRegistration() . '</td><td>' . $carData->getPicture() . '</td></tr>';


        } ?>
        <form method="POST" action=''>
            <input type="submit" name="button1"  value="My Button">
        </form>
    </tbody>
  </table>  
</form>

<?php require('template/footer.phtml') ?>
  • 写回答

1条回答 默认 最新

  • doubeng3216 2015-01-04 19:38
    关注

    The error you're getting is because you're using single quotes (') within a single quote encapsulated string. You can fix this in multiple ways:

    1. use double quotes (") inside your string:

      echo '<form method="POST" action="">
          blablabla
      </form>';
      
    2. escape the single quotes in your string:

      echo '<form method="POST" action=\'\'>
          blablabla
      </form>';
      
    3. use the heredoc syntax:

      echo <<<HTML
      <form method="POST" action=''>
          blablabla
      </form>
      HTML;
      
    4. exit out of php after the echo statement:

      echo ?>
      <form method="POST" action=''>
          blablabla
      </form>
      <?php;
      

    You can then put the correct echo statement in the foreach loop:

    <?php foreach ($view->carDataSet as $carData) {
        echo '<tr> <td>' . $carData->getType() . '</td> <td>' . $carData->getMake() . '</td> <td>' . $carData->getModel() . '</td> <td>' . $carData->getColour() .'</td> <td>' . $carData->getPrice() . '</td> <td>' . $carData->getYearOfRegistration() . '</td><td>' . $carData->getPicture() . '</td></tr>';
        //one of the four above options for the button echo statement
    } ?>
    

    Read this to correctly learn how to use strings

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来