molsonbubba Posted March 9, 2013 Share Posted March 9, 2013 (edited) 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 March 9, 2013 by trq Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 9, 2013 Share Posted March 9, 2013 Read the manual on mysql_fetch_array Quote Link to comment Share on other sites More sharing options...
Lukeidiot Posted March 9, 2013 Share Posted March 9, 2013 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]; } Quote Link to comment Share on other sites More sharing options...
molsonbubba Posted March 9, 2013 Author Share Posted March 9, 2013 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; Quote Link to comment Share on other sites More sharing options...
Barand Posted March 9, 2013 Share Posted March 9, 2013 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; Quote Link to comment Share on other sites More sharing options...
amg182 Posted March 9, 2013 Share Posted March 9, 2013 <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 Quote Link to comment Share on other sites More sharing options...
molsonbubba Posted March 9, 2013 Author Share Posted March 9, 2013 (edited) Thanks guys. It is not working. I tried both suggestions. I get same reult as before...a dropdown box that is empty. This is beyond frustrating. Edited March 9, 2013 by molsonbubba Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 9, 2013 Share Posted March 9, 2013 Post your updated code. Quote Link to comment Share on other sites More sharing options...
molsonbubba Posted March 9, 2013 Author Share Posted March 9, 2013 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;?> Quote Link to comment Share on other sites More sharing options...
Barand Posted March 9, 2013 Share Posted March 9, 2013 You seem to be one query short of a solution Quote Link to comment Share on other sites More sharing options...
molsonbubba Posted March 10, 2013 Author Share Posted March 10, 2013 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; Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 10, 2013 Share Posted March 10, 2013 You have all of that within a function? Quote Link to comment Share on other sites More sharing options...
molsonbubba Posted March 10, 2013 Author Share Posted March 10, 2013 I do not believe that I do. That's all of it. Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 10, 2013 Share Posted March 10, 2013 Then why are you using return? And where is your select element? Quote Link to comment Share on other sites More sharing options...
molsonbubba Posted March 10, 2013 Author Share Posted March 10, 2013 http://www.rsjoomla.com/support/documentation/view-article/94-auto-populate-a-list-from-a-table.html Quote Link to comment Share on other sites More sharing options...
trq Posted March 10, 2013 Share Posted March 10, 2013 Then why are you using return?You can also use return from within an included file. Quote Link to comment Share on other sites More sharing options...
molsonbubba Posted March 10, 2013 Author Share Posted March 10, 2013 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. Quote Link to comment Share on other sites More sharing options...
Solution molsonbubba Posted March 10, 2013 Author Solution Share Posted March 10, 2013 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; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.