Jump to content

Pupulate dropdown form MySql


molsonbubba
Go to solution Solved by molsonbubba,

Recommended Posts

I am stuck. I am trying to populate a dropdown box in my form from MySql database. Connection to db is working but I can't seem to be able to get the dropdown box on my form to populate...all I see is Please Select with an empty clickable row below. Help!? Thanks.

 

 

$items = "|Please Select[c]\n";

 

$host="localhost"; // Host name

$username="xxxxxxxxxx"; // Mysql username

$password="xxxxxxxx"; // Mysql password

$db_name="test_db"; // Database name

$tbl_name="test_table"; // Table name

 

//Connect to server and select database.

$conn = mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

if(! $conn){ die('Could not connect: ' . mysql_error());}echo "Connected successfully\n";

 

$result = mysql_query("SELECT * FROM test_table" ) or die(mysql_error());

 

foreach ($result as $r)

$items .= $r->id . '|' . $r->corpprice . "\n";

return $items;

Edited by trq
Link to comment
Share on other sites

I am stuck. I am trying to populate a dropdown box in my form from MySql database. Connection to db is working but I can't seem to be able to get the dropdown box on my form to populate...all I see is Please Select with an empty clickable row below. Help!? Thanks.

 

 

$items = "|Please Select[c]\n";

 

$host="localhost"; // Host name

$username="xxxxxxxxxx"; // Mysql username

$password="xxxxxxxx"; // Mysql password

$db_name="test_db"; // Database name

$tbl_name="test_table"; // Table name

 

//Connect to server and select database.

$conn = mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

if(! $conn){ die('Could not connect: ' . mysql_error());}echo "Connected successfully\n";

 

$result = mysql_query("SELECT * FROM test_table" ) or die(mysql_error());

 

foreach ($result as $r)

$items .= $r->id . '|' . $r->corpprice . "\n";

return $items;

You don't use for each to execute the query.

 

while($row = mysql_fetch_assoc($result)){

$row[table_column];

}

Link to comment
Share on other sites

Thanks.

With this array prints and all seems OK except the dropdown down does not populate.

 

$items = "|Please Select[c]\n";

$host="localhost"; // Host name
$username="xxxxxxxxxx"; // Mysql username
$password="xxxxxxxx"; // Mysql password
$db_name="test_db"; // Database name
$tbl_name="test_table"; // Table name

//Connect to server and select database.
$conn = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

if(! $conn){ die('Could not connect: ' . mysql_error());}echo "Connected successfully\n";

$result = mysql_query("SELECT * FROM test_table" ) or die(mysql_error());

 

while($r[]=mysql_fetch_assoc($result));

echo "<pre>";
print_r ($r);
echo "</pre>";
 


foreach ($result as $r)
$items .= $r->id . '|' . $r->corpprice . "\n";
return $items;

Link to comment
Share on other sites

if you use $r->id then you will need mysql_fetch_object().

 

if you use mysql_fetch_assoc() then use $r['id'] to reference fields in the array.

 

eg

 

 

$result = mysql_query("SELECT * FROM test_table" ) or die(mysql_error());

$options = '';
while($r = mysql_fetch_object($result));
{
    $options .= "<option value='$r->id'>$r->corpprice</option>\n";
}

return $options;
Link to comment
Share on other sites

<select name='drop'>
<option "Input" value="choose from list"></option>
<?php
$combo = "SELECT field FROM table";
$result = mysql_query($combo) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<option value='". $row['field'] ."'>". $row['field'] ."</option>";
}
?>
</select>

 

Hope this helps

Link to comment
Share on other sites

I see <option value=">"</option> in the dropdownbox with the suggested script below by Barand. I noticed there are no ' at $r->corpprice where they are present at '$r->id'. Adding ' did not change the outcome. Thank you. Going to Chapters to see if they got any books :).

 

<?php

$options = "|Please Select[c]\n";

$host = 'localhost';
$user = 'xxxx';
$password = 'xxxx';
$database = 'test_db';
$link = new mysqli($host, $user, $password, $database);
if (mysqli_connect_errno()) {
 die('Unable to connect to database [' . $db->connect_error . ']');
}
$options = '';
while($r = mysql_fetch_object($result));
{
    $options .= "<option value='$r->id'>$r->postdate</option>\n";
}

return $options;

?>

Link to comment
Share on other sites

Haha...well that was just silly of me, I missed the SELECT query. I have been staring at the same thing way too much. Unfortunately it did not make it work. Still <option value=">"</option> in the dropdownbox.

 

$options = "|Please Select[c]\n";

$host="localhost"; // Host name
$username="xxxxxxxxxx"; // Mysql username
$password="xxxxxxxx"; // Mysql password
$db_name="test_db"; // Database name
$tbl_name="test_table"; // Table name

//Connect to server and select database.
$conn = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
if(! $conn){ die('Could not connect: ' . mysql_error());}echo "Connected successfully\n";

$result = mysql_query("SELECT * FROM test_table" ) or die(mysql_error());

 

$options = '';
while($r = mysql_fetch_object($result));
{
    $options .= "<option value='$r->id'>$r->postdate</option>\n";
}

return $options;

Link to comment
Share on other sites

i am trying to dynamically populate one dropdown box. i am using rsform pro by rsjoomla generated form. the code they suggest is linked above but it does not work. their tech support us not of any help at all. i am a novice with all of this. it seems simple from what i googled and read so far but it just wont work for me. if any one can help it woluld be great, if not just let me know please that it cant be done and i will look at other solutions to my problem. thanks guys.

Link to comment
Share on other sites

  • Solution

i am trying to dynamically populate one dropdown box. i am using rsform pro by rsjoomla generated form. the code they suggest is linked above but it does not work. their tech support us not of any help at all. i am a novice with all of this. it seems simple from what i googled and read so far but it just wont work for me. if any one can help it woluld be great, if not just let me know please that it cant be done and i will look at other solutions to my problem. thanks guys.

This is the code I tried. It does not work. There are no errors. The drop down box has Please Select and one empty line below.

 

$items = "|Please Select[c]\n";

 

 

        $conf =& JFactory::getConfig();

 

        $host         = 'localhost'; //replace your IP or hostname

        $user         = 'xxxx'; //database user

        $password     = 'xxx';//database password

        $database    = 'test_db'; //database name

        $debug         = $conf->getValue('config.debug');

 

        $options    = array ( 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database ' );

 

        $db =& JDatabase::getInstance( $options );

 

        if ( JError::isError($db) ) {

            header('HTTP/1.1 500 Internal Server Error');

            jexit('Database Error: ' . $db->toString() );

        }

 

        if ($db->getErrorNum() > 0) {

            JError::raiseError(500 , 'JDatabase::getInstance: Could not connect to database <br />' . 'joomla.library:'.$db->getErrorNum().' - '.$db->getErrorMsg() );

        }

 

        $db->debug( $debug );

 

$db =& JFactory::getDBO();

$db->setQuery("SELECT corpprice_darkochre, postdate FROM tableoreapi");

$result = $db->loadObjectList();

 

 

foreach ($result as $r)

      $items .= $r->corpprice_darkochre . '|' . $r->postdate . "\n";

 

return $items;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.