0

我需要有人看看为什么 CakePHP (Ver 2.2.2) 正在做它正在做的事情?好的,我的代码细分如下。DIV PageList 是一个 JQuery Accordion,有两个下拉菜单,一个用于管理员添加新用途,另一个用于查看所有用户并进行一些更改。添加用户很好,并且没有任何问题。

我的问题是(或者至少我认为是)使用 Form Helper,我之前确实在我的 foreach 循环之外进行了 Form Create 调用并且工作没有任何问题,我会找到我想要更改某些内容然后保存的用户, 没问题。当我声明要对结果进行分页时,我开始遇到问题。当表单创建在 foreach 之外并且就在我的表代码开始之前,只有最后显示的结果才能保存所做的更改。我找到了问题的原因,那就是 ID 字段始终默认为最后显示的结果。所以我将 Form Create 移到我的 foreach 循环中。

这现在有效,但由于某种原因,它立即开始结束,因此我的输入现在位于表单标签之外?我不明白为什么,因为我在 foreach 循环的底部有 end Form Helper end() 调用。因此,它应该只以自己的形式显示每个用户。

我不确定这是否是一个问题,并且仍然可以像这样使用它。但是一个问题是我的表格现在在我的表格之后开始,这不会通过 W3C 验证,这不是一个大问题。旁注,还可以使用表格吗?我正在与他们一起列出数据?我正在尝试学习 HTML 5 规范。但还没有真正看到任何关于桌子的东西让......

非常感谢。格伦·柯蒂斯。

echo '<div id="PageList">';

$OutputHolder = '<h3 class="Background-Titles">Add User</h3>';
$OutputHolder .= '<div class="Background-Content" id="AdminAddUser">';
$OutputHolder .=  $this->Form->create('User', array('url'=>'http://glenns-showcase.net/projects/cake/DrWho/adminadd/', 'inputDefaults' => array('div' => false) ) ); 
$OutputHolder .=  $this->Form->input('username');
$OutputHolder .=  $this->Form->input('password');
$OutputHolder .=  $this->Form->input('firstname');
$OutputHolder .=  $this->Form->input('surname');
$OutputHolder .=  $this->Form->input('email');
$OutputHolder .= 'Select Role For User <br />';
$OutputHolder .=  $this->Form->radio('role_id', $opt_role, $attributes);
$OutputHolder .= '<br />Select Active State For User <br />';
$OutputHolder .=  $this->Form->radio('active', $opt_active, $attributes);
$OutputHolder .=  $this->Form->end('Submit'); 
$OutputHolder .= '</div>'; //End for AddUser
echo $OutputHolder;

$OutputViewUsers = '<h3 class="Background-Titles">View All Users</h3>';
$OutputViewUsers .= '<div class="Background-Content" id="AdminViewUsers">';

