Passing information from one glance to another

EDIT 1. Query results from my database are displayed on my view page. There will be a button next to each unique result:

// 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 the user clicks on one of these buttons (say button 4), I would like to map the user's selection from the array to 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 selection will be stored there, so I can display it in another view like this:

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


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

      

However, what is actually being displayed is the last result displayed on first_view.php, not what the user selects.

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

END OF EDIT1

ORIGINAL QUESTION

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

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

This 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 ?>

      

+2


a source to share


5 answers


What you do (as I see it) is that you create a form, generate row

sumbit buttons and close that form.

If you want to use forms as an indication of what the user wants to buy, you need to create a form for each line from the result, something like this (it 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 -->

      

So every time the user clicks the "buy-it" button, submits another form and in your controller / view you can get id

what the user wants to buy through $this->input->post('idToAdd')

.

+1


a source


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



+1


a source


Where did the array come from? I'm guessing it came from a database or some other "predictable" data source? In this case, you can simply pass a link to that data (the primary key from the database, for example) to the URL when linking to the second page. If you don't have enough information to do this, you may need to rethink your design. Having one kind of access to the content of another separate view would be a bad idea. You can, of course, create view snippets (just the PHP file you "added" to your other templates) to minimize code duplication.

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

EDIT

What should happen is that your form is submitted to your controller. Your controller then checks the POST data and decides which selection the user made from the list. It then redirects to the second controller, passing the parameter in the url so that the second controller can search for an item from the database.

Simplest example, not using your specific use case, just some semantic pseudocode. Hope you can follow along here. An example is a fictional multilingual bookstore where the user selects a language before viewing a list of books in that language. The framework code is purely fictional. I've left out everything, including the health check, just for brevity, so you can only see the parts that do this workflow:

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 that 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);
  }
}

      

The BookController can now implement a view that displays all books in the language selected on the previous controller. All that was 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. This is a bad habit. Be RESTful.

+1


a source


It looks like you need two controllers and two views.

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

http://mysite.com/list/

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

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

This third parameter provides information that you use to query your database to find more information about the book. Fill in this request as part of controller 2 and pass data to view 2.

Other answers suggested using session data. I think this is such a weird idea since session data is for user information and book information is user independent.

+1


a source


You can always use session or flashdata .

You can save data when you show the first page. Then you can get it in the following view.

0


a source







All Articles