View previous topic :: View next topic |
Author |
Message |
cditty Smarty Rookie
Joined: 13 May 2003 Posts: 15
|
Posted: Thu May 15, 2003 2:53 pm Post subject: Array to select box |
|
|
Hopefully someone can help here. I can't seem to get this to work like I need. I am taking the results from a mysql_fetch_array and attempting to put them into a select box. When I try, I only get the last item returned from the query.
Code: |
$sql = mysql_query("select gmtr_id, gmtr_name from account_gmtr_t where gmtr_name like 'METERS%'") or die(mysql_error());
$selectMeter = mysql_fetch_array($sql);
<select name="acct">
{foreach from=$selectMeter item=gmtr_id}
<option value="{$gmtr_id}">{$gmtr_id} {$gmtr_name}
{/foreach}
</select>
|
Someone please lead me to the light.
Chris |
|
Back to top |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Thu May 15, 2003 3:10 pm Post subject: |
|
|
Try
Code: | $sql = mysql_query("select gmtr_id, gmtr_name from account_gmtr_t where gmtr_name like 'METERS%'") or die(mysql_error());
while($row = mysql_fetch_assoc($sql)) {
$selectMeter[] = $row;
}
<select name="acct">
{foreach from=$selectMeter item=gmtr}
<option value="{$gmtr.gmtr_id}">{$gmtr.gmtr_id} {$gmtr.gmtr_name}
{/foreach}
</select> |
_________________ "Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane |
|
Back to top |
|
cditty Smarty Rookie
Joined: 13 May 2003 Posts: 15
|
Posted: Thu May 15, 2003 3:23 pm Post subject: |
|
|
GOOD GRIEF!!!!! That was the problem? Brackets? 2 stinkin brackets???
Let's kick it up a notch now. How about with it being in a function?
Code: |
function selectMeter(){
$sql = mysql_query("select gmtr_id, gmtr_name from account_gmtr_t") or die(mysql_error());
$row[] = mysql_fetch_array($sql);
echo mysql_num_rows($sql);
return $row;
}
<select name="acct">
{foreach from=$selectMeter item=meter}
<option value="{$meter.gmtr_id}">{$meter.gmtr_id} {$meter.gmtr_name}
{/foreach}
</select>
|
Template
Code: |
$chooseMeter_tpl = new smart_template;
$chooseMeter_tpl->assign('selectMeter', selectMeter()); /* load drop down values */
$chooseMeter_tpl->display('tpl_page_main_chooseMeter.tpl');
unset($chooseMeter_tpl);
|
Even though there is 561 records returned, only the first one is displayed.
Chris |
|
Back to top |
|
toma Smarty Regular
Joined: 25 Apr 2003 Posts: 62
|
Posted: Thu May 15, 2003 4:59 pm Post subject: |
|
|
That's because you're not iterating through your results after your query. You're only storing the first row you retreive then you quit. You need to add the while loop aztek posted.
Better yet, quit using mysql_* functions and use a database abstraction layer like pear (pear.php.net). Using pear you can use:
$rows = $db->getAssoc("select key, value from table");
and $rows will be an associative array as
$rows[key] => value
with all records in it. |
|
Back to top |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Fri May 16, 2003 2:36 am Post subject: |
|
|
Well the problem was foreach loops through an indexed array of rows so it would be
Code: | array(0 => array('col1' => 'val1', 'col2' => 'val2'), 1 => array('col1' => 'val3', 'col2' => 'val4')) |
so you use foreach to extract out each inner array and then print those values for the select box. Using [] just made it add it to an array and not assign it directly to that variable. _________________ "Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane |
|
Back to top |
|
nprioleau Smarty Rookie
Joined: 15 Feb 2016 Posts: 7
|
Posted: Tue Feb 16, 2016 1:13 pm Post subject: Selected value is incorrect |
|
|
Would this be the right syntax when editing the form??
Code: | <select name="cont_contactGroup">
{foreach from=$contgrp item=grp}
<option selected="{$contact.cont_contactGroup}" value="{$grp.id}">{$grp._id} {$grp.Title}
{/foreach}
</select> |
Where $contact.cont_contactGroup is the value from the DB? |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Feb 16, 2016 6:31 pm Post subject: |
|
|
{html_options} … |
|
Back to top |
|
|