Jump to content

trouble with for PHP for loop return value


jepler

Recommended Posts

I haven't created loops in PHP so I need some help. I'm trying to return a total arithematic value as items are looping.

[code]foreach($items as $item) {

if (strstr($row_Recordset1['schedule_id'], $item['id'])){ //if an item in the loop meets this condition
$number+10 ; //add +10 to the number
}
$number + 1;
}

echo $number; //this should be the total value returned from the loop
[/code]

I know the code above is terrible but hopefully it can reveal what I'm trying to do.thx.
Link to comment
Share on other sites

You have to assign the value to the variable like so:

[code]
foreach($items as $item) {

if (strstr($row_Recordset1['schedule_id'], $item['id'])){ //if an item in the loop meets this condition
$number = $number+10 ; //add +10 to the number
}
$number = $number + 1;
}

echo $number; //this should be the total value returned from the loop
[/code]
Link to comment
Share on other sites

thanks for your reply. Okay, got it. But I guess that's not going to help me do what I want to do. Instead of performing a running tally, I guess I need instead to return the total for each instance of the loop and then set the number back to 0, or something.

I want to do something like this:

[code]<input name="<?php echo $row_Recordset1['schedule_id']; ?>
" type="checkbox" id="<?php echo $row_Recordset1['schedule_id']; ?>"
value="<?php echo $row_Recordset1['class_name']; ?>"

<?

if the schedule_id number IS found somewhere in the array item,
echo "checked onClick=goSomeWhere";

if the schedule_id number is NOT found in the array item,
echo "onClick=goSomeWhereElse";

?>
>[/code]
Link to comment
Share on other sites

after reviewing what i just posted, i see i left out the original code I had a question about!! Sorry about that! Maybe I'm approaching it the wrong way? I want an input checkbox field to be checked and an OnClick event to be available if the item is in the array; if it's not, I want a different onClick event to be available in the input checkbox field. Seems like I can get the first condition to work but the second onClick event shows up in every input box regardless.

<? <?php do { ?>
<input name="<?php echo $row_Recordset1['schedule_id']; ?>
" type="checkbox" id="<?php echo $row_Recordset1['schedule_id']; ?>"
value="<?php echo $row_Recordset1['class_name']; ?>"

<?php
foreach($items as $item) {
  if (strstr($row_Recordset1['schedule_id'], $item['id'])) { 
    $number += 10;
    echo "checked onClick=goSomeWhere"; //if the schedule_id number IS found somewhere in the array item check the box and print this onClick function;
  } else {
    $number += 1;
  }
}

if ($number > 10) {
    echo "onClick=goSomeWhereElse";  //if the condition is not met above print this other function;

?>

>

<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
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.