play_ Posted August 19, 2009 Share Posted August 19, 2009 Sorry, couldn't really come with a descriptive subject for thread. Basically I have form inputs. Here's a screenshot: The fields under the 'item' column are named items[]. The fields under the 'brand' column are named brands[]. The fields under the 'price' column are named prices[]. The user is not required to fill any of them out. However, if the user fills one field out, the other 2 in that row, if left blank, would default to 'n/a'. So how would one go about accomplishing this? In my head, i thought of doing a count for each $_POST array. i would have to see which $_POST (brands, items, prices) has the most elements and use that for the for-loop So let's assume $_POST['brands'] has the most elements $final = array(); for( $i = 0; $i <= count($_POST['brands']; $i++) { $tmp = array(); $curr_brands = $_POST['brands'][$i]; $curr_items = $_POST['items'][$i]; $curr_prices = $_POST['prices'][$i]; if( isset($curr_brands) ) { $tmp['brands'] = $curr_brands; } else { $tmp['brands'] = 'n\a'; } if( isset($curr_prices) ) { $tmp['prices'] = $curr_prices; } else { $tmp['prices'] = 'n\a'; } if( isset($curr_items) { $tmp['items'] = $curr_items; } else { $tmp['items'] = 'n\a'; } $final[] = $tmp } But I haven't tried this code, but it does show what I have in mind. Was wondering if there's a better, more professional way of accomplishing this. If not, how can i determine which $_POST arrays has the most elements? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/170963-solved-stomped-array-problem/ Share on other sites More sharing options...
JonnoTheDev Posted August 19, 2009 Share Posted August 19, 2009 First of all you do not want to use a count of any post data for your loop as empty fields will mean it wont loop the required number of times. You need to count the number of rows displayed i.e 3 rows to use in the for loop. <?php $numRows = 3; for($x = 0; $x < $numRows; $x++) { if(!strlen(trim($_POST['brands'][$x]))) { $_POST['brands'][$x] = "n/a"; } if(!strlen(trim($_POST['items'][$x]))) { $_POST['items'][$x] = "n/a"; } if(!strlen(trim($_POST['prices'][$x]))) { $_POST['prices'][$x] = "n/a"; } } print_r($_POST['brands']); print_r($_POST['items']); print_r($_POST['prices']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/170963-solved-stomped-array-problem/#findComment-901736 Share on other sites More sharing options...
play_ Posted August 19, 2009 Author Share Posted August 19, 2009 Well you're right. That's an easier way to do it. I don't have to assign $numRows, as count($_POST['brands']) returns 3, even if they're blank. In any case, there is javascript to dynamically add more fields, so I cant hardcode the exact number. Thanks for the tip! Quote Link to comment https://forums.phpfreaks.com/topic/170963-solved-stomped-array-problem/#findComment-901745 Share on other sites More sharing options...
JonnoTheDev Posted August 19, 2009 Share Posted August 19, 2009 Thanks for the tip! No probs Quote Link to comment https://forums.phpfreaks.com/topic/170963-solved-stomped-array-problem/#findComment-901750 Share on other sites More sharing options...
play_ Posted August 19, 2009 Author Share Posted August 19, 2009 Alright, i settled for this $info = array(); for($i = 0; $i < count($brands); $i++ ) { $curr_brands = $_POST['brands'][$i]; $curr_items = $_POST['items'][$i]; $curr_prices = $_POST['prices'][$i]; if( (strlen($curr_brands)) || (strlen($curr_items)) || (strlen($curr_prices)) ) { // if we're here, one of the fields in this row has a value if( !strlen($curr_brands) ) $curr_brands = 'n/a'; if( !strlen($curr_items) ) $curr_items = 'n/a'; if( !strlen($curr_prices) ) $curr_prices = 'n/a'; $tmp = array(); $tmp['brands'] = $curr_brands; $tmp['items'] = $curr_items; $tmp['prices'] = $curr_prices; $info[] = $tmp; } return $info; } Quote Link to comment https://forums.phpfreaks.com/topic/170963-solved-stomped-array-problem/#findComment-901767 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.