Page 1 of 1
Not displaying table fields when pulled from an array
Posted: Mon Jan 24, 2011 10:29 am
by JustPlainJef
Howdy again. Hope this is easy. Let's say you have a 2D array such as:
Code: Select all
<?php
$array = array( array("a", "apple", "alfred", "a_secret"),
array("b", "banana", "bob", "b_secret"),
array("c", "cherry", "charlie", "c_secret")
);
?>
We can display the table with
Code: Select all
<?php
echo "<table>";
for each($array as $row)
{
echo "<tr>";
For each($row as $item)
{
echo "<td>$item</td>";
}
echo "</tr>";
}
echo "</table>";
?>
What I'm not sure of is what if I don't want to display the last column, the "secret." I'd assume I'd have to do something based on the key names...
I've done some searching but haven't had any luck....
Thanks!
Jef
Re: Not displaying table fields when pulled from an array
Posted: Mon Jan 24, 2011 11:15 am
by social_experiment
Assuming it's only the last element you don't want to show, this should do the trick. (Modify for table use)
Code: Select all
foreach ($array as $row) {
$count = count($row);
$countNotLast = $count - 1;
for ($i = 0; $i < $countNotLast; $i++) {
echo $row[$i] . '<br />';
}
}
Hth
Re: Not displaying table fields when pulled from an array
Posted: Mon Jan 24, 2011 11:20 am
by gooney0
Jef,
You could either remove the unwanted key from the array or use an if statement to not display it.
The question is how will you know which key contains the secret? Is it the 4th key? or does it have a certain name?
If it's the 4th key you'll need to keep track of the number of keys you're looping through.
$num=0;
foreach($items as $row)
{
if($num !="3") // We're starting at zero
{
// print the data
}
$num++;
}
Re: Not displaying table fields when pulled from an array
Posted: Mon Jan 24, 2011 11:37 am
by JustPlainJef
gooney0 wrote:Jef,
You could either remove the unwanted key from the array or use an if statement to not display it.
The question is how will you know which key contains the secret? Is it the 4th key? or does it have a certain name?
If it's the 4th key you'll need to keep track of the number of keys you're looping through.
$num=0;
foreach($items as $row)
{
if($num !="3") // We're starting at zero
{
// print the data
}
$num++;
}
Thanks guys... Both new things to store in my brain...
It wouldn't be the last item I'd be hiding, but I would know which ones I'd want to hide. As it stands right now, I think I'd be hiding 0, 2, and 4, in an array with about 12 fields, but I've been curious to figure out how to do it. Normally, I think of something I could do and think "That could be done by doing this." Even if I'm not right (usually aren't, just getting my feet wet), at least I have ideas.
For the array, I learned how to print the table, but could not figure out how to skip certain (column) fields.
So taking gooney's example, could I use if($num != 0 OR $num != 2 OR $num != 4) or would I have to use nested Ifs?
Re: Not displaying table fields when pulled from an array
Posted: Tue Jan 25, 2011 1:13 pm
by gooney0
Jef,
Basically yes. If you're selecting the "secrets" by their number...
example:
Print the items UNLESS they are the 0,2, or 4th key.
if($num !=0 && $num !=2 && $num !=4) // If not 0, 2, or 4
{
// print the item
} else {
// do nothing or something different
}
It would be cleaner if you could use the keys of the array instead of their number as the numbers could change latter on.
For instance this would be nicer:
$people[0]["Nickname"]="gooney0";
$people[0]["Secret"]="Don't show this!";
foreach($people as $person)
{
foreach(array_keys($person) as $key)
{
// print out each key for a person
if($key != "Secret")
{
// If the key isn't a secret let's print stuff
print "his {$key} is ";
print $person[$key]; // This is the value
}
}
}
Output:
his Nickname is gooney0
(the secret isn't printed)