Jump to content

PHP conditional to echo or not echo


ljones
 Share

Recommended Posts

I am a php beginner and am having a hard time trying hide rows if fields are empty. i have a lot of blank rows that I want to not show. Also, I have long URLs and would rather have the link show as "see website" instead of echoing the website field. (it's too long for my mobile media query view).

The logic I am looking for is: IF <a href='{$child->ten_website}'

is empty then do not show AND do not echo

>see website</a>

but if it not empty echo everything.

Below is the context of the code.

Any help would be greatly appreciated.


if($page->numChildren) {

               echo "<table width='100%'><tr><td class='room_no'>Room No.</td><td class='cont_info'>Contact Info</td></tr>";
    
                         echo "<tr><td> </td>";
                         echo "<td> </td></tr>";
                         echo "<tr><td class='room_no'><br />{$child->title}</td><td class='cont_info'><p>{$child->ten_pri_name}<br />";
                         echo "{$child->ten_first_name} {$child->ten_last_name}";
                         echo "<br /><a href='{$child->ten_website}'>";
                         echo "See website</a><br />";
                         echo "<a href='mailto:{$child->ten_email}'>{$child->ten_email}</a><br />";
                         echo "{$child->ten_phone_no}<br />{$child->Media_Business}</p></td>";
                         echo  "</tr><tr><td> </td></tr>";
                       
                   
                         }
                   
               echo "</table>";	
                
                }
Link to comment
Share on other sites

hm... you want to do this for each children, right? So, what you have to use is a foreach() command

foreach($page->children as $child) {
           your table goes here;
           }

Inside the table you can refer to each page, exactly as you did: {$child->title}

as i understood, you want to test if the field was populated to the corresponding sentence. Then you would have to do this for each one of them:

if($child->ten_email) { echo "See website</a><br /><a href='mailto:{$child->ten_email}'>{$child->ten_email}</a>"; }

I hope I understood well what you asked.

Also, you should notice that in your code, in case the condition wouldn't be satisfied, the table closing tag would be echoed alone because it's the only element outside the IF block.

UPDATE: Ah, I was completely distracted! I mixed the website with the email... sorry, I'm glad you got it right :)

Link to comment
Share on other sites

You can't just echo an array, but you need to walk through all the records in the array. It should be something like this:

if($page->children) {

          echo "<table width='100%'><tr><td class='room_no'>Room No.</td><td class='cont_info'>Contact Info</td></tr>";
		   
	foreach($children as $child) {
                         echo "<tr><td> </td>";
                         echo "<td> </td></tr>";
                         echo "<tr><td class='room_no'><br />{$child->title}</td><td class='cont_info'><p>{$child->ten_pri_name}<br />";
                         echo "{$child->ten_first_name} {$child->ten_last_name}";
                         echo "<br /><a href='{$child->ten_website}'>";
                         echo "See website</a><br />";
                         echo "<a href='mailto:{$child->ten_email}'>{$child->ten_email}</a><br />";
                         echo "{$child->ten_phone_no}<br />{$child->Media_Business}</p></td>";
                         echo  "</tr><tr><td> </td></tr>";

               }
			   
           echo "</table>";   
                
    }

If you want don't want the website field being displayed when empty, try something like this:

echo "{$child->ten_first_name} {$child->ten_last_name}<br />";
if ($child->ten_website) {
  echo "<a href='{$child->ten_website}'>";
  echo "See website</a><br />";
}

I didn't test the code, just wrote it quickly in the browser, but I hope it helps.

/Jasper

@Diogo: you were too quick for me...  :)

Link to comment
Share on other sites

