0

I have a PHP code ready which will fetch data from a mysql database,The PHP code will also display the data in tabular form.

What I want to Achieve:

I have a dummy database named "stackoverflow" with two tables "customer" & "receipt" both of these tables have a field called "date" with datatype as date as well. full schema here

I want some selected entries from these tables to appear in an html table. the entries should be sorted by date in increasing order.

What I have tried and accomplished:

The data is fetched and shown in tabular form successfully, but date wise sorting is partially complete.

output of my result (without php code since jsfiddle doesn't support php)

I have used PHP to compare dates between dates of each table and then show the respective data

<?php
        while( $kb = mysql_fetch_assoc($customer) ){
            customer_data($kb); //function which echos <tr>'s of customer table
                while($cd = mysql_fetch_assoc($reciept)){ //function which echos <tr>'s of reciept table
                    if($cd['date'] <= $kb['date']){
                    reciept_data($cd);
                    break;
                }
            }
        }
?>

Problems I am facing:

The sorting is not exactly like what I wanted. (only some of them are as per increasing date order).

link to my whole project file with database schema and sample data (just import into phpmyadmin) for reference: http://www.mediafire.com/?bcbvkn8uq89ya6n

If you are still confused about what kind of output I am trying to get.I made a table in excel here's the Image: image

I am struggling with this problem since a couple of days. I think this is due to my lack of sql skills?. Any help would be appreciated.

Thanks!

UPDATE:

This is how I pickup data from the tables using php:

$customer = mysql_query("SELECT * FROM customer WHERE customer_name = '$name' ORDER BY date ASC ");

$reciept = mysql_query("SELECT * FROM reciept WHERE name = '$name' ORDER BY date ASC");

After some research, I came across the UNION operator which joins two select statements, and I developed this query:

SELECT DATE, customer_name, grand_total
FROM customer
WHERE customer_name =  'JHON'
UNION ALL 
SELECT DATE, name, recd_amount
FROM reciept
WHERE name =  'JHON'

but I could only select equal no. of columns in both select statements, and how do I order by date?


Rails gem naming convention

Suppose there is Foo service and it has RESTful API. If I want to create wrapper lib for it:

bundle gem foo-api-client

Then it creates following structure:

foo-api-client/Gemfile
foo-api-client/Rakefile
foo-api-client/LICENSE.txt
foo-api-client/README.md
foo-api-client/.gitignore
foo-api-client/foo-api-client.gemspec
foo-api-client/lib/foo/api/client.rb
foo-api-client/lib/foo/api/client/version.rb

I don't need separately api module, and I wonder should I leave it or maybe try to change to: fooapi-client/lib/fooapi/client.rb

Is there some naming convention for this?

4

1 回答 1

0

您应该在 if 语句中使用 '==' 进行相等比较。'=' 将为变量赋值

   if($cd['date'] = $kb['date']){ 

   if($cd['date'] == $kb['date']){

联合订购

 $query = " (SELECT DATE, customer_name, grand_total
FROM customer
WHERE customer_name =  'JHON')
UNION ALL 
    (SELECT DATE, name, recd_amount
     FROM reciept
     WHERE name =  'JHON')
    order by  customer.Date desc"

一个帮助链接 MYSQL UNION ORDER BY

于 2013-10-31T11:44:36.987 回答