-1

Please Help Me.

I have form=myform like this

<form name='myform' method='post' action='update.php'>  
                  <div id='add-new-button' style='padding:9px;'>

                  <input type='submit' name='pilih' value='Update' ></div>
                  <div class='tbl_sp'> 

          <table cellspacing='0' cellpadding='0' id='table-detail'><thead >
          <tr>
          <th>No</th>
          <th>Custname</th>
          </tr></thead><tbody>";
          $counter = 1;
$query= mssql_query("SELECT top 10 * from Customer ");

$f=1;
while ($r=mssql_fetch_array($query))
{
    if (is_float($counter/2)) {$class = "class='alt'"; } else { $class = ""; };

    echo"<tr $class>
<td><input type='hidden' name='data".$f."' value='".$r['No']."' />$r[tNo]</td>
<td>$r[CustName]</td>
</tr>


    "; $f++;
          $counter++;  
          }
echo"</tbody></table><input type='hidden' name='m' value='".$f."' /></div><div class='center'>
          <table cellspacing='0' cellpadding='0' id='table-detail'><thead >
          <tr>
          <th>Check</th>
          <th>Tools</th>
          </tr></thead><tbody>";
          $counter = 1;
$query= mssql_query("SELECT  * from tools");

$g=1;
while ($r=mssql_fetch_array($query))
{
    if (is_float($counter/2)) {$class = "class='alt'"; } else { $class = ""; };

    echo"<tr $class>
<td><input type='checkbox' name='id".$g."' value='".$r['tools']."' /></td>
<td>$r[tools]</td>
</tr>


    "; $g++;
          $counter++;  
          }";

</div>";
echo"</tbody></table></form><input type='hidden' name='n' value='".$g."' /> </div>";
}}

results enter image description here

after form action update.php with code like this

    $tools_total = 0;
$customer_total = 0;
foreach ($_POST as $key => $value) {

  if (strpos($key, 'data') === 0) {
    $customer_total++;            //getting total number of checked customers
  }
  if (strpos($key, 'id') === 0) {
    $tools_total++;               //getting total number of tools
  }
}

$k=1;
for($i=1; $i<=$customer_total; $i++)
{

    ($k>$tools_total)?$k=1:''; //reset tools counter

    //This if statement is not necessary, since I'm guessing
    //your $_POST['m'] and $_POST['n'] are giving you the total set values.
    if (isset($_POST['id'.$k]) && isset($_POST['data'.$i])){ 
        $data=$_POST['data'.$i];
        $ini=$_POST['id'.$k];
        echo " tools ='$ini'  and customer='$data' <br>\n";
    }

    $k++;
}

How to code with results like this.

tools ='AB'and Customer='1'     
tools ='AC'and Customer='2'     
tools ='AB'and Customer='3'

Note:

$_POST['m'] = count customer
$_POST['n'] = count tools

Table customer (Count=10) Table Tools (Count=20) Description: if I Check table Tools then table customer will set to table tools that in check. example: I Check Tools AB and AC and results must be:

tools AB and customer 1 
tools AC and customer 2 
tools AB and customer 3 
tools AC and customer 4 
tools AB and customer 5 
tools AC and customer 6
tools AB and customer 7
tools AC and customer 8 
tools AB and customer 9 
tools AC and customer 10 
  • you're looping on both counts, but only ever check if `id$k` exists. Shouldn't you check if `data$i` exists as well? – Marc B Feb 18 '14 at 04:49
  • So if I'm right, you want each `customer` to be allotted to each `tool` in sequence, and when the last `tool` is reached, you want to start from the first `tool` again till the end of `customer`? – AyB Feb 18 '14 at 04:56
  • @ICanHasCheezburger that's right – user3300597 Feb 18 '14 at 05:07

1 Answers1

0

Try:

Check Edited Post Below
/*
$tools_total = 0;
$customer_total = 0;

foreach ($_POST as $key => $value) {
  if (strpos($key, 'data') === 0) {
    $customer_total++;            //getting total number of checked customers
  }
  if (strpos($key, 'id') === 0) {
    $tools_total++;               //getting total number of checked tools
  }
}

$k=1;
for($i=1; $i<=$customer_total; $i++)
{

    ($k>$tools_total)?$k=1:''; //reset tools counter

    if (isset($_POST['id'.$k]) && isset($_POST['data'.$i])){ 
        $data=$_POST['data'.$i];
        $ini=$_POST['id'.$k];
        echo " tools ='$ini'  and customer='$data' <br>\n";
    }

    $k++;
}
*/

Edited post:

Change your HTML of data input tag to:

