Alternating Row colors while incrementing by 10

Hello, this is my first post ever on a help board. I don’t see my first post although It says I’ve tried to post twice, sorry if I’ve screwed up already. I think I have done all the searching and editing and trial and error that I can do or understand. My problem is in the second block of php code.

What I need is for my code to take in a value for centigrade $centStart =$Start (the input as an int)and return a value for farhenheit $centEnd= $End(the input as an int). My formula is correct and works, but I am then supposed to increment the centigrade value into a table and display the temperatures in the table with alternating row colors. It seems like I should make the $i++ be $i=$i+10 but when I do I lose all of my rows except for the first one. So I’ve posted the code here as it increments by 1.
I hope I’ve done this right so you will see easily where I’m confused.
Any help is most appreciated .

[code]

PHP/Web220 Assignment5_2.php

Assignment 5_2

Centigrade to Fahrenheit Converter

[/code] [php]<?php //-------retrieve Variables from the form------- $Start = $_REQUEST ["start"]; $End = $_REQUEST ["end"];

//----declaring variables----

$Start_error = false;
$End_error = false;
$Start_error_msg = “”;
$End_error_msg = “”;

$rowcol= 0;
$rownum = 0;
$test4Infinity = ‘’;

//----decisions on data returned from form—
//----convert from Fahrenheit to Celsius using the formula
//----fahrenheit = centigrade * 9/5 + 32;

if ($Start == “”)
{
$Start_error_msg ="
You did not enter the Start temperature!
";
$Start_error = true;
}
elseif (is_numeric($Start) == false)
{
$Start_error_msg = "
Enter numeric value for Start Temperature!
";
$Start_error = true;
}

if ($End == “”)
{
$End_error_msg = "
You did not enter the End temperature!
";
$End_error = true;
}
else
{
if ($End < $Start)
{
$End_error_msg ="
Start Temperature cannot be greater than the End temperature!
";
$End_error = true;
}
}
if ($Start_error == true)
echo “

$Start_error_msg

”;
if ($End_error == true)
echo “

$End_error_msg

”;
if($Start_error == false && $End_error == false)

?> [/php]

[code]

[/code] [php]<?php $centStart = (int) $Start; $centEnd = (int) $End;

$table_row = ‘’;

  for($i=$centStart; $i<$centEnd; $i++)
 { $rownum =$rownum +1;
    if($rownum % 2 != 0 )
    	$rowcol = "beige";    #even row #F5F5DC
    else              
    	$rowcol = "bisque";   #odd row #FFE4C4
		
	$convert = $i * 9/5 + 32;

	$table_row .= "<tr style=\"background-color: $rowcol\"><td>$i</td><td>$convert</td></tr>\n";
 }
 
 echo '<tr><td></td></tr>';
 echo $table_row;

?> [/php]

[php]<?php $test4Infinity++;
if ($test4Infinity >10)
{
break;
}

?>[/php]

[code]

Temperature in Centigrade Temperature in Fahrenheit
[/code] [php] <?php include("web220_footer.html"); ?>[/php] [php][/php]

Well, is this all one page? Also, you do not show where the start and end variable come from.
Assuming you are just showing some of the code, one problem jumped out to start with:

Your line:
$table_row .= “<tr style=“background-color: $rowcol”>

$i$convert\n”;

Should be something like this:
$table_row .= "<tr style=“background-color: " . $rowcol . “”>

” . $i . “” . $convert . “\n”;

Also, what is “$test4Infinity++” all about? I don’t see what that is for???

Thanks,
No, there is another file which is where $Start and $End come from. I didn’t want to post too much at once, but I will If you think I should. I tried your configuration of the $table_row and it just put more decimals into the numbers of my temperatures.

The $test4Infinity is because I was getting an infinite loop and couldn’t find a better way to stop it. The table I created should only have 10 rows so I stopped it at 10 until I can get this other problem solved, and then I may leave it anyway.

Well, this code works if you put it into a blank .PHP page. So, maybe that will help you.
Compare it to what you currently have… Good luck…
[php]
echo ‘

’;
$centStart = “30”;
$centEnd = “42”;
$table_row = ‘’;

for($i=$centStart; $i<$centEnd; $i++)
{
$rownum =$rownum +1;
if($rownum % 2 != 0 )
{
$rowcol = “beige”; #even row #F5F5DC
}else{
$rowcol = “bisque”; #odd row #FFE4C4
}
$convert = $i * 9/5 + 32;
$table_row .= "<tr style=“background-color: " . $rowcol . “”>

\n”;
}

echo ‘

’;
echo $table_row;
echo ‘
” . $i . “ ” . $convert . “
’;
[/php]

This has helped some, at least my table no longer collapses when I attempt to increment by 10. That in itself is amazing since I’ve been working on this for over a week!! Thank you!!! I am still going to post my code as it is now in case you or anyone else can help me figure this out. It is the second block of PHP that is giving me kaniptions. I’ll keep hacking away at it in the meantime, maybe I’ll have an epiphany. Thanks again.

[code]

PHP/Web220 Assignment5_2.php

Assignment 5_2

Centigrade to Fahrenheit Converter

[/code]

[php]<?php
//-------retrieve Variables from the form-------
$Start = $_REQUEST [“start”];
$End = $_REQUEST [“end”];

//----declaring variables----

$Start_error = false;
$End_error = false;
$Start_error_msg = “”;
$End_error_msg = “”;

$rowcol= 0;
$rownum = 0;
$test4Infinity = ‘’;

//----decisions on data returned from form—
//----convert from Fahrenheit to Celsius using the formula
//----fahrenheit = centigrade * 9/5 + 32;

if ($Start == “”)
{
$Start_error_msg ="
You did not enter the Start temperature!
";
$Start_error = true;
}
elseif (is_numeric($Start) == false)
{
$Start_error_msg = "
Enter numeric value for Start Temperature!
";
$Start_error = true;
}

if ($End == “”)
{
$End_error_msg = "
You did not enter the End temperature!
";
$End_error = true;
}
else
{
if ($End < $Start)
{
$End_error_msg ="
Start Temperature cannot be greater than the End temperature!
";
$End_error = true;
}
}
if ($Start_error == true)
echo “

$Start_error_msg

”;
if ($End_error == true)
echo “

$End_error_msg

”;
if($Start_error == false && $End_error == false)

?> [/php]

[code]

[/code]

[php]<?php
echo’

Temperature in Centigrade Temperature in Fahrenheit
’;

$centStart = (int) “$Start”;
$centEnd = (int) “$End”;

$table_row = ‘’;

  for($i=$centStart; $i<$centEnd; $i=$i+10)
 { $rownum =$rownum +1;
    if($rownum % 2 != 0 )
    	$rowcol = "beige";    #even row #F5F5DC
    else              
    	$rowcol = "bisque";   #odd row #FFE4C4
		
	$convert = $i * 9/5 + 32;

	$table_row .= "<tr style=\"background-color:". $rowcol . "\"><td>" . $i . "</td><td>" . $convert."</td></tr>\n";
 }
 
 echo '<tr><td></td></tr>';
 echo $table_row;

echo ‘

’;
?> [/php]

[php]<?php $test4Infinity++;
if ($test4Infinity >10)
{
break;
}

?>[/php]

[php] <?php include("web220_footer.html"); ?>[/php]

Well, I am heading out for a few hours. I will check in when I get back.

The issue, I think is your for-next loop…
You are looping thru from start to end, by 1’s, but, the loop could be a large number…
You could change it to be something like this:
[php]
$temp=$centEnd-$centStart;
if($temp>10)
$temp=10;

for($i=$centStart; $i<$centStart+$temp; $i=$i+10)
ETC ETC ETC
}
[/php]
This would do the loop but limit it to 10 only and not need for the infinity stuff…

Well, post what you are having trouble with the second PHP group and I will look at it later tonight when I get back… Good luck…

Well after an evening of on track and off again I can get it to increment by 10 once. So now I have two not so nice looking rows of alternate colors. I agree that it seems to fall apart with this block of php coding. Possibly in the $table_row (etc, etc,) syntax, or the following echos?? I am still trying by trial and error to iron it out but I’m having my doubts after all this.
Thanks again so much for the help.

