0

-Suppose I have a table "User_Gender" with fields "idGender,Gender_Name" that has Genders stored in it as Male and Female.
-And I have a Select Input Field on My say html.php:

<select name="Gender_Selector" onchange="document.forms[0].submit();">
<!-- USER GENDER FROM DATABASE IS CALLED HERE-->
<?php $System->Genders();?>
<!-- USER GENDER FROM DATABASE ENDS HERE-->
</select>

-And so I have the Function "Genders()" in My say class.php
-Now the Function is in two version the Prepare and without the use of Prepare MySqli Function:

VERSION 1:

public function Genders()
        {
        /* THIS FUNCTION RETURNS A CONNECTION TO MY DATABASE*/
        $MySqli=$this->MySqli_Instantiator();

        $Sql="SELECT idGender,Gender_Name FROM User_Gender ORDER BY idGender ASC";
        $Query=$MySqli->query($Sql);
        $Rows_Count=$Query->num_rows;

        $Gender_Array= array();
        $Count=0;

        while($Fetch=$Query->fetch_array())
                {
                    $Count=$Count+1;
                    $Last_Cout=$Count;
                    $Rows_Count=$Rows_Count-1;

                    $Gender_Array[$Count]['Value']=$Fetch["idGender"];
                    $Gender_Array[$Count]['Name']=$Fetch["Gender_Name"];

                    if($Rows_Count==0)
                    {       
                        for($First=1;$First<=$Last_Cout;$First++) 
                        {
                            $Selected = "";
                            if($_REQUEST["Gender_Selector"] == $Gender_Array[$First]['Value']) 
                                {
                                $Selected = "selected";
                                }   
                            echo "<option ".$Selected." value=".$Gender_Array[$First]['Value'].">".$Gender_Array[$First]['Name']."</option>";
                        }
                    }
                }
        }

-This Version seems to work fine for Me and I get the Results as required in My Select Input field with two Options:
1.Male
2.Female

VERSION 2:

public function Genders()
        {
        $MySqli=$this->MySqli_Instantiator();

        $Sql="SELECT idGender,Gender_Name FROM User_Gender ORDER BY idGender ASC";
        $Query=$MySqli->prepare($Sql);
        $Query->execute();
        $Query-bind_result($idGender,$Gender_Name);
        $Rows_Count=$Query->num_rows;

        $Gender_Array= array();
        $Count=0;

        while($Fetch=$Query->fetch())
                {
                    $Count=$Count+1;
                    $Last_Cout=$Count;
                    $Rows_Count=$Rows_Count-1;

                    $Gender_Array[$Count]['Value']=$idGender;
                    $Gender_Array[$Count]['Name']=$Gender_Name;

                    if($Rows_Count==0)
                    {       
                        for($First=1;$First<=$Last_Cout;$First++) 
                        {
                            $Selected = "";
                            if($_REQUEST["Gender_Selector"] == $Gender_Array[$First]['Value']) 
                                {
                                $Selected = "selected";
                                }   
                            echo "<option ".$Selected." value=".$Gender_Array[$First]['Value'].">".$Gender_Array[$First]['Name']."</option>";
                        }
                    }
                }
        $Query->close();
        }

-With this Version it seems NOT to work fine for Me and I get the Results with One More/Firts record repeated in My Select Input field and so I get three Options with One of them repeated:
1.Male
1.Male
2.Female

QN:
-What is that I'm doying wrong? because even if I use it on Multiple records there seems to be More repeated records and I dont like it.
-Any "DO's" and "DONT DO's"?
-Any clarification is appreciated.

2
Contributors
6
Replies
25
Views
2 Years
Discussion Span
Last Post by Bile
0

-Oh,thak You Traevel for showing Me that I skipped it when I was writting here but its there so in the Original code.

0

I don't get the whole Gender_Array part. You store the results from the query in a multidimensional array only to retrieve it again in the same loop? And the rows_count? Are you doing a double loop then countering the double loop with row count? Why not one loop.

    while($Query->fetch()){
        $Selected = "";
        if($_REQUEST["Gender_Selector"] == $idGender){
            $Selected = "selected";
        }
        echo "<option ".$Selected." value=".$idGender.">".$Gender_Name."</option>";
    }

Would that not suffice?

0

-Yeap,Its true about Gender_Array and all what You said.I find its Importance when I het to a point where I have a default value say "Select Gender" which I assign it to the Same array prior to the loop as it doesnt come form the database and so to speak I do the trick with this:

$Gender_Array= array();
$Count=0;
$Gender_Array[$Count]['Value']=0;
$Gender_Array[$Count]['Name']="Select Gender";

And so it becomes the firts entry in My Options.
The Only thing I use to control which loop is executed is by using the rows_count the external loop while($Query->fetch()){} is executed to fetch all the entries in My database and so when rows_count reaches 0 that is all the entries are loaded to My Gender_Array I start to loop through the Gender_Array and load them in My Options.
-Is the Idea so far good? And Mind You I just used the Gender thing but I have real data that keeps on growing as long as new entry requirement comes.

0

I don't know what you mean. But if it works for you then use it in this way.

Though I reckon the double printing comes from the loop-within-loop part.

0

-Ok Traevel,but as I said,its only when I use the prepare function thats when the double printing ocurs but when I use the other version I posted (VERSION 1) everything goes perfect.

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.