Jump to content

Recommended Posts

I have situation like this

 

$key=array($_post['abc']);

$all=array();

foreach ($key as $row)

{

$all=explode(",",$row);

}

 

result is

0=>1

1=>'xyz'

2=>'04/04/2015'

......

 

I want to assign index as field name for each index

 

result should be like this

id=>1

name=>'xyz'

date=>'06/04/2015'

....

 

and it should assign this field name for each index

 

Please let me know

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/
Share on other sites

I don't know why you're doing it like you are, if I understand correctly. There is no need to put the POST value in a separate array and then loop over it, only to explode the value and assign it to another array.

 

This assumes you have a form using arrays for the field name, IE:

<input name="abc[]" type="checkbox" />

<input name="abc[]" type="checkbox" />

$updated = array();
if (isset($_POST['abc']) && is_array($_POST['abc']))
{
  foreach($_POST['abc'] as $field)
  {
    //grab id, name and date and assign to variables
    list($id, $name, $date) = explode(',', $field);

    //append $updated array with new array using wanted named key => field pairs
    $updated[] = array(
      'id'   => $id,
      'name' => $name,
      'date' => $date
    );
  }
}

print_r($updated);
//assuming $_POST['abc'][0] = '1,xyz,06/04/2015'
//assuming $_POST['abc'][1] = '2,abc,06/05/2015'
array(
  0 => array(
    'id'   => '1',
    'name' => 'xyz',
    'date' => '06/04/2015'
  ),
  1 => array(
    'id'   => '2',
    'name' => 'abc',
    'date' => '06/05/2015'
  )
);

If you're meaning your form is set up like this and NOT using arrays for input names:

<input name="abc" type="checkbox" />

 

then it would be more like

$updated = array();
if (isset($_POST['abc']) && ! empty($_POST['abc']))
{
  //grab id, name and date and assign to variables
  list($id, $name, $date) = explode(',', $_POST['abc']);

  //append $updated array with new array using wanted named key => field pairs
  $updated = array(
    'id'   => $id,
    'name' => $name,
    'date' => $date
  );
}

print_r($updated);
//assuming $_POST['abc'] = '1,xyz,06/04/2015'

array(
  'id'   => '1',
  'name' => 'xyz',
  'date' => '06/04/2015'
);
Edited by CroNiX

Guru Yo are close to what I was aksing

 

here is my $_post['abc'] result

 

$_Post['abc']='1,xyz,06/04/2015'

 

now when I explode it result would be like this

0=>1

1=>'xyz'

2=>'06/04/2015'

 

and when I do it

list($id,$name,$date)=explode(',',$_POST['abc'])

 

I am getting result

id=>1

name->'xyz'

date=>'06/04/2015'

 

but I want to get id,name and date for each element.

 I have tried the way you shown in above post but I am not getting any result. no array.

 

Please let me know how can I show id,name ,date for each selected array index

 

Thanks in advance

 

and guys I am beginner not pro. so ia m kindly asking your help.

Alternative

$updated = array();
$fieldnames = array('id', 'name', 'date');

if (isset($_POST['abc']) && ! empty($_POST['abc']))
{
  //grab id, name and date and assign to variables
  $updated = array_combine($fieldnames, explode(',', $_POST['abc']));
}

Giving

$updated Array
(
    [id] => 1
    [name] => xyz
    [date] => 06/04/2015
)

This is the first time you have indicated that $_POST['abc'] is an array and not string.

 

try

$updated = array();
$fieldnames = array('id', 'name', 'date');

if (isset($_POST['abc']) && ! empty($_POST['abc']))
{
  //grab id, name and date and assign to variables
  foreach ($_POST['abc'] as $abc) {
    $updated[] = array_combine($fieldnames, explode(',', $abc));
  }
}
echo '<pre>',print_r($updated, true),'</pre>';

Should give something like this

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => fred
            [date] => 2015-05-06
        )

    [1] => Array
        (
            [id] => 2
            [name] => tom
            [date] => 2015-05-07
        )

)

here is my issue again from first page I am getting value from checkbox like this

 

below is value for ($_post['abc'])

 

0=>'1','xyz','06/08/2015

1=>'2','pqr','07/08/2015

 

what i want is

 

id=>1

