使用 codeigniter 对列表中的单个项目进行评分

发布于 2024-10-31 17:25:59 字数 770 浏览 2 评论 0原文

我花了很长时间弄清楚如何对项目列表中的单个项目进行评分,

此代码让我对多个项目进行评分,但不对单个项目进行评分:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢华的一滴泪 2024-11-07 17:25:59

如果用户应该选择要评分的项目(而不是同时对所有项目进行评分),您应该允许他这样做(仅显示一项,让他使用单选按钮选择一项...),然后您应该能够通过 PHP 端检索要修改的项目的索引。

最后,为了只修改一项,您的代码应该如下所示(PHP 端,您当然也必须更新您的 HTML 表单)。

$i = $_POST['item_index']; // Here I'm supposing that you have added radio buttons
                           // named 'item_index' to allow user to choose the item to rate

$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);

事实上,如果没有 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)

$i = $_POST['item_index']; // Here I'm supposing that you have added radio buttons
                           // named 'item_index' to allow user to choose the item to rate

$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);

In fact it would be nearly your original code without the for loop.

柠檬 2024-11-07 17:25:59

循环遍历整个列表只是为了限制您想要的项目,这有点糟糕。

以下是如何使用一些数组函数来完成此操作的示例:

$last=true; // false for first
if($last){
   $id=end($_POST['item_id0']);
}else{
   $id=reset($_POST['item_id0']);
}
// alternative: $id=($last)?end($_POST['item_id0']):reset($_POST['item_id0']);

// test id
if($id===false){
   // no item was supplied
}
if(!isset($_POST['document_rating'][$id])){
   // somehow, the item id doesn't have a matching document rating
}

// everything is okay!
$doc_item_id = $_POST['item_id0'][$id];
$doc_rating  = $_POST['document_rating'][$id];

$it_rt = array(
    'item_id' => $doc_item_id,
    'rating'  => $doc_rating,
);

$this->purchases_model->update_document($it_rt);

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:

$last=true; // false for first
if($last){
   $id=end($_POST['item_id0']);
}else{
   $id=reset($_POST['item_id0']);
}
// alternative: $id=($last)?end($_POST['item_id0']):reset($_POST['item_id0']);

// test id
if($id===false){
   // no item was supplied
}
if(!isset($_POST['document_rating'][$id])){
   // somehow, the item id doesn't have a matching document rating
}

// everything is okay!
$doc_item_id = $_POST['item_id0'][$id];
$doc_rating  = $_POST['document_rating'][$id];

$it_rt = array(
    'item_id' => $doc_item_id,
    'rating'  => $doc_rating,
);

$this->purchases_model->update_document($it_rt);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文