Forms +database+maps

13»

Comments

  • edited 8:00AM
    i have locations form in admin to add locations for frond end how i can do this please guide me.i just want the user can only enter locations from there i mean front end
  • edited 8:00AM
    one thing more which will be very best for maps ...what if i want to export a data of one million records in database ...no doubt it will go to the relevant columns like phone,city address etc but it will not have their lat and long ..so i have an idea if we have such large data how can we get their lat and long at run time on front end using geo map for all the locations in the datbase...but i am stuck i can get geo location for one address but when i use loop it does not ...any body have any idea
  • edited 8:00AM
    that is mostly because Google map api has a request limit, and personally i feel it is very bad idea to get lat and long at run time. It simply means inefficient by everytime requesting Google map API. It is better to modify the database to add in lat and long columns.
  • edited September 2014
    as for the front end adding "location" field.

    Since you know how to work controller + views (posts above).

    It is very simple to build the forms in views. You can use normal html tags to build forms in views.

    Or you can use form builders to build the form. http://docs.getfuelcms.com/general/forms

    you only need to post to the correct path.

    e.g. (untested)

    in application/views/location.php

    < form method="location/index">
    < input type="text" name="location" />
    < input type="submit" name="submit" value="submit" />
    < /form>

    in application/controllers/location.php
    Class Location extends CI_Controller{
    function Categories(){
    parent::__construct();
    $this->load->database();//for you talk to database
    }
    function index(){
    if($_POST){
    $location= $this->input->post('location');
    echo $location;
    //then do your own database checking
    }
    $this->fuel->pages->render('location');
    }
    }
  • edited 8:00AM
    thats great let me check it
  • edited 8:00AM
    @onegun one thing i have to put the query to insert there if i am not wrong
    if($_POST){
    $location= $this->input->post('locations');
    echo $location;
    //then do your own database checking


    mysql_query("INSERT INTO locations.............");
  • edited September 2014
  • edited 8:00AM
    this part is not working i want to check/test it as u guided
    $location= $this->input->post('location');
    echo $location;
    its not showing $location value
  • edited 8:00AM
    1. check if the method post path is correct and if values posted by doing print_r($_POST); exit; to see if get any value
    2. would suggest you take tutorial here http://www.getfuelcms.com/blog/2010/12/09/learning-fuel-cms-part-1
  • edited 8:00AM
    <?php
    class location_model extends CI_Model {
    public $foreign_keys = array('category_id' => array(FUEL_FOLDER => 'fuel_categories_model', 'where' => array('context' => 'locations'))); // map foreign keys to table models

    public $required = array('city');

    function __construct()
    {
    parent::__construct(); //table name


    //echo 'testing';

    $this->load->library('form_builder');

    // load the custom form fields
    $this->form_builder->load_custom_fields(APPPATH.'config/custom_fields.php');

    // create fields

    $options = array('SME'=>'SME','Technology'=>'Technology','Financial'=>'Financial','Research'=>'Research'); //this should supply list of options
    $fields['type'] = array('label' => 'Type', 'type' => 'select',
    'options' => $options, 'value' => '', 'mode' => 'single');

    $fields['business_sector'] = array('label' => 'Business Sector');
    $fields['industry_title'] = array('label' => 'Industry Title');
    $fields['name'] = array('label' => 'Name');
    $fields['address'] = array('type' => 'textarea', 'cols' => 40, 'rows' => 5,'label' => 'Address', 'class' => 'no_editor');

    $query = mysql_query('select * from cities');

    while($row = mysql_fetch_array($query)){
    $data[$row['name']] = $row['name'];
    }

    $cities = $data;//array('Rawalpindi'=>'Rawalpindi','Islamabad'=>'Islamabad','Faisalabad'=>'Faisalabad','Lahore'=>'Lahore'); //this should supply list of options
    $fields['city'] = array('label' => 'City', 'type' => 'select',
    'options' => $cities, 'value' => '', 'mode' => 'single');

    $fields['email'] = array('type' => 'email');
    $fields['website'] = array('label' => 'Website');
    $fields['phone'] = array('type' => 'phone');
    $fields['mobile'] = array('label' => 'Mobile');
    $fields['fax'] = array('label' => 'Fax');
    //$fields['my_button'] = array('type' => 'button', 'value' => 'My Button');






    // set the fields
    $this->form_builder->set_fields($fields);



    // render the page
    $vars['form'] = $this->form_builder->render();
    $this->load->view('location', $vars);


    if(isset($_POST['Submit'])){
    $location= $this->input->post('type');
    echo $location;

    $this->load->database('locations');//for you talk to database

    // $field_info = $this->location_model->db->field_info('locations', 'city');
    $sql = "INSERT INTO locations ".
    "(type) ".
    "VALUES('$location')";
    $retval = mysql_query($sql);






    }







    }

    function index () {









    }



    }
    here if i refresh the page it sends automatically the type to database as well i want to get all values of $fields i am stuck here please any help
  • edited October 2014
    no really sure what is your question. what do you mean of automatically send the type to database. And where do you want to get all values of $fields.

    this form is served for front end right?

    if it is, it is not recommended to do form building in model. in basic mvc, model usually only handle database
  • edited 8:00AM
    Good morning ...My question is that when i refresh the page,the empty form or last submitted values again submits in the database i mean it is submitted to the database on refreshing the page.....and second thing i have put the form in model because in about all guidelines of fuel and in the module of admin like locations the forms is in the model thats why i have put it there....
    how i can stop the form submit on page refresh..
  • edited 8:00AM
    I think the problem is because you put it in __construct function. And i also believe for backend, even u put the form builders in the model layer, it is enclosed in form_fields function


    function form_fields($values = array(), $related = array())
    {
    $fields = parent::form_fields($values, $related);

    // ******************* ADD CUSTOM FORM STUFF HERE *******************
    $fields['image']=array('type'=>'file', 'class'=>'multifile', 'multiple'=>true);
    $fields['image']['folder']='images/ambassadors';
    $fields['video_img']["folder"]='images/ambassadors';

    return $fields;

    }
    again, i would suggest you put all the codes in controller layer under index() function.
  • edited 8:00AM
    thanks i appreciate for your suggestion and thanks as well
    but i faced a problem when i put the form in controller and have some query for a field like dropdown than its creating issue cant call it from model as well not working in controller if u shorlty give example of model and controller having the scenario i need then might be i will catch it..bundle of thanks
  • edited 8:00AM
    and how i will show the form using above way as u have mentioned not i can use echo $form...
  • edited 8:00AM
    better for you to post codes here with comment on what you want to do, then maybe can help you look at the codes and fix them
  • edited 8:00AM
    ok
    This is location model in controller
    <?php
    class location_model extends CI_Model {

    public $required = array('city');

    function __construct()
    {
    parent::__construct(); //table name


    // load Form_builder;
    $this->load->library('form_builder');

    // load the custom form fields
    $this->form_builder->load_custom_fields(APPPATH.'config/custom_fields.php');

    // create fields



    $options = array('SME'=>'SME','Technology'=>'Technology','Financial'=>'Financial','Research'=>'Research'); //this should supply list of options
    $fields['type'] = array('label' => 'Type', 'type' => 'select',
    'options' => $options, 'value' => '', 'mode' => 'single');
    $fields['business_sector'] = array('label' => 'Business Sector');
    $fields['industry_title'] = array('label' => 'Industry Title');
    $fields['name'] = array('label' => 'Name');
    $fields['address'] = array('label' => 'Adress','type' =>'textarea','cols'=>'30','rows'=>'3');




    $query = mysql_query('select * from cities');

    while($row = mysql_fetch_array($query)){
    $data[$row['name']] = $row['name'];
    }

    $cities = $data;//array('Rawalpindi'=>'Rawalpindi','Islamabad'=>'Islamabad','Faisalabad'=>'Faisalabad','Lahore'=>'Lahore'); //this should supply list of options
    $fields['city'] = array('label' => 'City', 'type' => 'select',
    'options' => $cities, 'value' => '', 'mode' => 'single');


    $fields['email'] = array('label' => 'Email','type' => 'email');
    $fields['website'] = array('label' => 'Website');
    $fields['phone'] = array('label' => 'phone');
    $fields['mobile'] = array('label' => 'mobile');
    $fields['fax'] = array('label' => 'Fax');

    // set the fields
    $this->form_builder->set_fields($fields);

    // render the page
    $vars['form'] = $this->form_builder->render();
    $this->load->view('location', $vars);


    if(isset($_POST['Submit'])){
    $type= $this->input->post('type');
    $business_sector= $this->input->post('business_sector');
    $industry_title= $this->input->post('industry_title');
    $name= $this->input->post('name');
    $address= $this->input->post('address');
    $city= $this->input->post('city');
    $email= $this->input->post('email');
    $website= $this->input->post('website');
    $phone= $this->input->post('phone');
    $mobile= $this->input->post('mobile');
    $fax= $this->input->post('fax');
    // echo $location;

    $this->load->database('locations');//for you talk to database

    // $field_info = $this->location_model->db->field_info('locations', 'city');
    $sql = "INSERT INTO locations ".
    "(type,business_sector,industry_title,name,address,city,email,website,phone,mobile,fax,published) ".
    "VALUES('$type','$business_sector','$industry_title','$name','$address','$city','$email','$website','$phone','$mobile','$fax','no')";
    $retval = mysql_query($sql);






    }









    function index () {


    }



    }
    }
    and this is view in views
    <?php echo $form; ?>


    in above form i want to show categories as well as dropdown in locations module by admin......and second thing when i refresh the page it inserts the data or empty even if any fields is required
    and remaing you can check if any correction is needed.
  • edited October 2014
    hi i tied your controller and made few fixes, no changes on your view

    <?php

    class Location extends CI_Controller{
    function Location(){
    parent::__construct();
    $this->load->database();
    }


    function index(){
    //
    if(isset($_POST['Submit'])){
    $type= $this->input->post('type');
    $business_sector= $this->input->post('business_sector');
    $industry_title= $this->input->post('industry_title');
    $name= $this->input->post('name');
    $address= $this->input->post('address');
    $city= $this->input->post('city');
    $email= $this->input->post('email');
    $website= $this->input->post('website');
    $phone= $this->input->post('phone');
    $mobile= $this->input->post('mobile');
    $fax= $this->input->post('fax');

    print_r($_POST);

    //db query below

    //redirect if you want
    redirect('location/');
    exit;
    }

    $this->load->library('form_builder', array(
    'form_attrs' => array(
    'method' => 'post',
    'action' => 'location/index'
    )));

    $options = array('SME'=>'SME','Technology'=>'Technology','Financial'=>'Financial','Research'=>'Research');
    $fields['type'] = array('label' => 'Type', 'type' => 'select', 'options' => $options, 'value' => '', 'mode' => 'single');
    $fields['business_sector'] = array('label' => 'Business Sector');
    $fields['industry_title'] = array('label' => 'Industry Title');
    $fields['name'] = array('label' => 'Name');
    $fields['address'] = array('label' => 'Adress','type' =>'textarea','cols'=>'30','rows'=>'3');

    $cities=array();
    $query = $this->db->query('select * from cities');

    foreach($query->result() as $city){
    $cities[$city->name] = $city->name;
    }

    $fields['city'] = array('label' => 'City', 'type' => 'select', 'options' => $cities, 'value' => '', 'mode' => 'single');


    $fields['email'] = array('label' => 'Email','type' => 'email');
    $fields['website'] = array('label' => 'Website');
    $fields['phone'] = array('label' => 'phone');
    $fields['mobile'] = array('label' => 'mobile');
    $fields['fax'] = array('label' => 'Fax');

    // set the fields
    $this->form_builder->set_fields($fields);

    // render the page
    $vars['form'] = $this->form_builder->render();
    $this->load->view('location', $vars);
    }
    }
  • edited 8:00AM
    facing lot of issues using this method errors on db ...not getting main layout so i switch back to my way ....now i have only issue how i can stop submission on page refresh and how can i use locations module categories here in form as dropdown and if put the form in index function in model not in construct then the echo $form is not working...my bad luck
  • edited 8:00AM
    i think it will be more better if there is a clear cut tutorial example for front end ...the example for contact page for front end is only related to email not values to database so think a registeration system for front end exapmle having view model and conroller it will be more helpful
    thanks
  • edited 8:00AM
    One thing more i am using this part of code to get lat and long at run time and assigning them to the address i have round about one million addressses but the google map api has limit upto 2500 how i can resolve it
    see the code below
    foreach($data as $key=>$addr){
    $address = $addr[5].' '.$addr[6];
    $prepAddr = str_replace(' ','+',$address);
    $geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
    $output= json_decode($geocode);
    print_r($output);
    //if(count($output->results) > 0){
    $latitude = $output->results[0]->geometry->location->lat;
    $longitude = $output->results[1]->geometry->location->lng;
    $data[$key][0] = $latitude;
    $data[$key][1] = $longitude;


    i am using all this in locations module
Sign In or Register to comment.