name=>xyz

date=>06/08/2015

id=>2

name=>pqr

date=>07/08/2015

 

Please let me know if you can get idea what i am talking about

 

MERGED

Here is my form and I am trying to update value in sql server

<div class="container">    <form method="post" name="Update" action="update3.php"  />     <table id="chkbx1" border="1" id="results" class="hovertable" >        <thead>            <tr >                                <th>check</th>                <th>Lname</th>                <th>Role</th>                <th>ExpireDate</th>                <th>Add Days</th>            </tr>        </thead>        <tbody>            <?php            $serverName = DB_SERVER;            $connectionInfo = array("UID" => DB_USER, "PWD" => DB_PASS, "Database" => DB_NAME);            $conn = sqlsrv_connect($serverName, $connectionInfo);            if ($conn === false) {                die(print_r(sqlsrv_errors(), true));            }            // Change SQL HERE Season DESC or Season to sort by FALL or SUMMER            $sql = "select  datarooms.Dataroomid,memberinfo.lastname,role,dateexpire,memberinfo.memberid from"                    . " datarooms  join membership on membership.dataroomid=datarooms.dataroomid "                    . "  join memberinfo on memberinfo.memberid=membership.memberid "                    . " where   datarooms.Dataroomid= '" . 69 . "'    order by datarooms.expdate desc";            $stmt = sqlsrv_query($conn, $sql);            if ($stmt === false) {                die(print_r(sqlsrv_errors(), true));            }            $counter = 1;            while ($row = sqlsrv_fetch_array($stmt)) {                echo '<tr>';                echo '<td>' . '<input id="abc" type="checkbox" name=abc[] " value="' . $row['memberid'] . '"   ">' . '</td>';                 echo '<td style="display:none;">' .  $row['memberid']. '</td>';                echo '<td>' . $row['lastname'] . '</td>';                               //echo '<td>'.$row['role'].'</td>';                echo "<td>" . '<select id="combo' . $counter . '" name="combo" class="mycombo" >'                . '<option ';                if ($row['role'] == "Administrator") {                    echo "selected";                }                echo '>Administrator</option>'                . '<option  ';                if ($row['role'] == "Read-Only") {                    echo "selected";                }                echo '>Read-Only</optiion>'                . '<option ';                if ($row['role'] == "Contributor") {                    echo "selected";                }                echo '>Contributor</optiion>'                . '<option ';                if ($row['role'] == "View-Only") {                    echo "selected";                }                echo '>View-Only</optiion>'                . ' </select>'                . "</td>";                echo '<td>' . date_format($row['dateexpire'], "m/d/Y") . '</td>';                echo "<td>" . '<select id="daycombo" class="mycombo" >                                <option>1</option>                                <option>3</option>                                <option>7</option>                                <option>14</option>                                <option>30</option>'                . '</select>'                                        . "</td>";                                echo '</tr>';                $counter++;            }            echo '</tbody>';            echo '</table>';//            echo "<input type='submit' id='button1' name='submit' value='submit' style='float:center;margin-top:220px;' >";            echo "<input id='button1' type='button' class='move'  value='Submit'style='float:left;margin-top:400px;'';/>";            sqlsrv_free_stmt($stmt);            ?>        </tbody>    </table></form></div><!--<script src="../assets/js/jquery.dataTables.min.js"></script><script src="../assets/js/datatables.js"></script>--><script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript"></script><script type="text/javascript">                $('#button1').click(function() {                                          iterate();        });    $(".mycombo").change(function() {    ($(this).find("option:selected").text());    });         function iterate() {        var values = new Array();        $.each($("input[name='abc[]']:checked").closest("td").siblings("td"),                function(i, idx) {                    if ($(this).find("select").attr("class") == "mycombo") {                        values.push($(this).find("option:selected").text());                    } else {                        values.push($(this).text());//                          alert("value: " + values.join(", "));                    }                });        var url = 'update3.php';        var form = $('<form action="' + url + '" method="post">' +                '<input type="text" name="abc" value="' + values + '" />' +                                                                                           '</form>');        $('body').append(form);        $(form).submit();            }</script>

MERGED

 

On update 3 I want to update selected record in sql server. Guru here is my code. please help me now

Edited by Ch0cu3r
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.