Jump to content

Accessing arrays


Recommended Posts

Im sorry guys, im not good in arrays. What if i have this code:

 

<?PHP

$sql = "SELECT purchase_id FROM purchases";

$res = mysql_query($sql);

while ($row = mysql_fetch_array($res)) {

?>

<tr>

  <td align="center"><input type="checkbox" id="pid[]" name="pid[]" value="<?PHP echo $row[0] ?>" onClick="GetCheckedBoxes()"></td>

</tr>

<?PHP

}

 

How should i do the "GetCheckedBoxes()" function to get the values of the checked checkboxes?

Link to comment
https://forums.phpfreaks.com/topic/157479-accessing-arrays/
Share on other sites

What is getCheckedBoxes supposed to do?

 

If you want to mark it as checked based on a value in $row you should add a checked attribute to the the checkbox and set it to the value checked.

( <input type="checkbox' checked="checked" /> )

And I would advice to use onchange instead of onclick.

Link to comment
https://forums.phpfreaks.com/topic/157479-accessing-arrays/#findComment-830252
Share on other sites

First of all, you shouldn't have more than *1* element with the same ID on the same page. In your code, you can have multiple <INPUT> tags with ID "pid[]", which is bad.

 

To get all checkboxes, you'll have to write a JavaScript loop that loops through all the checkboxes and check if they're checked.

Link to comment
https://forums.phpfreaks.com/topic/157479-accessing-arrays/#findComment-830464
Share on other sites

Thanks for your replies.

 

I’m making this function wherein a user can pay their supplier in lump sum. Whenever the user selects/deselects a purchase (that is using checkboxes), the sum of the selected purchases is shown in a readonly textbox. Originally, my plan is to use Ajax. That's why I posted it here: http://www.phpfreaks.com/forums/index.php/topic,250672.0.html.

 

Somebody told me that I can use javascript only to be able to do that. What im trying to do now is somewhat like follows:

 

<form name="form1" method="post" action="">

<?PHP

$sql = "SELECT purchase_id, or_num, DATE_FORMAT(delivery_date, '%b %e, %Y'), line_total, balance

      FROM purchases

      WHERE supplier_id = '$supplier_id' AND balance > 0

      ORDER BY delivery_date DESC, or_num";

$res = mysql_query($sql); $num = mysql_num_rows($res);

 

while ($row = mysql_fetch_array($res)) {

?>

<td><input type="checkbox" id="pid[]" name="pid[]" value="<?PHP echo $row[0] ?>" onClick="getCheckedBoxes()"></td>

  <td><a href="purchase.php?purchase_id=<?PHP echo $row[0] ?>"><?PHP echo $row[1] ?></a></td>

  <td><?PHP echo $row[2] ?></td>

  <td><?PHP echo $row[3] ?></td>

  <td><input type="text" id="balance[]" name="balance[]" value="<?PHP echo $row[4] ?>"</td>

  <?PHP

}

?>

<tr class="total">

  <td colspan="4"><b>Total Amount</b></td>

  <td><input type="text" size="20" id="tprice" name="tprice" readonly></td>

</tr>

<input type="hidden" name="num_rows" value="<?PHP echo $num ?>">

?>

</form>

 

// javascript

function getCheckedBoxes() {

  var num_rows = document.form1.num_rows.value;

  var totalAmount = 0;

  for (i = 0; i < num_rows; i++) {

      if (document.getElementById('pid').checked==true) {

        var tempSum = eval(parseFloat(document.getElementById('balance').value));

        totalAmount =+ tempSum;

      }

  }

  document.form1.tprice.value = totalSum;

}

 

Please correct my javascript. I can't get it done. Thanks alot in advanced.

Link to comment
https://forums.phpfreaks.com/topic/157479-accessing-arrays/#findComment-830995
Share on other sites

My apologies ken2k7. Anyway, here it is: Please comment on my javascript if it is correct. The function should get the sum of balances all selected purchases to a particular supplier.

 

<script type="text/javascript">
function getCheckedBoxes() {
   var num_rows = document.form1.num_rows.value;
   var totalAmount = 0;
   for (i = 0; i < num_rows; i++) {
      if (document.getElementById('pid').checked==true) {
         var tempSum = eval(parseFloat(document.getElementById('balance').value));
         totalAmount =+ tempSum;
      }
   }
   document.form1.tprice.value = totalSum;
}
</script>

 

<form name="form1" method="post" action="">
<?PHP
$sql = "SELECT purchase_id, or_num, DATE_FORMAT(delivery_date, '%b %e, %Y'), line_total, balance
      FROM purchases
      WHERE supplier_id = '$supplier_id' AND balance > 0
      ORDER BY delivery_date DESC, or_num";
$res = mysql_query($sql); $num = mysql_num_rows($res);

while ($row = mysql_fetch_array($res)) {
?>
<td><input type="checkbox" id="pid[]" name="pid[]" value="<?PHP echo $row[0] ?>" onClick="getCheckedBoxes()"></td>
   <td><a href="purchase.php?purchase_id=<?PHP echo $row[0] ?>"><?PHP echo $row[1] ?></a></td>
   <td><?PHP echo $row[2] ?></td>
   <td><?PHP echo $row[3] ?></td>
   <td><input type="text" id="balance[]" name="balance[]" value="<?PHP echo $row[4] ?>"</td>
   <?PHP
}
?>
<tr class="total">
   <td colspan="4"><b>Total Amount</b></td>
   <td><input type="text" size="20" id="tprice" name="tprice" readonly></td>
</tr>
<input type="hidden" name="num_rows" value="<?PHP echo $num ?>">
?>
</form>

 

Thanks in advanced. More power to you guys.

 

 

Link to comment
https://forums.phpfreaks.com/topic/157479-accessing-arrays/#findComment-831408
Share on other sites

That script makes no sense. You're using a for loop where a variable i is set, but you never use it. Also, there is a problem with your HTML. If you view source the page, you would see that there are multiple element with the ID pid[]. An ID should be unique on the page, so you shouldn't have more than 1 element with the same ID.

 

My apologies if that came out a bit harsh. No offense intended. :)

