Jump to content

[SOLVED] Array PRO needed! Data driven form Array data, aaarrgh!


methodman

Recommended Posts

I need some help collecting the data from this data driven form into a SINGLE array so that i can pull the information one variable at a time from the $_POST array after the form is submitted???

 

DETAILS:

the form is created by looping through the data in my database and filling the form with prior submitted information. Think of it as a checklist, in which some items are 'complete' and others are 'incomplete'. I need the list to remain up to date each time i login to work with the form.

 

here is the code for the $_POST submission data collection. The problem is that i cannot seem to get the information back out of the array in any useful format. I tried to use a foreach() loop but the collected data wasn't correct.

 

I used the print_r() function to show me what is happening in the array, here:

 

Array

(

    [404] => 1

    [notes] =>

    [405] => 1

    [406] => 1

    [407] => 2

    [408] => 2

    [409] => 2

    [410] => 2

    [411] => 2

    [412] => 2

    [413] => 2

    [414] => 2

    [415] => 2

    [416] => 2

    [417] => 2

    [418] => 2

    [419] => 2

    [submit] => Save updates

)

 

I need an output that looks more like this:

 

Array (

    [ '404', '1', 'string of text from the textbox' ],

    [ '405', '2', 'string of text from the 2nd textbox' ],

    [ '406', '1', 'string of text from the 3rd textbox' ],

    [ and so on...]

    [ $step_id, $curr_status, $notes ] << variables in above array..

);

 

there are a total of 16 text boxes / status radio buttons. I would like to keep the flexibility to add more items in the future, so a standard for(...i++...) loop structure will not work.

 

I am trying to see if this will work:

while(list($step_id, $curr_status, $notes) = each ($_POST))

 

this is the info for the above variables:

[ '404', '1', 'string of text from the textbox' ]

 

I am not having any luck with that statement, but those are the 3 variables i need to pass. Each of the 3 vars will need to be passed for all 16 radio buttons and the 16 associated textboxes.

 

i am completely clueless, and any help is greatly appreciated!!

Here is the code for the database $query and the form:

 

// the code to query the database and insert the new information - this doesn't seem to work at all...

if(isset($_POST['submit']))
{
    // write the updates to the status table
while(list($curr_status, $notes) = each($_POST))
{
	$query = "UPDATE status SET curr_status = '$curr_status', notes = '$notes' " .
			"WHERE status_id = '$status_id' AND user_id = '$client_id'";
	mysqli_query($dbc, $query)or die('ERROR UPDATING DB, status and notes were NOT updated');	
}	
	print_r($_POST);
	echo '<p>Your updates have been saved.</p>';
}

//generate the updated form by looping through the status array
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '?user_id=' . $_GET['user_id'] . '">';
echo '<h3>Client update</h3><br />';
$phase = $updates[0]['phase_name'];
echo '<fieldset><legend>' . $updates[0]['phase_name'] . '</legend>';

foreach($updates as $update)
{
//only start a new fieldset if the phase has changed
if($phase != $update['phase_name'])
{
	$phase = $update['phase_name'];
	echo '</fieldset><fieldset><legend>' . $update['phase_name'] . '</legend>';
}

//display the step form field
echo '<label ' . ($update['curr_status'] == NULL ? 'class="error"' : '') . ' for="' . $update['status_id'] . '">' . 
$update['step_name'] . ':</label>';

echo '<input type="radio" id="' . $update['status_id'] . '" name="' . $update['status_id'] . '" value="2" ' .
($update['curr_status'] == 2 ? 'checked="checked"' : '') . ' /><span id="statusInc">Incomplete</span> ';

echo '<input type="radio" id="' . $update['status_id'] . '" name="' . $update['status_id'] . '" value="1" ' .
($update['curr_status'] == 1 ? 'checked="checked"' : '') . ' /><span id="statusComp">Complete!</span><br />';

echo '<textarea id="notes" name="notes" rows="3" cols="80">' . $update['notes'] . '</textarea><br />';

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die ('ERROR CONNECTING TO DATABASE');
$query = "INSERT INTO status (date) VALUES (NOW())";
$timestamp = mysqli_query($dbc, $query) or die ('ERROR with the timestamp query');          
//echo date("D, d M y g a"); // the date must be sent in a post to register for each individual item	
}
echo '</fieldset>';
echo '<input type="submit" value="Save updates" name="submit" />';
echo '</form>';
?>

