djp120 Posted February 5, 2008 Share Posted February 5, 2008 Hi all, Really a bit stuck here....if anyone has any ideas i would be very grateful! I have some data in a field (varchar) that may look something like "CODE Hello World, Hope all is well?". I want to perfrom a SELECT on the table for all entries with "CODE" as the first word. My problem is "CODE" may well be "CODE45" and therefore can be any length. I do know however the "CODE" part will be seperated by a space. I basically want to select all entries with "CODE" as the initial part of the data and output all data that follows the "CODE". For Example...SELECT * FROM data WHERE field.first word = 'CODE' Thank you! Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 is CODE always the first 4 characters? if so... <?php $sql = "SELECT field FROM table WHERE field LIKE 'CODE%'"; $result = mysql_result($sql) or die(mysql_error()); while($row = mysql_fetch_array($result)){ $text = substr($row['field'],4); print $text.'<br>'; } ?> Quote Link to comment Share on other sites More sharing options...
djp120 Posted February 5, 2008 Author Share Posted February 5, 2008 Thanks for the quick response... Unfortunately, CODE can vary in length and would always be precise. I would need to distinguish between CODE and CODE1 for example and only select the relative rows that it appears on. Cheers! Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 can you give a few more cases? If there will always be a space after the CODE#, you could do: case for "CODE Hello World, Hope all is well?": $sql = "SELECT field FROM table WHERE field LIKE 'CODE% '"; case for "CODE45 This is some text". $sql = "SELECT field FROM table WHERE field LIKE 'CODE45% '"; Quote Link to comment Share on other sites More sharing options...
djp120 Posted February 5, 2008 Author Share Posted February 5, 2008 Yeah, there will always be a space after the CODE#. For example, a selection of data may be: CODE12 Some Text CODE42 Some Text CODE2 Some Text CODE1 Some Text CODE244 Some Text Dan Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 Are you looking for something like this then? <table border="1"> <tr> <th>Code</th> <th>Message</th> </tr> <?php $sql = "SELECT field FROM table WHERE field LIKE 'CODE%'"; $result = mysql_result($sql) or die(mysql_error()); while($row = mysql_fetch_array($result)){ list($code,$text) = explode(' ',$row['field'],2); ?> <tr> <td><?=$code?></td> <td><?=htmlspecialchars($text)?></td> </tr> <?php } ?> </table> Quote Link to comment Share on other sites More sharing options...
djp120 Posted February 5, 2008 Author Share Posted February 5, 2008 I'm really sorry but i cannot seem to get that to work, it does look something like im after tho i think. I have this code after I have set up my connection to the database and I have a record in the table markers with the data in field name is "CODE34 This is 4 CODE34". <table border="1"> <tr> <th>Code</th> <th>Message</th> </tr> <?php $sql = "SELECT * FROM markers WHERE name LIKE 'CODE%'"; $result = mysql_result($sql) or die(mysql_error()); while($row = mysql_fetch_array($result)){ list($code,$text) = explode(' ',$row['name'],2); ?> <tr> <td><?=$code?></td> <td><?=htmlspecialchars($text)?></td> </tr> <?php } ?> </table> Thank you for your continued help, it is much appreciated! Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 let's start with something simpler...what is the output of this: <?php $sql = "SELECT * FROM `markers` WHERE `name` LIKE 'CODE%'"; $result = mysql_result($sql) or die(mysql_error()); print "Found ".mysql_num_rows($result)." rows<br>"; while($row = mysql_fetch_array($result)){ print_r($row); } ?> Quote Link to comment Share on other sites More sharing options...
djp120 Posted February 5, 2008 Author Share Posted February 5, 2008 Ok..now have that working and reverted to a previous post...It now strips the data perfectly. Thank you... If it's not too much trouble you would mind explaining what the following code means... list($code,$text) = explode(' ',$row['name'],2); Thanks again! Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 The explode() function is for splitting a string. The first argument is what you are splitting on, the second argument is the string you wish to split, and the third argument limits the number of pieces returned. Without the limit, it would return an array with the string split on EVERY space. Ok, so the explode function, used as it is, returns an array with 2 items. The first is everything before the first space (CODE#) and the second is everything after the first space (the rest of the string). What list() does is assign parts of an array. So, the first item of the array goes into $code and the second part goes into $text. It does the same thing as (but is a lot easier then using) this: $parts = explode(' ',$row['name'],2); $code = $parts[0]; $text = $parts[1]; Got it? Quote Link to comment 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.