Jump to content

Substr


Phpnewbie23

Recommended Posts

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

Link to comment
Share on other sites

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);

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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

 

 

 

 

 

Link to comment
Share on other sites

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>";
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.