Link to comment
https://forums.phpfreaks.com/topic/157479-accessing-arrays/#findComment-831410
Share on other sites

Im sorry, i posted the wrong code. But thank you for your comment ken2k7. Anyway, please check these codes if it makes sense now.

 

<script type="text/javascript">
function getCheckedBoxes() {
   var num_rows = document.form1.num_rows.value;
   var totalAmount = 0;
   for (i = 0; i < num_rows; i++) {
      if (document.forms["form1"].elements["pid[i]"].checked==true) {
         var tempSum = eval(parseFloat(document.forms["form1"].elements["balance[i]"].value));
         totalAmount =+ tempSum;
      }
   }
   document.form1.tprice.value = totalSum;
}
</script>

 

<form name="form1" method="post" action="">
<?PHP
$sql = "SELECT purchase_id, or_num, DATE_FORMAT(delivery_date, '%b %e, %Y'), line_total, balance
      FROM purchases
      WHERE supplier_id = '$supplier_id' AND balance > 0
      ORDER BY delivery_date DESC, or_num";
$res = mysql_query($sql); $num = mysql_num_rows($res);

while ($row = mysql_fetch_array($res)) {
?>
<td><input type="checkbox" name="pid[]" value="<?PHP echo $row[0] ?>" onClick="getCheckedBoxes()"></td>
   <td><a href="purchase.php?purchase_id=<?PHP echo $row[0] ?>"><?PHP echo $row[1] ?></a></td>
   <td><?PHP echo $row[2] ?></td>
   <td><?PHP echo $row[3] ?></td>
   <td><input type="text" name="balance[]" value="<?PHP echo $row[4] ?>"</td>
   <?PHP
}
?>
<tr class="total">
   <td colspan="4"><b>Total Amount</b></td>
   <td><input type="text" size="20" id="tprice" name="tprice" readonly></td>
</tr>
<input type="hidden" name="num_rows" value="<?PHP echo $num ?>">
?>
</form>

 

 

Thank you again.

Link to comment
https://forums.phpfreaks.com/topic/157479-accessing-arrays/#findComment-832117
Share on other sites

I want to find out what checkboxes are checked with JavaScript because I want to show the sum of all checked purchases in the "tprice" read only textbox before submitting the form. Whenever the user selects/deselects a purchase (that is using checkboxes), the sum of the selected purchases is shown in a readonly textbox. That is why i have two arrays, the pic[] which stores the purchase_id's and the balance[] which stores the balance of each purchase.

Link to comment
https://forums.phpfreaks.com/topic/157479-accessing-arrays/#findComment-832958
Share on other sites

Then use something like this:

 

<script type="text/javascript">
function numCheckedBoxes () {
     var inputs = document.forms.form1.getElementsByTagName("input");
     var num_checked = 0;
     for (var len = inputs.length; --len > -1; ) {
          if (inputs[len].type == "checkbox" && inputs[len].checked) num_checked++;
     }
     window.alert(num_checked);
}
</script>

Use that function. Call it with numCheckedBoxes();

Link to comment
https://forums.phpfreaks.com/topic/157479-accessing-arrays/#findComment-832984
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.