Page 1 of 1

substr from query

Posted: Tue Jan 06, 2004 11:19 pm
by apek
hello all gurus...
i have a problem with the substr function..
firstly i wanna tell that i'm a newbie in php..

now i describe my problem...
i have a combobox whick the lists i load up from mysql database...

the list contains ID such as B01,E01 and D01...
i call all these ID's Major ID...

There are another ID's such as B02,B03,E02,E03,D02 and D03 which i call Minor ID...

all wanna do is when a user select the major ID from the first combobox(Major ID) then PHP automatically recognize the first letter (let say the first letter from B01 is B) and then load up the the second combobox (Minor ID) with ID that only started with B....(do u understand?i'm sorry..my english is pretty bad...

for reference,i enclose here my silly code snippets:

Code: Select all

<select name="noTag" size="1">

            <option value="0">Choose Major ID&nbsp;&nbsp;&nbsp;</option>

            <?php



   //----------------------------------------------------

   //          Access Data From MySQL

   //          TABLE : tPartMajor

   //----------------------------------------------------



    $query2 = "Select * from ".$DBprefix."tPartMajor";

    $mesin = mysql_query($query2);

            while ($dbq = mysql_fetch_array($mesin)) &#123;

            echo("<OPTION VALUE=$dbq&#1111;idMajor] selected>$dbq&#1111;idMajor]</option>\n");

            $front=substr($dbq&#1111;idMajor],0);

            

            

     &#125;

?>

          </select>

      </td>

    </tr>

    <tr> 
      <td valign="top" height="27"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>ID 

        Minor:</b></font></td>

      <td valign="top"> 
        <select name="noTag2" size="1">

          <option value="0">Choose Minor ID</option>

    

<?php

   //----------------------------------------------------

   //         Access Data From MySQL

   //         TABLE : tPartMinor

   //----------------------------------------------------



    $query2 = "Select * from ".$DBprefix." tPartMinor where substring(idMinor,0)='$front'";

    $mesin = mysql_query($query2);

            while ($dbq = mysql_fetch_array($mesin)) &#123;

            echo("<OPTION VALUE=$dbq&#1111;idMinor] selected>$dbq&#1111;idMinor]</option>\n");

     &#125;

?>



        </select>

pls help me.........

Posted: Wed Jan 07, 2004 4:05 am
by igoy
substring works like this...

Code: Select all

<?php

$variable = "This is iGoy";

echo substr($variable, 0, 4); 
// above will output - This

echo substr($variable, 6, 4);
// above will output - iGoy;

?>
for more on substr function, check this out.

follow this link : [php_man]substr[/php_man]

....

Posted: Wed Jan 07, 2004 4:17 am
by apek
igoy..
i know that...
i read the manual at php.net..
but all the examples at php.net do not meet my problem here...

Posted: Wed Jan 07, 2004 4:29 am
by m3mn0n
How about using mysql's left() function?

Code: Select all

LEFT(str,len) 
Returns the leftmost len characters from the string str: 
mysql> SELECT LEFT('foobarbar', 5);
        -> 'fooba'

.......

Posted: Wed Jan 07, 2004 5:26 am
by apek
so how my sql statement will look like??because look at my original sql statement..there a WHERE statement that i equal to $front.
The $front variable on the other hand came from the earlier operation...
pls help me..

Posted: Wed Jan 07, 2004 7:14 am
by m3mn0n
Using multiple WHERE's goes something like this:

Code: Select all

WHERE (foo=bar) AND (php=l33t)

Posted: Wed Jan 07, 2004 7:17 am
by apek
can help me fix my code above????
i still cant get the output i want...argghh!!!