0

I have lot of details that should be printed as ordered list with HTML in PHP. If I do it manualy, it takes more coding. Instead of that can any body help me in indexing with loop for the code. Also I request, if any variable is blank, to avoid the output. My PHP code is given below.

<?php
$f_name = strtoupper($_REQUEST['f_name']);
$l_name = strtoupper($_REQUEST['l_name']);
$age = strtoupper($_REQUEST['age']);
       ...............
       ...............
$place = strtoupper($_REQUEST['place']);
$email = strtoupper($_REQUEST['email']);
$web = strtoupper($_REQUEST['web']);
?>

<b> DETAILS:</b> <br />
<ol>
<li> <?php echo  $f_name  ?> </li>
<li> <?php echo  $l_name  ?> </li>
     .....    
<li> <?php echo  $email   ?> </li>
<li> <?php echo  $web     ?> </li>
</ol>
alex
  • 5,516
  • 2
  • 36
  • 60
Mohan
  • 109
  • 1
  • 9

3 Answers3

2

You can print an arbitrary array (like your request array) like the following. It iterates over each value in the given REQUEST array.

<?php
//[...]
echo "<b> DETAILS:</b> <br />";
echo "<ol>";
foreach ($_REQUEST as $key => $value){
   // Print each value
   echo "<li>" . strtoupper($value) . "</li>";
} 
echo "</ol>";
//[...]
?>

More information on how to iterate over array in PHP you can find here or here.

Community
  • 1
  • 1
alex
  • 5,516
  • 2
  • 36
  • 60
  • 1
    Maybe add an `if` condition before printing: `I request, if any variable is blank, to avoid the output`. :) –  Jul 24 '14 at 09:35
  • 1
    Yes that would probably be a good idea, but that @Mohans turn... :) – alex Jul 24 '14 at 09:37
0

First, create an array like :

$data = array(
  'f_name' => strtoupper($_REQUEST['f_name']),
  'l_name' => strtoupper($_REQUEST['l_name']),
  ...
);

Next you use foreach on $data variable :

<ol>
  <?php
  foreach($data as $value)
  {
    if(!empty($value))
    {
      echo '<li>', $value, '</li>';
    }
  }
  ?>
</ol>
Safranil
  • 31
  • 2
  • 1
    Its not neccessary to create an array, `$_REQUEST` is already an array on which you can iterate (like shown in my answer) – alex Jul 24 '14 at 09:32
  • Use the $_REQUEST array directly is not safe, it can display some data like cookies and url parameters... [link](http://php.net//manual/fr/reserved.variables.request.php) – Safranil Jul 24 '14 at 10:04
  • Why should cookie data be unsafe? Creating an array yourself does not save any code compared to the original one. Further more you can excluded (blacklist) some value like from cookies from inside of the loop, if desired. – alex Jul 24 '14 at 12:51
  • The day it will validate the data, it will be easily possible. In addition, we are to keep only the data of interest. For the $_REQUEST array, I mean the fact of direct access without checking the data. – Safranil Jul 24 '14 at 16:52
0
<b> DETAILS:</b> <br />
<ol>
<?php foreach ($_REQUEST as $key => $value){ ?>
   <li><?php echo strtoupper($value); ?></li>
<?php } ?>
</ol>

Simply use this format for parsing the data

Harini Sekar
  • 760
  • 7
  • 14