vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   REQUEST: Query from 4 tables with a short query line (https://vborg.vbsupport.ru/showthread.php?t=182984)

ameenov 06-19-2008 06:53 PM

REQUEST: Query from 4 tables with a short query line
 
I'm developing an accounting system & i would like to query the following:

1- Select the order from the table (orders) filed (id), by passing the id across the browser using ($_GET['id']). then output the result with the fields (userid,items). Note: items = is a text file with a list of items which are coma separated.
2- calculate the total of the items paid from (images) field (price). using the (items) from the (orders) table.
3 - Select & output the name of the client from table (users) filed name (fname).
4- Select & output the the total of amount paid by the client from table (orders_receipts) filed name (amount).
------------------------
I made a query which worked but it is very long, i want to make it short query.

#DATABASE
PHP Code:



    
-- phpMyAdmin SQL Dump
    
-- version 2.8.2
    
-- 
    -- 
Hostlocalhost
    
-- Generation TimeJun 192008 at 10:53 PM
    
-- Server version5.0.22
    
-- PHP Version5.1.4
    
-- 
    -- 
Database: `account`
    -- 

    -- --------------------------------------------------------

    -- 
    -- 
Table structure for table `images`
    -- 

    
CREATE TABLE `images` (
      `
imageidint(11NOT NULL auto_increment,
      `
catidint(11NOT NULL default '0',
      `
useridint(11NOT NULL default '0',
      `
comp_idint(11NOT NULL default '0',
      `
namevarchar(255NOT NULL default '',
      `
codevarchar(255) default NULL,
      `
detailstext NOT NULL,
      `
pricedecimal(11,3) default NULL,
      `
quantityint(11NOT NULL default '0',
      `
quantity_storeint(11NOT NULL,
      `
dateint(25NOT NULL default '0',
      `
activetinyint(1NOT NULL default '0',
      `
hitsint(11NOT NULL default '0',
      `
imagevarchar(255) default NULL,
      `
thumbnailvarchar(255) default NULL,
      `
typetinyint(1NOT NULL default '0',
      `
widthint(11NOT NULL default '0',
      `
heightint(11NOT NULL default '0',
      `
sizeint(11NOT NULL default '0',
      `
buy_typetinyint(1NOT NULL default '0',
      `
buy_timeint(25) default NULL,
      `
timeint(25NOT NULL default '0',
      `
expire_dateint(25NOT NULL,
      `
periodint(25) default NULL,
      
PRIMARY KEY  (`imageid`)
    ) 
ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=;

    -- 
    -- 
Dumping data for table `images`
    -- 

    
INSERT INTO `imagesVALUES (1000'????''54''???????? ??? ...''25.000'33453000NULLNULL00000NULL00NULL);
    
INSERT INTO `imagesVALUES (2000'???????''dgfd''''200.000'33453000NULLNULL00000NULL00NULL);
    
INSERT INTO `imagesVALUES (3000'?????''none''''100.000'150120000NULLNULL00000NULL00NULL);

    -- --------------------------------------------------------

    -- 
    -- 
Table structure for table `orders`
    -- 

    
CREATE TABLE `orders` (
      `
idint(11NOT NULL auto_increment,
      `
useridint(11NOT NULL default '0',
      `
itemstext NOT NULL,
      `
amountdecimal(11,3NOT NULL,
      `
discountdecimal(11,3NOT NULL,
      `
trans_idbigint(25NOT NULL,
      `
payment_idbigint(25NOT NULL,
      `
messagetext NOT NULL,
      `
dateint(25NOT NULL default '0',
      
PRIMARY KEY  (`id`)
    ) 
ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=;

    -- 
    -- 
Dumping data for table `orders`
    -- 

    
INSERT INTO `ordersVALUES (24'3,3,1''0.000''0.000'00''1213815142);
    
INSERT INTO `ordersVALUES (35'1''0.000''0.000'00''1213816354);
    
INSERT INTO `ordersVALUES (44'2''0.000''0.000'00''1213870333);

    -- --------------------------------------------------------

    -- 
    -- 
Table structure for table `orders_receipts`
    -- 

    
CREATE TABLE `orders_receipts` (
      `
idint(11NOT NULL auto_increment,
      `
orderidint(11NOT NULL,
      `
amountdecimal(11,3NOT NULL,
      `
received_fromvarchar(255NOT NULL,
      `
typetinyint(1NOT NULL,
      `
dateint(25NOT NULL,
      `
bank_namevarchar(255NOT NULL,
      `
check_numvarchar(255NOT NULL,
      
PRIMARY KEY  (`id`)
    ) 
ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=;

    -- 
    -- 
Dumping data for table `orders_receipts`
    -- 

    
INSERT INTO `orders_receiptsVALUES (52'200.000''????? ????'01213876957'''');
    
INSERT INTO `orders_receiptsVALUES (62'25.000'''01213879058'''');
    
INSERT INTO `orders_receiptsVALUES (74'200.000''hassan'01213888806'''');

    -- --------------------------------------------------------

    -- 
    -- 
Table structure for table `users`
    -- 

    
CREATE TABLE `users` (
      `
useridint(10unsigned NOT NULL auto_increment,
      `
user_groupint(11NOT NULL,
      `
useremailvarbinary(255NOT NULL default '                                                                                                                                                                                                                                                               ',
      `
infotext NOT NULL,
      `
fnamevarchar(255character set latin1 collate latin1_bin NOT NULL default '',
      `
companyvarchar(255NOT NULL,
      `
cprint(10NOT NULL,
      `
zipvarchar(15character set latin1 collate latin1_bin NOT NULL default '',
      `
telvarchar(255character set latin1 collate latin1_bin NOT NULL default '',
      `
mobilevarchar(255NOT NULL,
      `
faxvarchar(255character set latin1 collate latin1_bin NOT NULL default '',
      `
countryint(11NOT NULL default '0',
      `
cityvarchar(255character set latin1 collate latin1_bin NOT NULL default '',
      `
websitevarchar(255NOT NULL default '',
      `
poboxvarchar(255NOT NULL,
      `
areavarchar(255NOT NULL,
      `
blockvarchar(255NOT NULL,
      `
roadvarchar(255NOT NULL,
      `
buildingvarchar(255NOT NULL,
      `
reg_dateint(25NOT NULL default '0',
      `
photovarchar(255NOT NULL default '',
      
PRIMARY KEY  (`userid`)
    ) 
ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=;

    -- 
    -- 
Dumping data for table `users`
    -- 

    
INSERT INTO `usersVALUES (400x616d65656e40686f746d61696c2e636f6d'??????? ???'0xc387c3a1c393c3adc38f20c3a3c38dc3a3c38f20c383c3a3c3adc3a4'???? ????????'801110653''0x3937333339353430353633'97339540563'0x31373535363638380'''malkiya''2001''????????''15463''45487''9898'0'');
    
INSERT INTO `usersVALUES (500x616d65656e40686f746d61696c2e636f6d'??????? ???'0xc3a3c38dc393c3a420c38cc39ac39dc391''0''0x3937333339353430353633'97339540563'0x31373535363638380'''malkiya''''????????''15463''45487''9898'0'');
    
INSERT INTO `usersVALUES (70''''0x537465766965''801210653''0x353433353433'5435435'''0''''''''''''''0''); 


#PHP QUERIES

PHP Code:



    $view_orders 
mysql_query("SELECT * FROM orders ORDER BY id DESC LIMIT 1, 50")Or Die(MySQL_Error());

    
/////////////////////////////////////////////////////////////////////////////////////////////

    /////////
    
echo "
    <table width='95%' border='1' align='center' cellpadding='2' cellspacing='0' bordercolor='#666666' bgcolor='#F5F4F3' id='AutoNumber1' style='border-collapse: collapse'>
    <tr align='center'>
      <td width='6%' bgcolor='#CCCCCC'>حذف</td>
      <td width='7%' bgcolor='#CCCCCC'>تعديل</td>
      <td width='11%' bgcolor='#CCCCCC'>الحالة</td>
      <td width='12%' bgcolor='#CCCCCC'>المتبقي</td>
      <td width='10%' bgcolor='#CCCCCC'>المدفوع</td>
      <td width='15%'  valign='middle' bgcolor='#CCCCCC'>السعر</td>
      <td width='39%' valign='middle' bgcolor='#CCCCCC'>أسم العميل</td>
      </tr>
    </table>
    "
;

    
///////
    
while($order_row mysql_fetch_array($view_orders)){


    
///////////////////////// COULCULATE ITEMS PRICES TOTAL
    
$total 0;

    
$items explode(',',$order_row['items']);
    
$contents = array();
    foreach (
$items as $item) {
    
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1;
    }
    foreach (
$contents as $imageid=>$qty) {
    
// Query
    
$Products_querymysql_query("select * from images WHERE imageid='$imageid' ") Or Die(MySQL_Error());
    while(
$row_images mysql_fetch_array($Products_query)){
    
$total += ($row_images['price'] * $qty);
    } 
// END WHILE
    
// END FOR EACH
    //////////////-----------------------------

    
$total number_format(($total$order_row['discount']), 3'.''');
    
///////////////////////// END COULCULATE ITEMS PRICES TOTAL

    /////+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ////----------------------------------------
    
$paid_info "00.000";
    
$view_orders_receipts mysql_query("SELECT * FROM orders_receipts WHERE orderid='{$order_row['id']}' ")Or Die(MySQL_Error());
    
$num_orders_receipts mysql_num_rows($view_orders_receipts);

    while(
$orders_receipts_row mysql_fetch_array($view_orders_receipts)){
    
$paid_info += stripslashes($orders_receipts_row['amount']);
    } 
// end while
    
$paid_info number_format($paid_info3'.''');
    
mysql_free_result($view_orders_receipts);
    
//----
    
$view_users mysql_query("SELECT fname FROM users WHERE userid='{$order_row['userid']}' ")Or Die(MySQL_Error());
    
$num_users mysql_num_rows($view_users);
    if (
$num_users 1) {
    
$CLIENT_NAME "";
    } else {
    while(
$users_row mysql_fetch_array($view_users)){
    
$CLIENT_NAME stripslashes($users_row['fname']);
    }
// end while
    
// end if no result
    
mysql_free_result($view_users);
    
////----------------------------------------

    
$balance_info number_format(($total$paid_info), 3'.''');

    if (
$balance_info <= 0) { $status_info "<font color='green'>مدفوع</font>"; } else {$status_info "<font color='red'>غير مدفوع</font>"; }
    
/////+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    // go on
    
echo "
    <table width='95%' border='1' align='center' cellpadding='2' cellspacing='0' bordercolor='#666666' bgcolor='#F5F4F3' id='AutoNumber2' style='border-collapse: collapse'>
      <tr align='center'>
        <td width='6%'><a href='orders.php?action=delcon&id=
{$order_row['id']}'>حذف</a></td>
        <td width='7%'><a href='orders_cart.php?id=
{$order_row['id']}' target='_blank'>تعديل</a></td>
        <td width='11%'>
$status_info</td>
        <td width='12%'  dir='rtl'>
$balance_info</td>
        <td width='10%'  dir='rtl'>
$paid_info</td>
        <td width='15%'  valign='middle' dir='rtl'>
{$total}</td>
        <td width='39%' valign='middle'><a href='orders_cart.php?id=
{$order_row['id']}' target='_blank'>$CLIENT_NAME</a></td>
      </tr>
    </table>
    "
;
    } 



All times are GMT. The time now is 02:24 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.02427 seconds
  • Memory Usage 1,886KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (1)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete