Home > Enterprise >  Split a comma delimited string into an array and display on html page
Split a comma delimited string into an array and display on html page

Time:10-29

basically I'm making a page where the information from mySQL database will be displayed. I have a column named topics in the database where the string (VARCHAR) goes like this:

Marketing, Business, Law, Medicine, ...

I'm trying to break up this string after a comma and display them in a single line one by one like this:

<h6>Marketing</h6>
<h6>Business</h6>
<h6>Law</h6>
<h6>Medicine</h6>
<h6>...</h6>

I already have a loop for other rows and I'm not sure if it's possible to make a loop in the loop, I'm not even sure if what i'm trying to achieve is possible but I belive it is. Here goes my full PHP code:

<?php
include_once '../handlers/db_conn.php';

$sql = $conn->prepare("SELECT * FROM esc WHERE hosting_country = ?");
$sql->bind_param("s", $hosting_country);

$hosting_country = 'Poland';
$sql->execute();

$result = $sql->get_result();
$resultCheck = mysqli_num_rows($result);

if ($resultCheck > 0) {

} else {
  echo '<p >Nothing Found</p>';
}

while($escrow = $result->fetch_assoc()) {

?>

<div >
  <div >
    <h2><?php echo $escrow['project_name'] ?></h2>
    <i ></i>
    <h3><?php echo $escrow['hosting_country'] ?></h3>
    <i ></i>
    <h3><?php echo $escrow['start_date'] ?> - <?php echo $escrow['end_date'] ?></h3>
    <h4 ><?php echo $escrow['objectives'] ?></h4>
    <h5>Topics</h5>
    <h6><?php echo $escrow['topics'] ?></h6>
    <hr>
    <a href="#">Read more</a>
  </div>
</div>

<?php
}
?>

I'm wondering if it's possible to create another loop in this loop for element, separate this string after a comma and display one by one in tag? Any help would be greatly appreciated. Thanks.

EDIT

This is what I'm trying to achieve:

This is how I want it to look

CodePudding user response:

Quite simple (and yes, you may have as many nested loops inside your code as you want):

Use explode to split your string, then loop over it.

<!-- inside your loop... -->
<h5>Topics</h5>
<?php foreach(explode(", ", $escrow["topics"]) as $topic) { ?>
<h6><?= $topic ?></h6>
<?php } ?>

CodePudding user response:

You can do so with a simple explode by comma, trim (to remove spaces) and join (could be empty, but a newline is nicer).

$topics = 'Marketing, Business, Law, Medicine';
echo join(PHP_EOL, array_map(fn($topic) => '<h6>'. trim($topic) . '</h6>', explode(',' , $topics)));

Output

<h6>Marketing</h6>
<h6>Business</h6>
<h6>Law</h6>
<h6>Medicine</h6>

CodePudding user response:

You could split into an array and then map the array into containing tags:

function h6($n)
{
    return "<h6>$n</h6>";
}

$in = "Medicine,Arts,Law";
$components = preg_split("/,/", $in);
$html = implode(array_map('h6', $components));
echo($html);

CodePudding user response:

Use the following code to explode

<!DOCTYPE html>
<html>
<body>

<?php
$str = "Hello,World!,Beautiful,Day!";
$arr = explode (",", $str);//;('Hello','World!','Beautiful','Day!');
foreach ($arr as $item) {
    echo "<h6>".$item."</h6>";
}


?>

</body>
</html>

'''

  • Related