hardikspider123 Posted June 4, 2015 Share Posted June 4, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/ Share on other sites More sharing options...
Barand Posted June 4, 2015 Share Posted June 4, 2015 What does $_post['abc'] contain? Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513193 Share on other sites More sharing options...
hardikspider123 Posted June 4, 2015 Author Share Posted June 4, 2015 it contain selected value from checkbox and its an array Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513197 Share on other sites More sharing options...
Barand Posted June 4, 2015 Share Posted June 4, 2015 So you want us to tell you how to get from X to your desired results when you won't even show us what X looks like? Not going to happen. Good luck with that. Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513199 Share on other sites More sharing options...
ginerjm Posted June 4, 2015 Share Posted June 4, 2015 Actually $_post['abc'] is probably undefined and contains nothing. Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513200 Share on other sites More sharing options...
hardikspider123 Posted June 4, 2015 Author Share Posted June 4, 2015 Sorry I dint mean it like that. result would be like this for $_post['abc'] 0=>1,xyz,06/04/2015 Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513201 Share on other sites More sharing options...
ginerjm Posted June 4, 2015 Share Posted June 4, 2015 $_post['abc'] does not exist unless you are naming one of you input fields that way. Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513202 Share on other sites More sharing options...
hardikspider123 Posted June 4, 2015 Author Share Posted June 4, 2015 I am getting data using $_post['abc'] and all values will be on 0 index but later I explode it all values Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513203 Share on other sites More sharing options...
ginerjm Posted June 4, 2015 Share Posted June 4, 2015 Perhaps you mean to type here $_POST['abc']? Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513204 Share on other sites More sharing options...
hardikspider123 Posted June 4, 2015 Author Share Posted June 4, 2015 no I don't want to type $_post['abc'] I just want to show my field name for each array index Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513205 Share on other sites More sharing options...
DavidPR Posted June 4, 2015 Share Posted June 4, 2015 I think he means that $_post should be written as $_POST... $key=array($_post['abc']); to $key=array($_POST['abc']); Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513206 Share on other sites More sharing options...
ginerjm Posted June 4, 2015 Share Posted June 4, 2015 I think I was pretty clear. Obviously there is a communication problem here. Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513207 Share on other sites More sharing options...
CroNiX Posted June 4, 2015 Share Posted June 4, 2015 (edited) 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 June 4, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513208 Share on other sites More sharing options...
hardikspider123 Posted June 5, 2015 Author Share Posted June 5, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513301 Share on other sites More sharing options...
Barand Posted June 5, 2015 Share Posted June 5, 2015 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513305 Share on other sites More sharing options...
hardikspider123 Posted June 5, 2015 Author Share Posted June 5, 2015 (edited) It give me below error array_combine():both parameters should have an equal number of elements Boolean false Edited June 5, 2015 by hardikspider123 Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513310 Share on other sites More sharing options...
Barand Posted June 5, 2015 Share Posted June 5, 2015 Then $_POST['abc'] doesn't contain 3 comma-separated items Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513311 Share on other sites More sharing options...
Barand Posted June 5, 2015 Share Posted June 5, 2015 (edited) deleted Edited June 5, 2015 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513312 Share on other sites More sharing options...
hardikspider123 Posted June 8, 2015 Author Share Posted June 8, 2015 if I select first checkbox I am getting answer as I expected but If I check another checkbox I am getting array_Combine(): both parameter should have an equal number of elements Boolean false error Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513457 Share on other sites More sharing options...
Barand Posted June 8, 2015 Share Posted June 8, 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 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513460 Share on other sites More sharing options...
hardikspider123 Posted June 8, 2015 Author Share Posted June 8, 2015 (edited) I am getting an error that Invalid argument supplied for foreach() Sorry I am beginner and I am giving you lot of troubles. But thanks for your patience and help. I really appriciate Edited June 8, 2015 by hardikspider123 Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513461 Share on other sites More sharing options...
CroNiX Posted June 8, 2015 Share Posted June 8, 2015 It would probably help if you also posted your form so we aren't guessing. You're only giving us bits and pieces of the relevant info. Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513462 Share on other sites More sharing options...
hardikspider123 Posted June 8, 2015 Author Share Posted June 8, 2015 but I can not copy code here. how can i copy here in this forum? Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513463 Share on other sites More sharing options...
hardikspider123 Posted June 8, 2015 Author Share Posted June 8, 2015 (edited) 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 June 8, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513464 Share on other sites More sharing options...
hardikspider123 Posted June 9, 2015 Author Share Posted June 9, 2015 Guru I have uploaded my code here. Can you please help me here? Quote Link to comment https://forums.phpfreaks.com/topic/296645-need-help-with-an-arrya-index/#findComment-1513516 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.