Jump to content

Recommended Posts

Ok, I am trying to join 3 tables together to retrieve information to display on an invoice. I am new to joining tables and I keep confusing myself.

 

Heres what I've got:

 

customers table - has customerid, name, address, city, state, zip, and country

 

orders table - has orderid, customerid, amount, date, order_status, ship_name, ship_address, ship_city, ship_state, ship_zip, ship_country, c_phone, c_email, card_type, card_number, card_month, card_year, card_name

 

order_items table - has orderid, isbn, item_price, and quanity

 

 

Here's what i'm trying to do:

 

display address

 

display shipping address

 

display orders

 

display credit card information

 

Thank you,

 

`Jake

Link to comment
https://forums.phpfreaks.com/topic/52962-solved-creating-invoice/
Share on other sites

so would i do three seperate querys under one connection?

 

Like:

$conn = db_connect();

//then

$query = select * from customers where

//then do a second query??

$query = select * from orders where

//and a third??

$query = select * from order_items

//??? I'm not sure exactly how to separate them???

Not jumbled up, that is the structure of the arrays when printed.  But I now see a flaw in the logic I gave you, try this instead:

 

<?php
$query = "SELECT * FROM orders WHERE whatever";
$result = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {
     $rows[] = $row; // added the [] =) my bad.
}

echo '<pre>' . print_r($rows) . '</pre>';

?>

ok ok, i see

 

now it returns this

 

Array ( [0] => Array ( [orderid] => 14 [isbn] => 30-015 [item_price] => 15.82 [quantity] => 1 ) )

1

 

with this code:

 

$query = "SELECT * FROM order_items WHERE orderid = '$coolid'";
$result = mysql_query($query, $db) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {
   $rows[] = $row; 

}


echo '<pre>' . print_r($rows) . '</pre>';


That should answer the question

 

What do I do when I am retrieving multible rows, like from the order items table where I have 5 rows where the orderid column equals 14 ?

 

Should it not? If there were multiple rows in that table with that id it would have been returned in that array.

I'm sorry, I'm a little slow.

 

How do I implement that in with this following code?

 

$query = "SELECT * FROM order_items WHERE orderid = '$coolid'";
$result = mysql_query($query, $db) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {
   $rows[] = $row; 

}

<?php

$query = "SELECT * FROM order_items WHERE orderid = '$coolid'";
$result = mysql_query($query, $db) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {
   $rows[] = $row;
}

$i = 1;

foreach($rows as $rowid => $rowarray) {
echo "<b>---- Row $rowid ----</b><br />\n\n";
foreach($rowarray as $rowfield => $rowvalue) {
  echo " $rowfield = $rowvalue<br />\n";
}
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.