Welcome Guest, Not a member yet? Register   Sign In
Getting array from database, displaying values in inputs
#1

[eluser]dallen33[/eluser]
I've been fiddling with this all day and can't figure it out.

I have a form that you can enter email address in. Right now, there are 3 fields to enter the emails into. However, the administrator of the site can determine how many emails can be entered by changing a value in a config file.

So the form for entering emails is displayed like this:
Code:
<?php
for ($i = 1; $i <= $this->config->item('book_proof_contacts', 'booking'); $i++)
{
    ${"contacts$i"} = array('name' => 'contacts'.$i, 'value' => set_value('contacts'.$i));
}

for ($i = 1; $i <= $this->config->item('book_proof_contacts', 'booking'); $i++)
{
    echo form_input(${"contacts$i"});
}
?&gt;
This displays three (because the config is set to 3) input fields. Nice. When you submit, the input values are added to a database with spaces in-between the email address. The following is in my controller.

Code:
$contacts = '';
for ($i = 1; $i <= $this->config->item('book_proof_contacts', 'booking'); $i++)
{
    $contacts .= $this->input->post('contacts'.$i).' ';
}
The above would be submitted into the "contacts" column in a table, like this: "[email protected] [email protected] [email protected] ".

Here's the problem. I want to display these on page in &lt;input&gt;s. Now, the amount of inputs would have to be based on the config amount. But there can be empty fields, so if someone only submits one email, you still get three inputs but only one is full.

What I'm doing right now is building a function called get_contacts:
Code:
function get_contacts($id)
{
    $query = $this->db->get_where('ads', array('id' => $id));
    if ($query->num_rows() > 0):
        $result = '';
        $row = $query->row();
        if ($row->contacts):
            $array = explode(" ", $row->contacts, -1);
            return $array;
        endif;
    else:
        return FALSE;
    endif;
}

Then on the page where I want to display the contacts, I do this:
Code:
$contacts = $this->booking->get_contacts($ad_row->id);

if ($contacts):
    echo form_open($this->uri->uri_string(), array('id' => 'add_client'));
    foreach ($contacts as $array):
        echo '&lt;input type="text" name="contacts[]" value="'.$array.'" /&gt;';
    endforeach;
    echo form_submit('submit', 'Change Contacts');
    echo form_close();
else:
    echo 'No Contacts.';
endif;

The problem I have is that an input is only displayed for each contact. But I want there to be 3 (or whatever the amount) input fields, even if only one or two is filled. I can't figure out how to do this. I tried for loops but it ended up looping the array value.

Anyone have any idea? I'm lost!
#2

[eluser]crikey[/eluser]
Hi GodsHand,

What I'd do is loop through the number of contacts (based on the config value), and then for each iteration: if the contacts array isn't empty use array_shift to grab the next value, otherwise set the value to '', and print your input field with the value.

Sorry, no pseudocode because I'm pressed for time, but that's the idea.

EDIT: Oops, what I meant was loop through the input fields required, based on the config value.
#3

[eluser]rameners[/eluser]
You can try it by hard-coding the limit. e.g... IMO

Code:
for (i=0; i < 3; i++)
{
//if condition: set condition if email is not = ""
//echo the input field then set the value to index i
//else
//echo the input field having no values
}
#4

[eluser]dallen33[/eluser]
I figured it out, so thought I'd post my solution incase it can help someone! Thanks for your help guys!
Code:
if ($contacts):
    echo form_open($this->uri->uri_string(), array('id' => 'add_client'));
        for ($i = 0; $i <= count($contacts)-1; $i++):
            echo '&lt;input type="text" name="contacts[]" value="'.$contacts[$i].'" /&gt;';
        endfor;
        if (count($contacts) < $this->config->item('book_proof_contacts', 'booking')):
            for ($i = count($contacts); $i <= $this->config->item('book_proof_contacts', 'booking')-1; $i++):
                echo '&lt;input type="text" name="contacts[]" value="" /&gt;';
            endfor;
        endif;
    endif;
I have a -1 in my loops because there's an extra space at the end of my database field that doesn't need to be there.




Theme © iAndrew 2016 - Forum software by © MyBB