<input type='hidden' name='data[]' value='".$r['No']."' />

Change your HTML of id input tag to:

<input type='checkbox' name='id[]' value='".$r['tools']."' />

Now your PHP code to:

$tools_total = count($_POST['id']);

$k=0;
foreach($_POST['data'] as $key=>$val){
    ($k>($tools_total-1))?$k=0:''; //reset tools counter
    $data=$val;
    $ini=$_POST['id'][$k];
    echo " tools ='$ini'  and customer='$data' <br>\n";
    $k++;
}
AyB
  • 11,609
  • 4
  • 32
  • 47
  • the Script is work but Not The Results tools ='AB'and Customer='1' tools ='AC'and Customer='2' tools ='AB'and Customer='3' – user3300597 Feb 18 '14 at 05:46
  • Please be clear. Is that the result you see or the result you need? If that's what you need, what do you see as output? – AyB Feb 18 '14 at 05:49
  • I didn't quite get that, may I know what output the above code gives? – AyB Feb 18 '14 at 06:15
  • i Edit My Question. The Output your script if i check table user value 3 then in table customer output value 3 – user3300597 Feb 18 '14 at 06:28
  • @user3300597 I really don't understand where you're stuck. I take it the output in your edited question is the one you actually need, so can I know what output you are receiving with the above written code? – AyB Feb 18 '14 at 06:34
  • @user3300597 Can I know the value of `$_POST['n']`? – AyB Feb 18 '14 at 07:16
  • $_POST['n'] = Count 30 tools and $_POST['m']= Count 100 customer – user3300597 Feb 18 '14 at 07:19
  • `$_POST['n']` and `$_POST['m']` are the number of tools and customers you have checked, correct? Can I see the code of how you are setting these values? – AyB Feb 18 '14 at 07:21
  • @user3300597 Can you try my updated code and see if it works like you needed? – AyB Feb 18 '14 at 07:30
  • No. $_POST['n'] and $_POST['m'] is total count.How can I sent u code php? – user3300597 Feb 18 '14 at 07:31
  • Ms.* Does it show you any output at all? Can you `var_dump($_POST);` and paste it here? Also `echo $tools_total; echo $customer_total;` after the `foreach` and paste here please. – AyB Feb 18 '14 at 07:43
  • very hard Description Mr. – user3300597 Feb 18 '14 at 07:53
  • I'll give up if you're not being cooperative. Can you please try the code in the above comment and paste the output here? – AyB Feb 18 '14 at 08:00
  • I will cooperative Mr. the output (0 0 0 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 10 0 10 0 10 0 10 1 10 2 10 ) – user3300597 Feb 18 '14 at 08:06
  • So I think `2` is the `$tools_total` and `10` is `$customer_total` Now can you please paste the output of `var_dump($_POST)`? I know it's difficult to paste into comments but you do not have enough reputation for me to open a chat page. – AyB Feb 18 '14 at 08:10
  • Yups I don't have reputataion because I newbie.where I Paste var_dump($_POST)? – user3300597 Feb 18 '14 at 08:15
  • 2 is total tool checked.and 10 is total customer. – user3300597 Feb 18 '14 at 08:26
  • @user3300597 You can edit your question and paste your `$_POST` details. Shouldn't be a problem. – AyB Feb 18 '14 at 08:31
  • @user3300597 I had asked you to `var_dump($_POST);` and paste the results which you didn't. Also I don't want to make massive loads of comment here having to repeat myself several times. – AyB Feb 18 '14 at 09:46
  • the results var_dump($_POST):array(18) { ["pilih"]=> string(6) "Update" ["data1"]=> string(9) "1" ["data2"]=> string(9) "2" ["data3"]=> string(9) "3" ["data4"]=> string(9) "4" ["data5"]=> string(9) "5" ["data6"]=> string(9) "6" ["data7"]=> string(9) "100113515" ["data8"]=> string(9) "100113519" ["data9"]=> string(9) "100113531" ["data10"]=> string(9) "200413884" ["m"]=> string(2) "11" ["tanggal"]=> string(19) "2014-02-18 17:02:11" ["user"]=> string(7) "tes" ["status"]=> string(0) "" ["id11"]=> string(5) "AB" ["id12"]=> string(3) "AC" ["n"]=> string(2) "34" } – user3300597 Feb 18 '14 at 10:09
  • @user3300597 Please check the updated post. Whenever you have multiple inputs using similar name, use [input arrays](http://stackoverflow.com/questions/1010941/html-input-arrays) , they are easier to manage than custom counters. – AyB Feb 18 '14 at 10:29