Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditioning the generation of an html table

Status
Not open for further replies.

Kharas

Programmer
Jan 30, 2007
39
0
0
MX
I was wondering if you could condition the generation of an html table with php.

So far I know this works:

<?php do { ?>
<table>
</table>
<?php } while (); ?>

But what about this for example?

<?php if ($rows_query <= 0) {echo "Query rows are empty";} else { do { ?>
<table>
</table>
<?php } while (); } ?>

Thanks in advance
 
At first glance I see no problem with what you are trying to do, but it would be hard to follow if buried in more code.

My first instinct would be to encase the html in a string and echo the string. Avoiding the confusing breaks in php.

Code:
if($rows_query <= 0) {
   echo "Query rows are empty.";
} else {
   do {
      echo "<table></table>";
   } while ();
}
 
A further note:

You would of course have to format the table data before the echo or print. Also, the empty while() would be leaving you open to infinite loop.
 
Yeah, I just wrote it simply to avoid further confusion while getting my point across.
 
Putting the html code inside the echo "" statement doesn't seem doable, for there are more pieces of php code inside the table. When it finds the next "?>" it stops =/

That's why I thought of the do function. It keeps on going even after the break, but if combined with the 'else' part of the 'if syntax', it doesn't work either.

I'm going in circles here.
 
I've done things similar to what you seem to be attempting.

Its a personal taste for me, but I typically do something like this:

Code:
if($query_rows <= 0){
   echo "Query returned nothing.";
} else {
   $output = "<table>";
   for($i=0; $i<$query_rows; $i++){
      $output .= "<tr><td>Row $i:</td><td>$row_data</td></tr>";
   }
   $output .= "</table>";
}
 
whoops, forgot to add this line after that last

$output .= "</table>";
echo $output;
}
 
The table I want to condition does not repeat itself. Rows within it do, but those I got under control.

Now, the table (simplified) looks like this:

Code:
First row (has the headers of each field: Id, Manufacturer, Model, Year, Price and Img thumbnail)

<table width="400" border="0" cellspacing="0" cellpadding="0">

 <tr align="left">

 <td width="50" scope="col"><h4><?php if ($rows_conteo_prod <= 0) { echo "Actualmente no hay productos existentes en esta categoría";} else {echo "Siitto_ID";} ?></h4></td>

 <td width="30" height="30" align="center" scope="col">&nbsp;</td>

 <td scope="col"><h4><?php if ($rows_conteo_prod <= 0) { echo "";} else {echo "Marca";} ?></h4></td>
	  <td scope="col">&nbsp;</td>

 <td scope="col"><h4><?php if ($rows_conteo_prod <= 0) { echo "";} else {echo "Modelo";} ?></h4></td>
			
 <td scope="col">&nbsp;</td>

 <td scope="col"><h4><?php if ($rows_conteo_prod <= 0) { echo "";} else {echo "Año";} ?></h4></td>

 <td scope="col">&nbsp;</td>
            <td scope="col"><h4><?php if ($rows_conteo_prod <= 0) { echo "";} else {echo "Precio";} ?></h4></td>

 </tr>  Ends first row, starts second row with the actual content and the php code that makes that content row repeat itself.

<?php do { ?>

  <tr bgcolor="<?php if ($rowcolor == "#FFFFFF") { $rowcolor = "#CCCCCC" ; } else {$rowcolor = "#FFFFFF" ;} {echo $rowcolor;} ?>">

  <td width="50" align="left" scope="col" >&nbsp;<a href="inv_detalle.php?siittoID=<?php echo $row_lista_main['mont_ID']; ?>"><?php echo $row_lista_main['mont_ID']; ?></a></td>
   
  <td width="30" height="30" align="center" scope="col"><img src="<?php if ($row_lista_main['img_url1'] = "Imagenes/p.gif") {echo "Imagenes/blank.gif"; } else {echo $row_lista_main['img_url1']; } ?>" height="30" width="30"></td>

  <td align="left" scope="col"><?php if ($rows_conteo_prod <= 0) { echo "";} else {echo $row_lista_main['marca_name'];} ?></td>
			
  <td scope="col">&nbsp;</td>
            <td align="left" scope="col"><?php if ($rows_conteo_prod <= 0) { echo "";} else {echo $row_lista_main['modelo'];} ?></td>

  <td scope="col">&nbsp;</td>
            <td align="left" scope="col"><?php if ($rows_conteo_prod <= 0) { echo "";} else {echo $row_lista_main['year'];} ?></td>

  <td scope="col">&nbsp;</td>
            <td align="left" scope="col"><?php if ($rows_conteo_prod <= 0) { echo "";} else {echo "$".$row_lista_main['precio'];} ?></td>

   </tr>

Ends row and then comes the 'while' for the 'do' function.

<?php } while ($row_lista_main = mysql_fetch_assoc($lista_main)); ?>

