Phpnewbie23 Posted March 31, 2009 Share Posted March 31, 2009 I am trying to remove the D:/ from an combo box populate by a ODBC connection. I have tired trim which seemed to work. But removed the D's from some of my values inside the combo box. See Example 1 Example using <?php $val = trim($client, 'D:/'); ?> <-----------> Combox Item1 D:/Dog Combox Item2 D:/Cat Combox Item2 D:/Tree <-----------> Example OutPut <-----------> Combox Item1 og Combox Item2 Cat Combox Item2 Tree <-----------> After that i tired substr Example using <?php echo substr($client,3); ?> <-----------> Combox Item1 D:/Dog Combox Item2 D:/Cat Combox Item2 D:/Tree <-----------> Example OutPut <-----------> Combox Item1 D:/Dog Combox Item2 D:/Cat Combox Item2 D:/Tree <-----------> Using trim i get the kind of result i am looking for but cannot work out why some of my values that start with a D for example Dog are removed. After that i would like to have my combo box values sorted alphabetically my code is kind of messed up has i have had a go at both and nearly got it working all help well appricated <?php include 'db_conn.php'; echo "<br>"; echo "<form action='index.php' method='POST'>"; echo "Client: <select name='Client'>"; while(odbc_fetch_row($rs)){ //($rs, $rownumber=0)) //(odbc_fetch_row($rs)) // array($rs) $client=odbc_result($rs,"BACKUP_LOCATION"); //$alpha[] = odbc_fetch_row($rs); //sort($alpha); //foreach ($alpha as $key => $val) { //$val = trim($client, 'D:'.'); echo substr($client,3); echo "<option value='null'>$client</option>"; } //} echo '</select>'; ?> Cheers James Quote Link to comment https://forums.phpfreaks.com/topic/151904-substr/ Share on other sites More sharing options...
PHP Monkeh Posted March 31, 2009 Share Posted March 31, 2009 Use a combination of substr() and strlen() if that's the way you'd like to go, like this: $val = substr($client, 3, strlen($client)); Quote Link to comment https://forums.phpfreaks.com/topic/151904-substr/#findComment-797661 Share on other sites More sharing options...
taquitosensei Posted March 31, 2009 Share Posted March 31, 2009 try this $val_exp=explode(":\/", $client); $val=$val_exp[1]; should give you everything after the D:/ it will also accommodate other drive letters to without any changes. Quote Link to comment https://forums.phpfreaks.com/topic/151904-substr/#findComment-797663 Share on other sites More sharing options...
ScotDiddle Posted March 31, 2009 Share Posted March 31, 2009 Phpnewbie23, The following will work, and it doesn't care about position or string length... Scot L. Diddle, Richmond VA <?php $val = str_ireplace('D:/', '', $client); // case insensitive replace : str_ireplace($thisValue, $withNewValue, $inThisVar); ?> Quote Link to comment https://forums.phpfreaks.com/topic/151904-substr/#findComment-797665 Share on other sites More sharing options...
Phpnewbie23 Posted March 31, 2009 Author Share Posted March 31, 2009 Thanks for the quick reply i tried Phpnewbie23, The following will work, and it doesn't care about position or string length... Scot L. Diddle, Richmond VA Code: [select] <?php $val = str_ireplace('D:/', '', $client); // case insensitive replace : str_ireplace($thisValue, $withNewValue, $inThisVar); ?> which worked a treat.. thanks for that. My next issue i would like the combo box values to be shown alphabetical but my code below show this <--------------> Combo Item 1 Dog Combo Item 2 Dog Combo Item 3 Dog Combo Item 4 Dog Combo Item 5 Cat Combo Item 6 Cat Combo Item 7 Cat Combo Item 8 Cat Combo Item 9 Tree Combo Item 10 Tree Combo Item 11 Tree Combo Item 12 Tree <--------------> <?php include 'db_conn.php'; echo "<br>"; echo "<form action='index.php' method='POST'>"; echo "Client: <select name='Client'>"; while(odbc_fetch_row($rs)){ //($rs, $rownumber=0)) //(odbc_fetch_row($rs)) // array($rs) $client=odbc_result($rs,"BACKUP_LOCATION"); $alpha[] = odbc_fetch_row($rs); sort($alpha); foreach ($alpha as $key => $val) { $val = str_ireplace('D:/', '', $client); echo "<option value='null'>$val</option>"; } } echo '</select>'; ?> Again all help well appricated. Cheers James Quote Link to comment https://forums.phpfreaks.com/topic/151904-substr/#findComment-797670 Share on other sites More sharing options...
lonewolf217 Posted March 31, 2009 Share Posted March 31, 2009 technically it is sorting correctly. since the numbers come before the words (i.e. "1" before "Dog" in the string, it will sort on the numbers, then it will sort on the words. Is this actually the results you want in the dropdown or is it just an example ? if the options will ALWAYS have "combo item" text in the option, there is no reason to store that in the database. Also, if the specific number has no relation to the item name (i.e. "1" will not always be paired with "Dog") why dont you just store the last word in the line in your database, and then just output the rest as necessary <?php $alpha[] = odbc_fetch_row($rs); sort($alpha); $i = 1; //counter foreach ($alpha as $key => $val) { $val = str_ireplace('D:/', '', $client); echo "<option value='null'>Combo Item $i $val</option>"; $i++; //increment your counter } Quote Link to comment https://forums.phpfreaks.com/topic/151904-substr/#findComment-797672 Share on other sites More sharing options...
Phpnewbie23 Posted March 31, 2009 Author Share Posted March 31, 2009 Thanks lonewolf217 The issue i have just worked out is what i planned to do will not work at all...... GRRRRRRR > My Idea now is to create a text file with all clients names in it. Somehow populate the combo box with the clients names in php. Then on selecting Z company so the data for that company. Any ideas anyone i keep drawing blanks. Cheers James Quote Link to comment https://forums.phpfreaks.com/topic/151904-substr/#findComment-797863 Share on other sites More sharing options...
lonewolf217 Posted March 31, 2009 Share Posted March 31, 2009 why a text file? store it in a define file define.php <?php $clients = array('cust1','cust2','cust3','cust4'); ?> otherpage.php <?php include 'define.php'; echo "<SELECT MULTIPLE 5>"; foreach($clients as $client) { echo "<OPTION>".$client."</OPTION>"; } echo "</SELECT>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/151904-substr/#findComment-797870 Share on other sites More sharing options...
Phpnewbie23 Posted April 1, 2009 Author Share Posted April 1, 2009 Yes good suggestion lonewolf217 but we could be adding/deleting new clients. I wanted a easy and user friendly way for users to delete and add customers as not all the users are PC friendly Quote Link to comment https://forums.phpfreaks.com/topic/151904-substr/#findComment-798418 Share on other sites More sharing options...
Yesideez Posted April 1, 2009 Share Posted April 1, 2009 Still having problems removing the drive label? Quote Link to comment https://forums.phpfreaks.com/topic/151904-substr/#findComment-798420 Share on other sites More sharing options...
Phpnewbie23 Posted April 1, 2009 Author Share Posted April 1, 2009 Thanks Yesideez yes that is sorted just but unfortnatly not need as i found out. Last night i just need this blooming combo box to sort out ahhhhhh Quote Link to comment https://forums.phpfreaks.com/topic/151904-substr/#findComment-798446 Share on other sites More sharing options...
lonewolf217 Posted April 1, 2009 Share Posted April 1, 2009 Yes good suggestion lonewolf217 but we could be adding/deleting new clients. I wanted a easy and user friendly way for users to delete and add customers as not all the users are PC friendly then store it in the database. have a form that people can use to add, modify or delete entries from the list Quote Link to comment https://forums.phpfreaks.com/topic/151904-substr/#findComment-798507 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.