Thanks a million guys. This works perfectly! I am not sure if there is an easier way to avoid having to put in so many echos, but I had to indent everything in order to make sure my table did not break.

    echo "<table width='100%'>";
                echo          "<tr>";
                echo          "<td class='room_no'>Room No.</td>";
                echo             "<td class='cont_info'>Contact Info</td>";
                echo          "</tr>";
                echo          "<tr>";
                echo             "<td> </td>";
                echo             "<td> </td>";
                echo          "</tr>";                           
                          
                
             foreach($page->children as $child) {
                echo          "<tr>";
                echo               "<td class='room_no'>{$child->title}</td>";                                                    
                echo               "<td class='cont_info'>";
                           
                       if($child->ten_pri_name)     { echo "<strong>{$child->ten_pri_name}</strong><br />"; }
                       if($child->ten_first_name)   { echo "<strong>{$child->ten_first_name}"; }
                       if($child->ten_last_name)   { echo " {$child->ten_last_name}<br /></strong>"; }
                       if($child->ten_website)        { echo "<a href='{$child->ten_website}'>view website</a><br />"; }
                       if($child->ten_email)            { echo "<a href='mailto:{$child->ten_email}'>email {$child->ten_first_name}</a><br />"; }
                       if($child->ten_phone_no)     { echo "{$child->ten_phone_no}<br />"; }
                       if($child->Media_Business)  { echo "{$child->Media_Business}"; }
                         
                echo                                 "</td>";
                echo         "</tr>";
                echo         "<tr>";
                echo              "<td> </td>";
                echo              "<td> </td>";
                echo         "</tr>";
                         }

     echo "</table>";	
Link to comment
Share on other sites

Hello ljones

You don't need to echo everything, especially if its plain html.

Also you may use the code tag:

 <?php ... 

for posting code in the forum. <?php is for highligthing php code properly .

Since I'm so mad and am cleaning my own code currently, I was so crazy to clean up your code and made it as short as possible: ;)


<table width='100%'>
 <tr>
   <td class='room_no'>Room No.</td>
   <td class='cont_info'>Contact Info</td>
 </tr>
 <tr>
   <td> </td>
   <td> </td>
 </tr>
 <?php foreach($page->children as $child): ?>
 
 <tr>
   <td class='room_no'><?php echo $child->title?></td>

   <td class='cont_info'>
     	<?php
     	echo $child->ten_pri_name ? "\n<strong>{$child->ten_pri_name}</strong><br />" : '' ;
echo $child->ten_first_name ? "\n<strong>{$child->ten_first_name}" : '';
    	echo $child->ten_last_name ? "\n{$child->ten_last_name}<br /></strong>" : '' ;
echo $child->ten_website ? "\n<a href='{$child->ten_website}'>view website</a><br />" : ' n/a ';
echo $child->ten_email ? "\n<a href='mailto:{$child->ten_email}'>email {$child->ten_first_name}</a><br />" : '' ;
echo $child->ten_phone_no ? "\n{$child->ten_phone_no}<br /> " : '' ;
echo $child->Media_Business ? "\n{$child->Media_Business}" : '' ;
     ?>
 	</td>
 </tr>
 <?php endforeach; ?>
 
 <tr>
   <td> </td>
   <td> </td>
 </tr>
</table>

Or this is also possible to seperate html and php even more.

 <?php foreach($page->children as $p): ?>
 
 <tr>
   <td class='room_no'><?php echo $p->title ?></td>

<td class='cont_info'>
<?php
       $name = $p->ten_pri_name ? $p->ten_pri_name : '' ;
       $firstname = $p->ten_first_name ? $p->ten_first_name : '' ;
       $lastname = $p->ten_last_name ? $p->ten_last_name : '' ;
       $website = $p->website ? $p->website : 'n/a' ;
       $email = $p->ten_email ? $p->ten_email : '' ;
       $phone = $p->phone_no ? $p->phone_no : '-';
       $mb = $p->Media_Business ?  $p->Media_Business : '';
       ?>
       <strong><?php echo $name ?></strong>
       <br />
       <strong><?php echo $firstname ?> <?php echo $lastname ?></strong>
       <br />
       <a href='<?php echo $website ?>'>view website</a>
       <br />
       <a href='mailto:<?php echo $email ?>'>email <?php echo $firstname ?></a>
       <br />
       <?php echo $phone ?>
       <br />
<?php echo $mb ?>
 	</td>
 </tr>
 <?php endforeach; ?>
Link to comment
Share on other sites

Soma, on your last example you are still printing the unnecessary html tags when the fields are not populated.

LJones, you can also separate html and php like this. Although it makes the code longer, it's maybe more readable:

<?php if($child->ten_email):?>
     <a href='mailto:<?php echo $child->ten_email?>'>email <?php echo $child->ten_first_name?></a>
     <br />
<?php endif;?>

or, with short tags:

<?if($child->ten_email):?>
     <a href='mailto:<?=$child->ten_email?>'>email <?=$child->ten_first_name?></a>
     <br />
<?endif;?>

And yep, I would also remove all those echos. Readability of the code is more important than indentation on source view :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...