Question

The output of the following code gives: MilkYoghurtCheese AppleOrangeBanana. Being new to php, I am having difficulty producing these elements as dot points in a list, rather than combined words in a sentence.

Is there a way to put these into dot points?

<?php 

$newArray =array();
$DairyArray=array();
$FruitArray=array();

$DairyArray= array( 
        '1' => 'Milk',
        '2' => 'Yoghurt',
    '3' => 'Cheese',

);
 $FruitArray = array( 
        '9' => 'Apple'
       '10' => 'Orange',
       '11' => 'Banana',

 if ($_POST['Dessert'] == 'Yes') //This represents the condition that a checkbox is
 checked

 {

  $newArray = array_merge($DairyArray, $FruitArray);

  foreach ($newArray as $key => $value)

  {
     echo $value >;
  }

 }

What I am trying to achieve as output:

• Milk • Yogurt • Cheese • Apple • Orange • Banana

Any help would be greatly appreciated, and I will try to see if I can answer some of your questions too.

Thanks

Andrew

Was it helpful?

Solution

The echo command only outputs the raw data in the variable (in your case array). You want to pimp it out a bit for a nice display like this:

$newArray = array_merge($DairyArray, $FruitArray);
echo "<ul>";
foreach ($newArray as $key => $value)
{ 
   echo "<li>" . $value . "</li>";
}
echo "</ul>";

Keep in mind that HTML by default requires explicit instructions on how to display something, and omits line breaks, more than one space at a time and many other things. When we deal with data in variables, for the most part, they are generally the raw data without any formatting. You will always need to make it formatted nicely for your web pages - or get used to horrible text only pages :)

OTHER TIPS

Use implode instead of foreach

<?php

$DairyArray= array( 
        '1' => 'Milk',
        '2' => 'Yoghurt',
    '3' => 'Cheese'

);

$FruitArray = array( 
        '9' => 'Apple',
       '10' => 'Orange',
       '11' => 'Banana'
);

$newArray = array_merge($DairyArray, $FruitArray);

echo ".".implode(".",$newArray);
?>

Also if you want to use bullet then you can use

<?php 

echo "<ul><li>";
echo implode("</li><li>",$newArray);
echo "</li></ul>";

?>

<style>
ul li { float:left; margin-right:25px; }
</style>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top