Forums

Generate dropdown from database

beautifiers 18 Jan, 2008
hi,
I'm newbie in programming...tried to do a dropdown using the code below but i can't make it works. Maybe there is mistake on my codes. Anybody plz help me...
<?php 
global $database;
	
$database->setQuery( "SELECT * FROM #__chronoforms_15;" );
if (!$database->query()) {
echo "<script> alert('".$database->getErrorMsg()."'); 
  window.history.go(-1); </script>
";

$rows = $database->loadObjectList();
}
?>

<select name="orgtype_id">
    <?php
    foreach($rows as $key=>$value) {
    ?>
    <option value="<?= $value["cf_id"] ?>">
      <?= $value["orgtype_name"] ?></option>
    <?php
    }
    ?>
  </select>


Edited to add code tags
GreyHead 18 Jan, 2008
Hi beautifiers,

You are pretty close. The problem is that loadObjectList returns an array of objects, not of arrays. You also seem to have misplaced a '}' So try this version:
<?php 
global $database;
$database->setQuery( "SELECT * FROM #__chronoforms_15;" );
if (!$database->query()) {
  echo "<script> alert('".$database->getErrorMsg()."'); 
    window.history.go(-1); </script>";
}
$rows = $database->loadObjectList();
?>
<select name="orgtype_id">
<?php
foreach($rows as $row) {
  echo "<option value='".$row->cf_id."'>".$row->orgtype_name."</option>";
}
?>
</select>
Bob

PS I changed the syntax a little to reduce the number of <? tags and make it simpler to see (for me at least).
sjnims 06 Jun, 2008
I have a similar issue, and don't really understand how to do this with php...visual studio makes this so easy with asp.net, but I digress...

I'm using community builder at my website, but the code for basically everything is quite extensive and I'd rather not modify any of it. What I want to end up doing using chronoforms is create a new community builder registration form.

Background: With CB, it allows you to create custom fields. CB stores all the fields (regardless of type) in jos_comprofiler_fields with any field values (for a radio, multi select, or drop down) in a table called jos_comprofiler_field_values. I've created many such fields and added field values, e.g field = state, field values = all U.S. states. On the CB registration form the user can then select the state from a drop down which is populated by the fields in the database.

What I want to do on my chronoforms registration page is recreate the population of the drop down field using the same tables (no need to reinvent the wheel).

Any ideas?

Thanks,

Steve Nims
<!-- w --><a class="postlink" href="http://www.gammanu.org">www.gammanu.org</a><!-- w -->
GreyHead 06 Jun, 2008
Hi Steve,

Have you looked at the CB Registration Plugin for ChronoForms? I'm not sure if it will pick up your custom fields out of the box - I don't use CB - but it's a good place to start.

Bob
sjnims 06 Jun, 2008
I think what the CF plugin only does is map the CF form field name entered data to the correct database field in jos_comprofiler, not load values from jos_comprofiler_field_values.
GreyHead 06 Jun, 2008
Hi Steve,

Sorry, I misunderstood your email :-(

It's completely possible to do this. You'll need a little PHP in the front of your Form HTML to read the CB tables and re-create the form fields. Not too difficult a project.

Probably the place to start is with the code around line 947 of comprofiler.php

Bob
This topic is locked and no more replies can be posted.