0

i working on Authorize.net gateway i need to create below array for itemized bill

$sql= "select *  from shop where cusid = '1'";
 $sqlexc= mysql_query($sql) or die(mysql_error());

$line_items = array();
while ($title = mysql_fetch_array($sqlexc)) {
    $line_items[] = ('Coupon',' $title[6]', '1', '0.99', 'Y');
}

how to create below kind of array

/*    $line_items = array(
    "Coupon1','description','2','10.99','Y'",
    "Coupon2','description','2','10.99','Y'",
    "Coupon3','description','2','10.99','Y'",);*/
2
  • You may want to re-read the array you want to output, don't think that's what you mean. Commented Jun 30, 2012 at 2:43
  • Your array doesn't make sense. What are your double quotes, single quotes and commas mean? Is this an array of strings? Or is this an array of objects that contain 4 properties? Commented Jun 30, 2012 at 2:47

2 Answers 2

0

uh.. if you want it to be exactly the same...

$i=0;
$line_items = array();   
while ($title = mysql_fetch_array($sqlexc)) {
    $line_items[] = "Coupon" . ++$i . "', '" . $title[6] . "', '2', '10.99', 'Y'";
}

However I would use mysql_fetch_assoc over mysql_fetch_array (actually I would switch to PDO as mysql is deprecated)

2
  • how to add comma to the last records like $line_items = array( "Coupon1','description','2','10.99','Y'", "Coupon2','description','2','10.99','Y'", "Coupon3','description','2','10.99','Y'",); Commented Jun 30, 2012 at 4:36
  • You don't need one.. That is purely optional. Look at php.net/manual/en/language.types.array.php Look at "The comma after the last array element is optional and can be omitted." Commented Jun 30, 2012 at 4:49
0
$line_items = array();
while ($title = mysql_fetch_array($sqlexc)) {
    $line_items[] = $title;
}
foreach ($line_items as $row){
    echo $row[0]." ".$row[1]." ".$row[2]." ".$row[3]."<br>";
}
0

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.