-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.