如何从foreach获取特定元素(来自数据库(PDO)的数据)

时间:2017-12-20 10:24:36

标签: php html css

我正在使用foreach写下我的PDO结果中的所有元素(我在DB上提出了一个问题,它返回了一些data --> lot of data)。我把所有这些都写到了表格中(基本上它看起来像管理界面,如果你需要想象它,当你在CMS中管理并希望看到所有帖子或文章时,它就是这样)。

问题是当我想从该表中选择一行时(例如:我想选择文章New age of cellphones,这是在41行)并打开这篇文章(会有一些选项)如编辑,删除等)它选择FIRST元素(首先写成article -> lowest ID)。你能救我一下吗?

foreach($resultedArticle as $data){
    $short_content = substr($data["Content"], 0, 30);
    $short_description = substr($data["Description"], 0, 30);
    echo "                      
        <tr>
            <td>". $data['ID'] ."</td>
            <td>". $data['Name'] ."</td>
            <td>". strip_tags($short_description) ."...</td>
            <td>". strip_tags($short_content) ."...</td>
            <td>". $data['Type']. "</td>
            <td>". $data['Created'] ." </td>
            <td>". $data['Modified'] ."</td>
            <td>". $data['Status'] . "</td>
            <td> <a href='#modal1' class='modal-trigger' title='Zobrazit detaily zápisu'><i class='material-icons'>fullscreen</i></a></td>
            <td> <a href='#modal_edit' title='Editovat zápis'><i class='material-icons'>mode_edit</i></a></td>
            <td> <a href='../../core.php?action=deleteArticle&ID=". $data['ID'] ."' title='Smazat zápis'><i class='material-icons'>clear</i></a></td>
        </tr>
        <div id='modal_edit' class='modal modal-fixed-footer'>
                    <div class='modal-content'>
                      <h4>Edit your article</h4>

                            <div class='row'>
                                  <form class='col s12' method='POST' action='../../core.php?action=editArticle'>
                                      <div class='row'>
                                        <div class='input-field col s6'>
                                          <input name='name' id='name' type='text' class='validate' placeholder='". $data["Name"] ."'/>
                                          <label for='name'>Název</label>
                                        </div>
                                      </div>
                                      <div class='row'> 
                                        <div class='input-field col s6'>
                                          <input name='description' id='description' type='text' class='validate' placeholder='". $data["Description"] ."'/>
                                          <label for='description'>Popisek</label>
                                        </div>
                                      </div>
                                      <div class='row'>
                                        <input id='ID' name='ID' type='hidden' value='". $data["ID"] ."'/>
                                        <button id='submit' class='btn waves-effect waves-light right' type='submit' name='action'> Upravit projekt </button>
                                      </div>
                                  </form>
                            </div>
                        </div>
                        <div class='modal-footer'>
                            <a href='#!'' class='modal-action modal-close waves-effect waves-green btn-flat ''>Zrušit</a>
                        </div>
                    </div>
        ";
编辑:您可以注意到我在同一个foreach中创建模态窗口(窗体)也没有问题吗?

1 个答案:

答案 0 :(得分:1)

请尝试使用此代码我已将ID添加到每个模态中 基本上这不是使用模态的正确方法,而不是你可以在循环外只创建一个模型并用jQuery或javascript填充所有数据,现在它将起作用

foreach($resultedArticle as $data){

    $modal_id = "#modal_edit_$data['ID']";         

    $short_content = substr($data["Content"], 0, 30);
    $short_description = substr($data["Description"], 0, 30);
    echo "                      
        <tr>
            <td>". $data['ID'] ."</td>
            <td>". $data['Name'] ."</td>
            <td>". strip_tags($short_description) ."...</td>
            <td>". strip_tags($short_content) ."...</td>
            <td>". $data['Type']. "</td>
            <td>". $data['Created'] ." </td>
            <td>". $data['Modified'] ."</td>
            <td>". $data['Status'] . "</td>
            <td> <a href='#modal1' class='modal-trigger' title='Zobrazit detaily zápisu'><i class='material-icons'>fullscreen</i></a></td>
            <td> <a href='".$modal_id."' title='Editovat zápis'><i class='material-icons'>mode_edit</i></a></td>
            <td> <a href='../../core.php?action=deleteArticle&ID=". $data['ID'] ."' title='Smazat zápis'><i class='material-icons'>clear</i></a></td>
        </tr>
        <div id='" .$modal_id. "' class='modal modal-fixed-footer'>
            <div class='modal-content'>
                <h4>Edit your article</h4>
                <div class='row'>
                    <form class='col s12' method='POST' action='../../core.php?action=editArticle'>
                        <div class='row'>
                            <div class='input-field col s6'>
                                <input name='name' id='name' type='text' class='validate' placeholder='". $data["Name"] ."'/>
                                <label for='name'>Název</label>
                            </div>
                        </div>
                        <div class='row'> 
                            <div class='input-field col s6'>
                                <input name='description' id='description' type='text' class='validate' placeholder='". $data["Description"] ."'/>
                                <label for='description'>Popisek</label>
                            </div>
                        </div>
                        <div class='row'>
                            <input id='ID' name='ID' type='hidden' value='". $data["ID"] ."'/>
                            <button id='submit' class='btn waves-effect waves-light right' type='submit' name='action'> Upravit projekt </button>
                        </div>
                    </form>
                </div>
            </div>
            <div class='modal-footer'>
                <a href='#!'' class='modal-action modal-close waves-effect waves-green btn-flat ''>Zrušit</a>
            </div>
        </div>
    ";