Ends the table
         
        </table>

What the little pieces of PHP code inside the header and content rows do is basically show themselves if there are products, or show nothing if there's nothing to be shown (0 products).

What comes up when there are no products is a single message that reads "There are currently no products in this category".

But since the rows are still there, it appears all scrambled.

By conditioning the generation of the table itself, I can go for a clean "No products" message with no hassle.

Now, several things come to mind:

1- How do I stop the php openining and closing tags to interfere with the string of html code to be put inside the echo's ""??

2- Regardless of #1 being doable, if I can condition the generation of the whole table itself, I could remove the conditioning part of the php code inside the table, but not the echoing of the dynamic values, so...

I hope myself a little bit more clear =/
 
You are over complicating things checking in every row generation for the presence of a variable, it should only need to be done once.

Take this example:

Code:
$myqry="SELECT *FROM mytable WHERE somevalue=1;"
$res=mysql_query($myqry);
if(!$res){
echo "No results returned";
}
else{
echo "<table><tr>....column names...</tr>";

while($myrow=mysql_fetch_array($res)){
 echo "<tr><td>". $myrow['firstfieldname'] . "</td><td>" .  $myrow['secondfieldname']."</td>...</tr>";
}

echo "</table>";
}


What this does is ti first checks if anything was returned, before actually starting to output the table. If there are results it will output the table, but I don't need to check at every row if there is data, I already know there is from the previous check. And it will dynamically generate all the rows needed.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
You're right in that. It was a temporary solutin. The final looks like this:

<?php
if ($rows_conteo_prod <=0)
{echo "<h4>Actualmente no hay productos existentes en esta categoría</h4>";}
else { do { ?>

<table>
</table>

<?php } while ($row_lista_main = mysql_fetch_assoc($lista_main)); } ?>
 
Since the main table's existence is now conditioned, the individual conditioning code is now gone.

is where it all starts, when you get to /inv_list.php check categories with no products and you'll see the final result.
 

Glad you got it sorted out.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks to helpful people in this forum that is ;)
Case closed.
 
2 things i noticed:

Pull out the <table> and </table> trags out side of the Do while loop, otherwise you'll get a different table for every row in your results.

Second use a While loop, instead of a DO while loop. If you use a Do While loop, the loop will get executed once before the call to mysql_fetch_array, and $row_lista_main will not be populated the first time around, and you will get errors. Because it has not been defined yet by the While condition.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Which of the two 'do while' loops are you talking about?
 
change this:
Code:
  if ($rows_conteo_prod <=0)
    {echo "<h4>Actualmente no hay productos existentes en esta categoría</h4>";}
    else {[red] do { ?>

<table>
</table>

<?php } while ($row_lista_main = mysql_fetch_assoc($lista_main));[/red] } ?>

to


Code:
  if ($rows_conteo_prod <=0)
    {echo "<h4>Actualmente no hay productos existentes en esta categoría</h4>";}
    else { 
echo "<table>";
while ($row_lista_main = mysql_fetch_assoc($lista_main)) { 
echo "<tr>...." . $row_lista_main['fieldname'] . "</td><td>"  ...."</tr>";
}  

echo "</table>";

} ?>

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
The thing is, I got:

Code:
<table width="400" border="1" cellspacing="0" cellpadding="0">

And an inside do while loop in the second row of such table. How do you get the double quotes to not interfere with eachother?
 
Use single quotes to surround them.

Code:
echo [red]'[/red]<table width="400" border="1" cellspacing="0" cellpadding="0">[red]'[/red];


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks for picking this up Vacunita, I had to duck out un-announced due to my wife going into labor.
 
no problem JBlair.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top