Jump to content

MySQL with PHP Complicated Query

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
j.smith1981

j.smith1981

    Newbie

  • Members
  • PipPip
  • 10 posts
I have probably one of the most advanced feature problems I have ever encountered here, its not really an error though as such its more making it concise for a business process.

Basically we have an ecommerce system where I am employed, that I have been assigned the task of developing solutions for, beyond what the out of the box solution can provide.

The pick sheets for the warehouse it currently provides are shoddy, they dont give any details for the customer whos ordered that product or anything like that, we currently use another one for the business to business side thats adequate enough for a user just with little amendments.

Enough of the waffle in describing the situation we are in or where our aims are, here's the SQL:

SELECT CONCAT(DATE_FORMAT(FROM_UNIXTIME(A3.date), '%e/%c/%Y'), ' ' ,DATE_FORMAT(FROM_UNIXTIME(A3.date), '%H:%i')) AS order_date, -- Displays date and time in 24hr format


-- Order information

A1.orderid AS orderid,


A3.status AS status,


-- Profile


A5.login AS username,


A5.title AS title,


A5.b_firstname AS f_name,  -- Billing but using it as first name


A5.b_lastname AS l_name, -- Billing but using it as last name


-- Profile Billing

A5.b_address AS b_address,


A5.b_city AS b_city,


A5.b_zipcode AS b_pcode,


-- Profile Shipping

A5.s_address AS s_address,


A5.s_city AS s_city,


A5.s_zipcode AS s_pcode,



-- Products line

A4.value AS adventcode,


A1.product AS descr,


A1.amount AS qty,


-- Order notes

A3.customer_notes AS cust_notes





-- A1.product, A1.amount AS qty, DATE_FORMAT(FROM_UNIXTIME(A3.date), '%e/%c/%Y') AS order_date

FROM xcart_order_details A1

LEFT JOIN xcart_pricing A2 ON A1.productid = A2.productid

LEFT JOIN xcart_orders A3 ON A1.orderid = A3.orderid

LEFT JOIN xcart_extra_field_values A4 ON A1.productid = A4.productid

LEFT JOIN xcart_customers A5 ON A3.login = A5.login

WHERE A2.membershipid = '2' AND A3.status = 'P' AND  A4.fieldid = '3'

ORDER BY A5.login AND A1.orderid ASC

Quite a long one I know.

Basically what its doing at the moment when i read it out in PHP.

I have recreated the system invoice and replaced that with Pick List for the header, in standard HTML using a php file.

But if a user was to order 2 seperate items say HP 11 Black 69ml ink cartridge and a HP 69 whatever cartridge (2 different items).

Its listing these as different pick lists (2 peices of paper), with different items only with the same order no.

Therefore is there any somewhat easy way of coding probably in PHP a function or something that makes it keep the orders of the same number together listing the products ordered in table rows?

This is just beyond me at the moment cant seem to work out how to do this or is there an example someone could bring up, I can then edit that to get what we require out of it.

I do have some structure to it though it would involve 2 primary parts but in 3 sections.

Header (prints out the customers id or username rather, order no, and then billing and shipping information respectively) and then the products they have ordered from the same order no, and finally a footer of their notes for that order, which would include something like 'If I am not here blah blah blah'.

Can anyone help?

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
So basically, you just want one piece of paper being printed out for the picklist, correct? Looking at your SQL it appears to pull all elements pulled out by a logged in customer. Given that, it should be easy to concat all orders into one pickslip. However, I don't think the problem is in your SQL but in your printing method. Can you post your PHP code, please?

#3
j.smith1981

j.smith1981

    Newbie

  • Members
  • PipPip
  • 10 posts
I will yes.

We actually have one working but I am finding it hard getting the cust_notes field to display, thought I could try and simplify the whole thing and make it allot easier for myself to understand should I ever have to change it.

I will go over the scenario as it where again, at the moment there's multiple pick lists, with the same order number on them, if the customer was to order multiple items say 5, 5 picklists will appear and yes it probably would be some concatination on the orderid I would suspect.

You never would have the same orderid for multiple customers just wouldnt happen.

I never thought about that.

Here's the code anyhow of what I already have:


<?php


$host = 'localhost'; // Sets the variable host as a literal no parsing for db connections

$user = 'root'; // username

$passwd = ''; // db password (abbreviated to unix password command)

$dbname = 'xcart';


$conn = mysql_connect($host, $user, $passwd) or die(); // makes a connection to the MySQL server


mysql_select_db($dbname); // Selects the actual database itself


// SQL query to retrieve picking ticket information

$db_query = "SELECT CONCAT(DATE_FORMAT(FROM_UNIXTIME(A3.date), '%e/%c/%Y'), ' ' ,DATE_FORMAT(FROM_UNIXTIME(A3.date), '%H:%i')) AS order_date, -- Displays date and time in 24hr format


-- Order information

