Jump to content

Newb needs help parsing database field


billerda

Recommended Posts

Hi All,

 

Not sure if strpos() is the way to go with this or not.

Here's the scenario. I have a field in my MySql database called "Items" containing a value like "001, 002, 003, 004, 005".

So when I pull the database field like $row["Items"] and put it into a string (strItems) I want to then scan the value and see if it contains "003" and if it does do something different in my code the the other numbers.

 

In ASP I would do this something like <% if InStr(strSubCategory, "003") <> 0 then %>

 

How do I pull it off in PHP?

 

Thanks in advance!

 

Bill

Link to comment
Share on other sites

hey there,

 

i would suggest taking that string and converting it to an array, since thats what it looks like anyhow.

ex:

<?php

$strItems = '001, 002, 003, 004, 005';  // your string
$SearchFor = '003' ;  // what you are searching for 

$arrayItems = explode(',', $strItems );  // new array created by splitting the data between the comma

// now you check if that value is found in the array 
if ( in_array($SearchFor, $arrayItems ) ) :
    echo 'i found ' . $SearchFor. ' in the array ';
else :
    echo 'i did not find the string in the array, why?' ;
endif ; 

 

 

good luck.

Link to comment
Share on other sites

Hey freakstyle,

 

Thanks for the suggestion, looks like a step in the right direction.

Turns out I'm going to have to use a Case statement instead of an If statement.

Something like

 

Case "001" was found in string

    do this

Case "002" was found in string

    do that

Case "003" was found in string

    do something else

etc

 

What would you recommend in this instance for determining what numbers are found in the string?

 

Thanks again

 

Bill

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.