2
<?php   
$content = "my content";
$parts = array("Part 1 :", "Part 2 :", "Part 3 :","Part 4 :", "Part 5 :");
$name_list = array("Part 1 :\n", "Part 2 :\n", "Part 3 :\n","Part 4 :\n", "Part 5 :");

$email_body = "Product:\n" .str_replace($parts, $name_list, $content);
?>

How I can obtain the same result as in the first script, but I want to import the second array $name_list from my database.

I did the script below, but it doesn't work.

<?php 
$query  = "SELECT * FROM my_table WHERE  id LIKE '%$id%'";
$search = mysql_query($query ) or die(mysql_error());

$name_list = array();
while ($row = mysql_fetch_array($search)) {
    $name_list[] = $row['name'] . ":\n,";
}
$name_list = explode(", ", $name_list);
?>
0

1 Answer 1

2

remove this line $name_list = explode(",",$name_list);, because $name_list already in array, and you can use explode to convert string to array, e.g..

<?php
    $name_list = "Part 1 :\n, Part 2 :\n, Part 3 :\n, Part 4 :\n, Part 5 :";
    $name_list = explode(",", $name_list);
    print_r($name_list);
?>

output

Array
(
    [0] => Part 1 :

    [1] =>  Part 2 :

    [2] =>  Part 3 :

    [3] =>  Part 4 :

    [4] =>  Part 5 :
)

so try below code

<?php 
$query  = "SELECT * FROM my_table WHERE  id LIKE '%$id%'";
$search = mysql_query($query ) or die(mysql_error());


$name_list = array();
while ($row = mysql_fetch_array($search)) {
    $name_list[] = $row['name'].":\n,";
}

$parts = array("Part 1 :", "Part 2 :", "Part 3 :","Part 4 :", "Part 5 :");

$email_body = "Product:\n" .str_replace($parts, $name_list, $content);

?>
Sign up to request clarification or add additional context in comments.

1 Comment

remove this line <code> $name_list = explode(",",$name_list);</code> only, because explode require string, but your passing array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.