I'm not getting any results in my option tag although I can see the results when I var_dump the variable

what am I not seeing?

<label for="employerid" class="label">
               
				 <?php 
				$result = DB::getInstance()->query("SELECT id, employer FROM employers");
					if($result->count()){
						$select='<select name="employerid" id="employerid" class="input">';
						foreach($result as $row){
						echo '<option value="' .$row->id.'">' .$row->employer.'</option>';
						
						}
					}
				$result->close();
				$select.='</select>';
						echo $select;
				
                ?>

here is the var_dump($result)

object(DB)#3 (7) { ["_pdo":"DB":private]=> object(PDO)#4 (0) { } ["_query":"DB":private]=> object(PDOStatement)#7 (1) { ["queryString"]=> string(34) "SELECT id, employer FROM employers" } ["_error":"DB":private]=> bool(false) ["_results":"DB":private]=> array(2) { [0]=> object(stdClass)#5 (2) { ["id"]=> string(1) "1" ["employer"]=> string(11) "Parks Place" } [1]=> object(stdClass)#8 (2) { ["id"]=> string(1) "2" ["employer"]=> string(6) "SimpeQ" } } ["_count":"DB":private]=> int(2) ["_fetchAll":"DB":private]=> NULL ["_close":"DB":private]=> NULL }

Your code echoes the option values as part of the in-line code, but builds $select separately and echoes that later on. Shouldn’t this line:

echo '<option value="' .$row->id.'">' .$row->employer.'</option>';

be

$select .= '<option value="' .$row->id.'">' .$row->employer.'</option>';

No, it doesn’t change anything. But it was a good point because now I know that I had two problems: the one you just pointed out with the concatenated = and the one that I still don’t know where it is.

here is the old mysqli equivalent that used to work like a charm. I have changed my design to use PDO along the way and reconstructing everything while using PDO.

//include("includes/connect-db.php");
								if($result = $dbc->query("SELECT id, employer FROM employers")){
									$select = '<select name="employerid" id="employerid" class="input">';
									
									while($row = $result->fetch_assoc()){
										$select .='<option value="' .$row['id'].'">' .$row['employer'].'</option>';
									}
										$result->close();
								}
									$select.='</select>';
									echo $select;
								?><br/>

refreshed look at var_dump($result) after the line : if($result->count()){

Employer object(DB)#3 (5) { ["_pdo":"DB":private]=> object(PDO)#4 (0) { } ["_query":"DB":private]=> object(PDOStatement)#7 (1) { ["queryString"]=> string(34) "SELECT id, employer FROM employers" } ["_error":"DB":private]=> bool(false) ["_results":"DB":private]=> array(2) { [0]=> object(stdClass)#5 (2) { ["id"]=> string(1) "1" ["employer"]=> string(11) "Parks Place" } [1]=> object(stdClass)#8 (2) { ["id"]=> string(1) "2" ["employer"]=> string(6) "SimpeQ" } } ["_count":"DB":private]=> int(2) }

Do a

echo '<pre>' . print_r($result, 1) . '</pre>';

to get a more pleasing breakdown on what it looks like? That’s what I do.

here what it looks like:

Employer
DB Object
(
    [_pdo:DB:private] => PDO Object
        (
        )

    [_query:DB:private] => PDOStatement Object
        (
            [queryString] => SELECT id, employer FROM employers
        )

    [_error:DB:private] => 
    [_results:DB:private] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1
                    [employer] => Parks Place
                )

            [1] => stdClass Object
                (
                    [id] => 2
                    [employer] => SimpeQ
                )

        )

    [_count:DB:private] => 2
)

As you can see, there are two results but they are not showing inside the option tag. The drop down box is compressed on itself and nothing comes out when clicking the arrow down. like there are 0 values in the option tag

What do you get if you var_dump($row) inside the foreach loop?

i was able to figure it out about 10 minutes ago.

the problem was in the foreach loop:

foreach($result as $row)

it should have been:
foreach($result->results() as $row)

that made it work perfectly.

NOw the only thing I need to figure out is how to get the value that I am pulling from database to show as selected. I have tried various ways but still working on getting that buttoned up. WOuld you kown a way?

Something along these lines:

$select .='<option value="' .$row['id'].'"' . ($[row['id']==$myid? ' selected>' : '>') . $row['employer'].'</option>';

That assumes it’s $row[‘id’] that you want to check, and $myid that you want to check it against. If the two are the same, it outputs ‘selected’, if not, it does not.

I found the solution…

 <label for="employerid" class="label">Employer</label>
 <select name="employerid" id="employerid" class="input">
 <option> Select an Employer </option>
<?php $result_emp = DB::getInstance()->query("SELECT id, employer FROM employers");
       if($result_emp->count()){                      
            foreach($result_emp->results() as $row){
		 $selected = $row->id == $user->data()->employerid ? ' selected="selected"' : null;?>
                   <option value="<?php echo $row->id ?>"<?php echo $selected?> ><?php echo $row->employer </option>
<?php  
             }
        }
 ?>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.