SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I output databse result to this select

    Hi,

    Can you help me to output the database result to this select

    Code PHP:
    <?php
    $aTranslated = array(
    	'german' => array(
    		'Montag',
    		'Dienstag',
    		'Mittwoch',
    		'Donnerstag',
    		'Freitag',
    		'Samstag',
    		'Sonntag',
    	),
    	'english' => array(
    		'Monday',
    		'Tuesday',
    		'Wednesday',
    		'Thursday',
    		'Friday',
    		'Saturday',
    		'Sunday',
    	),
    );
     
    echo '<select name="end">';
    echo '<option value="0">date/month</option>';
    for ($iCounter = 1 ; $iCounter <= 10 ; $iCounter++ )
    {
    	$iTimeStamp = strtotime(sprintf('+%s Days',$iCounter));
    	echo sprintf('<option value="%s">%s %s</option>',
    		$iCounter,
    		$aTranslated['german'][date('w',$iTimeStamp)],
    		date('d/m',$iTimeStamp)
    	);
    }
    echo '</select>';
    ?>

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You don't appear to offer any database information nor a 'please'.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $rows['value'] is equal one of array indexes.

    I need to show the option, where the value is the same as $rows['value'],selected.

    And here you "please !".

  4. #4
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You still haven't given us any database information

  5. #5
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    $query = "SELECT end FROM listing WHERE id = '{$listing_id}' ";
    $result = mysql_query($query);
    $rows = mysql_fetch_array($result);

    Sorry if the topic doesn't reflect the question, again, I need to compare the value of the option with the $rows['end']

    If they are the same, I want the option with that value to selected (selected ="selected")

  6. #6
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Why don't you make an attempt at it first, then we can point out any mistakes you might have made and point you in the right direction? If you have the current value in a variable, it is really very easy, and there are hundreds of topics on this subject around

  7. #7
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can do like this, (the code should be in your "for")

    PHP Code:

            $rows 
    mysql_fetch_array($result);
            echo 
    sprintf('<option value="%s" %s>%s %s</option>',
                
    $iCounter,
                (
    $row['end'] == $iCounter) ? "selected='true'" "",
                
    $aTranslated['german'][date('w',$iTimeStamp)],
                
    date('d/m',$iTimeStamp)
            ); 

  8. #8
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stormrider View Post
    Why don't you make an attempt at it first,
    I don't even know how to start, thats sad.
    Quote Originally Posted by krzyk View Post
    You can do like this, (the code should be in your "for")

    PHP Code:

            $rows 
    mysql_fetch_array($result);
            echo 
    sprintf('<option value="%s" %s>%s %s</option>',
                
    $iCounter,
                (
    $row['end'] == $iCounter) ? "selected='true'" "",
                
    $aTranslated['german'][date('w',$iTimeStamp)],
                
    date('d/m',$iTimeStamp)
            ); 
    That can not work, it gives a new option which labeled as selected = 'true' Wednesday
    Last edited by runrunforest; Dec 18, 2008 at 07:47.

  9. #9
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    selected="true" is not the correct xhtml, you use selected="selected"

  10. #10
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    selected= "selected" still can not work.

  11. #11
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by krzyk View Post
    You can do like this, (the code should be in your "for")

    PHP Code:

            $rows 
    mysql_fetch_array($result);
            echo 
    sprintf('<option value="%s" %s>%s %s</option>',
                
    $iCounter,
                (
    $row['end'] == $iCounter) ? "selected='true'" "",
                
    $aTranslated['german'][date('w',$iTimeStamp)],
                
    date('d/m',$iTimeStamp)
            ); 
    Sorry for my previous post, this actually works, even with selected='true'. Thank you, krzyk. And Merry Christmas !


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •