Populating a combobox using PDO. The combo is empty

n Java, If you want to populate a combobox you first get into an array then populate it into the Combobox. I am assuming the process is similar in PHP. As you probably have guessed I am new to PHP. Loving it’s power just need to learn how to harness it.

I have an array using the fetchAll(); and storing it into $data. Then I run a for loop and storing into a new variable called $row. I am able to print my entire database out using the print_r method. Boom, my whole array is there. Then I think I stumbled onto away of putting into a combobox. The problem is it’s blank.

When I try to put the print_r method inside the loop and print the content I get the first place in my array. If I do this in Java, it should print everything as it goes through the loop. This is not happening. Then Depending on the location i put the print_r method it will just put the entire database into my combobox. The text that is inputted into the Combobox is an array format so it’s no good. Any suggestions or thoughts?


<?php

$username = "saori";
$password = "";
$hostname = "Jamal-pc";


try{
$conn = new PDO('mysql:host=Jamal-PC;dbname=japanesewords',$username,$password);
$sql ='SELECT Englishword FROM Japanesedefinition;';
$stmt = $conn->prepare($sql);
$stmt ->execute();
$data = $stmt->fetchAll(PDO::FETCH_COLUMN);

}catch(PDOException $e){
 echo 'ERROR: ' . $e->getMessage();
 }

foreach($data as $row){

}


?>



<select name="Japanese" id="Japanese">
<?php foreach($data as $row) : ?>
<option><?= $row; ?></option>
<?php endforeach ?>
</select>
<h3>Please Click this to look up your Japanese Word</h3>
<form action="index.php" method="POST">
<input type ="text" name="Japanese">
<input type ="submit" name ="Submit" value ="Submit">
</form>	
<h3>Use the fieldboxs down below to Enter your Japanese words into your dictionary</h3>
<form action ="lookup.php" method ="post">
EnglishWord:<input type ="text" name="EnglishWord">
Japaneseword: <input type ="text"	name="JapaneseWord">
	<input type	="submit" value="click">
</form>	

Well PHP’s dropdown list is somewhat different from Java API, if you are familiar with Java API Id recommend my PHP GUI API for you since the interface is so similar to Java’s GUI API:
http://www.phpclasses.org/package/7857-PHP-Render-HTML-pages-composed-programmatically.html

With this, you can simply do the following:


$dropdownlist = new DropdownList("listID");
$dropdownlist->add(new Option("optiontext", "optionID"));
......

How do I had this to PHP is there a tutorial. Sounds very straight forward.

Hi louis,

The code in your first example is almost there… rather than echoing the $row (which is an array) you need to output the specific DB columns from the row that you need. If your table has an ID column and a name column, the code would look like this:


<select name="Japanese" id="Japanese">
<?php foreach($data as $row) : ?>
    <option value="<?= $row['id']; ?>"><?= $row['name']; ?></option>
<?php endforeach ?>
</select>

My code was only meant as an example - you’d need to change the column names to reflect your table structure. Looking back at your original code, you probably need to do something like this:


<select name="Japanese" id="Japanese"> 
<?php foreach($data as $row) : ?> 
    <option value="<?= $row['Englishword']; ?>"><?= $row['Englishword']; ?></option> 
<?php endforeach ?> 
</select>

LoL, I understood that. I changed my columns accordingly and still it’s not populating. I would to try to debug this, but i honestly am lost. The approach seemed logical, but it’s just not working.

<select name="Japanese" id="Japanese">  
<?php foreach($data as $row) : ?>  
    <option value="<?= $row['id']; ?>"><?= $row['EnglishWord']; ?></option>  
<?php endforeach ?>  
</select>

Sorry, I didn’t mean to be patronising, but you’d be surprised how many people just copy and paste sample code without changing it. Try changing this line:


// from
$data = $stmt->fetchAll(PDO::FETCH_COLUMN);

// to
$data = $stmt->fetchAll(PDO::FETCH_ASSOC); 

Thank you so much for your help.

