H2S60: PHP loops, conditional execution, complexity x computation reductions:


for (init counter; test counter; increment counter) {
  code to be executed for each iteration;
}
R: W3-Schools

Hu: For loops are the most elementary loops, for the most part #, because they lack the perpetuity | clause<Turing> that comes by default, with while. W3: Parameters:

  • init counter: Initialize the loop counter value
  • test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
  • increment counter: Increases the loop counter value

H3S1: The counter is the most elementary paradigm of comp-sci:

Hu: At the University of Buffalo‘s foundational CSE-115 course, Dr. Hertz requires students to write at least 5 counting | functions in each homework assignment. Counting is analogous to effect<ch-3> in chess, and is rooted in the fact that the build-up of complexity in comp-sci is recursive, in that each higher layer, contains the lower layer(s), and the lowest layer of comp-sci is automation, and the most basic form of automation, is counting.

H4S1: PHP loop x HTML.multi-thread:

See<WP.MIC-H2S47,H3S5>

H3S2: Elementary for loop library-dump:

for ($x = 1; $x <= 9; $x++) {
	$text_column_input = $_POST["text$x"];
	echo $text_column_input; 
	$sql = "UPDATE dash_{$_GET['author']}_posts SET text$x='$text_column_input' WHERE post_ID='{$_GET["post_ID"]}'";
		$conn->query($sql);
	$url_column_input = $_POST["url$x"];
	$sql = "UPDATE dash_{$_GET['author']}_posts SET url$x='$url_column_input' WHERE post_ID='{$_GET["post_ID"]}'";
		$conn->query($sql);
} 

Hu: Wrote a looping function for writing 18 fields, corresponding to 9 rows of resulting user input, into 18 columns in a user-tb. Screen cap shows user input. Generally, I think loops for SQL-update are probably pretty rare, but because of the tabular formatting of the input, for my particular application, it makes sense, and I was able to reduce 18 lines of manual SQL UPDATE statements into a 6 line for statement, excluding the comments. Superglobals are notoriously difficult to maneuver inside of a SQL statement, so I abstracted its contents out into an ordinary variable, which added a line. This can be optimized later.

H3S2: Nested loops can elevate dimensionality #

<?php 
$entry_count_9 = 1;
$row_count_3 = 1;
while ($row_count_3 <= 3) {
	$count_container_2 = $row_count_3++;?>
<tr>
<?php
	$column_count_3 = 1; 
	while ($column_count_3 <= 3) {
		// echo $entry_count_9;
		$count_container = $column_count_3++;
		$url_column = 'url' . $entry_count_9; 
		$text_column = 'text' . $entry_count_9;
		?>
			<td><?php echo $entry_count_9++; ?>:<a href="<?php echo select_single_grid($post_author_username, $post_ID, $url_column);?>" data-type="URL" data-id="https://developer.wordpress.org/plugins/" target="_blank">
			<?php echo select_single_grid($post_author_username, $post_ID, $text_column);?></a></td>
<?php 
	}?>
</tr>
<?php
}?> 

Hu: Let’s analyze this through the lens of # the 3 loop_count variables: 1) $entry_count_9: this variable counts from 1-9, corresponding to MySQL SELECTs for each of the 9 # link entries; this variable is plugged into the SQL SELECT statements, via abstraction into $url_column and $text_column; it is also used to supply the numbering # scheme in each grid 2) $row_count_3: this variable fuels # the first while loop, which commands the second while loop to run 3 times, and establishes a <tr> before each run.3-times and </tr> after, as a row break 3) $column_count_3 runs each inner 3 times, populating the 3 columns of each row, first with 1-3, then 4-6, and finally 7-9, since the $entry_count_9 variable is never | reset.

H3S3: Foreach, loop through array:

Hu: Arrays, and foreach loops, which enable us to loop through arrays of pre.un-defined # length, are a powerful, and common combination, in all.comp-sci.

H3S4: Looping through multi.dimensional-arrays:

W3: We can also put a for loop inside another for loop to get the elements of the $cars array (we still have to point to the two indices):

$cars = array (
  array("Volvo",22,18),
  array("BMW",15,13),
  array("Saab",5,2),
  array("Land Rover",17,15)
);
<?php
for ($row = 0; $row < 4; $row++) {
  echo "<p><b>Row number $row</b></p>";
  echo "<ul>";
  for ($col = 0; $col < 3; $col++) {
    echo "<li>".$cars[$row][$col]."</li>";
  }
  echo "</ul>";
}
?>

References:

https://www.w3schools.com/php/php_looping_for.asp

https://stackoverflow.com/questions/38097501/php-nested-while-loop

https://www.php.net/manual/en/control-structures.foreach.php

https://www.w3schools.com/php/php_looping_foreach.asp

https://www.php.net/manual/en/function.array-push.php

https://www.w3schools.com/php/php_arrays_multidimensional.asp


Leave a Reply

Your email address will not be published. Required fields are marked *