PDF='MAIN' { ATTRIBUTES='*' { PLATFORM='php.txt'; REPTYPE='1'; ATTACHED='0'; PROJECT='36'; OBJECTNAME='customer_header.php'; LIBLOBJ='*FILES'; TITLE='Customer Header Program'; WEBPATH='http://esdi.excelsystems.com/wsexmp/'; TARGETPATH='/www/zendcore/htdocs/examples/'; FTPSITE='examples'; SET='46'; VERSION='4.00'; CRTBNUM='Revision 1811'; CRTTEMPLT='C:\\Program Files\\ESDI\\WebSmart\\Temp\\PHP-Page at a Time Maint.tpl'; SAVBNUM='Revision 1811'; LASTLOC='3'; VHTML='N'; LASTSAVE='12/24/2007 11:42:11'; OBJLIBRARY='XL_WEBEXMP'; SOURCELIBR='XL_WEBEXMP'; SOURCEFILE='QRPGLESRC'; } FTPREFINFO='*' { URL='192.168.0.100'; INITIALDIR='/www/zendcore/htdocs/examples/'; FTPPORT='21'; PASSIVEMODE='1'; WEBPATH='http://192.168.0.111:8150/wsphp/'; } FILES='*' { FILE='MU_CUSTF' { LIBRARY='XL_WEBDEMO'; ALIAS=''; RCDFORMAT='R_CUSTF '; LEVELID='1070921111624'; RFLEVELID='403ABD661EC5A'; EXTDS='0'; } } PANELS='*' { PANEL='ListHeader' { DESC='Main List header'; DETAILS=' Example 143: Customer Header-Detail

Example 143: Customer Header-Detail

Description:   This WebSmart PHP example makes a header and detail application to see a list of customers and their orders.

Customers (Header Program)

'; } PANEL='ListDetails' { DESC='Page body'; ITERATIONS='10'; DETAILS=' '; } PANEL='ListFooter' { DESC='Page footer'; DETAILS='
Action Customer Number Customer Name Address 1 City Country
Orders
$CMCUST $CMNAME $CMADR1 $CMCITY $CMCOUNT

Header Program:

  1. Start a new program named customer_header.php using the \'SQL Page at a Time Maint\' template.


  2. At the \'Additional Design Options\' step, accept the default options.


  3. At the \'File Maintenance Options\' step, uncheck all options except \'Allow display of full record\'.


  4. When prompted, add the file MU_CUSTF in XL_WEBDEMO and choose the fields CMCUST, CMNAME, CMADR1, CMCITY and CMCOUNT. The wizard will also prompt you to select fields for the single record display; choose any field (we won\'t be displaying this page anyway).


  5. Change the \'A\' tag for the \'display\' action in the segment \'ListDetails\' to this:
        <a href="customer_detail.php?cmcust=$CMCUST">Orders</a>
        
  6. Compile this program.


Detail Program:

  1. Start creating a new program named customer_detail.php using the \'SQL Record Listing\' template.


  2. Add the file MU_ORDHL1 in XL_WEBDEMO.


  3. Add the file MU_CUSTF in XL_WEBDEMO.


  4. Choose the fields OHORD, OHDESC, OHORDT, OHSTAT and OHOTOT.


  5. Add these lines to near the top of the program in your PHP:
    	// Global variables should be defined here
    	global $ww_custno;
    
    
    	// retrieve the customer number parameter
    	if (isset($_REQUEST[\'cmcust\']))
    		$ww_custno = $_REQUEST[\'cmcust\'];    
        
  6. Choose the fields OHORD, OHDESC, OHORDT, OHSTAT and OHOTOT.


  7. Replace the existing display() function with this new function
    	   
    function display()
    { 
    	// Make all global variables available here
    	foreach($GLOBALS as $arraykey=>$arrayvalue) 
    	{
    		global $$arraykey;
    	}
    	
    	
    	/*
    	 *
    	 *  Query to find the customer number, name, city, and country
    	 *
    	 */
    	$query = "select CMCUST, CMNAME, CMCITY, CMCOUNT 
    	              from XL_WEBDEMO/MU_CUSTF 
    	              where CMCUST = $ww_custno";
    	
    	// Fetch rows for page: relative to initial cursor 
    	if (!($stmt = db2_exec($db2conn, $query))) 
    	{
    		echo "<b>Error ".db2_stmt_error() .":".db2_stmt_errormsg(). "</b>"; 
    		die;
    	}
    	
    	$row = db2_fetch_assoc($stmt);
    	
    	$CMCUST = $row["CMCUST"];
    	$CMNAME = $row["CMNAME"];
    	$CMCITY = $row["CMCITY"];
    	$CMCOUNT = $row["CMCOUNT"];
    	
    	
    	
    	
    	/*
    	 *
    	 *  Query to find the orders for the customer
    	 *
    	 */			
    	$query = "select OHORD, OHDESC, OHORDT, OHSTAT, OHOTOT, CMCUST, CMNAME, CMCITY, CMCOUNT  
    	              from XL_WEBDEMO/MU_CUSTF inner join XL_WEBDEMO/MU_ORDHL1 on OHCUST=CMCUST 
    	              where OHCUST = $ww_custno";
    	
    	// Fetch rows for page: relative to initial cursor 
    	if (!($stmt = db2_exec($db2conn, $query))) 
    	{
    		echo "<b>Error ".db2_stmt_error() .":".db2_stmt_errormsg(). "</b>"; 
    		die;
    	}
    	
    	wrtseg("ListHeader");
    	
    	while ($row = db2_fetch_assoc($stmt))
    	{
    		// set color of the line
    		xl_set_row_color(\'altcol1\', \'altcol2\');
    		
    	
    		$OHORD = $row["OHORD"];
    		$OHDESC = $row["OHDESC"];
    		$OHORDT = $row["OHORDT"];
    		$OHSTAT	= $row["OHSTAT"];
    		$OHOTOT = $row["OHOTOT"];
    		
    		$mydate = strftime("%Y/%m/%d", strtotime($OHORDT));
    		
    		wrtseg("ListDetails");
    		
    		
    	}
    	
    	// closet the database connection
    	db2_close($db2conn);   
    	
    	wrtseg("ListFooter");	
    	
    }	   
        
  8. Compile this program and run the \'Customer Header\' program.



Header Program Definition:   customer_header.phw
Detail Program Definition:   customer_detail.phw

Rate This Example

Did this knowledge base article help you to achieve your goal?  Yes  No  Don\'t Know

Enter additional comments below.   If you want to hear back from us, include your contact information.

'; } PANEL='RcdDisplay' { DESC='Display an individual record'; DETAILS=' Customer Header Program - Display

Customer Header Program

Description:   Example description.

Customer Number:$CMCUST
Customer Name:$CMNAME
Address 1:$CMADR1
  1. Example step.


  2. Replace:
                          sample replaced text
                          
    with the following:
                          sample replacing text
                          

Rate This Example

Did this knowledge base article help you to achieve your goal?  Yes  No  Don\'t Know

Enter additional comments below.   If you want to hear back from us, include your contact information.

'; } PANEL='RtnToList' { DESC='Return to List'; DETAILS=' '; } } PREVIEWS='*' { PREVIEW='Main List' { SEG='ListHeader'; SEG='ListDetails' { REPEAT='10'; } SEG='ListFooter'; } PREVIEW='Display' { SEG='RcdDisplay'; } PREVIEW='Manage' { SEG='RcdManage'; } } ACTIONS=' // Program Name: customer_header.php // Program Title: Customer Header Program // Created by: TMC // Template family: Idaho // Template name: SQL Page at a time maintenance // Purpose: Maintain a database file using embedded SQL. Supports options for add, change, delete and display. // Program Modifications: // DB Connection code require(\'/esdi/websmart/v6.6/include/xl_functions001.php\'); $options = array(\'i5_naming\' => DB2_I5_NAMING_ON); global $db2conn; $db2conn = xl_db2_connect($options); global $wsnum; // This code is for the Rate this page section if (isset($_REQUEST[\'wsnum\'])) { $wsnum = $_REQUEST[\'wsnum\']; } // Check the cookie if(isset($_COOKIE[\'wsnumbc\'])) { $wsnum = $_COOKIE[\'wsnumbc\']; } else { setcookie(\'wsnumbc\', $wsnum); } // Global variables should be defined here global $ww_rrn, $ww_ordby, $ww_orddir, $ww_page, $ww_nx, $ww_prevpage, $ww_nextpage, $ww_listsize, $ww_mode, $ww_whrclause, $ww_selstring, $ww_program_state, $ww_count; // Set maximum list size to 10 for this program $ww_listsize = 10; // Initialize the previous page and count of records $ww_prevpage = 0; $ww_count = 0; // Create random field to avoid caching $rnd = rand(0, 99999); // retrieve the last state of the list: order-by column and direction (ascend/descend). if(isset($_SESSION[$pf_scriptname])) $ww_program_state = $_SESSION[$pf_scriptname]; if (isset($ww_program_state[\'ww_orddir\'])) $ww_orddir = $ww_program_state[\'ww_orddir\']; if (isset($ww_program_state[\'ww_ordby\'])) $ww_ordby = $ww_program_state[\'ww_ordby\']; if (isset($ww_program_state[\'ww_page\'])) $ww_page = $ww_program_state[\'ww_page\']; // Retrieve the rrn (if any) if (isset($_REQUEST[\'rrn\'])) $ww_rrn = $_REQUEST[\'rrn\']; // run the specified task switch($pf_task) { case \'default\': display(); break; // Record display option case \'disp\': disprcd(); break; } /******************** End of mainline code ********************/ // Load first page and use ordby parameter from form to determine new sort order, direction function display() { // Make all global variables available here foreach($GLOBALS as $arraykey => $arrayvalue) { if ($arraykey[0]!=\'_\' && $arraykey != \'GLOBALS\') global $$arraykey; } // Retrieve or set the page to list if (isset($_REQUEST[\'page\'])) $pagenum = (int)$_REQUEST[\'page\']; else $pagenum = 1; // Calculate next and previous page number $ww_prevpage = $pagenum - 1; $ww_nextpage = $pagenum + 1; // Compute table row cursor offset, offset starts at 0 $ww_nx = $pagenum * $ww_listsize; // Build select string for SQL exec bldselstr(); // Store the last used order-by settings: $ww_program_state[\'ww_orddir\'] = $ww_orddir; $ww_program_state[\'ww_ordby\'] = $ww_ordby; $ww_program_state[\'ww_page\'] = $pagenum; $_SESSION[$pf_scriptname] = $ww_program_state; // Build first page of table rows bldpage(); } // Build current page of rows up to listsize. function bldpage() { // Make all global variables available here foreach($GLOBALS as $arraykey=>$arrayvalue) { if ($arraykey[0]!=\'_\' && $arraykey != "GLOBALS") global $$arraykey; } // Output page and list header wrtseg(\'ListHeader\'); // Fetch rows for page: relative to initial cursor $ww_selstring = $ww_selstring." FETCH FIRST $ww_nx ROWS ONLY"; if (!($stmt = db2_exec($db2conn, $ww_selstring, array(\'CURSOR\' => DB2_SCROLLABLE)))) { echo "Error ".db2_stmt_error() .":".db2_stmt_errormsg(). ""; die; } // While SQL retrieves records and show them $index = $ww_nx - $ww_listsize + 1; while ($row = db2_fetch_assoc($stmt, $index)) { // set color of the line xl_set_row_color(\'altcol1\', \'altcol2\'); $ww_rrn = $row[\'00001\']; // Get the fields $CMCUST = $row["CMCUST"]; $CMNAME = $row["CMNAME"]; $CMADR1 = $row["CMADR1"]; $CMCITY = $row["CMCITY"]; $CMCOUNT = $row["CMCOUNT"]; wrtseg(\'ListDetails\'); $index++; } // test for more records $ww_count = $index - ($ww_nx - $ww_listsize) - 1; // show the footer wrtseg(\'ListFooter\'); // closet the database connection db2_close($db2conn); } // Build SQL Select string: function bldselstr() { // Make all global variables available here foreach($GLOBALS as $arraykey => $arrayvalue) { if ($arraykey[0]!=\'_\' && $arraykey != \'GLOBALS\') global $$arraykey; } $ww_selstring = "select rrn(MU_CUSTF),CMCUST, CMNAME, CMADR1, CMCITY, CMCOUNT from XL_WEBDEMO/MU_CUSTF"; /**** Build \'order-by\' clause ****/ // If a column header was clicked, set the order by field to // the correct column and control ascending and descending order // Check for a sort request if (isset($_REQUEST[\'ordby\'])) { $ordby = $_REQUEST[\'ordby\']; // If we previously sorted on this column, then reverse the order of the sort: if ($ordby == $ww_ordby) { if ($ww_orddir == \'A\') $ww_orddir = \'D\'; else $ww_orddir = \'A\'; } else { // Save last used column for sort. $ww_ordby = $ordby; // Ascending order $ww_orddir =\'A\'; } } // If a sort-by column exists then use that to build the order-by if ($ww_ordby <> "") { $ww_selstring = trim($ww_selstring) . " order by " . $ww_ordby; // If descending order: if ($ww_orddir == "D") $ww_selstring = trim($ww_selstring) . " DESC"; } else { // Otherwise just use the default order by $ww_selstring = trim($ww_selstring) . " order by CMCUST "; } } // Display details for selected record: function disprcd() { // Make all global variables available here foreach($GLOBALS as $arraykey=>$arrayvalue) { if ($arraykey[0]!=\'_\' && $arraykey != "GLOBALS") global $$arraykey; } // Fetch the row for page $sqlstr = "SELECT CMCUST, CMNAME, CMADR1 FROM XL_WEBDEMO/MU_CUSTF WHERE RRN(MU_CUSTF) = $ww_rrn"; if (!($result = db2_exec($db2conn, $sqlstr))) { echo "Error ".db2_stmt_error().":".db2_stmt_errormsg().""; die; } // put the result into global variable and show it $row = db2_fetch_assoc($result); // Get fields $CMCUST = $row["CMCUST"]; $CMNAME = $row["CMNAME"]; $CMADR1 = $row["CMADR1"]; // Release the database resource db2_close($db2conn); // output the segment wrtseg(\'rcddisplay\'); } '; }