I’m am getting the correct amount of blank spaces that correspond to the amount of request that I have queried. I am unsure on why they will not display. I made the change and it’s still not working. Just blank. As powerful as PHP is, I would think it would be a bit easier to populate a simple combobox. I have honestly been at this for 3 days. Again, other suggestions would be greatly appreciated.

Does it make any difference if you swap the short echo tags for full tags (ie. <?php echo $row['EnglishWord']; ?>)?

try{
$conn = new PDO('mysql:host=Jamal-PC;dbname=japanesewords',$username,$password);
$sql ='SELECT Englishword FROM Japanesedefinition;';
$stmt = $conn->prepare($sql);
$stmt ->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

}catch(PDOException $e){
 echo 'ERROR: ' . $e->getMessage();
 }



?>






<select name="Japanese" id="Japanese">  

<?php foreach($data as $row) : ?>  
    <option value="<?= $row["id"]; ?><?= $row["EnglishWord"]; ?></option>  
<?php endforeach ?>  

</select>

If type anything outside the php brackets it displays what I typed in the combobox, which is precisely the same amount of entries I have in my DB. I am perplexed at why it won’t populate. I have 4 columns, id, Japaneseword, EnglishWord, and Entry Date.

Okay, I get undefined index when I use the code with echo… And it puts in right into the combobox.

I’m not really sure what you mean. Just to be clear, what I was suggesting you try was this:


try{
    $conn = new PDO('mysql:host=Jamal-PC;dbname=japanesewords',$username,$password);
    $sql ='SELECT id, Englishword FROM Japanesedefinition;';
    $stmt = $conn->prepare($sql);
    $stmt ->execute();
    $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
}catch(PDOException $e){
    echo 'ERROR: ' . $e->getMessage();
}

?>

<select name="Japanese" id="Japanese"> 
<?php foreach($data as $row) : ?> 
    <option value="<?php echo $row['id']; ?>"><?php echo $row['Englishword']; ?></option> 
<?php endforeach ?> 
</select>

Note that you’ll need to change the SQL to select the ID so you can use it in the select box.

IT worked. Wow. Respect. I don’t know how to give you kudos, but you constantly responded to my posts and stayed with me until it was resolved. Helping a complete and total stranger without any type of reward. I don’t know how to express my gratitude to you. I was truly stuck at this point and you pulled through for me. I have had friends that wouldn’t invest that amount of time and energy into my problem.

Okay, can you please fix my logic as I noticed that you changed my approach. First you added an echo statement and made me use ID as well, why? Second, was my code faulted because I wasn’t echoing out my data.

Thanks.

WIth Java the code is much simpler in my opinion. I get a for loop going set it to the size of what I need it to be, and from there I would just use an add function to populate the combobox and then bam. My combobox is completely populated. Here is the code I was basing my PHP off of


 for (int x = 0; x < readExcel.ColumnOne.size(); x++) {
            column1 = readExcel.ColumnOne.get(x);
            box.addItem(column1);
        }

Look at the readexcel it’s an array that stored my Excel entries in. Then I set my loop for the size of array, which is the exact size of the excel file entries. From there, I set column1 as a blank string the entries get stored in the blank string. I then add the entries into the combobox via it’s function .add. Thats it.

I thought I could repeat the process, but I am lost at where I went wrong on doing this. Can you please explain to me thanks.

No worries man, glad I could help.

Your PHP install probably has short tags disabled, which is why it wasn’t outputing the data correctly. As for adding the ID, I just assumed that your select box is part of a form and you’d want to submit a value and I usually use the ID for that, but by all means change it to suit what you’re trying to do - it’s not necessary to use the ID if you don’t need it.

Okay, one question question, why do I need to have <?php echo $row[‘id’]; ?> this part of the code to work?

So the most crucial part of my error was the echo part of the statement. It needs to that method for the code to work correctly.

You don’t need it, you could do this if you wanted:

<option><?php echo $row['Englishword']; ?></option>