A1.orderid AS orderid,


A3.status AS status,


-- Profile


A5.login AS username,


A5.title AS title,


A5.b_firstname AS f_name,  -- Billing but using it as first name


A5.b_lastname AS l_name, -- Billing but using it as last name


-- Profile Billing

A5.b_address AS b_address,


A5.b_city AS b_city,


A5.b_zipcode AS b_pcode,


-- Profile Shipping

A5.s_address AS s_address,


A5.s_city AS s_city,


A5.s_zipcode AS s_pcode,



-- Products line

A4.value AS adventcode,


A1.product AS descr,


A1.amount AS qty,


-- Order notes

A3.customer_notes AS cust_notes





-- A1.product, A1.amount AS qty, DATE_FORMAT(FROM_UNIXTIME(A3.date), '%e/%c/%Y') AS order_date

FROM xcart_order_details A1

LEFT JOIN xcart_pricing A2 ON A1.productid = A2.productid

LEFT JOIN xcart_orders A3 ON A1.orderid = A3.orderid

LEFT JOIN xcart_extra_field_values A4 ON A1.productid = A4.productid

LEFT JOIN xcart_customers A5 ON A3.login = A5.login

WHERE A2.membershipid = '2' AND A3.status = 'P' AND  A4.fieldid = '3'

ORDER BY A5.login AND A1.orderid ASC";


$result = mysql_query($db_query);

$tempid = "";

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

