jepler Posted January 17, 2007 Share Posted January 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
owenjh Posted January 17, 2007 Share Posted January 17, 2007 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] Quote Link to comment Share on other sites More sharing options...
trq Posted January 17, 2007 Share Posted January 17, 2007 [code]<?phpforeach($items as $item) { if (strstr($row_Recordset1['schedule_id'], $item['id'])) { $number += 10; } else { $number += 1; }}echo $number;?>[/code] Quote Link to comment Share on other sites More sharing options...
jepler Posted January 17, 2007 Author Share Posted January 17, 2007 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] Quote Link to comment Share on other sites More sharing options...
jepler Posted January 17, 2007 Author Share Posted January 17, 2007 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']; ?>" <?phpforeach($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)); ?> Quote Link to comment 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.