Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Array to select box

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
cditty
Smarty Rookie


Joined: 13 May 2003
Posts: 15

PostPosted: Thu May 15, 2003 2:53 pm    Post subject: Array to select box Reply with quote

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
View user's profile Send private message
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Thu May 15, 2003 3:10 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
cditty
Smarty Rookie


Joined: 13 May 2003
Posts: 15

PostPosted: Thu May 15, 2003 3:23 pm    Post subject: Reply with quote

GOOD GRIEF!!!!! That was the problem? Brackets? 2 stinkin brackets??? Laughing

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
View user's profile Send private message
toma
Smarty Regular


Joined: 25 Apr 2003
Posts: 62

PostPosted: Thu May 15, 2003 4:59 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Fri May 16, 2003 2:36 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
nprioleau
Smarty Rookie


Joined: 15 Feb 2016
Posts: 7

PostPosted: Tue Feb 16, 2016 1:13 pm    Post subject: Selected value is incorrect Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Feb 16, 2016 6:31 pm    Post subject: Reply with quote

{html_options} …
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP