Page 1 of 1

[SOLVED] Need help with loop

Posted: Sat Sep 04, 2004 9:12 pm
by charp
There must be a way to simplfy the code below using a loop of some sort. I'm certain there is such a way, but I can't for the life of me work it out. I'm fairly new to PHP and just took a 3 month hiatus from working with it. Now I'm rusty.

Code: Select all

<?php
		
		$p1=$row["p1"];
		if ($p1 < 1) {$p1 = "-&nbsp;&nbsp;";}
		if ($p1 == 999) {$p1 = "Gym";}
		
		$p2=$row["p2"];
		if ($p2 < 1) {$p2 = "-&nbsp;&nbsp;";}
		if ($p2 == 999) {$p2 = "Gym";}
		
		$p3=$row["p3"];
		if ($p3 < 1) {$p3 = "-&nbsp;&nbsp;";}
		if ($p3 == 999) {$p3 = "Gym";}
		
		$p4=$row["p4"];
		if ($p4 < 1) {$p4 = "-&nbsp;&nbsp;";}
		if ($p4 == 999) {$p4 = "Gym";}
		
		$p5=$row["p5"];
		if ($p5 < 1) {$p5 = "-&nbsp;&nbsp;";}
		if ($p5 == 999) {$p5 = "Gym";}
		
		$p6=$row["p6"];
		if ($p6 < 1) {$p6 = "-&nbsp;&nbsp;";}
		if ($p6 == 999) {$p6 = "Gym";}
		
		$p7=$row["p7"];
		if ($p7 < 1) {$p7 = "-&nbsp;&nbsp;";}
		if ($p7 == 999) {$p7 = "Gym";}

?>

I tried several variations of the following with no luck:

Code: Select all

<?php
$roomCheck = array (0 => 'P0','P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7');

foreach ($roomCheck as $key => $value) {
	if ($row["$value"] < 1) { $roomNumber = "-&nbsp;&nbsp;";
	} elseif ($row["$value"] == 999) { $roomNumber = "Gym";
	} else { $roomNumber = $row["$value"];
	}
echo '<td align="right">'. $roomNumber.'</td>';
}

?>
What am I missing here?

Thanks in advance.

Posted: Sat Sep 04, 2004 9:39 pm
by wasabi
what is row doing? where did $row come from?

try changing $row to $key even better $key to $num
example: $num replaces $row. $room replaces $value.

Code: Select all

<?php
$roomCheck = array (0 => 'P0','P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7'); 

foreach ($roomCheck as $num => $room) { 
  if ($num["$room"] < 1) { $roomNumber = "-"; 
   } elseif ($num["$room"] == 999) { $roomNumber = "Gym"; 
  } else { $roomNumber = $num["$room"]; 
  } 
  //echo "NUMBER: $num <br /> ROOM: $room";
echo '<td align="right">'. $room.'</td>'; 
} 

PRINTS: P0P1P2P3P4P5P6P7
?>
not really 100% sure what your wanting.

Code: Select all

<?php
$roomCheck = array (0 => 'P0','P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7'); 

foreach ($roomCheck as $key => $value) { 
  if ($key["$value"] < 1) { $roomNumber = "-"; 
   } elseif ($key["$value"] == 999) { $roomNumber = "Gym"; 
  } else { $roomNumber = $key["$value"]; 
  } 
echo '<td align="right">'. $roomNumber.'</td>'; 
} 

?>
PRINTS: --------

Posted: Sat Sep 04, 2004 10:09 pm
by charp
More info... Here we go:

All of the code in the original post is inside the following:

Code: Select all

<?php
while ($row=mysql_fetch_array($result)) {
}
?>
Of all the values from $row=mysql_fetch_array($result), I need to perform a check on the values from p0 through p7. The original code performs the same check on each of those values, so there must be a way to loop through it.

Hope this clarifies the original question.

Posted: Sun Sep 05, 2004 12:16 am
by feyd
how about:

Code: Select all

<?php

$p = array();
for($x = 0; $x <= 7; $x++)
{
$val = isset($row['p' . $x]) ? $row['p' . $x] : 0;
if($val < 1) $p[$x] = '-';
elseif($val == 999) $p[$x] = 'Gym';
else $p[$x] = $val;
}

?>

Posted: Sun Sep 05, 2004 11:48 am
by charp
Thanks for the replies. They served to inspire a solution. After a bit of dinking around, I came up with this:

Code: Select all

<?php
for ($n=0; $n<=7; $n++) {
	if ($row[p.$n] < 1) { $roomNumber = "-&nbsp;&nbsp;";
	} elseif ($row[p.$n] == 999) { $roomNumber = "Gym";
	} else { $roomNumber = $row[p.$n];
	}
echo '<td align="right" bgcolor="#ffffff">'. $roomNumber.'</td>';
}
?>
Feyd, to my newbie eye, your solution seems to be along the same lines as my solution. However, I'm not following one line of that code:

Code: Select all

<?php
$val = isset($row['p' . $x]) ? $row['p' . $x] : 0;
?>
Can anyone translate that line of code for me? The question mark in particular has me baffled.

Thanks again!

Posted: Sun Sep 05, 2004 11:58 am
by feyd
charp wrote:Feyd, to my newbie eye, your solution seems to be along the same lines as my solution. However, I'm not following one line of that code:

Code: Select all

<?php
$val = isset($row['p' . $x]) ? $row['p' . $x] : 0;
?>
Can anyone translate that line of code for me? The question mark in particular has me baffled.
?: is a special operator normally called the ternary or trinary operator because it takes 3 terms:
expression1 ? expression2 : expression3

if expression1 is true, expression2 is returned.
if expression1 is false, expression3 is returned.

Posted: Sun Sep 05, 2004 1:17 pm
by charp
Wonderfully clear explanation. Thank you feyd.