开发者

Passing Info From One View to Another

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开发者_如何学JAVA 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 ?>


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').


You can use a parameter in the URL to pass information to the 2nd view.


It sounds like you need two controllers and two views.

Controller 1 queries the database to get a list of things. Call the controller "list", and the method "index".

http://mysite.com/list/

Controller 2 queries the database to get more information about one particular thing. Call the controller "item", the method "detail", and the third URL parameter will be the name of the item or some other identifier that you can pass into your database.

http://mysite.com/item/detail/436 or
http://mysite.com/item/detail/the-name-of-book-436

That third parameter provides the info that you use to query your database to find more info about the book. Complete this query as part of controller 2, and pass the data to view 2.

Other answers have suggested using session data. I think that's kind of a strange idea, since session data is for user-specific information and details about books are not user-specific.


Where did the array originally come from? I assume it came from a database or from some other "predictable" data source? In this case you can simply pass the reference to that data (a primary key from the database for example) via the URL when linking to the second page. If you don't have enough information to do this you may need to re-think your design. Having one view access the content of another distinct view would be a bad idea. You can of course create view fragments (just a PHP file you 'include' in your other templates) to minimize code duplication.

I'm not familiar with how CodeIgniter works but this is just general web development logic really.

EDIT

What should happen is that your form submits itself to your controller. Your controller then inspects the POST data and decides what selection the user made from the list. It then redirects to the second controller, passing a parameter in the URL so that the second controller can look up the item from the database.

A rudimentary example, not using your specific use-case, just some semantic pseudo-code. I hope you can follow along here. The example is a fictional multi-language book store where the user selects a language before seeing a list of books in that language. The framework code is purely fictional. I've omitted everything including sanity checking purely for brevity so you can see only the parts that make this workflow happen:

Language selection controller

class LanguagesController ... {
  public function selectLanguage() {
    // If the user submitted the form, collect the language_id
    // and redirect to the BooksController
    if ($this->_request->get('language_id')) {
      return $this->_response->redirect(array(
        'controller' => 'BooksController',
        'action' => 'viewBooks',
        'language_id' => $this->_request->get('language_id')
      ));
    }

    $languages = Language::findAllWithBooksInStock();
    $this->_view->set('languages', $languages);
  }
}

Language selection form

<form method="post">
  <fieldset>
     <label for="language">Select a language</label>
     <select name="language_id" id="language">
       <?php foreach ($languages as $lang): ?>
         <option value="<?php escape($lang->id); ?>"><?php escape($lang->name); ?></option>
       <?php endforeach; ?>
     </select>
  </fieldset>
  <fieldset>
    <input type="submit" name="submit" value="Continue" />
  </fieldset>
</form>

The BooksController which gets the language_id value

class BooksController ... {
  public function viewBooks() {
    $language = Language::findById($this->_request->get('language_id'));
    $books = Book::findAllWithLanguage($language);

    $this->_view->set('language', $language)
                ->set('books', $books);
  }
}

Now the BooksController can implement a view that displays all the books in the language selected at the previous controller. All that's been done to achieve this was to pass the primary key for the language in the URL between the two controllers.

PS: Don't use sessions for something like this. That's a bad habit to get into. Be RESTful.


You can always use session or flashdata.

You can store the data when you show the first page. You can then retrieve it in the next view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