Jump to content

How to select data of a select list from database table

- - - - -

  • Please log in to reply
26 replies to this topic

#1
justsachin4u

justsachin4u

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Hi frz, i want to know that how we can select data of a select list from database table. Suppose I have a form that possesses three select list for Date of Birth. The dob --- date, month and year is stored in a database table. When user edits his profile then all the value from database table should be fetched in related form fields. I am able to select all the values in textbox, checkbox, radio button etc. but facing problem in selecting select list data. The dob should be selected when user comes in that editing profile page. Your help is needed urgently. thanx in advance. I am waiting...

#2
Revolt

Revolt

    Programmer

  • Members
  • PipPipPip
  • 99 posts
Not sure what your problem really is.

Is it that you can't get the data from the database?

Or is it that you don't know how to select the specific item in a list that reflects the option currently in the server? I think this is your problem so I'll try to show you how to do it.

Ok so you probably generate a list from a bunch of options you have in your database and get something like this:

<select>

<option>Option 1</option>

<option>Option 2</option>

<option>Option 3</option>

...

</select>

What you have to do is in the cycle that generates this select (assuming you have a cycle), add a check to compare each option with the one you got from the database:


$result = mysql_query("Your query string here");

$selectedOptionInDB = mysql_result($result, 0);

$html = "<select>";

for($i = 0; $i < $number_options; $i++) {

    if ($selectedOptionInDB == $option[i]) {

        $html .= "<option selected='selected'>";

    } else {

        $html .= "<option>";

    }

    $html .= $option[i] . "</option>";

}

The trick is the selected attribute in the option element.

#3
justsachin4u

justsachin4u

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Here is my code -

<label for="cmbday">DOB: </label>
<select name="cmbday" id="cmbday">
<option value="DD">DD</option>
<?php
for($i=1;$i<=31;$i++)
{
echo "<option value='$i'>$i</option>";
}
?>
</select>
<select name="cmbmonth" id="cmbmonth">
<option value="MM">MM</option>
<?php
for($i=1;$i<=12;$i++)
{
echo "<option value='$i'>$i</option>";
}
?>
</select>
<select name="cmbyear" id="cmbyear">
<option value="YY">YY</option>
<?php
for($i=2000;$i>=1950;$i--)
{
echo "<option value='$i'>$i</option>";
}
?>
</select>


Suppose if some user's date of birth is 15-07-1980 and he want to edit his profile, then his dob should be pre-selected in the select list like this -

C:\Documents and Settings\Administrator\My Documents\My Pictures

#4
justsachin4u

justsachin4u

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Here is my code -

<label for="cmbday">DOB: </label>
<select name="cmbday" id="cmbday">
<option value="DD">DD</option>
<?php
for($i=1;$i<=31;$i++)
{
echo "<option value='$i'>$i</option>";
}
?>
</select>
<select name="cmbmonth" id="cmbmonth">
<option value="MM">MM</option>
<?php
for($i=1;$i<=12;$i++)
{
echo "<option value='$i'>$i</option>";
}
?>
</select>
<select name="cmbyear" id="cmbyear">
<option value="YY">YY</option>
<?php
for($i=2000;$i>=1950;$i--)
{
echo "<option value='$i'>$i</option>";
}
?>
</select>


Suppose if some user's date of birth is 15-07-1980 and he want to edit his profile, then his dob should be pre-selected in the select list like this -

Attached File  Form.JPG   11.08K   21 downloads

#5
Revolt

Revolt

    Programmer

  • Members
  • PipPipPip
  • 99 posts
Then I've already answered your question.

I suppose you know how to get that info from the database right?

Then assuming that info is in $day, $month, $year you have to change each of the for cycles as follows:

for($i = 1; i <= 31; $i++) {

    $selected = "";

    if ($day == $i) {

        $selected = "selected='selected'";

    }

    echo "<option value='$i' " . $selected . " >$i</option>";

}


#6
justsachin4u

justsachin4u

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
thanx a lot my frd. god bless u. if i'll face some other problem, may i ask you here???

#7
Revolt

Revolt

    Programmer

  • Members
  • PipPipPip
  • 99 posts
You're welcome to create a new topic with your problem and me or one of the other members will be glad to help you out ;)

#8
justsachin4u

justsachin4u

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
My frd I have a PHP script which is based on Associative Array. It is actually a horoscope type application. It shows visitor the description of various zodiac signs. When they click on a link, the related information of that zodiac sign is shown. First of all you check this script. Pictures will be not shown because I am not uploading pics due to slow net. But that is not an issue. Below is my script -

