FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Dynamic Initial Select @ listbox





fromegame
Hi there,

I have register form, you can register etc. and later on you can change your profile.
At the 'change profile' section, you can select the type of font you want to show the webpages in. I have a mysql database with the font names and font paths. But when you come on the 'change profile' page. The first entry in the listbox is selected. I want the font that someone is using now to be seleted. The problem is this:

My script wrote:

<select name="font" id="font">
<option value="trebuc" selected>Trebuchet MS</option>
</select>


So if you have multiple options, you have to do this:

Code:

<select name="font" id="font">
<?php
if($selected_font == "trebuc") {
?>
<option value="trebuc" [b]selected[/b]>Trebuchet MS</option>
<option value="trebucbd">Trebuchet MS Bold</option>
<?php
}elseif($selected_font == "trebucbd") {
?>
<option value="trebuc">Trebuchet MS</option>
<option value="trebucbd" selected>Trebuchet MS Bold</option>
<?php
}
?>
</select>


If I have like 100 fonts, I have to type so much, and it's much easier to just add it to the mysql-database.

Is there an easier way to do this.

Thanks in advance. Smile
Manofgames
Code:

$fonts = array('Trebuchet', 'Comic Sans Ms');
$current_font = 'Comic Sans Ms';
echo '<select name="font" id="font"> ';
foreach($fonts as $key => $v){
if($v = $current_font){
echo '<option value="'.$key.'" selected>'.$v.'</option>';
}
else{
echo '<option value="'.$key.'">'.$v.'</option>';
}
}
fromegame
I modified the script a bit and got this so far:

Code:

<?php
$fonts = array('Trebuchet MS' => 'trebuc', 'Trebuchet MS Bold' => 'trebucbd');
$current_font = $fetch['font'];
foreach($fonts as $key => $v){
   if($v == $current_font){
      echo '<option value="'.$v.'" selected>'.$key.'</option>';
   }else{
      echo '<option value="'.$v.'">'.$key.'</option>';
   }
}
?>


How can I get the mysql results in the $fonts array?
Manofgames
I assume your table has one column for the fonts name, and its alias.
If so, then this is an example setup:

Code:

<?php
$conn = mysql_connect('localhost', 'User', 'Pass');
mysql_select_db($conn);
$execute = "SELECT current_font FROM users WHERE user_id = '{$user_id'}'";
$result = mysql_fetch_assoc($execute);
$current_font = $result['current_font'];
mysql_free_result($execute);
$execute = "SELECT font_alias, font_name FONTS";
while($result = mysql_fetch_assoc($execute)){
$fonts[$result['font_name']] = $result['font_alias'];
}

foreach($fonts as $key => $v){
   if($v == $current_font){
      echo '<option value="'.$v.'" selected>'.$key.'</option>';
   }else{
      echo '<option value="'.$v.'">'.$key.'</option>';
   }
}
?>
fromegame
Thx, works now. Smile
Related topics
Select and click
PHP Book (Php and Mysql for Dynamic Web Sites)
How to get your dynamic PHP website crawled better by se ?
SQL Basics
Dynamic User Customizable Sites. CSS + PHP = Awesome!
Tutorial: PHP Installed Modules Dynamic Reference Tool
Create the Flash Logo (Photoshop Tutorial)
Jamatus' Dynamic image store
Windows XP Tricks & Tips!!!!
Need help with my dynamic sig.
Javascript and Dynamic Select Boxes
Randomness is an illusion. [philosophy/science]
Help me for a dynamic calendar
Dynamic SELECT boxes based on selections from other selects
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.