使用 codeigniter 对列表中的单个项目进行评分
我花了很长时间弄清楚如何对项目列表中的单个项目进行评分,
此代码让我对多个项目进行评分,但不对单个项目进行评分:
for($i=0;$i<2;$i++){
$doc_item_id = $_POST['item_id0'][$i];
$doc_rating = $_POST['document_rating'][$i];
$it_rt = array(
'item_id' => $doc_item_id,
'rating' => $doc_rating,
);
$this->purchases_model->update_document($it_rt);
}
而此代码让我仅对第一个项目进行评分(或最后一项取决于我放置“中断”的位置;):
foreach($_POST['item_id0'] as $doc_item_id){
foreach($_POST['document_rating'] as $doc_rating){
}
break;
}
$it_rt = array(
'item_id' => $doc_item_id,
'rating' => $doc_rating,
);
$this->purchases_model->update_document($it_rt);
任何关于如何纠正其中任何一个以便用户可以对他们选择的单个项目进行评分的想法将不胜感激,
I’m having a heck of a time figuring out how to post a rating for an individual item in a list of items,
this code let’s me rate multiple items, but not single items:
for($i=0;$i<2;$i++){
$doc_item_id = $_POST['item_id0'][$i];
$doc_rating = $_POST['document_rating'][$i];
$it_rt = array(
'item_id' => $doc_item_id,
'rating' => $doc_rating,
);
$this->purchases_model->update_document($it_rt);
}
whereas this code let’s me rate only the first item (or last item depending on where i put the "break;"):
foreach($_POST['item_id0'] as $doc_item_id){
foreach($_POST['document_rating'] as $doc_rating){
}
break;
}
$it_rt = array(
'item_id' => $doc_item_id,
'rating' => $doc_rating,
);
$this->purchases_model->update_document($it_rt);
any thoughts on how to correct either of these such that the user could rate the individual item of their choosing would be greatly appreciated,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果用户应该选择要评分的项目(而不是同时对所有项目进行评分),您应该允许他这样做(仅显示一项,让他使用单选按钮选择一项...),然后您应该能够通过 PHP 端检索要修改的项目的索引。
最后,为了只修改一项,您的代码应该如下所示(PHP 端,您当然也必须更新您的 HTML 表单)。
事实上,如果没有
for
,它几乎就是您的原始代码环形。If the user is supposed to choose the item to rate (instead of rating all items at the same time), you should allow him to do so (showing only one item, let him select one using radio buttons...), and then you should be able, by PHP side, to retrieve the index of the item to modify.
Finally, in order to modify only one item, your code should look like (PHP side, you will certainly have to update your HTML form as well)
In fact it would be nearly your original code without the
for
loop.循环遍历整个列表只是为了限制您想要的项目,这有点糟糕。
以下是如何使用一些数组函数来完成此操作的示例:
Looping through the entire list just to limit the item you want, is kinda bad.
Here's an example on how to do it using some array functions: