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

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

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思