Search the Community
Showing results for tags 'foreach loop'.
-
Hi I am new to PHP and I am having an issue with a foreach loop combined with an if statement, basically the if statement is getting the data from the first result in the foreach but not getting the second result, I have tried implement a count by using a variable to iterate but it isnt working: Code as follows: foreach(findCustomAlerts($customerid) as $key=>$value){ echo "ID of cat : ".$rifdid = $value['Catid']."<br>"; echo "Visits required per day : ".$visitsneeded= $value['Visits required']."<br>"; } foreach(getTodaysVisits($device_id) as $rfid){ foreach($rfid as $key=> $read){ if($key ==$rifdid && $read < $visitsneeded) { echo"<br>". $key." has not visited enough"; } } } Ouput is : ID of cat : 5609 Visits required per day : 3 ID of cat : 23641 Visits required per day : 5 Array ( [rfid_id] => Array ( [23641] => 1 [5609] => 3 ) ). -------> this is the array data 23641 has not visited enough ----------------------------------------------- How can I get the loop to iterate to the next value?
-
I have 2 pages...1 one that has something like 500 checkboxes that list all of the active users in a database. When any number of checkboxes are selected it passes the variables from the checkboxes to the next page, asking for confirmation. Right now I have the 2nd page displaying numbers for the people, but doesn't display the correct entries. The code is below, hopefully it helps: if(isset($_POST['Update'])) //if the update button was pressed { if (isset($_POST['Player_number'])) //checks to see if any checkboxes are selected { //$Player_number = IMPLODE(',',$_POST['Player_number']); //putting the comma in between the array items echo $Player_number; ?> <input type="hidden" name="<?PHP $Player_number; ?>" value="<?PHP $Player_number; ?>"> $Player_number = IMPLODE(',',$_POST['Player_number']); //putting the comma in between the array items $result = mysql_query("SELECT * FROM `players` WHERE `Player_number` = '$Player_number' ORDER BY Player_Last_Name") or die(mysql_error()); while ($row = mysql_fetch_array($result)) //while ($row = mysql_fetch_assoc($result)) { // here is your data echo "FName: ".$row['Player_First_Name']; echo "lName: ".$row['Player_Last_Name']; echo "email: ".$row['player_email']; } foreach($_POST['Player_number'] as $row) { $result = mysql_query("SELECT `Player_First_Name`,`Player_Last_Name`,`player_email`, `Player_number` FROM `players` WHERE `Player_number` = '$Player_number' ORDER BY Player_Last_Name") or die(mysql_error()); $Player_number = IMPLODE(',',$_POST['Player_number']); //putting the comma in between the array items while($rows=mysql_fetch_array($result)) { echo "Player Number: ".$Player_number; //echo "pNumber: ".$row; //echo $rows['Player_First_Name']; ?> <tr bgcolor='<?PHP echo $bkcolor; ?>'> <td width = '20%' height="20"><div align="center"><input type="hidden" name="Player_number[]" value="<?php echo $row; ?>"></div></td> <td width= '20%' headers="20"><div align="center"><?php echo $rows['Player_First_Name']; ?></div></td> <td width= '20%' headers="20"><div align="center"><?php echo $rows['Player_Last_Name']; ?></div></td> <td width= '20%' headers="20"><div align="center"><?php echo $rows['player_email']; ?></div></td> <?PHP }//close of the while loop }//close of the foreach loop }//close of if (isset($_POST['Player_number'])) } //close of (isset($_POST['Update'] My issue that it goes through the For loop that is listed above, but list the player first name X amount of times.
- 1 reply
-
- whileloop
- foreach loop
-
(and 1 more)
Tagged with:
-
Say I have a records table with multiple records. There could be multiple records with the same name but different amounts. For Eg. Looking at the table below, the results should retrieve newest record-1 and record-2 because their amount is equals to or greater than 5.00. Record-3 is not selected because it falls below 3.00. record_id record_name record_amount 1 record-1 4.00 2 record-1 3.00 3 record-2 2.00 4 record-1 5.00 5 record-2 6.00 6 record-3 3.00 $get_records = $db->prepare("SELECT record_id, record_name FROM records WHERE record_amount >= :record_amount ORDER BY record_id DESC"); $get_records->bindValue(':record_amount', 5.00); $get_records->execute(); $result_records = $get_records->fetchAll(PDO::FETCH_ASSOC); if(count($result_records) > 0) { foreach($result_records as row) { $record_id = $row['record_id']; $record_name = $row['record_name']; echo $record_name; } } Currently the query above outputs ALL the rows that matches the >=. What I want to do is select only ONE row of each unique record name that matches the criteria. And that row is typically the last row that was inserted. So the output would be like only these two. How do I do that? 4 record-1 5.00 5 record-2 6.00
- 11 replies
-
- loop
- foreach loop
-
(and 3 more)
Tagged with:
-
I'm a beginner currently studying Object Oriented Programming. The code in question is quite long so I will just share the snippet that I can't understand. private $productlist = array(); public function addproduct($product){ $this->productlist[] = $product; } public function stockup(){ foreach($this->productlist as $product){ $product->addstock(100); } I'm sure that function addproduct adds product objects to the product list array. What does the foreach loop: foreach($this->productlist as $product) in the stockup function do? Does it take those values and store them back in $product?
-
I'm having a bit of trouble maintaining the value of a variable within a while do loop. It seems the pointer keeps pointing to the first id no matter how many loops there are. I've inserted a hidden field which pulls in each id with shows the IDs are fine but whenever I click on 'editForm' anchor it always points to either the first or last id in the loop, (depending on whether I put the editForm div tag before or after the loop). Can anyone out there help me fix this problem? The code is shown below: $('.formEditAnchor').click(function() { $('#editMyForm').submit(); }); <?php function editFunction($editID) { $database = new database(); $res = $database->selectDB("select content as EditContent from content where id = '$editID'"); $editForm = "<form method='POST' action='_scripts/_php/edit-file.php'>"; $editForm.= "<textarea name='saveContent' cols='70' rows='25'>"; while($row = mysql_fetch_array($res)) { $row = mysql_fetch_array($res); $editableFile = $row['EditContent']; $editableContent = file($editableFile); $editableString = implode($editableContent); } $editForm.= $editableString; $editForm.= "</textarea>"; $editForm.= "<input type='hidden' name='hiddenEditPath' value='<?php echo $editableFile; ?>' />"; $editForm.= "<br />"; $editForm.= "<input type='submit' name='saveFile' value='Save'>"; $editForm.= "</form>"; return $editForm; } function submitEditForm() { if (isset($_POST['formEditAnchor'])) { $usersReplyId = $_POST['hiddenUserReplyID']; $returnEdit = editFunction($usersReplyId); return $returnEdit; } } $result3 = $database->selectDB("select * from blog_content where content_id = '$id'"); $r = array(); while($rows = mysql_fetch_array($result3)) { $myID = $rows['id']; $file = $rows['content']; $contents = file($file); $string = implode($contents); ?> <form name="editMyForm" id="editMyForm" method="post" action"<?php echo $_SERVER["PHP_SELF"]?>"> <input type="hidden" name="hiddenUserReplyID" id="hiddenUserReplyID" value="<?php echo $myID ?>" /> <table> <tr> <tr> <td bgcolor="#F8F7F1" align="center">Date/Time:<?php echo $rows['datetime']; ?></td> </tr> <tr> <td bgcolor="#f8f7f1"><?php echo $string; echo "<br /><br />"; ?> </td> </tr> <tr> <td><a href="#editContent" class="create-content" id="formEditAnchor" onclick="<?php $myReturnEdit = submitEditForm(); ?>">Edit File</a></td> </tr> </tr> </table> </form> <?php } ?> <div id='editContent' class='create-topic'> <?php echo $myReturnEdit; ?>
- 1 reply
-
- while doloops
- pointers
-
(and 2 more)
Tagged with: