Page 1 of 1

The proper way to do it?

Posted: Wed Jan 03, 2007 7:55 pm
by me!
Ok what I want to do is return results from the db based on what options the user selects from a form.

I was using

Code: Select all

foreach ($line as $col_value) 
But am thinking there must be a better way to do it? The problem that I am having is the SELECT statment and the use of foreach since it will display all results in the $query :? ...


This is one of the ideas I had untill I relised this problem, I know it does not work, the if statments do work ok and they are the form options.

Code: Select all

$query = "SELECT pn_name, pn_uname, pn_phone, pn_duesexdate, barc_id FROM _users WHERE pn_duesexdate > 100 ORDER BY 'pn_name' ASC";

$result = mysql_query($query) or die('Query failed: ' . mysql_error());


// output table
echo '<table width="100%"  border="0" cellspacing="0" cellpadding="3">';

// Set the headings
echo '<tr><td><b>Member Name</b></td>'
if ($call== '1') echo '<td><strong>Call</strong></td>';
if ($phone== '1') echo '<td><strong>Phone</strong></td>';
if ($cell== '1') echo '<td><strong>Cell</strong></td></tr>';
if ($fax== '1') echo '<td><strong>Fax</strong></td></tr>';
if ($address== '1') echo '<td><strong>Address</strong></td></tr>';
if ($address== '1') echo '<td><strong>City</strong></td></tr>';
if ($address== '1') echo '<td><strong>State</strong></td></tr>';
if ($address== '1') echo '<td><strong>Zip</strong></td></tr>';
if ($ex_date== '1') echo '<td><strong>Ex. Date</strong></td></tr>';
if ($type== '1') echo '<td><strong>Mem. Type</strong></td></tr>';
if ($id== '1') echo '<td><strong>Mem. ID</strong></td>';

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
	   echo "<tr>";
   foreach ($line as $col_value) {
       echo '<td><div align="center">' . $col_value .'</div></td>';
   }
   echo '<td>
   			</td>';
   echo "</tr>";
}
echo "</table>";

Posted: Wed Jan 03, 2007 8:28 pm
by volka
I do not understand. What are you trying to achieve?

Posted: Wed Jan 03, 2007 8:31 pm
by feyd
Add the fields to be included in the output to an array. Using in_array() you can then compare the key available through foreach() of each element in the query result set.

Posted: Wed Jan 03, 2007 10:09 pm
by me!
Sorry if I was not very clear on what I am trying to do.

I want to generate a member roster for a club, BUT, the users will have the option to include or exclude items like phone numbers, e-mails, ect. (the items with the IF statements) from the output.

As for in_array() I am still new to this and just grasping a syntax of foreach(), so I am not sure how to implement it?

Posted: Thu Jan 04, 2007 11:01 am
by feyd
Do you understand how to add elements to an array? http://php.net/language.types.array

Do you understand the two syntax forms of foreach()? http://php.net/foreach. You'll want the second key-value pair variant.

Posted: Thu Jan 04, 2007 8:14 pm
by me!
feyd wrote:Do you understand how to add elements to an array? http://php.net/language.types.array
Not well, but getting better :oops:
feyd wrote:Do you understand the two syntax forms of foreach()? http://php.net/foreach. You'll want the second key-value pair variant.

Yes I have looked at them but did not totaly understand them untill looking at a lot of examples tonight.


The thing that I just got was the abolity to do this:
:D

Code: Select all

$result2 = mysql_query("SELECT * FROM _users ORDER BY barc_id asc");
		while ($mysql = mysql_fetch_array($result2)) 
		{
			echo'<tr>
    		<td>'. $mysql[pn_name] .'</td>
    		<td>'. $mysql[pn_uname] .'</td>
    		<td>'. $mysql[barc_id] .'</td>
  			</tr>'; 
		} 

Posted: Fri Jan 05, 2007 9:02 am
by feyd
Make sure to quote named array indices. :)

Posted: Fri Jan 05, 2007 11:03 am
by aaronhall
feyd wrote:Make sure to quote named array indices. :)
For example, $mysql['pn_name'] instead of $mysql[pn_name]. If you don't quote the indices, PHP will first assume you are referencing a constant, throw a notice, and then look for the key in the array.