Mark
Mark

Reputation: 37

html tag not shown properly in php

$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>

Upvotes: 0

Views: 221

Answers (3)

JMM
JMM

Reputation: 26837

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:

$myfile = mysql_query($query_myfile, $db) or die(mysql_error());


$groups = array();

while ( $row_myfile = mysql_fetch_assoc( $myfile ) ) {

  $typ_d = $row_myfile[ 'typ_d' ];

  $groups[ $typ_d ][ 'h2' ] = $row_myfile[ 'typ_d' ];

  $groups[ $typ_d ][ 'h3' ] = $row_myfile[ 'test' ];

  $groups[ $typ_d ][ 'files' ][] = <<<DOCHERE
<li>
<a href="myfileDetail.php?myfile_id="{$row_myfile[ 'myfile_id' ]}">{$row_myfile[ 'shortname' ]} {$row_myfile[ 'name' ]}</a>
</li>
DOCHERE;

}
// while


foreach ( $groups as &$group ) {

  $group[ 'files' ] = join( "\n\n", $group[ 'files' ] );

  $group = <<<DOCHERE
<h2>
{$group[ 'h2' ]}
</h2>

<h3>
{$group[ 'h3' ]}
</h3>

<ul>

{$group[ 'files' ]}

</ul>
DOCHERE;

}
// foreach


echo join( "\n\n\n", $groups );

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 the h2 and h3 keys each time, assign $groups[ $typ_d ] by reference to shorten the other assignment statements, etc.

Upvotes: 2

Dan Blows
Dan Blows

Reputation: 21184

As @jit pointed out, you're not echoing the <ul> tag anywhere. The structure of your code makes this a little difficult.

Restructure your code along the MVC design pattern:

  1. A model runs the database query and assigns the results to an array.
  2. A controller gets the results of that query and passes them to a view.
  3. A view has nicely formatted HTML with only simple PHP statements.

The view would do something like (obviously, you need to format to your needs):

<?php if($type1 == $row->getType() ?><h2><?php echo $type1 ?></h2><?php endif ?>
<h3><?php echo $type2 ?></h2>
<ul>
<?php foreach($results as $row) : ?>
  <li><a href="<?php echo $row->getLink() ?>"><?php echo $row->getText() ?></a></li>
<?php endforeach ?>
</ul>

Some of the benefits of this approach are that your database query is more reusable, and the output HTML is much easier to read and edit.

Upvotes: 1

Naveen Kumar
Naveen Kumar

Reputation: 4601

Use <br/> at end of each echo statement.

Upvotes: 0

Related Questions