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
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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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>';
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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