$OutputViewUsers .=  '<table border="0">';
$OutputViewUsers .=  '<tr><td>Username</td><td>First Name</td><td>Surname</td><td>E-mail</td><td>Stutus</td><td>Level</td><td>Save</td></tr>';
foreach($users as $key => $value) {
    $OutputViewUsers .= $this->Form->create('UserEdit', array('url'=>'http://glenns-showcase.net/projects/cake/DrWho/adminedit/', 'inputDefaults' => array('div' => false) ) );
    $BaseLink = $users[$key]['User']; 
    $OutputViewUsers .= "<td class=\"AdminViewUsersUN\">" .($BaseLink  ['username']) . "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('fname', array('default' => $BaseLink  ['firstname'], 'label' => false,  'class' => 'AdminViewUsersFN') ) . "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('sname', array('default' => $BaseLink  ['surname'], 'label' => false,  'class' => 'AdminViewUsersSN') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('email', array('default' => $BaseLink  ['email'], 'label' => false,  'class' => 'AdminViewUsersEM') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->select('active', $ActOpts, array('empty' => false, 'default' => $BaseLink  ['active'], 'class' => 'AdminViewUsersAS') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->select('role', $RoleOpts, array('empty' => false, 'default' => $users[$key]['Role']['id'],  'class' => 'AdminViewUsersUL') ). "</td>";
    $OutputViewUsers .= $this->Form->hidden('id', array('default' => $BaseLink  ['id'], ) );
    $OutputViewUsers .= "<td>" . $this->Form->submit('Save', array('div' => false, 'class' => 'AdminViewUsersSB') ). "</td></tr>"; 
    //$test = array('label' => 'Save', 'div' => array('class' => 'AdminViewUsersSB'));
    $OutputViewUsers .=  $this->Form->end(); 

} //End of foreach loop
$OutputViewUsers .= '</table>';

$OutputViewUsers .= $this->Paginator->numbers(array('first' => 2));
$OutputViewUsers .= '</div>'; //End for AllUsers 
echo $OutputViewUsers;

echo '</div>'; //End for PageList

更新 :::

这就是它现在的样子!但这现在,或多或少是我以前的样子。我添加了缺少的标签。但这现在只让最后一个结果起作用!我对每页 5 个结果进行分页,页面上的前四个结果将不起作用,因为它们的 ID 号错误,每次推送任何“保存”时,只提供最后一个结果的 ID。

我不确定您所说的“为输入字段编制索引”是什么意思,但据我所知,“为提交元素提供包含相关记录的适当索引的唯一名称”,其中有一个隐藏字段提供 ID。

在我看来,我必须将整个表单放在我的循环中,以便每个返回的结果都是自己的表单,那么应该没有错误!但是,我不能将表格的开头放在循环中,或者我对每个结果都有一个标题!我想我应该能够解决这个问题!

非常感谢您的帮助!

$OutputViewUsers = '<h3 class="Background-Titles">View All Users</h3>';
$OutputViewUsers .= '<div class="Background-Content" id="AdminViewUsers">';
$OutputViewUsers .= $this->Form->create('UserEdit', array('url'=>'http://glenns-showcase.net/projects/cake/DrWho/adminedit/', 'inputDefaults' => array('div' => false) ) );
$OutputViewUsers .=  '<table border="0">';
$OutputViewUsers .=  '<tr><td>Username</td><td>First Name</td><td>Surname</td><td>E-mail</td><td>Stutus</td><td>Level</td><td>Save</td></tr>';
foreach($users as $key => $value) {
    $BaseLink = $users[$key]['User']; 
    $OutputViewUsers .= "<tr><td class=\"AdminViewUsersUN\">" .($BaseLink  ['username']) . "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('fname', array('default' => $BaseLink  ['firstname'], 'label' => false,  'class' => 'AdminViewUsersFN') ) . "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('sname', array('default' => $BaseLink  ['surname'], 'label' => false,  'class' => 'AdminViewUsersSN') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('email', array('default' => $BaseLink  ['email'], 'label' => false,  'class' => 'AdminViewUsersEM') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->select('active', $ActOpts, array('empty' => false, 'default' => $BaseLink  ['active'], 'class' => 'AdminViewUsersAS') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->select('role', $RoleOpts, array('empty' => false, 'default' => $users[$key]['Role']['id'],  'class' => 'AdminViewUsersUL') ). "</td>";
    $OutputViewUsers .= $this->Form->hidden('id', array('default' => $BaseLink  ['id'], ) );
    $OutputViewUsers .= "<td>" . $this->Form->submit('Save', array('div' => false, 'class' => 'AdminViewUsersSB') ). "</td></tr>"; 
} //End of foreach loop
$OutputViewUsers .= '</table>';
$OutputViewUsers .=  $this->Form->end(); 
$OutputViewUsers .= $this->Paginator->numbers(array('first' => 2));
$OutputViewUsers .= '</div>'; //End for AllUsers 
echo $OutputViewUsers;

另一个更新::

现在这似乎起作用了,每个结果都在它自己的表单元素和所有输入中,或者现在在表单标签中。但是,每个结果现在都有自己的标题,因为我现在在表单中有表格,但现在在我的 foreach 循环中,所以每次循环时,它都会在结果顶部回显/打印标题。我正在为此努力。任何帮助仍然是最受欢迎的!!

foreach($users as $key => $value) {
    $OutputViewUsers .= $this->Form->create('UserEdit', array('url'=>'http://glenns-showcase.net/projects/cake/DrWho/adminedit/', 'inputDefaults' => array('div' => false) ) );
    $OutputViewUsers .=  '<table border="0">';
    $OutputViewUsers .=  '<tr><td>Username</td><td>First Name</td><td>Surname</td><td>E-mail</td><td>Stutus</td><td>Level</td><td>Save</td></tr>';
    $BaseLink = $users[$key]['User']; 
    $OutputViewUsers .= "<tr><td class=\"AdminViewUsersUN\">" .($BaseLink  ['username']) . "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('fname', array('default' => $BaseLink  ['firstname'], 'label' => false,  'class' => 'AdminViewUsersFN') ) . "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('sname', array('default' => $BaseLink  ['surname'], 'label' => false,  'class' => 'AdminViewUsersSN') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('email', array('default' => $BaseLink  ['email'], 'label' => false,  'class' => 'AdminViewUsersEM') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->select('active', $ActOpts, array('empty' => false, 'default' => $BaseLink  ['active'], 'class' => 'AdminViewUsersAS') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->select('role', $RoleOpts, array('empty' => false, 'default' => $users[$key]['Role']['id'],  'class' => 'AdminViewUsersUL') ). "</td>";
    $OutputViewUsers .= $this->Form->hidden('id', array('default' => $BaseLink  ['id'], ) );
    $OutputViewUsers .= "<td>" . $this->Form->submit('Save', array('div' => false, 'class' => 'AdminViewUsersSB') ). "</td></tr>"; 
    //$test = array('label' => 'Save', 'div' => array('class' => 'AdminViewUsersSB'));
    $OutputViewUsers .= '</table>';
    $OutputViewUsers .=  $this->Form->end(); 

} //End of foreach loop
4

2 回答 2

1

您只是在生成无效的 HTML。您tr在循环中缺少一个开始标签,但即使它存在,它仍然无效,因为tr只允许持有tdorth元素,因此浏览器可能会在表格之外呈现表单。

另见表格内的表格

一种解决方法是将整个表格包装在表单中,对输入字段进行索引,为提交元素提供包含相关记录的适当索引的唯一名称,并在服务器端使用此名称来确定要保存的记录。或者将提交的数据与数据库进行比较,只保存发生更改的记录。

附言。是的,表格很好,只要它们用于预期用途,用于描述表格数据。

于 2012-11-29T10:01:26.430 回答
0

我已将上述内容标记为答案,因为它使我走上了正确的道路,非常感谢!

现在,我的循环中有表格,表格中有表格。现在这意味着每个结果都在其自己的表单标签中。

对于标题,我刚刚使用 CSS 和 span 标签来排列它们!

非常感谢格伦。

这是我的最终代码,以防万一其他人发现它有任何用处。

$OutputViewUsers = '<h3 class="Background-Titles">View All Users</h3>';
$OutputViewUsers .= '<div class="Background-Content" id="AdminViewUsers">';
$OutputViewUsers .=  '<div class="AdminViewUsersListHeaders">
    <span class="AdminViewUsersUNHeader">Username</span>
    <span class="AdminViewUsersFNHeader">First Name</span>
    <span class="AdminViewUsersSNHeader">Surname</span>
    <span class="AdminViewUsersEMHeader">E-mail</span>
    <span class="AdminViewUsersASHeader">Stutus</span>
    <span class="AdminViewUsersULHeader">Level</span>
    <span class="AdminViewUsersSAHeader">Save</span></div>';


foreach($users as $key => $value) {
    $OutputViewUsers .= $this->Form->create('UserEdit', array('url'=>'http://glenns-showcase.net/projects/cake/DrWho/adminedit/', 'inputDefaults' => array('div' => false) ) );
    $OutputViewUsers .=  '<table border="0">';
    //$OutputViewUsers .=  '<tr><td>Username</td><td>First Name</td><td>Surname</td><td>E-mail</td><td>Stutus</td><td>Level</td><td>Save</td></tr>';
    $BaseLink = $users[$key]['User']; 
    $OutputViewUsers .= "<tr><td class=\"AdminViewUsersUN\">" .($BaseLink  ['username']) . "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('fname', array('default' => $BaseLink  ['firstname'], 'label' => false,  'class' => 'AdminViewUsersFN') ) . "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('sname', array('default' => $BaseLink  ['surname'], 'label' => false,  'class' => 'AdminViewUsersSN') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('email', array('default' => $BaseLink  ['email'], 'label' => false,  'class' => 'AdminViewUsersEM') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->select('active', $ActOpts, array('empty' => false, 'default' => $BaseLink  ['active'], 'class' => 'AdminViewUsersAS') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->select('role', $RoleOpts, array('empty' => false, 'default' => $users[$key]['Role']['id'],  'class' => 'AdminViewUsersUL') ). "</td>";
    $OutputViewUsers .= $this->Form->hidden('id', array('default' => $BaseLink  ['id'], ) );
    $OutputViewUsers .= "<td>" . $this->Form->submit('Save', array('div' => false, 'class' => 'AdminViewUsersSB') ). "</td></tr>"; 
    //$test = array('label' => 'Save', 'div' => array('class' => 'AdminViewUsersSB'));
    $OutputViewUsers .= '</table>';
    $OutputViewUsers .=  $this->Form->end(); 

} //End of foreach loop


$OutputViewUsers .= $this->Paginator->numbers(array('first' => 2));
$OutputViewUsers .= '</div>'; //End for AllUsers 
echo $OutputViewUsers; 
于 2012-11-30T14:03:49.203 回答