Not displaying table fields when pulled from an array

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

Post Reply
JustPlainJef
Forum Commoner
Posts: 42
Joined: Tue Jan 04, 2011 5:04 am
Location: McHenry, IL

Not displaying table fields when pulled from an array

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Not displaying table fields when pulled from an array

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
gooney0
Forum Commoner
Posts: 56
Joined: Fri Jan 21, 2011 1:40 pm
Location: Reston, VA

Re: Not displaying table fields when pulled from an array

Post 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++;
}
JustPlainJef
Forum Commoner
Posts: 42
Joined: Tue Jan 04, 2011 5:04 am
Location: McHenry, IL

Re: Not displaying table fields when pulled from an array

Post 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?
gooney0
Forum Commoner
Posts: 56
Joined: Fri Jan 21, 2011 1:40 pm
Location: Reston, VA

Re: Not displaying table fields when pulled from an array

Post 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)
Post Reply