Advertisement

05.25.2008 at 11:23AM PDT, ID: 23431475
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.1

Problem In PHP DB-Table-Viewing Script

Asked by macrolific in PHP and Databases, PHP Scripting Language

Tags:

Below is a wonderful PHP script that I've been developing over the last few days with the help of some very bright people here at EE.  It does the following 3 things, but the 2nd function interferes with the 3rd:

1.  Displays my database table (just the first 4 fields so far) in an html table
2.  Enables alphabetical sorting of columns (DB fields) by clicking on column headers
3.  Enables selection of columns to view with checkboxes at the top of the page

One problem remains: If I have fewer than all 4 checkboxes checked and am therefore viewing fewer than all 4 columns, sorting any column -- by clicking any column header -- causes the page to reload with all checkboxes checked and all columns appearing.

For example, when the page first loads, I see 4 checkboxes checked and 4 columns of data.  If I then deselect 2 checkboxes and hit submit, the page reloads with 2 checkboxes checked and 2 columns of data.  Let's say I then click a column header to sort by that column.  After the page reloads, the table is sorted as expected, but now all 4 columns appear and all 4 checkboxes are checked.

So sorting works, but it defeats the ability to limit the number of columns with the checkboxes.

Can anyone figure out how to get the script's sorting function to stop causing all columns to appear?

Thanks!Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
<?php
//let the server give you feedback upon errors/warnings
error_reporting(E_ALL);
 
$DB_SERVER="";//SPECIFY IP or DOMAIN OF YOUR DB SERVER
$DB_USERNAME="";
$DB_PASSWORD="";
$DB_NAME="";//SPECIFY NAME OF YOUR DATABASE
 
//set the default state for the checkboxes
$checkedFields = array();
$checkedFields['invoice_num'] = " checked='checked' ";
$checkedFields['firstname'] = " checked='checked' ";
$checkedFields['lastname'] = " checked='checked' ";
$checkedFields['middle'] = " checked='checked' ";
 
if( isset($_REQUEST['fieldTracker']) )
{
	//determine the "unchecked" ones 
	if( empty($_REQUEST['invoice_num']) )
		$checkedFields['invoice_num']="";
	if( empty($_REQUEST['firstname']) )
		$checkedFields['firstname']="";
	if( empty($_REQUEST['lastname']) )
		$checkedFields['lastname']="";
	if( empty($_REQUEST['middle']) )
		$checkedFields['middle']="";
}
 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
 "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<div>
  <form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="select" id="select">
    <input type="checkbox" name="invoice_num" value="1" <?=$checkedFields['invoice_num']?> />Invoice
    <input type="checkbox" name="firstname" value="1" <?=$checkedFields['firstname']?> />First Name
    <input type="checkbox" name="lastname" value="1" <?=$checkedFields['lastname']?> />Last Name
    <input type="checkbox" name="middle" value="1" <?=$checkedFields['middle']?> />Middle
    <input name="fieldTracker" type="submit" value="submit" />
  </form>
</div>
<?php
 
if ( !($link = mysql_connect($DB_SERVER, $DB_USERNAME, $DB_PASSWORD)) ) {
    echo 'Could not connect to mysql: ' . mysql_error();
    exit;
}
if (!mysql_select_db($DB_NAME, $link)) {
    echo 'Could not select database: ' . mysql_error();
    exit;
}
$sql    = 'SELECT * FROM tbl_authnet_data';
if (isset($_REQUEST['Orderby']))
{
      $sql    = $sql . " Order By ". $_REQUEST['Orderby'];
}
$result = mysql_query($sql, $link) or die("Query Error: " . mysql_error());
 
if (!$result) {
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
$colspan=0;
print "<table align='center' border='0' cellpadding='2' bgcolor='#CCCCCC'>";
print "<tr style='color: white;'>";
if( !empty($checkedFields['invoice_num']) )
{
	++$colspan;
	print "<th><a href='{$_SERVER[PHP_SELF]}?Orderby=invoice_num'>Invoice</a></th>"; 
}
if( !empty($checkedFields['firstname']) )
{
	++$colspan;
	print "<th><a href='{$_SERVER[PHP_SELF]}?Orderby=firstname'>1st Name</a></th>"; 
}
if( !empty($checkedFields['lastname']) )
{
	++$colspan;
	print "<th><a href='{$_SERVER[PHP_SELF]}?Orderby=lastname'>Last Name</a></th>"; 
}
if( !empty($checkedFields['middle']) )
{
	++$colspan;
	print "<th><a href='{$_SERVER[PHP_SELF]}?Orderby=middle'>Middle Initial</a></th>";
}
 
print "</tr>";
print "<tr><td colspan='{$colspan}'></td></tr>";
 
// Define colors for alternating rows
$color1 = "#fffaf0";
$color2 = "#ffffff";
$row_count = 0;
 
while ($row = mysql_fetch_assoc($result))
{
	$row_color = ($row_count % 2) ? $color1 : $color2;
		print "<tr>";
		foreach ($row as $field => $value)
		{
		if( isset($checkedFields[$field]) && !empty($checkedFields[$field]) )
           	print "<td nowrap='nowrap' align='center' bgcolor='{$row_color}'><font size='2'>{$value}</font></td>";
		}
		print "</tr>";
		$row_count++;
}
print "</table>";
 
?>
</body>
</html>
 
Loading Advertisement...
 
[+][-]05.25.2008 at 11:35AM PDT, ID: 21643351

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.25.2008 at 11:46AM PDT, ID: 21643381

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.26.2008 at 08:33AM PDT, ID: 21646860

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: PHP and Databases, PHP Scripting Language
Tags: PHP, MySQL, HTML
Sign Up Now!
Solution Provided By: HackneyCab
Participating Experts: 1
Solution Grade: A
 
 
[+][-]05.26.2008 at 08:38AM PDT, ID: 21646876

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.26.2008 at 11:07AM PDT, ID: 21647462

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.27.2008 at 12:37PM PDT, ID: 21654915

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.27.2008 at 02:51PM PDT, ID: 21656057

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628