<html>
<head>
<title>PHP Arrays</title>
<style type="text/css">
.box
{
color:#0000FF;
text-decoration:none;
border:1px solid red;
width:15px;
padding:1px 5px;
margin:5px;
}
.box:hover
{
color:white;
background-color:#FF0000;
}
.nobox
{
color:#0000FF;
text-decoration:none;
}
.nobox:hover
{
color:#FF0000;
text-decoration:underline;
}
</style>
<?php
$zodiac = array(
1=>array("Aries","Your element: Fire","Your ruling planets: Mars","Symbol: The Ram","Your stone: Ruby","Aries Secret Desire: To lead the way for others"),2=>array("Taurus","Your element: Earth","Your ruling planets: Venus","Symbol: The Bull","Your stone: Emerald","Taurus Secret Desire: To have a secure,happy and wealthy life/marriage"),3=>array("Gemini","Your element: Air","Your ruling planets: Mercury","Symbol: The Twins","Your stone: Aquamarine","Gemini's Secret Desire: To be ahead of the crowd"),4=>array("Cancer","Your element: Water","Your ruling planets: Moon","Symbol: The Crab","Your stone: Moonstone","Cancer's Secret Desire: To feel safe (emotionally,spiritually,romantically and financially)"),5=>array("Leo","Your element: Fire","Your ruling planets: Sun","Symbol: The Lion","Your stone: Peridot","Leo's Secret Desire: To be a star"),6=>array("Virgo","Your element: Earth","Your ruling planets: Mercury","Symbol: The Virgin","Your stone: Sapphire","Virgo's Secret Desire: To love and be loved in return"),7=>array("Libra","Your element: Air","Your ruling planets: Venus","Symbol: The Scales","Your stone: Opals","Libran's Secret Desire: To live an easy,uncomplicated life"),8=>array("Scorpio","Your element: Water","Your ruling planets: Pluto","Symbol: The Scorpion","Your stone: Topaz","Scorpio's Secret Desire: To triumph"),9=>array("Sagittarius","Your element: Fire","Your ruling planets: Jupiter","Symbol: The Archer","Your stone: Turquoise","Sagittarian's Secret Desire: To make a difference in the world"),10=>array("Capricorn","Your element: Earth","Your ruling planets: Saturn","Symbol: The Goat","Your stone: Garnet","Capricorn Secret Desire: To be admired by their family and friends and the world at large"),11=>array("Aquarius","Your element: Air","Your ruling planets: Uranus","Symbol: The Water Bearer","Your stone: Amethyst","Aquarian's Secret Desire: To be unique and original"),12=>array("Pisces","Your element: Water","Your ruling planets: Neptune","Symbol: The Fish","Your stone: Bloodstone","Pisces Secret Desire: To live their dreams and turn fantasies into realities"));
$images = array("","Images/ariestat.jpg","Images/taurustat.jpg","Images/geminitat.jpg","Images/cancertat.jpg","Images/leotat.jpg","Images/virgotat.jpg","Images/libratat.jpg","Images/scorpiotat.jpg","Images/sagittariustat.jpg","Images/capricorntat.jpg","Images/aquariustat.jpg","Images/piscestat.jpg");
$totalZ=count($zodiac);
$id=isset($_REQUEST["id"])?$_REQUEST["id"]:1;
?>
</head>
<body>
<form name="form1" method="post" action="">
<table width="50%" border="0" align="center">
<tr>
<td colspan="2"><img src="Images/Banner.jpg" width="100%" height="150"></td>
</tr>
<tr>
<td><strong>Zodiac Sign:</strong> <?=$zodiac[$id][0]?></td>
<td align="right"><img src="<?=$images[$id]?>"></td>
</tr>
<tr>
<td colspan="2"><strong>Description:</strong>
<ul>
<li><?=$zodiac[$id][1]?></li>
<li><?=$zodiac[$id][2]?></li>
<li><?=$zodiac[$id][3]?></li>
<li><?=$zodiac[$id][4]?></li>
<li><?=$zodiac[$id][5]?></li>
</ul>
</td>
</tr>
<tr>
<td colspan="2"><div align="center">
<?php
for($i=1;$i<=$totalZ;$i++)
{
echo "<a href='Horoscope.php?id=$i'><img src='$images[$i]' height='20px width='20px' border='0'></a>  ";
}
?>
</div></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<?php
for($i=1;$i<=$totalZ;$i++)
{
echo "<a class='box' href='Horoscope.php?id=$i'>$i</a>";
}
?>
</div></td>
</tr>
<tr align="center">
<td colspan="2">
<a class="nobox" href="Horoscope.php?id=1">First</a> |
<a class="nobox" href="Horoscope.php?id=<?=$_REQUEST["id"]>1?$_REQUEST["id"]-1:1?>">Prev</a> |
<a class="nobox" href="Horoscope.php?id=<?=$_REQUEST["id"]<$totalZ?$id+1:$totalZ?>">Next</a> |
<a class="nobox" href="Horoscope.php?id=<?=$totalZ?>">Last</a>
</td>
</tr>
</table>
</form>
</body>
</html>

Now this script works well. But I want to add some more features in it. I want to do following -

1. When visitor views a zodiac sign description, the related link should be converted into normal text and not remains as a hyperlink. For example, suppose when he clicks link number 5 then 5th zodiac sign will be seen but the link becomes a normal text. And this should happen with each clicked link. I have seen this many times on professional websites but couldn't get the trick behind it.

2. One more thing I want to do is that the clicked link appears in different CSS. Like when user clicks link number 5, then this link should change in different color and style. Thus visitor come to know which link is currently showing.

I am thankful to you if you help me.

#9
Revolt

Revolt

    Programmer

  • Members
  • PipPipPip
  • 99 posts
You just have to use a similar mechanism to that which I have shown you before. In the for cycle, if you detect that the current $id is equal to $i, then you don't add a <a> but add a <span> for example. You should also add "class='selected'" to this span so that you can change its style via CSS and tell the user that this is the current page he/she is viewing.

#10
justsachin4u

justsachin4u

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
I somewhat understand, but couldn't execute properly, here is my code -

<?php
for($i=1;$i<=$totalPage;$i++)
{
if($id==$i)
{
echo "<span>$i</span";
}
else
{
echo "<a href='TablePagination.php?id=$i'>$i</a>";
}
}
?>

I doesn't work properly. Where have i done mistake???

It gives following output -

Attached File  PHP.JPG   74K   38 downloads

Edited by justsachin4u, 06 July 2011 - 08:45 AM.


#11
Revolt

Revolt

    Programmer

  • Members
  • PipPipPip
  • 99 posts
Why do you say it doesn't work? Does it continue to put everything as links? If so check the value of the $id variable because it should be working

#12
justsachin4u

justsachin4u

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
I have attached snapshot of output in last page. plz check that




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users