dongzong2017 2010-04-27 13:56
浏览 80
已采纳

将信息从一个视图传递到另一个视图

EDIT 1. The results of a query from my database are displayed on my view page. Next to each unique result is a button:

// first_view.php    
<?php echo form_open('controller/method'); ?>
<?php foreach($results_found as $item): ?>
<p><?php echo $item->name ?></p>
<p><?php form_submit('buy','Buy This'); ?></p>

When a user clicks on one of these buttons (lets say button 4), I would like to display the user's choice from the array onto another view.

I've tried using this:

 // first_view.php
<?php echo $this->session->set_userdata('choice',$item); ?>

immediately before

// first_view.php    
<?php echo form_close(); ?>

thinking that the user's final choice would be stored there so I can display it on another view like this:

// second_controller.php
$c = $this->session->userdata('choice');


// second_view.php
echo 'Your Choose: '. $c;

However, what actually displays is the last result displayed on first_view.php, not what the user choose.

My question is, if the user clicked on button 4, how do I get that particular choice displayed onto another view?

END OF EDIT1

ORIGINAL QUESTION

My view page (CodeIgniter) displays the contents of an array in the form of several links. If a user clicks on a link, I want the user to be taken to another view page (via a controller) that gives more information concerning that particular link (which is stored in that particular position in the array)

My question is, how do I access and display the contents of that particular location within the array on the second view file without getting a "Undefined variable error" on the second view?

Here is what I am trying to do in code form

// my_controller.php
$array_name['variable_name'];
$this->load->view('first_view.php');

// first_view.php
<?php foreach($variable_name as $vn): ?>
<?php echo anchor('controller_name' $vn->info ?> // if user clicks on 3rd link
<?php endforeach ?>                              // I want to beable to access
                                                 // index 2 of the array in the 
                                                 // second view file so I can 
                                                 // display more info 
// second_view.php
<?php $vn[2]->info ?>
  • 写回答

5条回答 默认 最新

  • dpbfb7119 2010-05-02 11:36
    关注

    What you do (as I see it) is that you create form, generate row sumbit buttons and then close that form.

    If you want to use forms as indication of what user wants to buy, you have to generate form for each row from result, something like this (it's generated html, but you need to edit it to suit your needs):

    Edit (pseudocode wasn't enough :) :

    <!-- repeat row times -->
    <?php foreach($items as $item): ?>
    <p><?php echo $item->name; //echo name ?>
      <?php form_open(/*your settings*/); ?>
      <input type="hidden" name="idToAdd" value="<?php echo $item->id;?>" />
      <input type="submit" value="Buy this" />
      </form>
    </p>
    <?php endforeach; ?>
    <!-- end repeat -->
    

    This way, everytime user clicks 'buy-it' button, different form submits and in your controller/view, you can get id of what user wants to buy through $this->input->post('idToAdd').

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测