Jump to content


Photo

Ouputting Multiple lines with a loop


  • Please log in to reply
5 replies to this topic

#1 dootch

dootch
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 10 March 2006 - 06:11 PM

I currrently dump an orders .txt file of data to a table and want to output them to view in a browser I have that part. My problem is that the if an order has multiple line items is puts it on a different row in the table ex
order_id 123456, sku 1234, buyer John Doe
order_id 123456, sku 5678, buyer John Doe
So when I output the data it displays it as two records:
John Doe
Order #123456
sku 1234

John Doe
Order#123456
sku 5678

where I only want it to be one with the two parts listed
John Doe
Order#123456
sku 1234
sku 5678

I am thinking I need a loop within a loop or something to that effect. Any idea's?? Here's a snippet of the code so far
$i=0;
while ($i < $num) {
$name=mysql_result($result,$i,"recipient_name");
$address1=mysql_result($result,$i,"ship_address_1");
$address2=mysql_result($result,$i,"ship_address_2");
$city=mysql_result($result,$i,"ship_city");
$state=mysql_result($result,$i,"ship_state");
$zip=mysql_result($result,$i,"ship_postal_code");
$order=mysql_result($result,$i,"order_id");
$email=mysql_result($result,$i,"buyer_email");
$qty=mysql_result($result,$i,"quantity_purchased");
$sku=mysql_result($result,$i,"sku");
$product=mysql_result($result,$i,"product_name");
?>

<table >

<tr> 
<td><font face="Arial, Helvetica, sans-serif"><? echo "$name"; ?></font></td>
</tr><tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$address1"; ?></font></td>
</tr><tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$address2"; ?></font></td>
</tr><tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$city, $state $zip"; ?></font></td>
</tr><tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$order"; ?></font></td>
</tr><tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$email"; ?></font></td>
</tr><tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$qty $sku $product"; ?></font></td>
</tr>


#2 mem0ri

mem0ri
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 10 March 2006 - 08:56 PM

Yes...you need a loop within a loop...

...you will need to acquire all records from 1 customer with unique SKUs...

...then...loop an output of each SKU

...then...move on to the next customer.

#3 beamerrox

beamerrox
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts
  • LocationGrimshaw, Alberta, Canada

Posted 11 March 2006 - 02:57 AM

while($sys = mysql_fetch_assoc(mysql_query(""))){
$name = $sys['recipient_name'];
$address1 = $sys['ship_address_1'];
$address2 = $sys['ship_address_2'];
$city = $sys['ship_city'];
$state = $sys['ship_state'];
$zip = $sys['ship_postal_code'];
$order = $sys['order_id'];
$email = $sys['buyer_email'];
$qty = $sys['quantity_purchased'];
$sku = $sys['sku'];
$product = $sys['product_name'];

<table >
<tr> 
<td><font face="Arial, Helvetica, sans-serif"><? echo "$name"; ?></font></td>
</tr><tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$address1"; ?></font></td>
</tr><tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$address2"; ?></font></td>
</tr><tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$city, $state $zip"; ?></font></td>
</tr><tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$order"; ?></font></td>
</tr><tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$email"; ?></font></td>
</tr><tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$qty $sku $product"; ?></font></td>
</tr>

}

in my opinion this works better and more efficient.

#4 dootch

dootch
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 14 March 2006 - 11:55 PM

OK I am totally lost here I am trying to add the inner loop but its wont work here is what I have. Any idea's?
<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM orders";
$result=mysql_query($query);
$num=mysql_numrows($result);

// set up query to get all products ordered:
$query2="select sku, quantity_purchased, products_name from orders where order_id_id=".$order;
$result2=mysql_query($query2);// inner loop result set

while($inner_row = mysql_fetch_assoc($result2);

$i=0;
while ($i < $num) {
$name=mysql_result($result,$i,"recipient_name");
$address1=mysql_result($result,$i,"ship_address_1");
$address2=mysql_result($result,$i,"ship_address_2");
$city=mysql_result($result,$i,"ship_city");
$state=mysql_result($result,$i,"ship_state");
$zip=mysql_result($result,$i,"ship_postal_code");
$order=mysql_result($result,$i,"order_id");
$email=mysql_result($result,$i,"buyer_email");
$qty= $inner_row ['quantity_purchased'];
$sku= $inner_row ['sku'];
$product= $inner_row ['product_name'];
?>

<table >

<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$name"; ?></font></td>
</tr><tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$address1"; ?></font></td>
</tr><tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$address2"; ?></font></td>
</tr><tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$city, $state $zip"; ?></font></td>
</tr><tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$order"; ?></font></td>
</tr><tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$email"; ?></font></td>
</tr><tr>

<td><font face="Arial, Helvetica, sans-serif"><? echo "$qty $sku $product"; ?></font></td>
</tr>

<?
++$i;
}
echo "</table>";


?>


#5 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 15 March 2006 - 07:42 AM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Any idea's? [/quote]

Use XML and the built in XML Parser..

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#6 dootch

dootch
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 15 March 2006 - 10:13 PM

I have condensed my code a bit to get anything to work this bit shows the individual order_id's but no product info It is not passing the variable. Still trying but I don't really know enough to get it to work. Please help.
<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="select distinct order_id from orders ";
$result=mysql_query($query);
$num=mysql_numrows($result);

$query1="select sku, quantity_purchased, product_name from orders where order_id= " .$result. "";
$result1=mysql_query($query1);
$num1=mysql_numrows($result1);

$i=0;
while ($i < $num) {
$order=mysql_result($result,$i,"order_id");
$qty=mysql_result($result1,$i,"quantity_purchased");
$sku= mysql_result($result1,$i,"sku");
$product= mysql_result($result1,$i,"product_name");
?>

<table >
<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$order"; ?></font></td>
</tr><tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$qty $sku $product"; ?></font></td>
</tr>
<P CLASS="breakhere">
<?
++$i;
}
echo "</table>";

?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users