<?php echo''; $centStart = (int) "$Start"; $centEnd = (int) "$End"; $table_row = ''; $temp=$centEnd-$centStart; if($temp>10) $temp=10; for($i=$centStart; $i<$centEnd+$temp; $i=$i+10) { $rownum =$rownum +1; if($rownum % 2 != 0 ) $rowcol = "beige"; #even row #F5F5DC else $rowcol = "bisque"; #odd row #FFE4C4 $convert = $i * 9/5 + 32; $table_row .= "\n"; } echo ''; echo $table_row; echo '
$i $convert
'; ?>

What is this: ???

$centStart = (int) “$Start”;
$centEnd = (int) “$End”;

Shouldn’t it be???
$centStart = (int) $_POST[Start’];
$centEnd = (int) $_POST[‘End’];

??? You must pull these from the previous page… If that does it, let us know…

I declared my variables at the top of the file like this
to call it from the other file
//-------retrieve Variables from the form-------
$Start = $_REQUEST [“start”];
$End = $_REQUEST [“end”];

I changed what you suggested (see below)to this…because then I didn’t get undeclared index or undeclared variable errors. Seems like I need both, but not sure if both need to be $_REQUEST since changing the latter doesn’t seem to affect the outcome/ appearence of my table. My issue now is getting it to repeat the calculation up to 10 times(minimum of 6) without losing the increment by 10. I have repeatedly gotten it to do one or the other but never both at the same time. I’m trying to look at it simplistically, but the coding and recoding has really just confused me. ERGH!

<?php echo''; $centStart = (int)$_REQUEST['start']; $centEnd = (int) $_REQUEST['end']; $table_row = ''; ?>

Well, you don’t need the (int) if you you input numbers as PHP auto-types variables.
And, I didn’t mention $_REQUEST. I never use that. For a form, I always use $_POST[‘variable’]…

So this is correct:
//-------retrieve Variables from the form-------
$Start = $_POST [“start”];
$End = $_POST [“end”];

Let me know what else you need. I am home tonight and will check often…

Doing this gets me three rows! So it calculates three times and alternates row color just fine. I played around with the \n character in the same block of code (see below) knowing that it means: print a new line character. No matter what I do the table still calculates three times etc., or prints whatever I replace n with three times at the top of my table. That makes me think something within my table is repeating/looping three times, so I played around somemore and looked through my manuals, but I’m missing it. Can I somehow initiate the number of rows in the for loop? Am I at least on track with my thinking?

<?php $temp=$centEnd-$centStart; if($temp>10) $temp=10; for($i=$centStart; $i<$centEnd+$temp; $i=$i+10) { $rownum =$rownum +1; if($rownum % 2 != 0 ) $rowcol = "beige"; #even row else $rowcol = "bisque"; #odd row $convert = $i * 9/5 + 32; $table_row .= " $i $convert \n"; } echo ''; echo $table_row; echo ''; ?>

Your code is working correctly. I used 30 and 42 as the start and end.
It resulted in 3 output lines. This is correct!

You have the temp’s skipping by 10’s… So, 30, 40, 50… is correct.

Two things. First, did you want it to skip by 10’s? If so, okay, if not, change it to whatever.

Also, there is a problem with the FOR loop.
We are limiting the for to ten items max.
Now it says: for($i=$centStart; $i<$centEnd+$temp; $i=$i+10)

It should be: for($i=$centStart; $i<$centStart+$temp; $i=$i+10)
OR, even: for($i=$centStart; $i<$centStart+$temp; $i=$i+1)

This should fix you up nicely… Let me know…

AWESOME!
It was actually the $temp variable that contreolled the number of times it calculated. I changed it to 60 as in 60 degrees I guess or 6 * 10. Either way it works now. Thanks for your time and patience.

$temp=$centEnd-$centStart;
if($temp<60)
$temp=60;
for($i=$centStart; $i<$centEnd+$temp; $i=$i+10)
{

Glad it is working. Very nice feeling to get it done and working, isn’t it!

CYA in the Bitstream…

Sponsor our Newsletter | Privacy Policy | Terms of Service