Jump to content

Search the Community

Showing results for tags 'loop'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


  • Welcome to PHP Freaks
    • Announcements
    • Introductions
  • PHP Coding
    • PHP Coding Help
    • Regex Help
    • Third Party Scripts
    • FAQ/Code Snippet Repository
  • SQL / Database
    • MySQL Help
    • PostgreSQL
    • Microsoft SQL - MSSQL
    • Other RDBMS and SQL dialects
  • Client Side
    • HTML Help
    • CSS Help
    • Javascript Help
    • Other
  • Applications and Frameworks
    • Applications
    • Frameworks
    • Other Libraries
  • Web Server Administration
    • PHP Installation and Configuration
    • Linux
    • Apache HTTP Server
    • Microsoft IIS
    • Other Web Server Software
  • Other
    • Application Design
    • Other Programming Languages
    • Editor Help (Dreamweaver, Zend, etc)
    • Website Critique
    • Beta Test Your Stuff!
  • Freelance, Contracts, Employment, etc.
    • Services Offered
    • Job Offerings
  • General Discussion
    • PHPFreaks.com Website Feedback
    • Miscellaneous

Find results in...

Find results that contain...

Date Created

  • Start


Last Updated

  • Start


Filter by number of...


  • Start





Website URL








Donation Link

Found 8 results

  1. I have 3 staff members that need to pick vacation in a certain order. Each have X weeks vacation and can pick off a calendar their choice 1-3 weeks per round. I'm trying to loop through the DB to find who is next to pick with weeks left ~~~~~~First round of picking~~~~~~ STAFF 1 - PICK ORDER 1 - 3 weeks available STAFF 2 - PICK ORDER 2 - 5 weeks available STAFF 3 - PICK ORDER 3 - 3 weeks available Staff 1 takes 2 Weeks Staff 2 takes 1 Weeks Staff 3 takes 3 Weeks ~~~~~~Second round of picking~~~~~~ STAFF 1 - PICK ORDER 1 - 1 weeks available STAFF 2 - PICK ORDER 2 - 4 weeks available STAFF 3 - PICK ORDER 3 - No weeks left Staff 1 takes 1 Weeks Staff 2 takes 3 Weeks Staff 3 Skipped ~~~~~~Third round of picking~~~~~~ STAFF 1 - PICK ORDER 1 - No weeks left STAFF 2 - PICK ORDER 2 - 1 weeks available STAFF 3 - PICK ORDER 3 - No weeks left Staff 1 Skipped Staff 2 takes 1 Weeks Staff 3 Skipped ~~~~~~~~~~~~ All staff 0 weeks left end --calendar.php-- $year=2020; $sql = "SELECT * FROM vac_admin WHERE pick_year='$year'; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { $row_admin = mysqli_fetch_assoc($result); } $current_pick_staff = $row_admin['current_pick_staff']; $sql = "SELECT * FROM vac_pick_order WHERE pick_year='$year' && pick_order = '$current_pick_staff'"; $result = mysqli_query($conn, $sql); $row = mysqli_fetch_assoc($result); if($row['vac_c_counter'] < 0){ $emp_num = $row['emp_num']; }ELSE{ ?????????????????? goto next staff with weeks > 0 ?????Somthing like if ($current_pick_staff == 3){ $current_pick_staff = 1; }ELSE{ $current_pick_staff++; } ?????????????????? } ~<FORM>~~~~~~~~~~~~~~~~~~~~~ Staff with $emp_num can now pick ~~~~~~ $_POST -> $date = XXXX-XX-XX; $num_weeks = X; $emp_num; ~</FORM>~~~~~~~~~~~~~~~~~~~~~ --process.php-- $year = 2020; $date = $_POST['date']; $num_weeks = $_POST['num_weeks']; $emp_num = $_POST['emp_num']; $sql = "INSERT INTO vac_picks (pick_year,emp_num,date) VALUES ($year,$emp_num,$date)"; $sql = "UPDATE vac_pick_order SET vac_c_counter=vac_c_counter - $num_weeks WHERE emp_num='$emp_num'; $sql = "UPDATE vac_admin SET pick_order=pick_order +1 WHERE pick_year='$year' ; Then back to calendar.php until all weeks gone.
  2. I have a foreach loop that returns 5 users. I basically want to show only 1 of the 5 users. Every time that loop is run, it should show a random user of the 5, as oppose to the same user every single time. How can that be done? This is my query so far. $find_user = $db->prepare("SELECT user_id FROM users ORDER BY user_id ASC"); $find_user->execute(); $result_user = $find_user->fetchAll(PDO::FETCH_ASSOC); if(count($result_user) > 0) { foreach($result_user as $row) { $user_id = trim($row['user_id']); var_dump($user_id); } }
  3. StuHannah


    Hi, I want to create a loop within PHP and MySQL. At the moment my table has a column named active that is either 0 or 1 to represent active or not. I want to update my current loop. I know that there are 5 days we operate (at the moment) so I want to pull through the table using a variable so that I can loop through and select days that are active and for the days that are not active simply print a greyed out row. Can anyone point me in the right direction, I was thinking a whileloop so for example: // $i is the days we are active) while ($i = 1, $i <= 5, $i++) { echo "Day name"; // If the day is active then print the name } ​else if the day isn't active it will still be printed, but greyyed out. This is my current query: $query = "SELECT * FROM tbl_days WHERE Active = 1"; $result = mysqli_query($dblink, $query);if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { echo "<tr>"; echo "<td>" . $row["ID"] ."</td>"; echo "<td>" . $row["DayName"] . "</td>"; echo "<td>" . $row["Active"]. "</td>"; echo "</tr>"; } } else { echo "Nothing active on the system at the moment."; } Thanks in advance, and hope i've given enough information. S
  4. Say I have the following. record_id | record_name ----------------------------------- 1 one 2 two 3 three 3 three 3 three 4 four 5 five 5 five I want to search through through the table and find all the records. If there are multiple records with same id, I want to combine them into 1 variable and use that. So in the above example, I have 3 threes and 2 fives. I want to combine them so that I only get 1 three and 1 five. How can that be done? A normal query looks like this. $get_records = $db->prepare("SELECT * FROM records"); $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']; } }
  5. 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
  6. Note: This site is only local and used as a calculator, so to speak, and I have no concerns about data structures or injection at this time, merely the formula in this loop. I have a working php loop involving an html table that houses test values. I am attaching a screenshot of the table, but it pulls data from a database table for 4 test attributes per 8 tests. In other words, there are 8 test columns and each column is filled with 4 test variables from the database. The 5th row calculates a percentage from a formula and after all of this is done for the 8 tests, there is an overall percentage of the 8 average values. This makes more sense looking at the screenshot, but the final row is made up from the formula. This works perfectly, except the new files we're using can have less than 8 tests, so my old formula basing the answer off of 8 tests is inaccurate now. I'm looking for a fairly quick and dirty solution for the time being that will just count how many columns actually contain data. Here is the code for the table, and the php loop data is near the bottom. Basically, I want a better way to use a count in my form loop as well as my average where they are currently using 8. <table style="width:100%; border:none; border-collapse:collapse;"> <tr style="border:none;"> <th style="border:none; text-align: left;" >Meter Test</th> <th style="border:none;">Test 1</th> <th style="border:none;">Test 2</th> <th style="border:none;">Test 3</th> <th style="border:none;">Test 4</th> <th style="border:none;">Test 5</th> <th style="border:none;">Test 6</th> <th style="border:none;">Test 7</th> <th style="border:none;">Test 8</th> </tr> <tr style="border: none;" > <td style="border:none; text-align: left;">Test Rate GPM: </td> <td><? echo $row['test1TestRateGPM'];?> </td> <td><? echo $row['test2TestRateGPM'];?> </td> <td><? echo $row['test3TestRateGPM'];?> </td> <td><? echo $row['test4TestRateGPM'];?> </td> <td><? echo $row['test5TestRateGPM'];?> </td> <td><? echo $row['test6TestRateGPM'];?> </td> <td><? echo $row['test7TestRateGPM'];?> </td> <td><? echo $row['test8TestRateGPM'];?> </td> </tr> <tr> <td style="border:none; text-align: left;">Meter Volume: </td> <td><? echo $row['test1MeterVol'];?> </td> <td><? echo $row['test2MeterVol'];?> </td> <td><? echo $row['test3MeterVol'];?> </td> <td><? echo $row['test4MeterVol'];?> </td> <td><? echo $row['test5MeterVol'];?> </td> <td><? echo $row['test6MeterVol'];?> </td> <td><? echo $row['test7MeterVol'];?> </td> <td><? echo $row['test8MeterVol'];?> </td> </tr> <tr> <td style="border:none; text-align: left;">Tester Volume: </td> <td><? echo $row['test1TesterVol'];?> </td> <td><? echo $row['test2TesterVol'];?> </td> <td><? echo $row['test3TesterVol'];?> </td> <td><? echo $row['test4TesterVol'];?> </td> <td><? echo $row['test5TesterVol'];?> </td> <td><? echo $row['test6TesterVol'];?> </td> <td><? echo $row['test7TesterVol'];?> </td> <td><? echo $row['test8TesterVol'];?> </td> </tr> <tr> <td style="border:none; text-align: left;">Tester Accuracy: </td> <td><? echo $row['test1Accuracy'];?>% </td> <td><? echo $row['test2Accuracy'];?>% </td> <td><? echo $row['test3Accuracy'];?>% </td> <td><? echo $row['test4Accuracy'];?>% </td> <td><? echo $row['test5Accuracy'];?>% </td> <td><? echo $row['test6Accuracy'];?>% </td> <td><? echo $row['test7Accuracy'];?>% </td> <td><? echo $row['test8Accuracy'];?>% </td> <td style="border:none;">Overall</td> </tr> <tr> <td style="border:none; text-align: left;">Corrected Accuracy: </td> //here starts the loop within the table cell <?php $sum=0; for($i=1; $i<=8; $i++){ if($row["test".$i."TesterVol"] == 0){ $row["test".$i."TesterVol"] = 0.001; } $testFormA = $row["test".$i."MeterVol"] / $row["test".$i."TesterVol"]; $testFormB = $testFormA * $row["test".$i."Accuracy"]; $testFinalForm = $testFormB; $sum += $testFinalForm; ?> <td><?php echo round($testFinalForm,3) ?>%</td> <?php } $average = $sum / 8; ?> <td><?php echo round($average,5)?>% </td> </tr> </table>
  7. This is the code from my previous topic. It was for getting 3 countdown counters to work on the same page. It does that. But I didn't notice this up until now. It gives me this error and it keeps counting up the errors in firebug. TypeError: document.getElementById(...) is null This is the code. Can you tell me what's wrong with it? <script> $( document ).ready(function() { //Create object with the list of due dates //The 'name' will correspond to the field ID to populate the results var dueDates = { 'date1':'<?php echo $global_payment_due_1; ?>', 'date2':'<?php echo $global_payment_due_2; ?>', 'date3':'<?php echo $global_payment_due_3; ?>' }; var timer = setInterval(function() { //Instantiate variables var dueDate, distance, days, hours, minutes, seconds, output; //Set flag to repeat function var repeat = false; // Get todays date and time var now = new Date().getTime(); //Iterate through the due dates for (var dueDateId in dueDates) { //Get the due date for this record dueDate = new Date(dueDates[dueDateId]); // Find the distance between now an the due date distance = dueDate - now; // Time calculations for days, hours, minutes and seconds days = Math.floor(distance / (1000 * 60 * 60 * 24)); hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); seconds = Math.floor((distance % (1000 * 60)) / 1000); //Determine the output and populate the corresponding field output = "OVERDUE"; if (distance > 0) { output = days + "d " + hours + "h " + minutes + "m " + seconds + "s"; repeat = true; //If any record is not expired, set flag to repeat } document.getElementById(dueDateId).innerHTML = output; //If flag to repeat is false, clear event if(!repeat) { clearInterval(timer); } } }, 1000); }); </script>
  8. I have this code which works as expected: foreach($bidders as $val) { $losing_bidder = ($val->email); echo $losing_bidder; } Let us assume the output is: Bob@email.com Joe@email.com Cindy@email.com Willis@email.com I need to have the loop always ignore (or not echo, or not process) the last value in the array. What adjustment to the code would accommodate that?? Thanks much!
  • 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.