Welcome Guest, Not a member yet? Register   Sign In
Calling a list view from within a function does not show the view.
#1

[eluser]Jow272[/eluser]
Hello to you all,

I'm a bit stuck in the development of a site. I can't seem to get it working. Maybe one of you experts could lend me a hand?

What I want to do is this; I am building a site using CI (what else) in which at several instances adress data needs to be entered by the user. To preclude multiple instances of the same address in the address table I want to make sure that if the addres the user wants to add is already in the table, he or she will be noticed about the existance and if correct, can reuse the existing address.

This means that if the user enteres address data, the application needs to look this address up in the address table, if it finds an exact match it should automatically use the existing address or if it finds matching (but not exactly correct) addresses it should show a list of these matching addresses and give the user the option to choose the correct one. It is somewhat the same as when you search a person in LinkedIn or Twitter, you'll get a list of matching persons on a name.

Now, as this process will happen on more than one view, I would like to make a generel procedure for this whole process. I wanted to call it identify and place it in an address class.

Now my problem: I've writen the identify function and after first letting it find out if an exact match exists, I let it look in the table for matching addresses and now I want it to show these matching addresses in a list view.

If I call the identify function direct from URI everything works ok. The list is shown and I can select addresses and it gives feedback.

But if I call this function from an other function like a create function that is used to save the addres, It does not work. The list is not shown even though the complete code is processed (found that out debugging with echo's). The proces procedes through the identify function with finding matching addresses and past the row where the view is called and passes through even storing the address, but without showing the list page, it just ignores it! Sick

I've been trying to find a solution all morning now, but with no luck. I want to call te view from within the identify function by simply using the $this->load->view($view, $data) code. I also tried to solve the problem with using redirect(), but the problem with this is that it does not allow to send data(like the list) with it. So that's not an option.

Can anyone guide me in the wright direction?

Thanx, (sorry for the long text)

Michel
#2

[eluser]gigas10[/eluser]
Please show us some code that you believe is not working correctly.
#3

[eluser]Jow272[/eluser]
Hello Gigas10, sorry for my late responce we have family staying with us and I was not able to find some time to answer until now.

I actualy tried so much different code options that my code is pretty screwed up and it would take some time to get it the way I initially would like to have it. I thought it would be better to ask you experts what the best practise would be for the given situation. Also because I am not getting errors or so, only the list is not showing up which to me looks like I'm just not using the correct methode. I figured cleaning everything up so it is readable would be to much effort if yust someone could provide me with a way to do it wright.

But ok, of course your wright that it gives a better inside in the cause of the problem. So here it goes:

Lets start with the function calling the identify function: This create function is activated by submitting the page where the user enteres the address data.

Code:
public function create()
{
      $this->load->library('address');
      $this->load->model('Address_model');

      if ( $this->form_validation->run('story_form') === false )
      {
         $this->index();
      }
      else
      {
         // collect the address data for identification
         $address = array(
            'street'       => $this->input->post('street'),
            'number'       => $this->input->post('number'),
            'postal_code'  => $this->input->post('postal_code'),
            'place'        => $this->input->post('place'),
            'country'      => $this->input->post('country')
         );

         // identify the intered address
         $found_addresses = $this->address->identify($address);

         if ( count($found_addresses) > 0 ) // so at least one address if identified
         {
         // TODO: Here the user choise should be processed
         }
         else // there is no address in the table that looks identical to the provided data
         {
            // so store the new address data to the address table and retrieve the new address id
            $address_id = $this->Address_model->create($address);
         }
         $post_data = // processing the $_POST data

         // store the data
         if ( $this->Story_model->create($post_data) === FALSE )
            $this->index();
         else
            redirect('home');
      }

}

Then the identify function in the address class:

Code:
public function identify($address)
   {
      $address_model = new Address_model();

      $address_id = $address_model->get_address_id_based_on_data($address);

      if ( $address_id === FALSE ) // exact address is not found
      {
         // so look if an address that looks like the provided address is what the user wants
         $possible_addresses = $this->find_alternative_addresses($address);

         if ( $possible_addresses === FALSE ) // no address found at all so return false
         {
            return FALSE;
         }
         else // some addresses where found so ask the user which he means
         {
            $address_list = array();

            $countries = get_countries_for_menu($this->config->item('language'));

            foreach ($possible_addresses as $address)
            {
               $address_array = array(
                  'address_id'   => $address['address_id'],
                  'street'       => $address['street'] . " " . $address['number'],
                  'postal_code'  => $address['postal_code'],
                  'place'        => $address['place'],
                  'country'      => $countries[$address['country']]
               );

               array_push($address_list, $address_array);
            }

            $this->show_found_addresses_view($address_list, 'MY_Controller/identify/');
         }
      }
      else
      {
         return $address_id;
      }

      return $address_list = array();
   }

}

Then the show_found_addresses_view that is suppost to render and display the list in a selectable fassion

Code:
public function show_found_addresses_view($found_addresses, $controller)
   {
      // check if user is already logged in and adjust the header accordingly
      $data['header'] = $this->build_header();

      $body_data['controller'] = $controller;
      $body_data['key'] = 'value';
      $body_data['addresses'] = $found_addresses;

      // generate the body with contact information
      $data['body']=$this->load->view('bodies/identified_addresses_view', $body_data, TRUE);

      // load the main page view in which all views will be loaded
      $this->render('frame_view', $data);
   }

And as last the render function:

Code:
protected function render($view, $data)
   {
     // check if user is already logged in
    if (!isset($this->session) || $this->session->userdata('logged_in') != TRUE)
        // user not allowed to see this page
        $data = $this->load_prohibited_view();

      // load the main page view in which all views will be loaded
      $this->load->view($view, $data);
   } // EOF render

As mentioned in my starting text, when the user submits his address form with an address that is not exactly stored in the table, but has some addresses that are comparable he will not get the list but the address will be saved and the user is redirected to the home page. The code though does proceed through the identify function as expected. The address_list is filled and even the render function is called and processed. But the identified_address_view is not displayed.

Thanx for any help.




Theme © iAndrew 2016 - Forum software by © MyBB