SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Codeigniter data results into dropbox

    My latest challenge is to utilize Codeigniter for some new websites I am working on, things are going well but I am not running into a situation that I am not sure how to resolve.

    I retrieve some results from the database (in my model), I can then display those results to the screen using a foreach loop, but what I am really needing to do is populate a dropbox with the results with the following format restraints.

    1) I am retrieving 2 values from the database - number and name
    2) In my dropbox I would like the number to be the value (so I can use it to retrieve more detailed results)
    3) And I would like the name to be a combination of the number AND name from my original results

    i.e.

    Number Name
    2126 SomeName
    3478 SomeOtherName
    7931 AnotherName

    But in the dropbox (shown to the end user) I would like it to be:
    '2126 - SomeName'
    '3478 - SomeOtherName'
    '7931 - AnotherName'

    with just the number as the value of each <option>

    Hope I explained that clear enough for someone to assist, I am using CodeIgniter's form helper (and maybe that is where I need to step back and do it "traditionally" so the call for my dropbox is:

    echo form_dropdown('num', $thelist);

    Thanks in advance, hopefully someone can point me in a direction to look, I've been doing google searches and watching all sorts of things online to no avail thus far.

    Greg
    Greg Hicks
    Tekamba Computers, LLC
    greg@tekamba.com
    http://tekamba.com

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,834
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by GHicks View Post
    My latest challenge is to utilize Codeigniter for some new websites I am working on, things are going well but I am not running into a situation that I am not sure how to resolve.

    I retrieve some results from the database (in my model), I can then display those results to the screen using a foreach loop, but what I am really needing to do is populate a dropbox with the results with the following format restraints.

    1) I am retrieving 2 values from the database - number and name
    2) In my dropbox I would like the number to be the value (so I can use it to retrieve more detailed results)
    3) And I would like the name to be a combination of the number AND name from my original results

    i.e.

    Number Name
    2126 SomeName
    3478 SomeOtherName
    7931 AnotherName

    But in the dropbox (shown to the end user) I would like it to be:
    '2126 - SomeName'
    '3478 - SomeOtherName'
    '7931 - AnotherName'

    with just the number as the value of each <option>

    Hope I explained that clear enough for someone to assist, I am using CodeIgniter's form helper (and maybe that is where I need to step back and do it "traditionally" so the call for my dropbox is:

    echo form_dropdown('num', $thelist);

    Thanks in advance, hopefully someone can point me in a direction to look, I've been doing google searches and watching all sorts of things online to no avail thus far.

    Greg
    Using your current form_dropdown()

    I would be tempted to extract the post numeric result using strstr() or substr()
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  3. #3
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't need to extract a portion of the returned results, I need to combine 2 values together and restructure it into the results.

    Query to DB returns:

    [0][number] = 2126,
    [0][name] = SomeName,
    [1][number] = 3478,
    [1][name] = SomeOtherName,
    [2][number] = 7931,
    [2][name] = AnotherName

    I need to reformat it as:
    [0][number] = 2126,
    [0][name] = 2126 - SomeName,
    [1][number] = 3478,
    [1][name] = 3478 - SomeOtherName,
    [2][number] = 7931,
    [2][name] = 7931 - AnotherName

    So I can push that to my form_dropdown so it outputs:
    Code:
    <option value="2126>2126 - SomeName</option>
    <option value="3478">3478 - SomeOtherName</option>
    <option value="7931">7931 - AnotherName</option>
    Does this make a bit more sense?

    Greg
    Greg Hicks
    Tekamba Computers, LLC
    greg@tekamba.com
    http://tekamba.com

  4. #4
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    UPDATE - Code and

    Here is partially working code, I rethought the problem and decided to CONCAT the two DB values through my DB query, this eliminates the need to try to put it together afterwards and I have a basic array. Now I just need to get it to be in the array format that CodeIgniter is looking for to populate a drop down input field on my form.

    Anyone know how to get it to properly output so CodeIgniter can work with it?

    Code from my MODEL
    Code:
      public function GetAllJobs() {
        // Using ActiveRecords - set fields to retrieve data from
        $this->db->select('Job_Number, CONCAT(Job_Number, " - ",Company_Name) as ListJob', FALSE);
        // Using ActiveRecords - define the order by criteria
        $this->db->order_by('Job_Number', 'asc');
        // Using ActiveRecords - process the query against the DB
        $query = $this->db->get('Jobs');
    
        if($query->num_rows() > 0):
          return $query->result();
        else:
          return false;
        endif;
      }
    Code from my Controller
    Code:
      public function jobs() {
        $this->config->set_item('page_title', 'Jobs Navigation Page');
        $this->config->set_item('page_desc', 'View/Edit details pertaining to past and present jobs');
        $this->config->set_item('main_content', 'jobs/main_nav');
        $this->load->model('Jobs_model');
        $JobList['AllJobs'] = $this->Jobs_model->GetAllJobs();
        $this->load->view('includes/template', $JobList);
      }
    Code from my View
    Code:
    <div>
      <?php
      // THESE LINES OUTPUT THE EXAMPLE BELOW
      // WHICH IS EXACTLY WHAT I AM EXPECTING
      // JUST NEED TO POPULATE THE DROPDOWN WITH IT
      // INSTEAD OF JUST ECHO'ING TO SCREEN
      echo "Job List: <br />";
      foreach($AllJobs as $EachJob) {
        echo $EachJob->Job_Number . ", " . $EachJob->ListJob . "<br />";
      }
      // END OF CODE JUST TO TEST DATABASE ARRAY VALUES
      // NOW START THE "TRUE" CODE FOR THE SITE
      echo form_open('jobs/addeditjob');
      echo form_label('Job # ', 'jobnum');
      //echo form_dropdown('jobnum', $AllJobs); (THIS CODE CAUSES PAGE TO HANG)
      echo form_close();
      ?>
    </div>
    Output of DB Query from test lines in View
    2196, 2196 - Some Company
    3991, 3991 - Another Company
    4482, 4482 - Another Company
    6531, 6531 - Different Business
    7943, 7943 - Names Not Important
    7982, 7982 - Names Not Important
    Greg Hicks
    Tekamba Computers, LLC
    greg@tekamba.com
    http://tekamba.com

  5. #5
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    UPDATE #2:

    I found a post online that has now allowed me to populate the dropdown, but it is setting the value and user displayed values, to be the same, here is the updated Model that creates code to populate the dropdown:

    Code:
      public function GetAllJobs() {
        // Using ActiveRecords - set fields to retrieve data from
        $this->db->select('Job_Number, CONCAT(Job_Number, " - ",Company_Name) as ListJob', FALSE);
        // Using ActiveRecords - define the order by criteria
        $this->db->order_by('Job_Number', 'asc');
        // Using ActiveRecords - process the query against the DB
        $query = $this->db->get('Jobs');
    
        if($query->num_rows() > 0):
          $JobList = $query->result();
          foreach ($JobList as $CurrentJob) {
            $ListAllJobs[$CurrentJob->Job_Number] = $CurrentJob->Job_Number; (This line is redundant, I had hoped it would use THIS as the VALUE for the option statements in the compiled HTML)
            $ListAllJobs[$CurrentJob->ListJob] = $CurrentJob->ListJob;
          }
          $ListJobs = $ListAllJobs;
          return $ListJobs;
        else:
          return false;
        endif;
      }
    Greg Hicks
    Tekamba Computers, LLC
    greg@tekamba.com
    http://tekamba.com

  6. #6
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,834
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by GHicks View Post
    UPDATE #2:

    I found a post online that has now allowed me to populate the dropdown, but it is setting the value and user displayed values, to be the same, here is the updated Model that creates code to populate the dropdown:

    Code:
      public function GetAllJobs() {
        // Using ActiveRecords - set fields to retrieve data from
        $this->db->select('Job_Number, CONCAT(Job_Number, " - ",Company_Name) as ListJob', FALSE);
        // Using ActiveRecords - define the order by criteria
        $this->db->order_by('Job_Number', 'asc');
        // Using ActiveRecords - process the query against the DB
        $query = $this->db->get('Jobs');
    
        if($query->num_rows() > 0):
          $JobList = $query->result();
          foreach ($JobList as $CurrentJob) {
            $ListAllJobs[$CurrentJob->Job_Number] = $CurrentJob->Job_Number; (This line is redundant, I had hoped it would use THIS as the VALUE for the option statements in the compiled HTML)
            $ListAllJobs[$CurrentJob->ListJob] = $CurrentJob->ListJob;
          }
          $ListJobs = $ListAllJobs;
          return $ListJobs;
        else:
          return false;
        endif;
      }
    Try this:

    http://www.johns-jokes.com/sitepoint_forum_ghicks.html

    Source at page bottom.
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  7. #7
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,834
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by GHicks View Post
    UPDATE #2:

    I found a post online that has now allowed me to populate the dropdown, but it is setting the value and user displayed values, to be the same, here is the updated Model that creates code to populate the dropdown:

    Code:
      public function GetAllJobs() {
        // Using ActiveRecords - set fields to retrieve data from
        $this->db->select('Job_Number, CONCAT(Job_Number, " - ",Company_Name) as ListJob', FALSE);
        // Using ActiveRecords - define the order by criteria
        $this->db->order_by('Job_Number', 'asc');
        // Using ActiveRecords - process the query against the DB
        $query = $this->db->get('Jobs');
    
        if($query->num_rows() > 0):
          $JobList = $query->result();
          foreach ($JobList as $CurrentJob) {
            $ListAllJobs[$CurrentJob->Job_Number] = $CurrentJob->Job_Number; (This line is redundant, I had hoped it would use THIS as the VALUE for the option statements in the compiled HTML)
            $ListAllJobs[$CurrentJob->ListJob] = $CurrentJob->ListJob;
          }
          $ListJobs = $ListAllJobs;
          return $ListJobs;
        else:
          return false;
        endif;
      }
    Try this (BEWARE - not tested):
    PHP Code:
    public function GetAllJobs()
    {
      
    $RESULT = array(); # Default

      # Using ActiveRecords
        # set fields to retrieve data from
        
    $this->db->select('Job_Number, Company_Name'FALSE);
      
        
    # define the order by criteria
        
    $this->db->order_by('Job_Number''asc');
      
        
    # process the query against the DB
        
    $Jobs $this->db->get('Jobs');
        if(
    $Jobs->num_rows() > 0):

          
    $Jobs $query->result();
          foreach (
    $Jobs as $Job):
            
    $RESULT[] = $Job->Job_Number => $Job->Job_Number .' - ' .$Job->Company_Name;
          endforeach;
        endif;

      return 
    $RESULT# maybe empty array()

    Last edited by John_Betong; Jun 30, 2013 at 21:46. Reason: changed $RESULT to array()
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  8. #8
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, I will look at these suggestions and let you know how they work out.
    Greg
    Greg Hicks
    Tekamba Computers, LLC
    greg@tekamba.com
    http://tekamba.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •