Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

php - Get a string out of an array

Good day! I've created a file send.php to send an info about orders to my gmail. Almost everything is ok, but I can't send names of products from cart. I have to send just a string(title) from the DB on mail. How can I get a title from $g which is a row?

<?php

require 'phpmailer/PHPMailer.php';
require 'phpmailer/SMTP.php';
require 'phpmailer/Exception.php';


$g = $_SESSION['cart_list'];
$name = $_POST['name'];
$tel = $_POST['tel'];
$c = $single["title"];


$title = "";
$body = "
<h2></h2>
<b></b> $name<br>
<b></b> $tel<br><br>
<b></b>$c<br>
";

$mail = new PHPMailerPHPMailerPHPMailer();
try {
$mail->isSMTP();   
$mail->CharSet = "UTF-8";
$mail->SMTPAuth   = true;
//$mail->SMTPDebug = 2;
$mail->Debugoutput = function($str, $level) {$GLOBALS['status'][] = $str;};


$mail->Host       = 'smtp.gmail.com'; 
$mail->Username   = '';
$mail->Password   = '';
$mail->SMTPSecure = 'ssl';
$mail->Port       = 465;
$mail->setFrom(''); 


$mail->addAddress('');  


$mail->isHTML(true);
$mail->Subject = $title;
$mail->Body = $body;


if ($mail->send()) {$result = "success";} 
else {$result = "error";}
} 
catch (Exception $e) {
$result = "error";
$status = "{$mail->ErrorInfo}";
}

echo json_encode(["result" => $result, "resultfile" => $rfile, "status" => $status]);

order.php(With products, sum, inlines, submit)

<?php
require_once "db.php";
require_once "header.php";
?> <br>
<div class="text-center">
</div><br>
<main>
<div class="container">
<section class='text-center mb-4'>
<div class='row'>
<?php $s = 0; ?>
<?php foreach( $_SESSION['cart_list'] as $single ) : ?>

<div class="col-xs-12 col-sm-12 col-md-6 col-lg-4 col-xl-4 col-md-offset-1 
mb-5">
<div class='card'>
<div class='view overlay'>
<div class='qwe'>
<img class='card-img-top' src='/img/products/<?php echo $single['img'];? 
>.jpg'>
</div>
 <div class='mask rgba-white-slight'></div>

</div>
<div class='card-body text-center'>
  <div class="grey-text">     
<div class='title'><h5><?php echo $single['title'];?></h5></div>
</div>
<br>
<h5>
  <strong><?php echo $single['brand'];?>
    <span class='badge red mr-1'>
    <?php echo $single['category'];?>
    </span>
  </strong>
</h5>
<h4 class='font-weight-bold blue-text'>
  <strong><?php echo $single['price'];?>$</strong>
</h4>

</div>
</div>
</div>
<?php $s += $single['price']; ?>
<?php $g = $_SESSION['cart_list']  ?>
<?php endforeach; ?>
</div>
</section>


<hr><section class='text-center mb-4 font-weight-bold display-4'>
<label class="count" id="summ"><?php echo $s; ?>$</label>

</section>
    <p>
    <a href="cart.php"><button type="button" class="btn btn-primary"> 
</button></a> <br> <br>
    </p>
<form action="send.php" method="post">
<div class="text-center">
<input type="name" name="name" placeholder="Ваше " aria-label="" required>
<input type="tel" name="tel" placeholder="" aria-label="" required>
<input class="btn btn-primary" type="submit" value="" required>
</div>
</form>
</div>
</main>
</div>
<?php require_once "footer.php"; ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Getting the names is as simple as the following:

$titles = implode(', ', array_column($g, 'title'));

Assuming a dummy cart like this (I included a cyrillic value since you mentioned that in your follow up question):

$g = [
    ['title' => 'This is a title', 'something' => 'else'],
    ['title' => 'Some things', 'something' => 'else'],
    ['title' => 'Word', 'something' => 'else'],
    ['title' => 'Владимир', 'something' => 'else'],
];

the result will be a string:

This is a title, Some things, Word, Владимир

How it works

First we extract just the titles; array_column($g, 'title') will produce an array like this:

array(
    0 => 'This is a title',
    1 => 'Some things',
    2 => 'Word',
    3 => 'Владимир',
)

After that, all that is left is to implode all those array elements into a single string. I used a comma as the glue delimiter, but you could use whatever you need.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...