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 (PhpStorm, VS Code, 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

  1. Hi, I am trying to develop a room allocation application, but am struggling a bit to get my head around the coding. I am trying to break it down into small chunks and then maybe see if I can get everything to work together. I have a mysql table which contains a list of users. I have returned the results of the user IDs from my select query into an array. I also have another table which holds a numeric marker. The marker will change. My array of user IDs would look like: Array ( [0] => 1 [1] => 3 [2] => 5 ) My numeric marker could be: 1 What I need to do is do a complete loop through my array starting at the numeric marker position. This would return: [1]=>3 [2]=>5 [0]=>1 As you can see, it jumps back to the beginning of the array and outputs the first element to complete the loop. Depending on the numeric marker and this particular data set, the array could also run like: Marker = 0: [0]=>1 [1]=>3 [2]=>5 Marker = 2: [2]=>5 [0]=>1 [1]=>3 I have had a go at writing the code and if I start at numeric marker 1, I can get it to run to the end of the array. I just have no idea how to reset back to the beginning and run until I reach numeric marker minus 1. Here is what I have so far: $i = $numericMarker; foreach ($userID as $key => $value) { if ($key < $numericMarker) continue; echo '$i = '.$i.' $value = '.$value.'<br />'; $i++; } Any help you can give me would be greatly appreciated. Many thanks, John
  2. all, I have this block of code: If ($ctr = 4) { for ($block = 1; $block <= 9; $block++) { for ($ctr2 = 1; $ctr2 <= 3; $ctr2++) { //each line. for ($ctr3 = 1; $ctr3 <= 3; $ctr3++) { //char 1 - 3 in each line. switch ($ctr2) { //which line? Case 1: $strOut = $strOut . substr($Line1, $ctr3, 1); Case 2: $strOut = $strOut . substr($Line2, $ctr3, 1); Case 3: $strOut = $strOut . substr($Line3, $ctr3, 1); } } } } } what I'd like to do is streamline it. There are 3 lines of text being captured here. What is (supposed to be) happening is that each block of characters (3 characters per line positions, starting at position 1 or positions every 3 char increments after that) is being captured until all 9 chars are stored in $strOut. 9 chars = 3 chars on each line * 3 lines. is there any better way to do this mathematically? I realize that we can manipulate the variables and coding many different ways, but I'm not interested in that. I'm only interested in using math or leveraging techniques to speed up the process here. Resources from PHP would be OK but I'm trying to focus on the massive iteration and turning it into something more useful. comments welcome. thanks!
  3. How do I put condition upon exploding a variable that outputting different values. I've got a $variable from a loop, each loop the variable has a different content(style attribute) . sample content of the variable. . array(6) { [0]=> string(17) "font-family:Arial" [1]=> string(14) "font-size:11px" [2]=> string(13) "color:#000000" [3]=> string(16) "font-weight:bold" [4]=> string(25) "text-decoration:underline" [5]=> string(0) "" } array(4) { [0]=> string(17) "font-family:Arial" [1]=> string(14) "font-size:11px" [2]=> string(13) "color:#000000" [3]=> string(0) "" } array(3) { [0]=> string(15) "padding:1px 2px" [1]=> string(12) "display:none" [2]=> string(0) "" } array(1) { [0]=> string(0) "" } array(3) { [0]=> string(15) "padding:1px 2px" [1]=> string(12) "display:none" [2]=> string(0) "" } so if i'm going to explode it $style = explode(';', $variable); so to print out put; echo $style[0]; right? But how will I going to be more specifi. Like . . $style->font-family, or $style->font-weight Then make a condition etc. .
  4. Hey Guys! I've been trying to figure out how to tackle a solution for this, on the site you have a combobox to select a subject and add other fields aswell. My problem is if the subjects are the same group them together and only echo the subject once but echo the other fields aswell. if($execute){ $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $sthandre = $con->prepare("SELECT book.id, book.title, book.url, book.viewer, book.sub_id, subj.subject_id, subj.name FROM book LEFT JOIN subj ON(book.sub_id = subj.subject_id) WHERE book.username_id = :yourid ORDER BY subj.subject_id"); $sthandre->execute(array(':yourid'=>$userid)); while ($row = $sthandre->fetch()){ $bmid = ($row ['id']); $title = ($row ['title']); $url = ($row ['url']); $audience= ucfirst($row ['viewer']); $subid = ($row ['subject_id']); $subjectname = ($row ['name']); //somehow if the subject name is a duplicate echo it once and carry on with the other variables looping echo "$subjectname,$title,$audience" } } the current output; Subject | Title |audience ------------------------------------------------------ |Classic | The Who | Everyone| |Classic | The End | Mature | |Classic | Happy | Teen | |Other | Last | Everyone | what im hoping it to be; Subject | Title |audience ------------------------------------------------------ |Classic | The Who | Everyone| | | The End | Mature | | | Happy | Teen | |Other | Last | Everyone | it will be in another format its just easier to explain in a grid format. Thanks i appreciate it!
  5. I've been playing for this and I really can't figure how to count the sub array and post a value: Array ( [1] => Array ( [A] => Hello [b] => [C] => Hello [D] => ) [2] => Array ( [A] => [b] => world! [C] => [D] => world! ) [3] => Array ( [A] => [b] => [C] => [D] => ) [4] => Array ( [A] => Miscellaneous [b] => [C] => [D] => ) [5] => Array ( [A] => List [b] => bump [C] => [D] => ) ) So what I want to happen is: <table> <tbody> <tr> <td>Hello</td><td>B</td><td>Hello</td><td>D</td> </tr> <tr> <td></td><td>world!</td><td></td><td>world!</td> </tr> <tr> <td>A</td><td>B</td><td>C</td><td>D</td> </tr> <tr> <td>A</td><td>B</td><td>C</td><td>D</td> </tr> <tr> <td>A</td><td>B</td><td>C</td><td>D</td> </tr> </tbody> </table> I already count the main array, but I can't get the sub array and post them on a table data w/ each specific value.
  6. Hey there, so I have a custom Wordpress loop that is working just fine. Inside that loop I have a second loop using Salesforce API. What is happening is that the main loop is looping but the secondary Salesforce loop is showing the same record for each and not looping. There is a unique ID for each so that isn't the problem. while ($wp_query->have_posts()) : $wp_query->the_post(); $sid = get_the_ID(); $SalesForceId = get_post_meta($sid, 'SalesForceId', true); $query = "SELECT Name, Phone, BillingCity, BillingState from Account WHERE Id = '" . $SalesForceId . "'"; $response = $mySforceConnection->query(($query)); foreach ($response->records as $record) { $Name = $record->{Name}; $Phone = $record->{Phone}; $BillingCity = $record->{BillingCity}; $BillingState = $record->{BillingState}; } echo $Name; echo $BillingCity; echo $BillingState; endwhile; ?>
  7. Hi guys. I've built an application for my company, and everyone likes it, but on some of the "events" they create, there is an infinite loop. It doesn't always happen, so i was investigate the 'else' part of it. I've looked over the script a few times over the past few months, not religiously until today. i still can't seem to find the issue. Can someone please help me? <?php $rowtot=0; while($rowtot<count($resultsIOCC['IOCC'])){ foreach ($resultsIOCC['IOCC'] as $IOCC ) { if( $IOCC->ioccCostID == $Cost->CostID ) { if( ($IOCC->ioccLabor + $IOCC->ioccTravel + $IOCC->ioccMileage) > 0 ) { $rowtot = $IOCC->ioccLabor + $IOCC->ioccTravel + $IOCC->ioccMileage; echo money_format('$%i',$rowtot) . '<br>'; $rowstot += $rowtot; } else { echo "$" . number_format("0", 2) . '<br>'; $rowstot = ($rowstot + 0); } } $rowtot++; } } ?> I can post more of it if necessary, but I believe it's in this part of the script. I know the script is not very "professional" looking and the variables are terribly named (like rowstot and rowtot), but it was a learning experience at the time, I didn't know PHP and it was my first project.
  8. I am creating a Child Security Application called Shield Child Security. I ran into a roadblock, and I had to ask myself, "How will they take attendance?" So I want to take a mass status change. If you look at the picture you will see what I mean. <body> <div align="center"> <div style="width:460px; background-color:#000000; padding:11px;" align="left" class="shadow"> Children: <div style="background-color:#3399FF; padding:11px;" align="left" class="shadow"> <form action="" method="POST"> <table> <?php $query = mysql_query("SELECT * FROM Children WHERE classroom='$class'") or die("ERROR"); $numrows = mysql_num_rows($query); if ($numrows!=0){ while ($row = mysql_fetch_assoc($query)){ $cid = $row['id']; $fname = $row['firstname']; $lname= $row['lastname']; $status = $row['status']; $classroom = $row['classroom']; echo '<tr style="background-color:gray;"><td><input type="checkbox"></td><td align="center" width="150" name="'.$cid.'">[<a href="child.php?id='.$cid.'">'.$fname.' '.$lname.'</a>]</td>'; if ($classroom != 'none'){ if ($status == 1) { echo '<td align="center" width="200"><font color="red">Not Present</font>'; } else if ($status == 2){ echo '<td align="center" width="200"><font color="lightgreen">Present</font>';} } else { echo '<td align="center" vertical-align="middle"><br><form action="changeclass.php" method="POST"><select>Balls</select><input type="submit" value="+""></form></td>'; } echo '</td><td align="center"><a href="delete.php?id='.$cid.'&type=child">[ - ]</a></td></tr>'; } } else {echo "No Students";} ?> <tr> </tr> </table> <table> <td> <input type = "submit" value="Switch Status"> </td> </table </div> <br> <a href="classroom.php">Go Back</a> </div> </div> I'm sure its vulnerable to SQL Injection, but remember this is just a rough draft. A draft to get my ideas out in code. Peace! And Thank You!
  9. Im having a bit of trouble with my script. I wrote the script originaly to run from one variable which i changed manualy, but now im trying to put the whole script inside a loop. The script is now running all the way through fine on the first loop, starting starting the second but getting stuck at the part with my implode. This is my script that is around the implode area. Bear in mind that this code snippet is from a very large script. $t=1; while ($t <= $bm) { $mn=${'m'.$t}; $mn2[$t]="$mn"; $t++; } // The is where the script runs to, and the error comes from the next few lines and the script stops running $implode=implode(',', $mn2); print $implode; $query2="SELECT * FROM artists WHERE id IN(".implode(',', $mn2).")"; $result2=mysql_query($query2) or die(mysql_error()); $i=0; $bm2=$bm-1; while ($i <= $bm2) { $id3=mysql_result($result2,$i,"id"); ${"id3$i"} = $id3; $s1=mysql_result($result2,$i,"s1"); ${"s1$i"} = $s1; $s2=mysql_result($result2,$i,"s2"); ${"s2$i"} = $s2; $sl1=mysql_result($result2,$i,"sl1"); ${"sl1$i"} = $sl1; $sl2=mysql_result($result2,$i,"sl2"); ${"sl2$i"} = $sl2; $i++; } And this is the error that im getting ,,,You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',,)' at line 1 Any help or point in the right direction would be amazing right no. Thanks Eric
  10. Hi everyone I would like to know if anyone has ever successfully been able to display a TinyMCE editor in a while loop? It only displays the first loop, and not the second while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo '<form method="post" style="margin-top:20px;"><table> <tr> <td>Content Name</td> <td><input type="hidden" name="idwidget" value="'.$row["id"].'" /><input type="text" name="name" value="'.$row["content_name"].'" /></td> </tr> <tr> <td>Content</td> <td><textarea id="mcecontent" class="mcecontent" name="mcecontent" rows="15" cols="80">'.$row["content"].'</textarea></td> </tr> <tr> <td>Active</td> <td>'; if($row["active"] == "1"){ echo '<input type="checkbox" name="active" checked="checked" />'; } else { echo '<input type="checkbox" name="active" />'; } echo '</td> </tr> </table>'; } It is suppose to display for mcecontent textarea.
  11. I posted something similar to this recently but the variable isn't defined, as it was in the last one. I have two tables. The 'user' table contains the id, first name, and last name of the individual. The 'uinfo' table contains other pertinent info about the user. I want the code to look at the 'uinfo' table where reviewed = 0 and gather all of the ID's (uid) of those members. Then, based off of those ID's I want it to gather their names from the other table and list them so I can click on them. I'm just not quite sure how to do that. :-\ $sql="SELECT uid FROM uinfo WHERE reviewed='0'"; $result=mysql_query($sql); while($row = mysql_fetch_array($result)) { $uid = $row['uid']; $sql="SELECT FirstName, LastName FROM users WHERE id='$uid'"; $result=mysql_query($sql); while($row = mysql_fetch_array($result)) { $admin = $row['admin']; $FirstName = $row['FirstName']; $LastName = $row['LastName']; echo "<br><a href='blah.php?id=" . $uid . "'>" . $FirstName . " " . $LastName; } }
  12. Hello everyone. i'm new on this site and also new on PHP coding. I want to make a loop using the values of an object (name of the object is test). my object has categoryid,parentid and i want to make something like this forearch(categoryid){ foreach(parentid){ if (parentid == categoryid){ do something } } } Can anyone help me to do this loop? Thank you
  13. What I need to do is query a table based on a date. Then, if the query returns a "W" or an "H", I need to add one day to the date, then run the query again, until it doesn't return either a "W" or an "H". My code works fine when the initial query doesn't return a W or H, but when it does, it times out, seemingly in and endless loop. So clearly, something I'm doing with the loop isn't correct. Can someone help with my logic? Thanks very much from this first time visitor to PHP Freaks! Here's the code: <? $testdate = date("Y-m-d", strtotime("2012/09/30")); include("includes/databasedateretrieval.inc"); $dateverify = mysql_result($date_result,0,"1_day"); $dfresult = $dateverify; // Check for dating errors while ($dfresult == "W" || $dfresult == "H"){ //convert the date to time $todaydate = strtotime($testdate); //add 1 day to the date $todaydate = $todaydate + (24*60*60); //convert the date back to a format compatible with the table data $testdate = date("Y-m-d", $todaydate); //here I'm querying the table again to see if the new date returns a W or H $dateverify = mysql_result($date_result,0,"1_day"); $dfresult = $dateverify; } ?> <? echo $testdate; ?>
  14. Hi everyone, I am facing a problem with the following code snippet which invovles inserting data from a csv file into a mysql table. <?php if(isset($_POST['submit'])) { $file = $_POST['filepath']; $handle = fopen($file,"r"); $fileop = fgetcsv($handle,2000,","); //read only headers (first row) // build qry to select data from assessment,assessmenttype tables $qry = "SELECT assessmenttype.AssessType_Code, Assess_Num FROM assessment, assessmenttype WHERE assessmenttype.AssessType_Code = assessment.AssessType_Code AND Offer_ID = '$offerId';"; // execute query $result = mysql_query($qry) or die('Query failed: ' . mysql_error()); $c = 1; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $index = $_POST["lstAssess$c"]; $assessNum = $row['Assess_Num']; $assessCode = $row['AssessType_Code']; echo $assessCode." ".$assessNum." ".$fileop[$index]; $i = 1; while((${"fileop$i"} = fgetcsv($handle,10000,",")) !== false) { echo ${"fileop$i"}[$index]; /* $Stud_Lname = $fileop[0]; $Stud_Fname = $fileop[1]; $Stud_Id = $fileop[2]; $mark = $fileop[$index]; $qry = mysql_query("INSERT INTO gradebook (Stud_ID,Offer_ID,Assess_Num,AssessType_Code,GB_Mark) VALUES ('$Stud_Id','$offerId','$assessNum','$assessCode','$mark')")or die('Query failed: ' . mysql_error());*/ echo "<br>"; $i = $i + 1; } //echo $fileop[$index]; $c = $c + 1; } }else { echo "Error!"; } //close database connection mysql_close($conn); ?> As expected, each outer loop run should generate every inner loop run. This is not the case here. Only the first loop is successful and gets inserted into the DB. Can anyone point me to directions where I can tackle this issue?? Thanks in advance.
  • 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.