DnJ Posted November 12, 2006 Share Posted November 12, 2006 I have a php array that I am trying to alter to get the results I want.Original code[code]$arr = array();for( $i = 0 $i <= 9; $i++ ) { $arr[$i] = $i;}[/code]Would like to change the above array and drop the 0 - 10. The options I am looking to include in the array[code]<select><option value ="positive">Positive</option><option value ="negative">Negative</option><option value ="genuine" selected="selected">Genuine Contact</option><option value ="twaste">Time Wasters</option></select>[/code]New to php so actually have no idea if doing this even possible.Thanks for any help or input with this problem Link to comment https://forums.phpfreaks.com/topic/27027-php-array-help/ Share on other sites More sharing options...
poe Posted November 12, 2006 Share Posted November 12, 2006 <select><option value ="positive">Positive</option><option value ="negative">Negative</option><option value ="genuine" selected="selected">Genuine Contact</option><option value ="twaste">Time Wasters</option></select>try:[code]$ary = array( "positive"=>"Positive", "negative"=>"Negative", "genuine"=>"Genuine Contact", "twaste"=>"Time Wasters",);echo "<select>\n";foreach($ary as $k => $v) { echo "<option value=".$k.">".$v."</option>\n";}echo "</select>\n";[/code] Link to comment https://forums.phpfreaks.com/topic/27027-php-array-help/#findComment-123605 Share on other sites More sharing options...
extrovertive Posted November 12, 2006 Share Posted November 12, 2006 To modify the above for a selected item...[code]$ary = array( "positive"=>"Positive", "negative"=>"Negative", "genuine"=>"Genuine Contact", "twaste"=>"Time Wasters",);$keySelect = "twaste";echo "<select>\n";foreach($ary as $k => $v) { if($k === $keySelect) echo "<option value=".$k." selected=\"true\">".$v."</option>\n"; else echo "<option value=".$k.">".$v."</option>\n";}echo "</select>\n";[/code] Link to comment https://forums.phpfreaks.com/topic/27027-php-array-help/#findComment-123609 Share on other sites More sharing options...
DnJ Posted November 13, 2006 Author Share Posted November 13, 2006 Thank you, I appreciate you taking the time to help.I thought if I had the new array I would be able to implement it into my files, I obviously thought wrong!Original Code:[code]$arr = array();for( $i = 0 $i <= 9; $i++ ) { $arr[$i] = $i;}$t->assign ( 'rate_values', $arr );$t->assign('rendered_page', $t->fetch('rate.tpl') );[/code][u]rate.tpl - array reference[/u][code]{foreach item=item from=$rate_values} {if $item == 0} <td><input type="radio" name="txtrate" value="{$item}" checked="checked"/><b>{$item}</b></td> {else} <td><input type="radio" name="txtrate" value="{$item}"/><b>{$item}</b></td> {/if} {/foreach}[/code]How can I integrate the array, like I said - thought it would be easy I was wrong![code]$ary = array( "positive"=>"Positive", "negative"=>"Negative", "genuine"=>"Genuine Contact", "twaste"=>"Time Wasters",);$keySelect = "genuine";echo "<select>\n";foreach($ary as $k => $v) { if($k === $keySelect) echo "<option value=".$k." selected=\"true\">".$v."</option>\n"; else echo "<option value=".$k.">".$v."</option>\n";}echo "</select>\n";[/code]These options are stored in a database as well[code]$profid = trim( $_POST['profileid'] );$userid = trim( $_POST['userid'] );$rating = trim( $_POST['txtrate'] );if ( $userid && $profid && $rating ) { $sql = 'INSERT INTO ! ( userid, profileid, rating, rate_time ) VALUES ( ?, ?, ?, ? )'; $db->query( $sql, array( USER_RATING_TABLE, $userid, $profid, $rating, time() ) );[/code] Link to comment https://forums.phpfreaks.com/topic/27027-php-array-help/#findComment-123716 Share on other sites More sharing options...
doni49 Posted November 13, 2006 Share Posted November 13, 2006 I don't understand what you mean by "integrate the array". Link to comment https://forums.phpfreaks.com/topic/27027-php-array-help/#findComment-123749 Share on other sites More sharing options...
Destruction Posted November 13, 2006 Share Posted November 13, 2006 They mean there's a templating system involved that references an array that now doesn't exist under the new select array procedure.Let's work backwards from the template so we can follow this through...1:[code]{foreach item=item from=$rate_values} {if $item == 0} <td><input type="radio" name="txtrate" value="{$item}" checked="checked"/><b>{$item}</b></td> {else} <td><input type="radio" name="txtrate" value="{$item}"/><b>{$item}</b></td> {/if}{/foreach}[/code]2:[code]$arr = array();for( $i = 0 $i <= 9; $i++ ) { $arr[$i] = $i;}$t->assign ( 'rate_values', $arr );$t->assign('rendered_page', $t->fetch('rate.tpl') );[/code]1) $rate values is set by $t->assign('rate_values', $arr);2) $arr is a numeric array of 10 numbers and we want to change it. So we want to do something similar to this...[code]<?php$arr = array();$arr['positive'] = "Positive";$arr['negative'] = "Negative";$arr['genuine'] = "Genuine Contact";$arr['twaste'] = "Time Wasters";?>Instead of:<?php$arr = array();for( $i = 0 $i <= 9; $i++ ) { $arr[$i] = $i;}?>[/code]Then:[code]{foreach key=value from=$rate_values} {if $key == "genuine"} <td><input type="radio" name="txtrate" value="{$key}" checked="checked"/><b>{$value}</b></td> {else} <td><input type="radio" name="txtrate" value="{$key}"/><b>{$value}</b></td> {/if}{/foreach}Instead of:{foreach item=item from=$rate_values} {if $item == 0} <td><input type="radio" name="txtrate" value="{$item}" checked="checked"/><b>{$item}</b></td> {else} <td><input type="radio" name="txtrate" value="{$item}"/><b>{$item}</b></td> {/if}{/foreach}[/code]This may not be very clear straight away but I'm basically showing which sections to replace with what...I hope it's clear enough though but let me know and I'll try to explain.Dest Link to comment https://forums.phpfreaks.com/topic/27027-php-array-help/#findComment-123756 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.