Jump to content

seperating form values with arrays!!!


Monkuar

Recommended Posts

This is really cool if I can get it to work

 

My PHP CODE:

 

$ids = implode (",", $ibforums->input['checkbox']);

 

Grabs from this data in my HTML FORM:

 

<input name="checkbox[]"  type="checkbox" value="{$r['id']},{$r['friendid']}">

 

 

Now when I pass my input, I will get 2 different values in both array's how do I separate them out? I know how to seperate them out using implode, but I need to seperate BOTH of the array's that are being passed, and I need to use them separately.

 

Link to comment
Share on other sites

The counter part to implode, explode.

 

So if the value of checkbox is "1,2" and you run explode( ',' '1,2' ); you will get a value of array( 1, 2 );

 

if you want to split the entirety of the checkbox i'm pretty sure:

 

<?php

$idArray = array();
$friendIdArray = array();

foreach( $ibforums->input[ 'checkbox' ] AS $key => $val )
list( $idArray[], $friendIdArray[] ) = explode( ',', $val );

 

Now you should have two separate arrays of values with corresponding array keys.

Link to comment
Share on other sites

What? There is only ONE array: $_POST['checkbox']

 

The values of those array elements are simple a string with what appears to be two numbers separated by a comma. I don't know why you would want to implode the POST data. I would think you would want to run a foreach() loop on the checkbox array and then explode the value on the comma to get the two unique IDs and then perform an operation on them.

 

foreach($_POST['checkbox'] as $postVal)
{
    list($id, $friendid) = explode(',', $postVal);
    echo "ID: {$id}, Friend ID: {friendid}<br>\n";
}

Link to comment
Share on other sites

What? There is only ONE array: $_POST['checkbox']

 

The values of those array elements are simple a string with what appears to be two numbers separated by a comma. I don't know why you would want to implode the POST data. I would think you would want to run a foreach() loop on the checkbox array and then explode the value on the comma to get the two unique IDs and then perform an operation on them.

 

foreach($_POST['checkbox'] as $postVal)
{
    list($id, $friendid) = explode(',', $postVal);
    echo "ID: {$id}, Friend ID: {friendid}<br>\n";
}

 

it's not returning them seperated in " , " tho?

 

 

foreach($ibforums->input['checkbox'] as $postVal)
{
    list($id, $friendid) = explode(',', $postVal);
echo $friendid;
}

 

this will result in

 

echoing " 302 "

 

which is actually supposed to be 30,2

 

but i tried implode/explode the $friendid again and it's not working

the $id and $friendid do work and show both arrays, thank you, now i just need to stick a , between em

Link to comment
Share on other sites

Then something is NOT as you say it is. According to you the checkboxes are created like this:

<input name="checkbox[]"  type="checkbox" value="{$r['id']},{$r['friendid']}">

 

So, if you had three checkboxes with various values, they may look like this

<input name="checkbox[]"  type="checkbox" value="5,31">
<input name="checkbox[]"  type="checkbox" value="12,8">
<input name="checkbox[]"  type="checkbox" value="5,7">

 

If the user checked all those boxes the resulting POST array would be

[`] => "5,31",
[1] => "12,8",
[2] => "5,7"

 

That is an array with three string values. If you iterate through that array you can explode each value using the comma to get the two unique values. The code I provided should do just that - if your checkboxes are in the format you say they are.

Link to comment
Share on other sites

Then something is NOT as you say it is. According to you the checkboxes are created like this:

<input name="checkbox[]"  type="checkbox" value="{$r['id']},{$r['friendid']}">

 

So, if you had three checkboxes with various values, they may look like this

<input name="checkbox[]"  type="checkbox" value="5,31">
<input name="checkbox[]"  type="checkbox" value="12,8">
<input name="checkbox[]"  type="checkbox" value="5,7">

 

If the user checked all those boxes the resulting POST array would be

[`] => "5,31",
[1] => "12,8",
[2] => "5,7"

 

That is an array with three string values. If you iterate through that array you can explode each value using the comma to get the two unique values. The code I provided should do just that - if your checkboxes are in the format you say they are.

 

Yeah the array is not working though, if I use [1] like $id['1'] it only spits out the first character. I am using it inside the list function also.

 

This is my current code:

 

foreach($ibforums->input['checkbox'] as $postVal)
{
    list($id, $friendid) = explode(',', $postVal);
echo $id;
}

Exactly what you gave me, here are my checkboxes that a user will check (for instant)

 

 

<input name="checkbox[]" onclick="check(this)" type="checkbox" id="checkbox[]" value="3,30">

<input name="checkbox[]" onclick="check(this)" type="checkbox" id="checkbox[]" value="1,2">

 

 

So we should be getting 3 and 1 right?

 

I check those 2, and your code gives me:

31

 

which is right, it's getting 3 and 1, but i need it to be 3,1 which it is not displaying.

 

Thank you,

 

 

Link to comment
Share on other sites

$idArray = array();
$friendIdArray = array();

foreach( $ibforums->input[ 'checkbox' ] AS $key => $val ){
   list( $idArray[], $friendIdArray[] ) = explode( ',', $val );
}

echo implode(',', $idArray);
echo '<br>';
echo implode(',', $friendIdArray);

 

NOW THAT"S WHAT IM TALKING ABOUT!!!

 

WOW, Did not know you could freaking pass 2 array's in a value=XX this is great news LOL!!! man I bet I could even go up to 3-5 arrays eh? This is great news!! this code will be saved/etc so I can use in the future, thank you somuch, you just made my life 50% easier.

 

:thumb-up: :thumb-up: :thumb-up: :thumb-up: :thumb-up: :thumb-up: :thumb-up:

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.