0

I am using name field to get the option in dropdown from database

datas in my database
id name
1 Resham
2 Rukshana
3 Resham
4 sindya

how to write a php code for displaying the name only once in dropdown and if the

option Resham is choosen

then my output should be as

id name
1 Resham
3 Resham

and if the option sindhiya is choosen then my output should be as

id name
4 sindhiya

Please give me an example in php to do this

2
Contributors
4
Replies
19
Views
3 Years
Discussion Span
Last Post by My question
0

Below follows my code, in this,

I am using ind_reg_name field to get the option in dropdown from database

Datas in my database

id name
1 Resham
2 Rukshana
3 Resham
4 sindya

how to write a php code for displaying the name only once in dropdown and

if the option Resham is choosen then o/p should be as:

id name
1 Resham
3 Resham

and

if the option sindhiya is choosen then my o/p should be as:

id name
4 sindhiya

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;
    ?>
0

WHy don't you use a GROUP BY name and use a MAX on the id in the SQL?

SELECT MAX(ind_reg_id), ind_reg_name FROM ind_register GROUP BY ind_reg_name
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.