Jump to content

Strip & Search Help


djp120

Recommended Posts

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!

Link to comment
Share on other sites

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>';
  }
?>

Link to comment
Share on other sites

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% '";

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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?

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.