Jump to content

[SOLVED] Stomped. Array problem.


play_

Recommended Posts

Sorry, couldn't really come with a descriptive subject for thread.

 

Basically I have form inputs. Here's a screenshot:

array.png

 

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.

Link to comment
Share on other sites

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']);

?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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;
}

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.