在另一个循环之后循环变量

时间:2015-04-08 07:19:37

标签: php loops for-loop

我正在尝试循环一个变量,该变量的数据来自另一个数组循环

HERE是循环

for ($i=0; $i < $cid; $i++){
$message .= '<tr>
            <td>'.$pcode.'</td>
            <td>'.$pname.'</td>
            <td>'.$pprice.'</td>
            <td>'.$pqty.'</td>
            </tr>';
}

<td></td>中的变量是从此循环中生成的

foreach($_POST['item_cid'] as $key => $value) {

$cid = mysqli_real_escape_string($connection,$value);
$pcode = mysqli_real_escape_string($connection,$_POST['item_code'][$key]);
$pname = mysqli_real_escape_string($connection,$_POST['item_name'][$key]);
$pprice = mysqli_real_escape_string($connection,$_POST['item_price'][$key]);
$pqty = mysqli_real_escape_string($connection,$_POST['item_qty'][$key]);

}

我不知道如何使这个循环工作,我没有提交错误,

我正确循环吗?

更新后的代码:

<?php
session_start();
require('admin/connect.php');
require('includes/phpmailer/PHPMailerAutoload.php');

ini_set('display_errors',1);
error_reporting(E_ALL);

if (isset($_POST['submit'])) {

$resultArr = array();
$i = 0;
foreach($_POST['item_cid'] as $key => $value) {
//Data for Orders Table
    $cid = intval(mysqli_real_escape_string($connection,$value));
    $pcode = mysqli_real_escape_string($connection,$_POST['item_code'][$key]);
    $pname = mysqli_real_escape_string($connection,$_POST['item_name'][$key]);
    $pprice = mysqli_real_escape_string($connection,$_POST['item_price'][$key]);
    $pqty = mysqli_real_escape_string($connection,$_POST['item_qty'][$key]);

//SOLUTION FROM SANJAY
    $resultArr[$i] = array('cid' => $cid, 'pcode' => $pcode, 'pname' => $pname, 'pprice' => $pprice, 'pqty' => $pqty);
    $i++;

//Data for Customers Table
    $cname = mysqli_real_escape_string($connection,$_POST['item_cname'][$key]);
    $cemail = mysqli_real_escape_string($connection,$_POST['item_cemail'][$key]);
    $cphone = mysqli_real_escape_string($connection,$_POST['item_cphone'][$key]);
    $caddress = mysqli_real_escape_string($connection,$_POST['item_caddress'][$key]);
    $ctotal = mysqli_real_escape_string($connection,$_POST['item_ctotal'][$key]);


//$sql = "INSERT INTO orders (cid, ordprod_code, ordprod_name, ordprod_price, ordprod_qty) VALUES ('$value', '$pcode', '$pname', '$pprice', '$pqty')";
//$sql2 = "INSERT INTO customers (cid, cname, cemail, cphone, caddress, ctotal) VALUES ('$value','$cname','$cemail','$cphone','$caddress','$ctotal')";
    if ($connection->query($sql) === TRUE) {
        echo "Orders record created successfully \n";
    }
//     } else {
//        echo "Error: " . $sql . "<br>" . $connection->error;
//     }

    if ($connection->query($sql2) === TRUE) {
        echo "Customers record created successfully \n";
    }
//    } else {
//        echo "Error: " . $sql2 . "<br>" . $connection->error;


} // close the loop

print_r($resultArr);

//********************************
// START EMAIL FUNCTION
//********************************

$message = '<html><body>';
$message .= '<a href="http://www.example.com/"><img src="http://cdn.example.com/static/images/emailhead.jpg" alt="MY Site" /></a>';
$message .= '<h3>Customer Information:</h3>'; 
$message .= '<table rules="all" border="1" style="border-color: #ccc;" cellpadding="10">'; 
$message .= '<tr><td><strong>CustomerID</strong></td><td>'. $cid .'</td></tr>'; 
$message .= '<tr><td><strong>Name:</strong></td><td>'. $cname .'</td></tr>'; 
$message .= '<tr><td><strong>Email:</strong></td><td>'. $cemail .'</td></tr>'; 
$message .= '<tr><td><strong>Phone:</strong></td><td>'. $cphone .'</td></tr>'; 
$message .= '<tr><td><strong>Address:</strong></td><td>'. $caddress .'</td></tr>'; 
$message .= '</table>';
$message .= '<br />';
$message .= '<h3>Order Details:</h3>';
$message .= '<table rules="all" border="1" style="border-color: #ccc;" cellpadding="10">';
$message .= '<tr style="background:#eee;">
            <td><strong>Product Code</strong></td>
            <td><strong>Product Name</strong></td>
            <td><strong>Product Price</strong></td>
            <td><strong>Product Qty</strong></td>
            </tr>';

// SOLUTION FROM SANJAY

$i = 0;
for ($i=0; $i < ((isset($resultArr[$i]['cid']) && count($resultArr[$i]['cid']) > 0 ) ?$resultArr[$i]['cid'] : 0); $i++)

{
$message .= '<tr>
         <td>'.$resultArr[$i]['pcode'].'</td>
         <td>'.$resultArr[$i]['pname'].'</td>
         <td>'.$resultArr[$i]['pprice'].'</td>
         <td>'.$resultArr[$i]['pqty'].'</td>
         </tr>';
}

$message .= '<tr style="background:#eee;">
            <td colspan="2">Total Amount</td>
            <td>'.$ctotal.'</td>
            <td></td>
            </tr>';             
$message .= '</table>';
$message .= '</body></html>'; 

$pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i"; 
if (preg_match($pattern, $cemail)) { 
$cleanedFrom = $cemail; 
} else { 
return "The email address you entered was invalid. Please try again!"; 
} 

//***************************************
// SEND MAIL USING GMAIL SMTP SERVER
//***************************************
$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';                       // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'example@gmail.com';                   // SMTP username
$mail->Password = 'mypassword';               // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted
$mail->Port = 587;                                    //Set the SMTP port number - 587 for authenticated TLS
$mail->setFrom(''.$cemail.'', ''.$cname.'');     //Set who the message is to be sent from
$mail->addReplyTo(''.$cemail.'', ''.$cname.'');  //Set an alternative reply-to address
$mail->addAddress('owner@example.com', 'YAQOOB');  // Add a recipient
$mail->addAddress('owner@example.com');               // Name is optional
$mail->addCC('');
$mail->addBCC('');
$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
$mail->addAttachment('/user/file.doc');         // Add attachments
$mail->addAttachment('/images/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'New order arrived from CustomerID #'.$cid.'';
$mail->Body    = ''.$message.'';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

if(!$mail->send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}

echo 'Message has been sent';

} // Data Inserted & Emailed Close IF Statement 


session_destroy();  
?>

2 个答案:

答案 0 :(得分:3)

试试这个:

$message = "";
$resultArr = array();
$i = 0;
foreach($_POST['item_cid'] as $key => $value) 
{
   $cid = mysqli_real_escape_string($connection,$value);
   $pcode = mysqli_real_escape_string($connection,$_POST['item_code'][$key]);
   $pname = mysqli_real_escape_string($connection,$_POST['item_name'][$key]);
   $pprice = mysqli_real_escape_string($connection,$_POST['item_price'][$key]);
   $pqty = mysqli_real_escape_string($connection,$_POST['item_qty'][$key]);
   $resultArr[$i] = array('cid' => $cid, 'pcode' => $pcode, 'pname' => $pname, 'pprice' => $pprice, 'pqty' => $pqty);
   $i++;
}
$i = 0;
for ($i=0; $i < (isset($resultArr[$i]['cid']) && $resultArr[$i]['cid'] ?$resultArr[$i]['cid'] : 0); $i++)
{
$message .= '<tr>
         <td>'.$resultArr[$i]['pcode'].'</td>
         <td>'.$resultArr[$i]['pname'].'</td>
         <td>'.$resultArr[$i]['pprice'].'</td>
         <td>'.$resultArr[$i]['pqty'].'</td>
         </tr>';
}

答案 1 :(得分:2)

这样的事情怎么样?

$message = "";
foreach($_POST['item_cid'] as $key => $value) 
{
   $cid = mysqli_real_escape_string($connection,$value);
   $pcode = mysqli_real_escape_string($connection,$_POST['item_code'][$key]);
   $pname = mysqli_real_escape_string($connection,$_POST['item_name'][$key]);
   $pprice = mysqli_real_escape_string($connection,$_POST['item_price'][$key]);
   $pqty = mysqli_real_escape_string($connection,$_POST['item_qty'][$key]);

   for ($i=0; $i < $cid; $i++)
   {
   $message .= '<tr>
            <td>'.$pcode.'</td>
            <td>'.$pname.'</td>
            <td>'.$pprice.'</td>
            <td>'.$pqty.'</td>
            </tr>';
   }
}