PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
charp
Forum Commoner
Posts: 85 Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA
Post
by charp » Sat Sep 04, 2004 9:12 pm
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 = "- ";}
if ($p1 == 999) {$p1 = "Gym";}
$p2=$row["p2"];
if ($p2 < 1) {$p2 = "- ";}
if ($p2 == 999) {$p2 = "Gym";}
$p3=$row["p3"];
if ($p3 < 1) {$p3 = "- ";}
if ($p3 == 999) {$p3 = "Gym";}
$p4=$row["p4"];
if ($p4 < 1) {$p4 = "- ";}
if ($p4 == 999) {$p4 = "Gym";}
$p5=$row["p5"];
if ($p5 < 1) {$p5 = "- ";}
if ($p5 == 999) {$p5 = "Gym";}
$p6=$row["p6"];
if ($p6 < 1) {$p6 = "- ";}
if ($p6 == 999) {$p6 = "Gym";}
$p7=$row["p7"];
if ($p7 < 1) {$p7 = "- ";}
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 = "- ";
} elseif ($row["$value"] == 999) { $roomNumber = "Gym";
} else { $roomNumber = $row["$value"];
}
echo '<td align="right">'. $roomNumber.'</td>';
}
?>
What am I missing here?
Thanks in advance.
Last edited by
charp on Sun Sep 05, 2004 1:16 pm, edited 1 time in total.
wasabi
Forum Newbie
Posts: 17 Joined: Sat Sep 04, 2004 9:38 am
Location: Adelaide
Post
by wasabi » Sat Sep 04, 2004 9:39 pm
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: --------
charp
Forum Commoner
Posts: 85 Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA
Post
by charp » Sat Sep 04, 2004 10:09 pm
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Sep 05, 2004 12:16 am
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;
}
?>
charp
Forum Commoner
Posts: 85 Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA
Post
by charp » Sun Sep 05, 2004 11:48 am
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 = "- ";
} 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!
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Sep 05, 2004 11:58 am
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.
charp
Forum Commoner
Posts: 85 Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA
Post
by charp » Sun Sep 05, 2004 1:17 pm
Wonderfully clear explanation. Thank you feyd.