Jump to content

nested loops with arrays


derrick katungi

Recommended Posts

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Second Testing</title>

<link href="styler.css" rel="stylesheet" type="text/css" />

</head>

 

<body>

 

<?php

 

error_reporting(0);

 

$num_rows = 16;

$number1 = 4;

 

$inn = array(7,10,13,16);

$outt = array(14,16,21,23);

$numm = array(3,5,7,9);

 

$r_time_in = min($inn);

 

for($i = 0; $i <= $num_rows; $i++)

{

$new_r_timer_in[$i] = $r_time_in;

$new_r_timer_out[$i] = $new_r_timer_in[$i] + 1;

$r_time_in += 1;

}

 

 

// for($y = 0; $y <= $number1; $i++)

// {

// for($i = 0; $i <= ($num_rows - 1); $i++)

// {

// if(($inn[$y] <= $new_r_timer_in[$i]) || ($outt[$y] <= $new_r_timer_out[$i]))

// {

// $numb[$i] = $numm[$i] + $numm[$i - 1];

// }

// else

// {

// $numb[$i] = $numm[$i - 1];

// }

// }

// }

 

 

for($i = 0; $i <= ($num_rows - 1); $i++)

{

echo '<table width="526" border="0"><tr>

<td width="169">

<input type="text" name="$r_in[]" value="'.$new_r_timer_in[$i].'" style="width:169px" readonly="readonly" class="text_non_color"/></td>

<td width="169">

<input type="text" name="$r_out[]" value="'.$new_r_timer_out[$i].'"  style="width:169px" readonly="readonly" class="text_non_color"/></td>

<td width="169">

<input type="text" name="$r_num[]" value="'.$numb[$i].'"  style="width:169px" readonly="readonly" class="text_non_color"/></td>';

}

print("</tr>");

 

?>

 

</body>

</html>

 

 

the code above works very well in its current state. three columns are produced but only two have data,

the third column is empty.

 

when executed, the code is supposed to check whether a range(7 - 8) is between the first element of array $inn and the first element of array $outt and then add the corresponding number in array $numm to the number in that(7 - 8) range

 

the commented out code is supposed to implement this idea but its not.........help.

Link to comment
https://forums.phpfreaks.com/topic/247270-nested-loops-with-arrays/
Share on other sites

thats true man but i have ever coded in C (borland) and i used to get funny results from $i < $num_rows so i switched to $i <= ($num_rows - 1)..but there's no biggie here, i still have a bigger problem dude....

 

this is my update so far...

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Second Testing</title>

<link href="styler.css" rel="stylesheet" type="text/css" />

</head>

 

<body>

 

<?php

 

error_reporting(0);

 

$num_rows = 16;

$number1 = 4;

 

$inn = array(7,10,13,16);

$outt = array(14,16,21,23);

$numm = array(3,5,7,9);

 

$r_time_in = min($inn);

 

for($i = 0; $i <= $num_rows; $i++)

{

$new_r_timer_in[$i] = $r_time_in;

$new_r_timer_out[$i] = $new_r_timer_in[$i] + 1;

$r_time_in += 1;

}

 

 

for($y = 0; $y <= $number1; $y++)

{

for($i = 0; $i <= ($num_rows - 1); $i++)

{

if(($inn[$y] <= $new_r_timer_in[$i]) || ($outt[$y] <= $new_r_timer_out[$i]))

{

$numb[$i] = $numm[$y] + $numm[$y - 1];

}

else

{

$numb[$i] = $numm[$y - 1];

}

}

}

 

 

for($i = 0; $i <= ($num_rows - 1); $i++)

{

echo '<table width="526" border="0"><tr>

<td width="169">

<input type="text" name="$r_in[]" value="'.$new_r_timer_in[$i].'" style="width:169px" readonly="readonly" class="text_non_color"/></td>

<td width="169">

<input type="text" name="$r_out[]" value="'.$new_r_timer_out[$i].'"  style="width:169px" readonly="readonly" class="text_non_color"/></td>

<td width="169">

<input type="text" name="$r_num[]" value="'.$numb[$i].'"  style="width:169px" readonly="readonly" class="text_non_color"/></td>';

}

print("</tr>");

 

?>

 

</body>

</html>

 

 

i have "un-commented" the section that had the nested loops and arrays and it outputs something that's definitely wrong

Need more info.

 

Between the first element of $inn which starts at 7 and ends at 16, and the first element of $outt which starts at 14 and ends at 23.  So that would mean you are taking out the last element of $inn.  I could help you, but am massively confused.

 

 

Similar with foreach(). This assumes count($min) == count($max) == count($res) and that the array keys for each triplet line up correctly.

 

<?php 

$age = 15;

$min = array(7,10,13,16);
$max = array(14,16,21,23);
$res = array(3,5,7,9);

foreach( $res as $key => $num ) {
if( $age >= $min[$key] && $age <= $max[$key]  )
	echo 'Age '.$age.' is between '.$min[$key].' and '.$max[$key].' so result is '.$num.'<br>';
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.