• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Array from view to database (HELP)

#1
[eluser]sapid_guy[/eluser]
Ok here is the thing

i have a form generated from a DB
the form contain a list of names with chose of SELECT and Text Area,

More

Form Code
Code:
<form action="" method="post" enctype="multipart/form-data">

<input type="checkbox" name="name[]" value="1.jpg"  />
<input type="text" name="queue[]" value="" maxlength="2" size="10" style="width:50%"  />

<input type="checkbox" name="name[]" value="Blue_hills.jpg"  />
<input type="text" name="queue[]" value="" maxlength="2" size="10" style="width:50%"  />

<input type="checkbox" name="name[]" value="Blue_hills1.jpg"  />
<input type="text" name="queue[]" value="" maxlength="2" size="10" style="width:50%"  />

Name the List:
<input type="text" name="list_name" value="" size="30" />
<input type="submit" value="Creat List" />
</form>

Array is my weak point in Programming.

When i use print_r

test2
Array ( [img_name] => Array ( [0] => 1.jpg [1] => Blue_hills.jpg ) )
Array ( [img_que] => Array ( [0] => 3 [1] => 2 [2] => [3] => ) )

why when i check the data the images selected is posted but the text area are posted even the empty once????

#2
[eluser]TheFuzzy0ne[/eluser]
Yes, they will be. Text inputs still have a value, even if the value is "".

#3
[eluser]sapid_guy[/eluser]
So how usually someone will work it out?!

#4
[eluser]TheFuzzy0ne[/eluser]
Your code is not clear enough for me to understand correctly.

What are you using print_r on? img_name and img_que do not match the names specified in your form. If you can show us what's really going on, someone may be able to assist you better.

#5
[eluser]sapid_guy[/eluser]
[quote author="TheFuzzy0ne" date="1234367466"]Your code is not clear enough for me to understand correctly.

What are you using print_r on? img_name and img_que do not match the names specified in your form. If you can show us what's really going on, someone may be able to assist you better.[/quote]



The View
Code:
echo form_open_multipart('lists/lists_home/register_list_det');
echo '<table border="1" cellpadding="4" cellspacing="0">
<tr>
<th>Content ID</th>
<th>Name</th>
<th>date_added</th>
<th>status</th>
<th>Thumb</th>
<th>Select</th>
<th>Queue</th>
</tr>';

