Welcome Guest, Not a member yet? Register   Sign In
HELP!! How I can pass an array data to index controller function?
#1

[eluser]Paganotti[/eluser]
Hi, I have a problem and I don't resolve it.

My problem is that I would pass an array data to index controller function but I don't Know how I do it.

I post an example becouse my english is not very well.

class ct_Ricerca extends Controller
{
var $Data;

function __construct()
{
//richiamo il costruttore della classe principale
parent::Controller();

//carico la libreria per la creazione di template
$this->load->library('parser');

$this->load->model("md_Ricerca");

}

function index($Data) // <----- I WOULD PASS AN ARRAY AS PARAMETERS
{
$this->Data = array(); //SOMETHING LIKE THIS
$this->Data = $Data; //SOMETHING LIKE THIS
}
}
#2

[eluser]sophistry[/eluser]
Did you read this page on the URI Routing Class in the manual.

It should help you get going.

You need to call your index by using a URI like this (EDITED):

example.com/ct_Ricerca/index/item1/item2/item3

and then parse out item1 2 and 3 in the index function.

HTH!
#3

[eluser]Paganotti[/eluser]
yes, I know that, but I have many parameters to pass a index controller becouse this data leave by database, so I would have for example 10 parameters and the url become very long.
#4

[eluser]sophistry[/eluser]
Ok.

Maybe you are asking a different question?

Please give more details.

For instance:

"I am using CI to interface to an older application (name of application or interface spec), what is the best way to do that?"
#5

[eluser]Michael Wales[/eluser]
The variables that are passed to Controller methods are automatically pulled from the URI. For example:
http://www.mybookstore.com/books/view/32

Could be (in code):
Code:
&lt;?php
class Books extends Controller {

  function view($id) {
    echo $id; // Echo's 32
  }

}
?&gt;

My question would have to be - why are you wanting to pass an array of data to the controller function? In trying to understand your explanation you are retrieving data from a database? Why not just do that within the controller method itself (or even better, a model). For the sake of simplicity - I'll place it all in the controller for now.

controllers/books.php
Code:
&lt;?php
class Books extends Controller {

  function Books() {
    parent::Controller();
  }

  // Index will retrieve a list of all of our books with some basic info
  function index() {
    $query = $this->db->get('books');
    foreach ($query->result() as $book) {
      $data['books'][$book->id]['title'] = $book->title;
      $data['books'][$book->id]['author'] = $book->author;
      $data['books'][$book->id]['price'] = $book->price;
    }
    $this->load->view('books/list', $data);
  }
}
?&gt;

views/books/list.php
Code:
&lt;?php foreach ($books as $id => $book) {
  echo '<p>Title: ' . $book['title'] . '(' . $book['price'] . ')<br />
        Author: ' . $book['author']</p>';
?&gt;
#6

[eluser]Paganotti[/eluser]
the other image that is modify is attached here
#7

[eluser]Paganotti[/eluser]
ok. I have a search controller called ct_Ricerca that permits to search an article, then the information of this article that I search, they are submit to modify controller that it permits tu modify this article and the input box are compile with information that they are received by ct_Ricerca controller for the user can modify this article. But my problem is that if the user has mistaken, he can click over Ricerca Avanzata link and the page is redirect to ct_Ricerca for a new research, but finaly I would have that this new research is precompiled with previous data of article.

Excuse me for my bad englesh and I hope that you have understood the same one. I have also linked 2 image for views of controllers ct_Ricerca and Modify.
#8

[eluser]Michael Wales[/eluser]
I think you are saying:
1. A user searches for an article.
2. That search returns a form that is prefilled with data to edit an article.
3. The user can click on a link to return to the search form, and you want that search form filled with the data from the original search?

Honestly, I would hate that as a user - but I don't know your particular application. I would just store the original search in a session and whenever people visit that search form, it fills itself with the data stored in the session.
#9

[eluser]sophistry[/eluser]
Yes. I agree. Sessions.




Theme © iAndrew 2016 - Forum software by © MyBB