SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2014
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    populate data into drop down list

    hey guys, so im trying to populate data from database into a drop down list. when i was working with mysql it worked fine and now i mgrated to pdo, so im still learning, and the coding changed and the drop down doesn't get populated. is there something wrong with my code?
    Code:
    <div class="dropdown">
    		<label>Choose an Event : <label/>
    		<select name="event" id="event">
    			<option value="" disabled selected>Choose</option>
    			<?php
    			require "connection.php";
    			$user_id = $_SESSION['sess_user_id'];
    			$eventnames = $dbh->prepare("SELECT event_id,event_name FROM event WHERE euser_id = $user_id");
    			$eventnames->execute(array('euser_id'));
    			if($eventnames->rowCount() > 0)
    			{
    				$eventnames->setFetchMode(PDO::FETCH_ASSOC);
    				while( $retrieve = $eventnames->fetch());
    				{
    					$id = $retrieve['event_id'];
    					$name = $retrieve['event_name'];
    					echo '<option value="'.$id.'">'.$name.'</option>';
    				}
    			}
    			?>
    		</select>
    	</div>
    previously the fetch query was done above <!doctype html> thought it was because of placement or something.
    Code:
    <?php
    			require "connection.php";
    			$user_id = $_SESSION['sess_user_id'];
    			$eventnames = $dbh->prepare("SELECT event_id,event_name FROM event WHERE euser_id = $user_id");
                            $events ="";
    			$eventnames->execute();
    			if($eventnames->rowCount() > 0)
    			{
    				$eventnames->setFetchMode(PDO::FETCH_ASSOC);
    				while( $retrieve = $eventnames->fetch());
    				{
    					$id = $retrieve['event_id'];
    					$name = $retrieve['event_name'];
    					$events .= '<option value="'.$id.'">'.$name.'</option>';
    				}
    			}
    			?>
    
    <div class="dropdown">
    		<label>Choose an Event : <label/>
    		<select name="event" id="event">
    			<option value="" disabled selected>Choose</option>
    			<?php
    			       echo $events;
                       	?>
    		</select>
    	</div>
    TIA!

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    409
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I'd do this:

    PHP Code:
    <div class="dropdown">
            <label>Choose an Event : <label/>
            <select name="event" id="event">
                <option value="" disabled selected>Choose</option>
                <?php
                
    require "connection.php";
                
    $user_id $_SESSION['sess_user_id'];
                
    $eventnames $dbh->prepare("SELECT event_id,event_name FROM event WHERE euser_id = :user");
            
    $eventnames->bindParam(':user'$user_id);
                
    $eventnames->execute();
                if(
    $eventnames)
                {
                    while( 
    $retrieve $eventnames->fetch());
                    {
                        
    $id $retrieve['event_id'];
                        
    $name $retrieve['event_name'];
                        echo 
    '<option value="'.$id.'">'.$name.'</option>';
                    }
                }
            
    $eventnames->CloseCursor();
                
    ?>
            </select>
        </div>
    If it didn't work, I'd add lots of echo() statements, and enable error reporting, so I can see how far it gets and what's going wrong. (Don't know why my indents don't work the same).
    http://www.firenza.net - my homage to a car from the 1970s

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2014
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i got :
    Array ( [0] => 00000 [1] => [2] => )
    when i added:
    Code:
    print_r($eventnames->errorInfo());
    what does that mean?

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    409
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hard to say. Where did you add that code, and did the drop-down list work?
    http://www.firenza.net - my homage to a car from the 1970s

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2014
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i added it where
    Code:
    $eventnames = CloseCursoe()
    is suppose to be. no the drop down did not work.

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2014
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    solution:

    Code:
    require "connection.php"; 
        $user_id = $_SESSION['sess_user_id']; 
        $eventnames = $dbh->prepare("SELECT event_id,event_name FROM event WHERE euser_id = :user"); 
        $eventnames->bindParam(':user', $user_id);
        $eventnames->execute();
        $retrieve = $eventnames->fetchAll();
    
       <div class="dd-wrap">
    		<label>Choose an Event : <label/>
    		<select name="eventss" onchange="getnames(this.value)">
    			<option value="" disabled selected>Choose</option>
    			<?php foreach($retrieve as $r): ?>
    			<option value="<?=$r['event_id']?>"><?=$r['event_name']?></option>
    			<?php endforeach ?>
    		</select>
    	</div>
    question solved.


Tags for this Thread

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
  •