HI All, I've been trying to make a membership management type script. This script would have a database with the members contact info and which office they hold, if any. The problem I am having is I would like the script to show the president's and vice president's Contact info all the time, but hide the contact info for everyone else when viewed by the general public, then if the user logs in all contact information is displayed.

Below is the part of the script that I have so far, The if logged in / out part works like I want, but I can't get it to show the President's info but hide the other members info.

if (($sub_office =="President")||($sub_office =="Vice President")||($sub_office =="Secretery")){
		$officer_show = "YES";}
	ELSE{$officer_show = "NO";}

if ($officer_show == YES){
$hphone = $homephone;
$cphone = $cellphone;
$adress = $address;
$cty = $city;
$st = $state;
$zp = $zip;
$eml = $email;
}
ELSE
{
$hphone = " ";
$cphone = " ";
$adress = " ";
$cty = " ";
$st = " ";
$zp = " ";
$eml = " ";
}
			if($_SESSION['auth']['status'] == 1){
				// do stuff here
$display_block .= "
<tr>
 <td>$sub_office</td>
 <td>$fname $lname</td>
 <td>$hphone / $cphone</td>
 <td>$eml</td>
 <td>$adress, $cty, $st $zp</td>
 <td>$sub_name</td>
</tr>"; 
			}

ELSE {
$display_block .= "
<tr>
 <td>$sub_office</td>
 <td>$fname $lname</td>
 <td>$hphone / $cphone</td>
 <td>$eml</td>
 <td>$adress, $cty, $st $zp</td>
 <td>$sub_name</td>
</tr>"; 
	}
}

I Have also seen the if ($sub_office =="President") done like this if ($sub_office !="President")
I can't seem to get it to work either way. If there is a better way of doing any of this, let me know. I would appreciate any help.

Recommended Answers

All 17 Replies

Well, I don't know all your database, but here's a sample script I would use:

if (!logged_in)
{
$query1 = mysql_query("SELECT * FROM membersinfo WHERE id = 'president'");
while ($row = mysql_fetch array($sql))
{
echo "<tr>
<td>$sub_office</td>
<td>$fname $lname</td>
<td>$hphone / $cphone</td>
<td>$eml</td>
<td>$adress, $cty, $st $zp</td>
<td>$sub_name</td>
</tr>"; 
}
} elseif (logged_in)
{
$query2 = mysql_query("SELECT * FROM membersinfo");
while ($row = mysql_fetch array($sql))
{
echo "<tr>
<td>$sub_office</td>
<td>$fname $lname</td>
<td>$hphone / $cphone</td>
<td>$eml</td>
<td>$adress, $cty, $st $zp</td>
<td>$sub_name</td>
</tr>"; 
}
}

This would work ok, but I forgot to mention that I still want to show everyone's name. I only want to show the contact info for the president, VP, and Secretary. Only the name and 'office' would be shown for the other members.

In which case, you could change the query. Under the if(!logged_in) part, after the first query:

$names = mysql_query("SELECT name FROM members WHERE id != 'president'");

while ($info = mysql_fetch array($names))
{
// echo names
}
//continue on with elseif

That should do the trick.

I would prefer not to change the sql query. This would create a plroblem in the list and also would make it difficult to do the logged in view. I'm not sure why the if() statements aren't working. I think they should. Thanks for the help anyway.

What problem exactly would you have by adding the sql query?

I wasn't sure how to get it to sort my my ordered list. I want the results sorted by office list. How could this be done with two seperate sql queries. NOTE: not all offices from one query are in a row.
eg.
President,
Vice President,
Other Offices...,
Secretary,
ect...

I did post this quetion in another forum and they were able to offer a simpler version of the page I wrote. (they eliminated some if statements, ect.)

I still don't have the page working so I would still appreciate help. Here is the PHP page that they suggested:

<?php

session_start();

include ('./config/db.php');

$sub_name = $_GET['sub_name'];

$connection = @mysql_connect($server, $dbusername, $dbpassword)
				or die(mysql_error());
				