{ // WILL REPEAT EACH ORDER IN SEQUENCE

#$tempuser = $row['username'];


if ($tempid != "" && $tempid !=$row["orderid"]) {


?>

</table>

<!-- END OF PRODUCTS -->






	</td>

</tr>



<tr>

	<td colspan="3">


	<br />

	<br />

	<table cellspacing="0" cellpadding="0" width="100%" border="0">


	<tr>

		<td align="center"><font style="FONT-SIZE: 14px; FONT-WEIGHT: bold;">Customer notes</font></td>

	</tr>


	</table>


	<table cellspacing="0" cellpadding="10" width="100%" border="1">

	<tr>

		<td style="height:50px;"></td> <!-- ORDER NOTES HERE -->

	</tr>

	</table>

	</td>

</tr>




</table>


<br class="pagebreak" />

<?php

}

if ($tempid == "" || $tempid !=$row["orderid"]) {




?>

        <table cellspacing="0" cellpadding="0" width="600" bgcolor="#ffffff">


<tr>

	<td>

	<table cellspacing="0" cellpadding="0" width="100%" border="0">

	<tr>

	<tr>

		<td valign="top"><br /><br /></td>

	</tr>

		<td width="100%">

		<table cellspacing="0" cellpadding="2" width="100%">

		<tr>


			<td valign="top">

<font style="FONT-SIZE: 28px"><b style="text-transform: uppercase;">Invoice</b></font>

<br /><br />

<b>Date: </b><?=$row['order_date'] ?><br /><b>Order id:</b><?=$row['orderid'] ?><br /><b>Order status:</b> Complete<br />

<b>Payment method:</b><br />SEC Pay (Pay Point) (SECPay)<br /><b>Delivery method:</b><br />Royal Mail

			</td>


			<td valign="bottom" align="right">

<b>Cartridge World Ltd</b><br />

Unit A3, Hornbeam Square West, Hornbeam Park, Harrogate<br />

HG2 8PA, England<br />

United Kingdom (Great Britain)<br />

Tel: 01423 878520<br />Fax: 01423 878521<br />E-mail: customerservice@cartridgeworld.co.uk<br /><br />

<br />

			</td>


		</tr>

		</table>

		</td>

	</tr>

	</table>

	<table cellspacing="0" cellpadding="0" width="100%" border="0">

	<tr>

		<td></td>

	</tr>


	<tr>

		<td bgcolor="#000000"><img height="2" src="/skin1/images/spacer_black.gif" width="100%" alt="" /></td>

	</tr>

	<tr>

		<td><img height="2" src="/skin1/images/spacer.gif" width="1" alt="" /></td>

	</tr>

	</table>

	<br />

	<table cellspacing="0" cellpadding="0" width="45%" border="0">


	<tr>

		<td><b>Title:</b></td> <td><?=$row['title'] ?></td>

	</tr>

	<tr>

		<td nowrap="nowrap"><b>First Name:</b></td>

		<td><?=$row['f_name'] ?></td>


	</tr>

	<tr>

		<td nowrap="nowrap"><b>Last Name:</b></td>

		<td><?=$row['l_name'] ?></td>

	</tr>

	<tr>

		<td><b>Phone:</b></td>


		<td><?=$row['phone'] ?></td>

	</tr>

	<tr>

		<td><b>E-mail:</b></td>

		<td><?=$row['email'] ?></td>

	</tr>

	</table>

	<br />


	<table cellspacing="0" cellpadding="0" width="100%" border="0">

	<tr>

		<td width="45%" height="25"><b>Billing Address</b></td>

		<td width="10%"> </td>

		<td width="45%" height="25"><b>Ship To Alternative Address</b></td>

	</tr>

	<tr>


		<td bgcolor="#000000" height="2"><img height="2" src="/skin1/images/spacer_black.gif" width="100%" alt="" /></td>

		<td><img height="2" src="/skin1/images/spacer.gif" width="1" alt="" /></td>

		<td bgcolor="#000000" height="2"><img height="2" src="/skin1/images/spacer_black.gif" width="100%" alt="" /></td>

	</tr>

	<tr>

		<td colspan="3"><img height="2" src="/skin1/images/spacer.gif" width="1" alt="" /></td>

	</tr>

	<tr>

		<td>


		<table cellspacing="0" cellpadding="0" width="100%" border="0">

		<tr>

			<td><b>Address:</b> </td>

			<td><?=$row['b_address'] ?></td>

		</tr>

		<tr>

			<td><b>City:</b> </td>


			<td><?=$row['b_city'] ?></td>

		</tr>

		<tr>

			<td><b>Postcode:</b> </td>

			<td><?=$row['b_pcode'] ?></td>

		</tr>

		</table>


		</td>

		<td> </td>

		<td>

		<table cellspacing="0" cellpadding="0" width="100%" border="0">

		<tr>

			<td><b>Address:</b> </td>

			<td><?=$row['s_address'] ?></td>


		</tr>

		<tr>

			<td><b>City:</b> </td>

			<td><?=$row['s_city'] ?></td>

		</tr>

		<tr>

			<td><b>Postcode:</b> </td>


			<td><?=$row['s_pcode'] ?></td>

		</tr>

		</table>

        </td>

	</tr>


<tr>

	<td colspan="3"> </td>

</tr>


<tr>

	<td width="45%" height="25"><b></b></td>

	<td colspan="2" width="55%"> </td>

</tr>

<tr>

	<td bgcolor="#000000" height="2"><img height="2" src="/skin1/images/spacer_black.gif" width="100%" alt="" /></td>

	<td colspan="2" width="55%"><img height="2" src="/skin1/images/spacer.gif" width="1" alt="" /></td>

</tr>

<tr>

	<td colspan="3"><img height="2" src="/skin1/images/spacer.gif" width="1" alt="" /></td>

</tr>


<tr>

	<td><table cellspacing="0" cellpadding="0" width="100%" border="0">

<tr valign="top">

	<td><b></b></td>

   	<td></td>

</tr>

</table></td>

<td colspan="2" width="55%"> </td>

</tr>





	</table>

	<br />

	<br />


<!-- TABLE PRODUCTS :: header -->

<table cellspacing="0" cellpadding="0" width="100%" border="0">


<tr>

<td align="center"><font style="FONT-SIZE: 14px; FONT-WEIGHT: bold;">Products ordered</font></td>

</tr>



</table>

<!-- END OF HEADER -->


<!-- products -->

<table cellspacing="0" cellpadding="3" width="100%" border="1">


<tr>

<th width="60" bgcolor="#cccccc">Advent Code</th>

<th bgcolor="#cccccc">Product</th>

<th nowrap="nowrap" width="100" bgcolor="#cccccc">Quantity Ordered</th>

<!-- <th nowrap="nowrap" width="100" bgcolor="#cccccc" align="center">Quantity Avail (Advent Data Sheet)</th> -->

</tr>


<?php

}

#while($row = mysql_fetch_array($result, MYSQL_ASSOC))

#{

?>


<tr>


<td align="center"><?=$row['adventcode'] ?></td>

<td><font style="FONT-SIZE: 15px"><?=$row['descr'] ?></font>

</td>

<td align="center"><?=$row['qty'] ?></td>



<!-- <td align="right" nowrap="nowrap"><span style="WHITE-SPACE: nowrap">Not completed yet (in progress)</span><br /></td> -->


</tr>


<?php

#}



$tempid = $row["orderid"];


}



if ($tempid != "" ) {



?>

</table>

<!-- END OF PRODUCTS -->






	</td>

</tr>



<tr>

	<td colspan="3">


	<br />

	<br />

	<table cellspacing="0" cellpadding="0" width="100%" border="0">


	<tr>

		<td align="center"><font style="FONT-SIZE: 14px; FONT-WEIGHT: bold;">Customer notes</font></td>

	</tr>


	</table>


	<table cellspacing="0" cellpadding="10" width="100%" border="1">

	<tr>

		<td style="height:50px;"></td> <!-- PUT ORDER NOTES HERE -->

	</tr>

	</table>

	</td>

</tr>




</table>


<br class="pagebreak" />

<?php

}

 ?>


The above was coded with the 2 of us working together just wanted to see if there was a more simple approach.

Many thanks,
Jeremy.