I am chosing an option from database using the field named as "name". since i have the name "supriya" 3 times in my database, it displays 3 times in my dropdown.

How do i restrict it by displaying the name only one time and once it is choosen, then it fetches all the 3 records having the name "supriya" and its id too.

Recommended Answers

All 12 Replies

where to use DISTINCT.

I am new to php, please help me

Below follows my code, where should i use DISTINCT here

INDEX.php

<html>
<head>
<script type="text/javascript">
function stateChanged(){
if(httpxml.readyState==4){

var myObject = eval('(' + httpxml.responseText + ')');


//var myObject = httpxml.responseText;
//document.getElementById("display").innerHTML=myObject;





var msg=myObject.value[0].message;
if(msg.length > 0){document.getElementById("msg").innerHTML=msg;}
else{document.getElementById("msg").style.display='none';}

var str="<table width='50%' bgcolor='#ffffff' align=center><tr><th>Name</th><th>Idea</th></tr>";
var color="#f1f1f1";
for(i=0;i<myObject.data.length;i++)
{ 
if((i%2)==0){color="#ffffff";}
else{color="#f1f1f1";}
str = str + "<tr bgcolor="+color+"><td>" + myObject.data[i].ind_reg_id + " </td><td>"+ myObject.data[i].ind_reg_name + "</a></td></tr>"

}



str = str + "</table>" ;


document.getElementById("display").innerHTML=str;
}
}

var url="subcat2.php";
var ind_reg_id=document.myForm.ind_reg_id.value;
url=url+"?ind_reg_id="+ind_reg_id;

url=url+"&kid="+Math.random();
//alert(url)
httpxml.onreadystatechange=stateChanged;
httpxml.open("GET",url,true);
httpxml.send(null);
// document.getElementById("display").innerHTML="Please Wait....";
document.getElementById("msg").style.background='#f1f1f1';
document.getElementById("msg").innerHTML="Please Wait ... ";
document.getElementById("msg").style.display='inline';
}

</script>

<?php
require "z_db.php";
echo "</head><body onload=\"ajaxFunction()\";>";
echo "<center><table border='0' width='100%' cellspacing='0' cellpadding='0' > <tr bgcolor='#ffffcc'><form name=myForm method='post' onSubmit=\"ajaxFunction(this.form); return false\">


<td align='center' colspan=2><font face='verdana, arial, helvetica' size='2' ><b> Select a Domain</b> </font></td></tr>";
echo "<tr>";
echo "<td align='center'>";
$query="SELECT * FROM ind_register order by  ind_reg_name";
$result=mysql_query($query);
echo mysql_error();
echo "<select name=ind_reg_id onChange=\"ajaxFunction()\"><option value=0>Show All</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[ind_reg_id]>$nt[ind_reg_name]</option>";
}

echo "</select>";
echo "</font></td>";
echo "</tr></form>";
echo "</table>";


?>
<div id=msg style="position:absolute; z-index:1; left: 1100px; top: 0px;" >This is message area</div>

<div id="display"><b>Records will be displayed here</b></div>

</body>
</html>

Try this

 <html>
    <head>
    <script type="text/javascript">
    function stateChanged(){
    if(httpxml.readyState==4){
    var myObject = eval('(' + httpxml.responseText + ')');
    //var myObject = httpxml.responseText;
    //document.getElementById("display").innerHTML=myObject;
    var msg=myObject.value[0].message;
    if(msg.length > 0){document.getElementById("msg").innerHTML=msg;}
    else{document.getElementById("msg").style.display='none';}
    var str="<table width='50%' bgcolor='#ffffff' align=center><tr><th>Name</th><th>Idea</th></tr>";
    var color="#f1f1f1";
    for(i=0;i<myObject.data.length;i++)
    { 
    if((i%2)==0){color="#ffffff";}
    else{color="#f1f1f1";}
    str = str + "<tr bgcolor="+color+"><td>" + myObject.data[i].ind_reg_id + " </td><td>"+ myObject.data[i].ind_reg_name + "</a></td></tr>"
    }
    str = str + "</table>" ;
    document.getElementById("display").innerHTML=str;
    }
    }
    var url="subcat2.php";
    var ind_reg_id=document.myForm.ind_reg_id.value;
    url=url+"?ind_reg_id="+ind_reg_id;
    url=url+"&kid="+Math.random();
    //alert(url)
    httpxml.onreadystatechange=stateChanged;
    httpxml.open("GET",url,true);
    httpxml.send(null);
    // document.getElementById("display").innerHTML="Please Wait....";
    document.getElementById("msg").style.background='#f1f1f1';
    document.getElementById("msg").innerHTML="Please Wait ... ";
    document.getElementById("msg").style.display='inline';
    }
    </script>
    <?php
    require "z_db.php";
    echo "</head><body onload=\"ajaxFunction()\";>";
    echo "<center><table border='0' width='100%' cellspacing='0' cellpadding='0' > <tr bgcolor='#ffffcc'><form name=myForm method='post' onSubmit=\"ajaxFunction(this.form); return false\">
    <td align='center' colspan=2><font face='verdana, arial, helvetica' size='2' ><b> Select a Domain</b> </font></td></tr>";
    echo "<tr>";
    echo "<td align='center'>";
    $query="SELECT DISTINCT FROM ind_register order by  ind_reg_name";
    $result=mysql_query($query);
    echo mysql_error();
    echo "<select name=ind_reg_id onChange=\"ajaxFunction()\"><option value=0>Show All</option>";
    while($nt=mysql_fetch_array($result)){
    echo "<option value=$nt[ind_reg_id]>$nt[ind_reg_name]</option>";
    }
    echo "</select>";
    echo "</font></td>";
    echo "</tr></form>";
    echo "</table>";
    ?>
    <div id=msg style="position:absolute; z-index:1; left: 1100px; top: 0px;" >This is message area</div>
    <div id="display"><b>Records will be displayed here</b></div>
    </body>
    </html>