$db = @mysql_select_db($db_name,$connection)
				or die(mysql_error());

/***
 *  Show publicly available data
 ***/
// Set up the roles that should be visible to the public
$rolesToShow = array("Master - President", "Overseer - Vice President", "Lecturer - Program Director", "Secretary");
$roles = "'". implode("', '", $rolesToShow)."'";
 
// Create and execute the query
$sql = "SELECT fname, lname, sub_office, email, homephone, cellphone, address, city, state, zip FROM MembersTable WHERE sub_name = '".$sub_name."' AND '".$sub_office."' IN($roles)";

$result = mysql_query($sql,$connection) or die(mysql_error());
 
// Display the public data
while($row = mysql_fetch_row($result)) {
  # Show data

  $fname = $row['fname'];
  $lname = $row['lname'];
  $sub_office = $row['sub_office'];
  $email = (isset($row['email']) ? $row['email'] : "&nbsp;");
  $homephone = (isset($row['homephone']) ? $row['homephone'] : "&nbsp;");
  $cellphone = (isset($row['cellphone']) ? $row['cellphone'] : "&nbsp;");
  $address = (isset($row['address']) ? $row['address'] : "&nbsp;");
  $city = (isset($row['city']) ? $row['city'] : "&nbsp;");
  $state = (isset($row['state']) ? $row['state'] : "&nbsp");
  $zip = (isset($row['zip']) ? $row['zip'] : "&nbsp;");
}
 
/***
 * Show member specific data
 ***/
// Create the SQL query based on the user's status.
if($_SESSION['User']['status'] == 1) {
  $sql2 = "SELECT fname, lname, sub_office, email, homephone, cellphone, address, city, state, zip FROM MembersTable WHERE sub_name = '".$sub_name."' AND '".$sub_office."' NOT IN($roles)";
}
else {
  $sql2 = "SELECT fname, lname, sub_office FROM MembersTable WHERE sub_name = '".$sub_name."' AND '".$sub_office."' NOT IN($roles)";
}
 
// Execute the query and display the results
$result2 = mysql_query($sql2,$connection) or die(mysql_error());
 
while($row = mysql_fetch_assoc($result2)) {
  // Set these based on whether the data was fetched or not
  // The (bool ? true : false) format is basically a compact if statement.
  $fname = $row['fname'];
  $lname = $row['lname'];
  $sub_office = $row['sub_office'];
  $email = (isset($row['email']) ? $row['email'] : "&nbsp");
  $homephone = (isset($row['homephone']) ? $row['homephone'] : "&nbsp");
  $cellphone = (isset($row['cellphone']) ? $row['cellphone'] : "&nbsp");
  $address = (isset($row['address']) ? $row['address'] : "&nbsp");
  $city = (isset($row['city']) ? $row['city'] : "&nbsp");
  $state = (isset($row['state']) ? $row['state'] : "&nbsp");
  $zip = (isset($row['zip']) ? $row['zip'] : "&nbsp");
 
  # Display data

$display_block .= "
<tr>
 <td>$sub_office</td>
 <td>$fname $lname</td>
 <td>$homephone / $cellphone</td>
 <td>$email</td>
 <td>$address, $city, $state $zip</td>
 <td>$sub_name</td>
</tr>"; 
}


$title = "<title>Officers of the ".$sub_name." Subordinate Grange</title>";
?>

<? include("./config/header.php"); ?>

</head>

<body>



<table width="85%" border="1" class="main"><tbody>
<tr><td colspan="6"><h2>Officers of <? echo "$sub_name"; ?> Subordinate Grange</h2></td></tr>

<tr><td><b>Office</b></td>  <td><b>Officer</b></td>  <td><b>Home  / Cell Phone #</b></td>  <td><b>Email</b></td>  <td><b>Address</b></td>  <td><b>Subordinate Grange</b></td></tr>

<? echo "$display_block"; ?>

<tr><td colspan="6"><!-- C. FOOTER AREA -->      


    <div class="footer">
      <? include ('./config/footerinfo.php'); ?>
    </div>      </td></tr>