Link to comment
Share on other sites

try

<?php
// the code to query the database and insert the new information - this doesn't seem to work at all...

if(isset($_POST['submit']))
{
    // write the updates to the status table
foreach ($_POST['notes'] as $status_id => $notes)
{
	$curr_status = $_POST['radio'][$status_id];
	$query = "UPDATE status SET curr_status = '$curr_status', notes = '$notes' " .
			"WHERE status_id = '$status_id' AND user_id = '$client_id'";
	mysqli_query($dbc, $query)or die('ERROR UPDATING DB, status and notes were NOT updated');	
}	
	print_r($_POST);
	echo '<p>Your updates have been saved.</p>';
}

//generate the updated form by looping through the status array
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '?user_id=' . $_GET['user_id'] . '">';
echo '<h3>Client update</h3><br />';
$phase = $updates[0]['phase_name'];
echo '<fieldset><legend>' . $updates[0]['phase_name'] . '</legend>';

foreach($updates as $update)
{
//only start a new fieldset if the phase has changed
if($phase != $update['phase_name'])
{
	$phase = $update['phase_name'];
	echo '</fieldset><fieldset><legend>' . $update['phase_name'] . '</legend>';
}

//display the step form field
echo '<label ' . ($update['curr_status'] == NULL ? 'class="error"' : '') . ' for="' . $update['status_id'] . '">' . 
$update['step_name'] . ':</label>';

echo '<input type="radio" id="' . $update['status_id'] . '" name="radio[' . $update['status_id'] . ']" value="2" ' .
($update['curr_status'] == 2 ? 'checked="checked"' : '') . ' /><span id="statusInc">Incomplete</span> ';

echo '<input type="radio" id="' . $update['status_id'] . '" name="radio[' . $update['status_id'] . ']" value="1" ' .
($update['curr_status'] == 1 ? 'checked="checked"' : '') . ' /><span id="statusComp">Complete!</span><br />';

echo '<textarea id="notes" name="notes[' . $update['status_id'] . ']" rows="3" cols="80">' . $update['notes'] . '</textarea><br />';

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die ('ERROR CONNECTING TO DATABASE');
$query = "INSERT INTO status (date) VALUES (NOW())";
$timestamp = mysqli_query($dbc, $query) or die ('ERROR with the timestamp query');          
//echo date("D, d M y g a"); // the date must be sent in a post to register for each individual item	
}
echo '</fieldset>';
echo '<input type="submit" value="Save updates" name="submit" />';
echo '</form>';
?>

Link to comment
Share on other sites

SASA == 'PHP array PRO' !

 

THANK YOUUUUU! it worked perfectly! here is the output:

Array

(

    [radio] => Array

        (

            [404] => 1

            [405] => 1

            [406] => 1

            [407] => 2

            [408] => 2

            [409] => 2

            [410] => 2

            [411] => 2

            [412] => 2

            [413] => 2

            [414] => 2

            [415] => 2

            [416] => 2

            [417] => 2

            [418] => 2

            [419] => 2

        )

 

    [notes] => Array

        (

            [404] => this is the very first text box that will display on the customers progress login page. I was really hoping to make this work correctly, however that doesnt seem to be possible yet.

            [405] => Text box 2

            [406] =>

            [407] =>

            [408] =>

            [409] =>

            [410] =>

            [411] =>

            [412] =>

            [413] =>

            [414] =>

            [415] =>

            [416] =>

            [417] =>

            [418] =>

            [419] =>

        )

 

    [submit] => Save updates

)

 

Exactly what i was looking for! after staring at the code for 5 days... i couldn't see it anymore. now i'm good to go

THANKS AGAIN!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.