Forum Moderators: coopster

Message Too Old, No Replies

Need <TR> not to show if nothing in php echo - possible?

         

Rightz

4:37 pm on Jun 24, 2008 (gmt 0)

10+ Year Member



Hi guys and girls,

Hope you are all well :)

I know nothing about php but have been trying to do the odd thing.

I have a table (in template.html) that has php echo statements loading from codes.php

The only problem I have is that I would like the TR not to show if there is nothing in the php echo.

Is this possible.

To get what I mean see my code:

My php code:

$atitle1 = "Blurb 1";

$acode1 = "More blurb 1";

$atitle2 = "";

$acode2 = "";

I wont always have things in $atitle2 but don't want to have to change the code in template.html everytime.

My html code:

<table width=500 border=1 bordercolor="#02679A" cellpadding=7 cellspacing=0><tr bgcolor="#02679A"><td align=center valign=center><font color=white><b>Title</b></font></td><td align=center valign=center><font color=white><b>Description </b></font></td></tr>

<tr><td><?php echo "$atitle1"; ?></td><td><?php echo "$acode1"; ?></td></tr>

<tr><td><?php echo "$atitle2"; ?></td><td><?php echo "$acode2"; ?></td></tr>

<tr><td><?php echo "$atitle3"; ?></td><td><?php echo "$acode3"; ?></td></tr>

</table>

So is there any way I can have php (or some other script) not show the tr with nothing in? I need this because the tables arent appearing correctly without anything in.

Please please please please give me any help you can.

Emma

andrewsmd

5:46 pm on Jun 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can echo your table code with php. Since you have a template.html I am curious as to whether you know how to use php templates. If you have pear, then this function would be extremely easy; as you can just set your template based on a if statement and keep adding to your table. Other wise I would output my table code with php. Wherever you table is in your html file, put a tag in like <?php require_once("tableOutput.php"); ?> Then in tableOutput.php output your table based on your parameters. I don't know where the values come from for $atitle1 and $acode1 but here is the if statements on how to check them. If you need to get the values from something then let me know a little more and I'll try to help. Anyways your if statement would look something like this.

//if the atitle is blank
if($atitle1 == ""){
//we don't do anything
}

//otherwise it equals something
else{
echo("<tr><td>{$atitle2}</td></tr>");
}
if you want the value of $atitle2 there then leave the squiggilies. If you want the actual text $atitle2 then remove the squiggilies. Let me know if I can help anymore.

dpinion

5:49 pm on Jun 24, 2008 (gmt 0)

10+ Year Member



You will need PHP to generate the tables for you via the "echo" statement. Something like the below:

<?php
echo "<table width=500 border=1 bordercolor=\"#02679A\" cellpadding=7 cellspacing=0><tr bgcolor=\"#02679A\"><td align=center valign=center><font color=white><b>Title</b></font></td><td align=center valign=center><font color=white><b>Description </b></font></td></tr>";
echo "<tr><td>$atitle1</td><td>$acode1</td></tr>";
//Note the use of the "IF" operator here
IF ($atitle2==NULL)
{
echo "<tr><td>$atitle2</td><td><$acode2</td></tr>";
}
echo "<tr><td>$atitle3</td><td>$acode3</td></tr></table>";
?>

Then just include the above in your include file along with your variables.

HTH!

andrewsmd

6:03 pm on Jun 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One other thing. If you have any php code in your html it must be named template.php. If you want to name it with the html extension then you have to implement the template class I mentioned earlier. I did a simple program to show you how you can do this. This is test.php (your template.html which needs to be php!)
<html>
<head>

</head>
<body>

<table>
<?php require_once("test2.php"); ?>
</table>

</body>
</html>

this is test2.php
<?php
for($i = 1; $i <= 10; $i++){

//the % function returns the remainder therefore if the
//remainder is 0 then the row is even because anything divided
//by 2 with a remainder of 0 is even otherwise it is odd
if(($i % 2) == 0){
echo("<tr><td>even row {$i}</td></tr>");
}//if

else{
echo("<tr><td>odd row {$i}</td></tr>");
}//else

}//for
?>

in your case echo your table tags when your variables are not null