No. this doesn't work

when i use DISTINCT the field in dropdown is displayed only once, but the dropdown doesnot reacts, whereaes i used GROUP BY which display the repeated field only once and also dropdown reacts

*But the problem on using GROUPBY is it fetches only one record, so what should i do *

I have the fieldname as name and id as id,
My data in database is

1 priya
2 hema
3 himanshu
4 sanjjev
5 himanshu
6 priya
7 asif
.
.
.

now i want to get the names in dropdown where it appears redundany of names but i need to display the names only once in dropdown and if the option in dropdown is choosen it should fetch the data regarding from database.

eg, if i choose the option himanshu in dropdown then the o/p should be as
3 himanshu
5 himanshu
8 himanshu.

how to code for this in php.

Just select those names (with group by or distinct), but don't use their id in the dropdown. Pass the name around instead.

SELECT DISTINCT ind_reg_name FROM ind_register ORDER BY ind_reg_name

sry this is not working

index.php

<html>
<head>

<script type="text/javascript">
function ajaxFunction()
{

//document.writeln(val)
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 stateChanged(){
if(httpxml.readyState==4){

var myObject = eval('(' + httpxml.responseText + ')');


//var myObject = httpxml.responseText;
//document.getElementById("display").innerHTML=myObject;





var msg=myObject.value[0].message;
if(msg.length > 0){document.getElementById("msg").innerHTML=msg;}
else{document.getElementById("msg").style.display='none';}

var str="<table width='50%' bgcolor='#ffffff' align=center><tr><th>Name</th><th>Idea</th></tr>";
var color="#f1f1f1";
for(i=0;i<myObject.data.length;i++)
{ 
if((i%2)==0){color="#ffffff";}
else{color="#f1f1f1";}
str = str + "<tr bgcolor="+color+"><td>" + myObject.data[i].ind_reg_id + " </td><td>"+ myObject.data[i].ind_reg_name + "</a></td></tr>"

}



str = str + "</table>" ;


document.getElementById("display").innerHTML=str;
}
}

var url="subcat2.php";
var ind_reg_id=document.myForm.ind_reg_id.value;
url=url+"?ind_reg_id="+ind_reg_id;

url=url+"&kid="+Math.random();
//alert(url)
httpxml.onreadystatechange=stateChanged;
httpxml.open("GET",url,true);
httpxml.send(null);
// document.getElementById("display").innerHTML="Please Wait....";
document.getElementById("msg").style.background='#f1f1f1';
document.getElementById("msg").innerHTML="Please Wait ... ";
document.getElementById("msg").style.display='inline';
}

</script>

<?php
require "z_db.php";
echo "</head><body onload=\"ajaxFunction()\";>";
echo "<center><table border='0' width='100%' cellspacing='0' cellpadding='0' > <tr bgcolor='#ffffcc'><form name=myForm method='post' onSubmit=\"ajaxFunction(this.form); return false\">


<td align='center' colspan=2><font face='verdana, arial, helvetica' size='2' ><b> Select a Domain</b> </font></td></tr>";
echo "<tr>";
echo "<td align='center'>";




$query="SELECT *  FROM ind_register GROUP BY  ind_reg_name";


$result=mysql_query($query);
echo mysql_error();
echo "<select name=ind_reg_id onChange=\"ajaxFunction()\"><option value=0>Show All</option>";
while($nt=mysql_fetch_array($result)){













echo "<option value=$nt[ind_reg_id]>$nt[ind_reg_name]</option>";


}

echo "</select>";
echo "</font></td>";
echo "</tr></form>";
echo "</table>";


?>
<div id=msg style="position:absolute; z-index:1; left: 1100px; top: 0px;" >This is message area</div>

<div id="display"><b>Records will be displayed here</b></div>

</body>
</html>

subcat2.php

<?Php

require "z_db.php"; 

@$ind_reg_id=$_GET['ind_reg_id'];
//$cat_id=1;

if(!is_numeric($ind_reg_id)){
echo "Data Error ";
exit;
}

$message="";

if($ind_reg_id>0){
$q=mysql_query("select ind_reg_id, ind_reg_name from ind_register where ind_reg_id=$ind_reg_id order by ind_reg_name");



}else{
$q=mysql_query("select ind_reg_id, ind_reg_name from ind_register order by ind_reg_name ");       


$ind_reg_id=0;
}
@$message .= mysql_error();

$str= "{ \"data\" : [ ";

while($nt=mysql_fetch_array($q)){
$str=$str."{\"ind_reg_id\" : \"$nt[ind_reg_id]\", \"ind_reg_name\" : \"$nt[ind_reg_name]\"},";
//$str=$str."{"myclass" : "$nt[class]"},";

}
$str=substr($str,0,(strLen($str)-1));
$message=$message. " Records displayed";
$str=$str."],\"value\" : [{\"ind_reg_id\" : $ind_reg_id,\"message\" : \"$message\"}]}";
//echo json_encode($str);

echo $str;
?>

Here i have same values for int_reg_id
so, it dspalys more then one time in dropdown
how to get over it

dropdown should displays only one option insted of duplicating values, and if that particular option is choosen then , both id and name should be fetched from database, according to the selected option in drodown.

PLZ help me.......

Here i have same values for int_reg_id

I already said NOT to use the id.

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.