Skip to main content
Known Participant
May 8, 2012
Question

Drop down list based on log in username - php mysql

  • May 8, 2012
  • 1 reply
  • 1695 views

I have a drop down list of client names on a php page that filters a second drop down list of site names.

At the moment, any user who logs in can see the entire list of clients, however I want to filter the list based on their username log in.

This is the entire page code, the section in bold is the drop down list in question:

<?php

if (!isset($_SESSION)) {

  session_start();

}

$MM_authorizedUsers = "asguser,admin,member";

$MM_donotCheckaccess = "false";

// *** Restrict Access To Page: Grant or deny access to this page

function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {

  // For security, start by assuming the visitor is NOT authorized.

  $isValid = False;

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username.

  // Therefore, we know that a user is NOT logged in if that Session variable is blank.

  if (!empty($UserName)) {

    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login.

    // Parse the strings into arrays.

    $arrUsers = Explode(",", $strUsers);

    $arrGroups = Explode(",", $strGroups);

    if (in_array($UserName, $arrUsers)) {

      $isValid = true;

    }

    // Or, you may restrict access to only certain users based on their username.

    if (in_array($UserGroup, $arrGroups)) {

      $isValid = true;

    }

    if (($strUsers == "") && false) {

      $isValid = true;

    }

  }

  return $isValid;

}

$MM_restrictGoTo = "accessdenied.html";

if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {  

  $MM_qsChar = "?";

  $MM_referrer = $_SERVER['PHP_SELF'];

  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";

  if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0)

  $MM_referrer .= "?" . $_SERVER['QUERY_STRING'];

  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);

  header("Location: ". $MM_restrictGoTo);

  exit;

}

?>

<html>

<body>

<script type="text/javascript">

function AjaxFunction(client_UID)

{

var httpxml;

try

  {

  // Firefox, Opera 8.0+, Safari

  httpxml=new XMLHttpRequest();

  }

catch (e)

  {

  // Internet Explorer

          try

                        {

                    httpxml=new ActiveXObject("Msxml2.XMLHTTP");

                    }

              catch (e)

                    {

                try

              {

              httpxml=new ActiveXObject("Microsoft.XMLHTTP");

              }

                catch (e)

              {

              alert("Your browser does not support AJAX!");

              return false;

              }

            }

  }

function stateck()

    {

    if(httpxml.readyState==4)

      {

var myarray=eval(httpxml.responseText);

// Before adding new we must remove previously loaded elements

for(j=document.testform.subcat.options.length-1;j>=0;j--)

{

document.testform.subcat.remove(j);

}

for (i=0;i<myarray.length;i++)

{

var optn = document.createElement("OPTION");

optn.text = myarray;

optn.value = myarray;

document.testform.subcat.options.add(optn);

}

      }

    }

    var url="dd.php";

url=url+"?client_UID="+client_UID;

url=url+"&sid="+Math.random();

httpxml.onreadystatechange=stateck;

httpxml.open("GET",url,true);

httpxml.send(null);

  }

</script>

<form name="testform" method='POST' action='mainck.php'>

 

Select first one

  <select name=cat onChange="AjaxFunction(this.value);">

  <option value=''>Select One</option>

<?

require "config.php";// connection to database

$q=mysql_query("SELECT DISTINCT * FROM qry_test GROUP BY client_name ASC");

while($n=mysql_fetch_array($q)){

echo "<option value=$n[client_UID]>$n[client_name]</option>";

}

?>

</select>

<select name=subcat>

</select><input type=submit value=submit>

</form>

</body>

</html>

----------------------------------------------------------------------------------------------------------------------------------------------

I think I need to amend the sql statement to something like this - but I haven't quite got it right:

SELECT DISTINCT * FROM qry_test WHERE username = colname GROUP BY client_name ASC

Where do I drop the code for the colname info?

name:colname

type: text

default value: -1

run time value: $_SESSION['MM_Username']

----------------------------------------------------------------------------------------------------------------------------------------------

Thanks!

This topic is closed to new replies. Start a new post to keep the conversation going.

1 reply

Participating Frequently
May 8, 2012

I don't do PHP but it would be something like:

$sql = sprintf (SELECT DISTINCT * FROM qry_test WHERE username = %s GROUP BY client_name ASC, $_SESSION['MM_Username'] );

or you can just embed the session variable into the sql string.

But why are you using the GROUP BY clause? If you just need a distinct list of names, use the DISTINCT keyword and reduce your select list to only the needed column.