Generate dropdown from database

Shows some Tutorials

Generate dropdown from database

Postby beautifiers on Fri Jan 18, 2008 10:03 am

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...
Code: Select all
<?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
beautifiers
Fresh Boarder
 
Posts: 3
Joined: Tue Jan 15, 2008 2:40 am

Re:Generate dropdown from database

Postby GreyHead on Fri Jan 18, 2008 2:43 pm

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:
Code: Select all
<?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).
Bob Janes
info at greyhead.net
ChronoForms Support If you like ChronoForms please vote or post a review at Joomla.org
User avatar
GreyHead
Platinum Boarder
 
Posts: 3412
Joined: Tue May 29, 2007 10:15 pm
Location: Brittany

Re:Generate dropdown from database

Postby beautifiers on Fri Jan 18, 2008 3:33 pm

Thanks alot!!:)
beautifiers
Fresh Boarder
 
Posts: 3
Joined: Tue Jan 15, 2008 2:40 am

Re:Generate dropdown from database

Postby sjnims on Fri Jun 06, 2008 3:50 am

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
www.gammanu.org
sjnims
Fresh Boarder
 
Posts: 10
Joined: Tue May 27, 2008 1:30 pm

Re:Generate dropdown from database

Postby GreyHead on Fri Jun 06, 2008 8:39 am

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
Bob Janes
info at greyhead.net
ChronoForms Support If you like ChronoForms please vote or post a review at Joomla.org
User avatar
GreyHead
Platinum Boarder
 
Posts: 3412
Joined: Tue May 29, 2007 10:15 pm
Location: Brittany

Re:Generate dropdown from database

Postby sjnims on Fri Jun 06, 2008 1:09 pm

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.
sjnims
Fresh Boarder
 
Posts: 10
Joined: Tue May 27, 2008 1:30 pm

Re:Generate dropdown from database

Postby GreyHead on Fri Jun 06, 2008 1:25 pm

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
Bob Janes
info at greyhead.net
ChronoForms Support If you like ChronoForms please vote or post a review at Joomla.org
User avatar
GreyHead
Platinum Boarder
 
Posts: 3412
Joined: Tue May 29, 2007 10:15 pm
Location: Brittany


Return to ChronoForms How To

Who is online

Users browsing this forum: MSNbot Media and 2 guests