</tbody></table>


</body>
</html>

Of coarse I added a few things to try to make it work for me (eg. sql connect info ect.).

Member Avatar for langsor

I only found one type of thing wrong on this page, all of your
$zip = (isset($row) ? $row : "&nbsp;");
lines have an unmatched bracket ) on the end of them.
So I changed them to this

$zip = ( isset($row['zip'] ) ? $row['zip'] : "&nbsp";

And played around with the rest of the code some too ... but no promises

<?php

session_start();

include( './config/db.php' );

$sub_name = $_GET['sub_name'];

$link = @mysql_connect( $server, $dbusername, $dbpassword ) or die(mysql_error());

$db = @mysql_select_db( $db_name ) or die(mysql_error());

/***
 *  Show publicly available data
 ***/
// Set up the roles that should be visible to the public
$rolesToShow = array( "Master - President", "Overseer - Vice President", "Lecturer - Program Director", "Secretary" );
$roles = "'".implode( "', '", $rolesToShow )."'";
 
// Create and execute the query
$sql = "SELECT * FROM `MembersTable` WHERE sub_name='$sub_name' AND '$sub_office' IN($roles)";

$result = mysql_query( $sql ) or die( mysql_error() );
 
// Display the public data
while( $row = mysql_fetch_row( $result ) ) {
  # Show data
  $fname = $row['fname'];
  $lname = $row['lname'];
  $sub_office = $row['sub_office'];
  $email = ( isset($row['email'] ) ? $row['email'] : "&nbsp;";
  $homephone = ( isset($row['homephone'] ) ? $row['homephone'] : "&nbsp;";
  $cellphone = ( isset($row['cellphone'] ) ? $row['cellphone'] : "&nbsp;";
  $address = ( isset($row['address'] ) ? $row['address'] : "&nbsp;";
  $city = ( isset($row['city'] ) ? $row['city'] : "&nbsp;";
  $state = ( isset($row['state'] ) ? $row['state'] : "&nbsp";
  $zip = ( isset($row['zip'] ) ? $row['zip'] : "&nbsp;";
}
 
/***
 * Show member specific data
 ***/
// Create the SQL query based on the user's status.
if( $_SESSION['User']['status'] == 1 ) {
  $sql2 = "SELECT * FROM MembersTable WHERE sub_name = '$sub_name' AND '$sub_office' NOT IN($roles)";
} else {
  $sql2 = "SELECT fname, lname, sub_office FROM MembersTable WHERE sub_name = '$sub_name' AND '$sub_office' NOT IN($roles)";
}
 
// Execute the query and display the results
$result2 = mysql_query( $sql2 ) or die( mysql_error() );
 
while ( $row = mysql_fetch_assoc( $result2 ) ) {
  // Set these based on whether the data was fetched or not
  // The (bool ? true : false) format is basically a compact if statement.
  $fname = $row['fname'];
  $lname = $row['lname'];
  $sub_office = $row['sub_office'];
  $email = ( isset($row['email'] ) ? $row['email'] : "&nbsp";
  $homephone = ( isset($row['homephone'] ) ? $row['homephone'] : "&nbsp";
  $cellphone = ( isset($row['cellphone'] ) ? $row['cellphone'] : "&nbsp";
  $address = ( isset($row['address'] ) ? $row['address'] : "&nbsp";
  $city = ( isset($row['city'] ) ? $row['city'] : "&nbsp";
  $state = ( isset($row['state'] ) ? $row['state'] : "&nbsp";
  $zip = ( isset($row['zip'] ) ? $row['zip'] : "&nbsp";
 
  # Display data
  $display_block .= <<<ENDLINE

  <tr>
   <td>$sub_office</td>
   <td>$fname $lname</td>
   <td>$homephone / $cellphone</td>
   <td>$email</td>
   <td>$address, $city, $state $zip</td>
   <td>$sub_name</td>
  </tr>
ENDLINE;
}

$title = "<title>Officers of the $sub_name Subordinate Grange</title>";
?>
<? include("./config/header.php"); ?>
</head>
<body>
<table width="85%" border="1" class="main">
  <tbody>
    <tr>
      <td colspan="6"><h2>Officers of <? echo "$sub_name"; ?> Subordinate Grange</h2></td>
    </tr>
    <tr>
      <td><b>Office</b></td>
      <td><b>Officer</b></td>
      <td><b>Home / Cell Phone #</b></td>
      <td><b>Email</b></td>
      <td><b>Address</b></td>
      <td><b>Subordinate Grange</b></td>
    </tr>
<? echo "$display_block"; ?>
    <tr>
      <td colspan="6"><!-- C. FOOTER AREA -->
        <div class="footer">
<? include ('./config/footerinfo.php'); ?>
        </div></td>
    </tr>
  </tbody>
</table>
</body>
</html>

This is still a no go. also if you look at
$zip = (isset($row) ? $row : "&nbsp;");
They are not mis matched.
Oh well, I'll keep fooling with it until I get it figured out.

Member Avatar for langsor

You're right about the not-mismatched ... guess I had already fooled with it some before noticing that I had un-matched the brackets :-\

So, what's not working...are you getting errors, is the html not formatting correctly, are you not getting data from the database ... ?

Just trying to help.

The names and offices show up properly, but none of the contact info shows up, wether logged in or not. I would assume that the sql query's are working fine, just the if logged in and if officer aren't working.

I do appreciate the help!

Member Avatar for langsor

Okay, I came in after-the-fact on this post and now I'm going to back up and take it from the start ... wish me luck.

First of all you need to test and make certain you have a 'true' value for session status when a member is logged in. That could be your only problem right now.

Please check my code to make sure all of your database login and table field names are correct -- change my code values if they are not
And check that the SESSION names-values are correct, and working

<?php

include( './config/db.php' );
$sub_name = $_GET['sub_name']; // If it's a form value, you probably want to use $_POST['sub_name'] with <form method="POST">

$officers = array( "President", "Vice President", "Secretery" ); // edit to suite your needs ...

session_start();
$session = ( $_SESSION['auth']['status'] == 1 ) ? TRUE : FALSE; // or ? ... ( $_SESSION['User']['status'] == 1 )

$link = @mysql_connect( $host, $user, $pass ) or die( mysql_error() );
@mysql_select_db( $dbase ) or die( mysql_error() );
// make sure these are the names of the fields in your database, change them in
// the $sql query `email` (etc) and the $obj->email values (bellow) if they are not ...
$sql = "SELECT `fname`, `lname`, `sub_office`, `email`, `homephone`, `cellphone`, `address`, `city`, `state`, `zip` FROM `MembersTable` WHERE sub_name='$sub_name'";
// You can just use this, if thesse are all or most of the values in the table ...
// $sql = "SELECT * FROM `MembersTable` WHERE sub_name='$sub_name'";
$result = mysql_query( $sql ) or die( mysql_error() );

$html = '';
while ( $obj = mysql_fetch_object( $result ) ) {
  $html .= <<<ENDHTML
    <tr>
      <td>$obj->sub_office</td>
      <td>$obj->fname $obj->lname</td>
ENDHTML;
  if ( $session || in_array( $obj->sub_office, $officers ) ) {
    $html .=<<<ENDHTML
      <td>$$obj->hphone / $cphone</td>
      <td>$$obj->email</td>
      <td>$$obj->address, $$obj->cty, $$obj->state $$obj->zip</td>
      <td>$$obj->sub_name</td>
ENDHTML;
  }
  $html .= "    <\tr>\n";
}

$title = "<title>Officers of the $sub_name Subordinate Grange</title>";

?>
<?php include("./config/header.php"); ?>
</head>
<body>
<table width="85%" border="1" class="main">
  <tbody>
    <tr>
      <td colspan="6"><h2>Officers of <? echo "$sub_name"; ?> Subordinate Grange</h2></td>
    </tr>
    <tr>
      <td><b>Office</b></td>
      <td><b>Officer</b></td>
      <td><b>Home / Cell Phone #</b></td>
      <td><b>Email</b></td>
      <td><b>Address</b></td>
      <td><b>Subordinate Grange</b></td>
    </tr>
<?php print $html; ?>
    <tr>
      <td colspan="6"><!-- C. FOOTER AREA -->
        <div class="footer">
<?php include ('./config/footerinfo.php'); ?>
        </div></td>
    </tr>
  </tbody>
</table>
</body>
</html>

Okay, my eyes are so blurry I can't read...hope I didn't make too many type-os

Ciao

Hi, yours seems to be really close except:
There is a bunch of < r> before the table (I can't find where they are coming from),
I also would like to insert &nbsp; into a cell if the sql row is empty for that column (to make the table look better)

I did have to make a couple of changes to make things work, but this is the closest I've gotten yet.

<?php

include( './config/db.php' );

$sub_name = $_GET['sub_name']; // If it's a form value, you probably want to use $_POST['sub_name'] with <form method="POST">

$officers = array( "Master - President", "Overseer - Vice President", "Secretery", "Lecturer - Program Director" ); // edit to suite your needs ...

session_start();
$session = ( $_SESSION['auth']['status'] == 1 ) ? TRUE : FALSE; // or ? ... ( $_SESSION['User']['status'] == 1 )

$link = @mysql_connect( $server, $dbusername, $dbpassword ) or die( mysql_error() );
@mysql_select_db( $db_name ) or die( mysql_error() );
// make sure these are the names of the fields in your database, change them in
// the $sql query `email` (etc) and the $obj->email values (bellow) if they are not ...

$sql = "SELECT `fname`, `lname`, `sub_office`, `email`, `homephone`, `cellphone`, `address`, `city`, `state`, `zip`,
(SELECT 
  CASE
   WHEN `sub_office` = 'Master - President' THEN 1
   WHEN `sub_office` = 'Overseer - Vice President' THEN 2
   WHEN `sub_office` = 'Lecturer - Program Director' THEN 3
   WHEN `sub_office` = 'Steward' THEN 4
   WHEN `sub_office` = 'Assistant Steward' THEN 5
   WHEN `sub_office` = 'Lady Assistant Steward' THEN 6
   WHEN `sub_office` = 'Chaplain' THEN 7
   WHEN `sub_office` = 'Treasurer' THEN 8
   WHEN `sub_office` = 'Building Treasurer' THEN 9
   WHEN `sub_office` = 'Secretary' THEN 10
   WHEN `sub_office` = 'Gate Keeper' THEN 11
   WHEN `sub_office` = 'Flora' THEN 12
   WHEN `sub_office` = 'Pomona' THEN 13
   WHEN `sub_office` = 'Ceres' THEN 14
   WHEN `sub_office` = 'Executive Committee' THEN 15
   WHEN `sub_office` = 'Pianist' THEN 16
   WHEN `sub_office` = 'Leadership' THEN 17
   WHEN `sub_office` = 'Membership' THEN 18
   WHEN `sub_office` = 'Legislative' THEN 19
   WHEN `sub_office` = 'Family Activities' THEN 20
   WHEN `sub_office` = 'Community Service' THEN 21
   WHEN `sub_office` = 'Fun Committee' THEN 22
   ELSE 23
  END) as sort
 FROM `MembersTable` WHERE sub_name='$sub_name'

ORDER BY sort ASC";

// You can just use this, if thesse are all or most of the values in the table ...
// $sql = "SELECT * FROM `MembersTable` WHERE sub_name='$sub_name'";
$result = mysql_query( $sql ) or die( mysql_error() );

$html = '';
while ( $obj = mysql_fetch_object( $result ) ) {
  $html .= <<<ENDHTML
    <tr>
      <td>$obj->sub_office</td>
      <td>$obj->fname $obj->lname</td>
ENDHTML;
  if ( $session || in_array( $obj->sub_office, $officers ) ) {
    $html .=<<<ENDHTML
      <td>$obj->homephone / $obj->cellphone</td>
      <td>$obj->email</td>
      <td>$obj->address, $obj->city, $obj->state $obj->zip</td>
      <td>$obj->sub_name</td>
ENDHTML;
  }
  $html .= "    <\tr>\n";
}

$title = "<title>Officers of the $sub_name Subordinate Grange</title>";

?>
<?php include("./config/header.php"); ?>
</head>
<body>
<table width="85%" border="1" class="main">
  <tbody>
    <tr>
      <td colspan="6"><h2>Officers of <? echo "$sub_name"; ?> Subordinate Grange</h2></td>
    </tr>
    <tr>
      <td><b>Office</b></td>
      <td><b>Officer</b></td>
      <td><b>Home / Cell Phone #</b></td>
      <td><b>Email</b></td>
      <td><b>Address</b></td>
      <td><b>Subordinate Grange</b></td>
    </tr>
<?php print $html; ?>
    <tr>
      <td colspan="6"><!-- C. FOOTER AREA -->
        <div class="footer">
<?php include ('./config/footerinfo.php'); ?>
        </div></td>
    </tr>
  </tbody>
</table>
</body>
</html>

I added the SELECT CASE so I could order the offices in a set order.
I do have the 'sub_name' sent from a form currently, but I don't want to switch it to the POST method. I want to keep it so I can make a link and have it go directly to a certain one, therefore I believe it should stay as GET.

Thanks for all your help so far.

Member Avatar for langsor

I was laying there in bed trying to go to sleep and realized I had not added the contingency for empty values or empty table cells ...

Try this

$html = '';
while ( $obj = mysql_fetch_object( $result ) ) {
  $display = ( $session || in_array( $obj->sub_office, $officers ) ? TRUE : FALSE;
  $homephone = ( $display && $obj->homephone ) ? $obj->homephone : '&nbsp';
  $cellphone = ( $display && $obj->cellphone ) ? $obj->cellphone : '&nbsp';
  $email = ( $display && $obj->email ) ? $obj->email : '&nbsp';
  $address = ( $display && $obj->address ) ? $obj->address : '&nbsp';
  $city = ( $display && $obj->city ) ? $obj->city : '&nbsp';
  $state = ( $display && $obj->state ) ? $obj->state : '&nbsp';
  $zip = ( $display && $obj->zip ) ? $obj->zip : '&nbsp';
  $sub_name = ( $display && $obj->sub_name ) ? $obj->sub_name : '&nbsp';
  $html .= <<<ENDHTML
    <tr>
      <td>$obj->sub_office</td>
      <td>$obj->fname $obj->lname</td>
      <td>$homephone / $cellphone</td>
      <td>$email</td>
      <td>$address, $city, $state $zip</td>
      <td>$sub_name</td>
    <\tr>
ENDHTML;
}

Hope this works for you.

Out of curiosity, what are you doing with these lines in the SELECT statement?

(SELECT 
  CASE
   WHEN `sub_office` = 'Master - President' THEN 1

...

Ok, First off the table looks better, but for some reason I'm getting < r> tags at the end of each row. These are place before the table. I don't understand where they are coming from.

The

(SELECT 
  CASE
   WHEN `sub_office` = 'Master - President' THEN 1

lines are to sort the results by a list I set. I don't want the rows sorted alphabetically. I want them according to office, eg the most important office (president) office to the lesser important (the committees).

Thanks Again for you wonderful help.

Member Avatar for langsor

I'm getting < r> tags at the end of each row. These are place before the table. I don't understand where they are coming from

I don't see them from the code you submitted earlier or my updates ... if you want to post your code in total again I'll sift through that for them.

Could they be coming from one of the include files?

Could they be coming from some copy-paste-replacement you made and left a telltale < r> (say from <tr> or <br>)?

Just guessing

And you're welcome about the help :-)

Hi again. I just found where they were coming from. you had typed <\tr> when it should have been </tr>. Once I changed that it made everything show, if I dare say, perfectly. By the way I knew the include files were fine.

Thanks Alot!

Member Avatar for langsor

I knew I was pretty tired when I wrote that stuff...glad you found the source of the problem.

Take care

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.