foreach ($query_img->result() as $row)
    {
        
    
      echo '<tr>';
      echo '<td>';
      echo $row->img_id;
      echo '</td>';
      
      echo '<td>';
      echo $row->img_name;
      echo '</td>';

      echo '<td>';
      echo $row->status;
      echo '</td>';

      echo '<td>';
      echo $row->date_added;
      echo '</td>';


    $atts = array(
                      'width'      => '800',
                      'height'     => '600',
                      'scrollbars' => 'yes',
                      'status'     => 'no',
                      'resizable'  => 'no',
                      'screenx'    => '0',
                      'screeny'    => '0'
                    );
                    
      echo '<td>';
      echo anchor_popup(base_url().'content/images/'.$row->img_name, '<img >img_name.'" />', $atts);
      echo '</td>';


      echo '<td>';
      echo form_checkbox('name[]', $row->img_name, FALSE);
      echo '</td>';
    
     $data_form = array(
              'name'        => 'queue[]',
              'value'       => '',
              'maxlength'   => '2',
              'size'        => '10',
              'style'       => 'width:50%',
            );
            
                
      echo '<td>';
      //echo form_input('queue','','','2','10','width:50%');
      echo form_input($data_form);
      //echo '&lt;input type="text" name="queue[]" value="" size="10" maxlength="2" /&gt;';
      //&lt;input type="text" name="title" value="" size="52" /&gt;
      echo '</td>';    
  
      echo '</tr>';
}

      echo '<tr>';
      echo '<td>';
      echo '--- ---';
      echo '</td>';
      
      echo '<td>';
      echo '-';
      echo '</td>';

      echo '<td>';
      echo '-';
      echo '</td>';

      echo '<td>';
      echo '-';
      echo '</td>';

      echo '<td>';
      echo '-';
      echo '</td>';

      echo '<td>';
      echo '-';
      echo '</td>';

      echo '<td>';
      echo '-';
      echo '</td>';
            
      echo '</tr>';



      echo '<tr>';
      echo '<td>Name the List:';
      echo '</td>';
      
      echo '<td>';
      echo '&lt;input type="text" name="list_name" value="" size="30" /&gt;';
      echo '</td>';

      echo '<td>';
      echo '-';
      echo '</td>';

      echo '<td>';
      echo '-';
      echo '</td>';

      echo '<td>';
      echo '-';
      echo '</td>';


      echo '<td>';
      echo '&lt;input type="submit" value="Creat List" /&gt;';
      echo '</td>';

      echo '<td>';
      echo '-';
      echo '</td>';      
      
      echo '</tr>';


echo '&lt;/form&gt;';

echo '</table>';


The Controller
Not yat Complete

Code:
echo $this->input->post('list_name');

$sql = "INSERT INTO list_img (list_name)
            VALUES (".$this->db->escape($this->input->post('list_name'))."
                    )";
                $this->db->query($sql);


     $form_data_img = array(
                        'img_name' =>       $this->input->post('name'),
                    
                    );

     $form_data_que = array(
                  
                        'img_que' =>          $this->input->post('queue')
                    );

echo count($form_data_img['img_name']);
//exit;
echo '<br /><br />';
print_r($form_data_img);
echo '<br /><br />';
print_r($form_data_que);
echo '<br /><br />';

$i=0;
while($i<count($form_data_img['img_name'])){
    
echo $form_data_img['img_name'];
echo '<br />';
echo $form_data_que['img_que'];    
    
    echo $i++;
    
}


Am traying to get the data and insert them into a table

the table

CREATE TABLE `list_img` (
`list_id` bigint(20) NOT NULL auto_increment,
`list_name` varchar(100) NOT NULL,
`date_created` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`list_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `list_img`
--

INSERT INTO `list_img` (`list_id`, `list_name`, `date_created`) VALUES
(1, 'list_img_default', '2009-01-20 22:54:40'),
(2, 'test', '2009-02-11 17:12:35');

-- --------------------------------------------------------

--
-- Table structure for table `list_img_content`
--

CREATE TABLE `list_img_content` (
`id` bigint(20) NOT NULL auto_increment,
`list_id` bigint(20) NOT NULL,
`file_name` varchar(100) NOT NULL,
`queue` int(2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `list_img_content`
--

INSERT INTO `list_img_content` (`id`, `list_id`, `file_name`, `queue`) VALUES
(1, 1, '2.jpg', 1),
(2, 1, '1.jpg', 2);




If you have noticed the controller still in construction
Because i been trying to solve the issues of the data posted first.


Thanks for the help.

#6
[eluser]TheFuzzy0ne[/eluser]
I would recommend that you run some validation in there somewhere.

Code:
echo $this->input->post('list_name');



$sql = "INSERT INTO list_img (list_name) VALUES ("
    .$this->db->escape($this->input->post('list_name'))
    .")";
/*
* Avoid running this query until you've validated your input
* otherwise you will end up with database anomalies.
*/

//$this->db->query($sql);

// We don't need to put these into an array, as they already are
// in an array.
$img_name = $this->input->post('name');
$img_que = $this->input->post('queue');

// We only want to process the fields that have been checked,
// so we use the $img_name array as our main reference:
// We only want to check img_que if an entry exists in $img_name.
foreach ($img_name as $key=>$value)
{
    // Just echo the values so you can see what happening.
    echo ""
    . "<p>"
    . "image name: " . $value . "<br />"
    . "image queue: " . $img_que[$key]
    . "</p>\n";
}

// You can modify the above to give you the data you need
// for your database insert.
// THIS is where the database insert should take place.

The code above is untested, but hopefully it should help simplify things for you a little.

#7
[eluser]sapid_guy[/eluser]
Thanks TheFuzzy0ne

Its working however; there is a small issue.

Lets say that the user had 4 images to select from

is the user selects 1 3 and 4

based on your code the result would be

test_list_images

image name: 1.jpg
image queue: 2

image name: Blue_hills1.jpg
image queue:

image name: Sunset.jpg
image queue: 1


So you see if the user skip any image the one after it will not be recognize,

it very strange behavior, i have never faced such a thing.

if the user select the 2 and 4 out of 4 choses

test_list_images

image name: Blue_hills.jpg
image queue:

image name: Sunset.jpg
image queue: 2

#8
[eluser]sapid_guy[/eluser]
I have changed the code.
i have changed it into steps

Step One
Select The images and name the list

Step Two
Select Queue


I think thats solve the issue though i dont think its the best way to solve such a thing.
Thanks TheFuzzy0ne for the input it did help.
However i would still pretty much like to see a solution for such issue.
Thanks All

#9
[eluser]TheFuzzy0ne[/eluser]
The code only iterates through the fields that have been selected. You could change have it do the same thing, but iterate all of the text fields on the page. I assumed you only wanted to get the information that had been selected. However, I do believe that my idea was flawed. The array indexes do not correspond with one another.

If you are relying on all of the data to be supplied, you will need to implement some validation to check it's all there. It's very easy for someone to manipulate post data, and break your application.

I think you may need to rethink your HTML layout. Perhaps do something like this:
Code:
&lt;form action="" method="post" enctype="multipart/form-data"&gt;

&lt;input type="checkbox" name="id[]" value="1"  /&gt;
&lt;input type="hidden" name="name[]" value="1.jpg" /&gt;
&lt;input type="text" name="queue[]" value="" maxlength="2" size="10" style="width:50%"  /&gt;

&lt;input type="checkbox" name="id[]" value="2"  /&gt;
&lt;input type="hidden" name="name[]" value="Blue_hills.jpg" /&gt;
&lt;input type="text" name="queue[]" value="" maxlength="2" size="10" style="width:50%"  /&gt;

&lt;input type="checkbox" name="id[]" value="3"  /&gt;
&lt;input type="hidden" name="name[]" value="Blue_hills1.jpg" /&gt;
&lt;input type="text" name="queue[]" value="" maxlength="2" size="10" style="width:50%"  /&gt;

Name the List:
&lt;input type="text" name="list_name" value="" size="30" /&gt;
&lt;input type="submit" value="Creat List" /&gt;
&lt;/form&gt;

Note the checkboxes have IDs instead. You could then iterate through the name array (which will contain all of the results, not just the ones that are checked), and you can test whether they are checked by checking the if the id array with the given index id is empty or not. The original code was only iterating through items that had been checked, but as I mentioned, it was flawed.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.