[FX.php List] Browsing a set of found records

Erik Andreas Cayré erik at cayre.dk
Sun Aug 20 07:58:43 MDT 2006


Den 20/08/2006 kl. 8.16 skrev Jonathan Schwartz:

> This next enhancement has been on my wish list for a long time....
>
> After performing a search, I would like to allow users to browse  
> the resulting records, one by one.  To me, this is much more  
> efficient then: search/view list/display detail/return to list/view  
> next.
>
> I know that this means saving the resulting set in an array and  
> then walking the array.  I just don't know how.
>
> I imagine it must look a lot like the standard for each code, along  
> with explode.  From the current script which lists the found set:
>
>  php foreach($searchResult['data'] as $key => $value) {
>
> 		echo "<td><a href=\"detail.php?recid=";
>   	  	$recordDetails=explode('.',$key);
> 		$currentRecord=$recordDetails[0];
> 		echo $currentRecord."\">".$value['myfield'][0];
>   	 	echo "</a></td>";
>
> In addition to echoing to screen, I'm guessing that I need to  
> create and populate an array.
>
> Or.....is this just a variation on PREV NEXT links, where the  
> groupsize is set to 1?
>
> Anyone want to steer me in the right direction?

I took the challenge, since I'll need a similar feature on one of my  
sites soon.

My test code is below. I hope it's readable, I put a few comments in  
to help...
The code is not ready for production: check for valid user input are  
missing and there may be other stuff missing...

All comments are welcome!

Code:
<?php
	session_start(); // Let's us remember stuff accross page loads
	require_once ('FX/FX.php');
	require_once ('inc/system.php'); // DB connection constants etc.

// Functions
	// Find products by name, without 'InnerArray'
	function findProducts($search)
	{
		global $fmshost;
		global $dataport;
		global $dbname;
		global $user;
		global $pass;
		$layout = 'products';
		$fx = new FX($fmshost, $dataport);
		$fx->SetDBData($dbname, $layout);
		$fx->SetDBUserPass($user, $pass);
		$fx->AddDBParam('webtexts_productname::da', $search);
		$return_data = $fx->FMFind(TRUE, 'object', FALSE);
		
		// Transform FM data into more useful array
		if (count($return_data) > 0) {
			$i = 1;
			foreach ($return_data AS $fmkey => $fmrecord) {
				list( $products[$i]['recid'], $test[$i]['modid'] ) = explode 
( '.', $fmkey );
				foreach ($fmrecord as $field => $value) {
					if (isset($value)) {
						$products[$i][$field] = $value;
					}
				}
				$i++;
			}
			return ($products);
		}
		return (NULL);
	}

	// Add link to string
	function addLink ($string, $URI = '')
	{
		$start = ($URI != '' ? '<a href="' . $URI . '">' : '');
		$end = ($URI != '' ? '</a>' : '');
		$html = $start . $string . $end;
		return ($html);
	}

	// Show navigation widget. Links using GET
	function showNavigationWidget ($count, $listRelativeURI, $current = 1)
	{
		$previous = '&lt;&lt;';
		$next = '&gt;&gt;';
		$list = 'List';
		
		$html = addlink ($previous, ($current > 1 ? $_SERVER['PHP_SELF'] .  
'?id=' . ($current - 1) : '')); // Link to previous record
		$html .= ' ' . addLink ($next, ($current < $count ? $_SERVER 
['PHP_SELF'] . '?id=' . ($current + 1) : '')); // Add link to next  
record
		$html .= ' &middot; ' . addLink ($list, $_SERVER['PHP_SELF'] .  
$listRelativeURI); // Add link back to list
		return ($html);
	}

// The page
	$head = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// 
EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<title>Test</title>
	<meta name="generator" content="BBEdit 8.2" />
</head>
';
	$body = '
<body><div style="margin-left: auto; margin-right: auto; width: 500px;">
<form method="get" id="showproducts" name="showproducts">
<label for="search">Search product name: </label>
<input name="search" id="search" type="text" size="20"  value="' .  
$_SESSION['search'] . '"/>
<button type="submit">Search</button>
</form>
';

	if (isset($_GET['search'])) // Find and/or show list of products
	{
		if ($_GET['search'] != $_SESSION['search']) // New search
		{
			$_SESSION['products'] = findProducts ($_GET['search']);
			$_SESSION['foundCount'] = count ($_SESSION['products']);
			$_SESSION['search'] = $_GET['search'];
		}
		
		if ($_SESSION['products'] == NULL) // Didn't find anything
		{
			$body .= '<p>No matches found for search term "' . $_SESSION 
['search'] . '"<br>
Please try another search term.</p>
';
		} else { // Show the list
		
			$body .= '<table border="1">
<caption>List of ' . $_SESSION['foundCount'] . ' products matching  
"' . $_SESSION['search'] . '"</caption>
';
		
			foreach ($_SESSION['products'] AS $id => $product)
			{
				$body .= '<tr><td><a href="' . $_SERVER['PHP_SELF'] . '?id=' .  
$id . '">' . $product['id'] . ' ' . $product 
['webtexts_productname::da'] . (array_key_exists 
('webtexts_suppdata1::da', $product) ? ', ' . $product 
['webtexts_suppdata1::da'] : '') . '</a>
';
			}
			$body .= '</table>
';
		}
	}
	
	elseif (isset($_GET['id'])) // Show product details, with links for  
browsing
	
	{
		$body .= '<table border="1" width="450px"><caption>Details for  
product ' . $_GET['id'] . ' of ' . $_SESSION['foundCount'];
		$body .= ' &middot; ' . showNavigationWidget($_SESSION 
['foundCount'], '?search=' . $_SESSION['search'], $_GET['id']) . '</ 
caption>
<tr><th>Association<td>' . $_SESSION['products'][$_GET['id']] 
['associations::name'] . '
<tr><th>Product name<td>' . $_SESSION['products'][$_GET['id']] 
['webtexts_productname::da'] . (array_key_exists 
('webtexts_suppdata1::da', $_SESSION['products'][$_GET['id']]) ? ',  
' . $_SESSION['products'][$_GET['id']]['webtexts_suppdata1::da'] :  
'') . '
<tr><th>Product price<td>' . $_SESSION['products'][$_GET['id']] 
['price'] . '
';
		
		$body .= '</table>
';
	}
	
	$body .='</div>
</body>
</html>';

print $head;
print $body;
?>




---
Erik Andreas Cayré
Spangsbjerg Møllevej 169
DK-6705 Esbjerg Ø

Home Tel: +45 75150512
Mobile: +45 40161183

»If you can't explain it simply, you don't understand it well enough.«
-- Albert Einstein

»If you don't have time to do it right, when will you have time to do  
it over?«
-- John Wooden, basketball coach


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1856 bytes
Desc: not available
Url : http://www.iviking.org/pipermail/fx.php_list/attachments/20060820/c34a0f88/smime-0001.bin


More information about the FX.php_List mailing list