$myfile = mysql_query($query_myfile, $db) or die(mysql_error());
$totalRows_myfile = mysql_num_rows($myfile);
$typ_d = '';
$test='';
while ( $row_myfile = mysql_fetch_assoc($myfile) )
{
if ( $typ_d != $row_myfile[ 'typ_d' ] )
{
$typ_d = $row_myfile[ 'typ_d' ];
echo "<h2>$typ_d</h2>";
}
if ( $test != $row_myfile[ 'test' ] )
{
$test = $row_myfile[ 'test' ];
echo "<h3>$test</h3>";
}
echo "<li><a href=\"myfileDetail.php?myfile_id=".$row_myfile['myfile_id']. "\">";
echo $row_myfile['shortname'].' ';
echo $row_myfile['name']; ?></a></li>
<?php } ?>
This code list out element in group and subgroups like this
Typ_d (group) generated by
echo "<h2>$typ_d</h2>";
test(subgroup) generated by
echo "<h3>$test</h3>";
then the lists generated by
echo "<li><a href=\"myfileDetail.php?myfile_id=".$row_myfile['myfile_id']. "\">";
echo $row_myfile['shortname'].' ';
echo $row_myfile['name']; ?></a></li>
I have a problem generation the html tag as follow
<h2>Typ_d1</h2>
<h3> Test1</h3>
<ul>
<li> List1</li>
<li> List2 </li>
<li>List3 </li>
</ul>
<h2>Typ_d2 </h2>
<h3>Test2</h3>
<ul>
<li> List1</li>
<li> List2</li>
<li> List3</li>
</ul>
<h2> Typ_d3</h2>
<h3> Test3</h3>
<ul>
<li> List1</li>
<li> List2 </li>
<li>List3 </li>
</ul>
I have tried so much but i cannot get it with the right order ..need help please
the current out put has not <ul> tag and I would like to put it in side the code
this is the current output
<li> List1</li>
<li> List2 </li>
<li>List3 </li>
<h2>Typ_d2 </h2>
<h3>Test2</h3>
<li> List1</li>
<li> List2</li>
<li> List3</li>
<h2> Typ_d3</h2>
<h3> Test3</h3>
<li> List1</li>
<li> List2 </li>
<li>List3 </li>
Ok, without doing a major restructuring of your code (e.g. into an MVC pattern), you’re better off compiling the data into an array first and then looping over that to generate the output, like @Blowski said. Something like this:
Notes:
You should almost certainly be html-escaping the variable values before outputting them in HTML.
I’ve eliminated the ugly concatenation for HTML output generation and replaced it with variable interpolation.
I’ve eliminated the ill-advised double-quoting of HTML output strings and used
HEREDOCs instead.There are a lot of variations on details of how you could do this, e.g. call
array_key_exists()in the while loop instead of just re-assigning theh2andh3keys each time, assign$groups[ $typ_d ]by reference to shorten the other assignment statements, etc.