Hi there,

I know a switch statement checks for the exact match of a variable but I was wondering if it could do a check to see if a sentance contained a certain word?

eg.

<?php
$x = "I have a cat";

    switch ($x)
	{
	case "Cat":
	  echo "they have a cat";
	  break;
	case "Dog":
	  echo "they have a dog";
	  break;
	case "Turtle":
	  echo "they have a turtle";
	  break;
	default:
	  echo "No Pets";
	}
?>

I dont want to have to write lots of IF statements and wondered if this was possible before I start coding up a large switch/case statement..

Thanks,
Chris

    Can you give us a more realistic example of what you're trying to do? There are a couple of ways you could do this, but it would really depend on what you're actually trying to do. For example, your code snippet above could be reduced down to:

    $x = "I have a cat"; 
    
    if(preg_match('/\b(cat|dog|turtle)\b/i', $x, $matches))
      echo "they have a " . strtolower($matches[1]);
    else
      echo "No Pets";

    or, even shorter:

    $x = "I have a cat";
    
    echo (preg_match('/\b(cat|dog|turtle)\b/i', $x, $matches) ? "they have a " . strtolower($matches[1]) : "No Pets");

      So I have a CSV file with approx 4000 rows in,

      After I have imported each row into the DB, I want to check the "Name" column for certain keywords.. if one of those keywords is present in the name then it will add a certain category name to another column called "Category"..

      Am I on the right track or is there a better way for me to acheive this?

      Thanks for your speedy reply! 🙂
      Chris

        include_me wrote:

        After I have imported each row into the DB, I want to check the "Name" column for certain keywords

        Why after? If you're doing the processing in the PHP script itself to INSERT the data in the first place, why not just do a bit more processing to determine the "Category" value and include it in the original INSERT statement, rather than trying to UPDATE the table afterwards?

        You didn't give us specific examples, but from what you're saying it sounds like you could use something like my preg_match() example above.

          Because I am using PHP MY Admin to import the CSV file.. and It wont let me do any processing on the data when im importing it using the built in tools.

            Is this a one-time situation, or could this happen in the future as well? If you said yes to the latter, it wouldn't be a bad idea to write up a simple PHP script that reads the CSV file and does the importing - that way you can have the flexibility of adding something like what you're talking about here.

            Either way, you're still going to have to parse the CSV file, read each entry, and possibly UPDATE the appropriate row in the DB... so you're doing about the same amount of work either way (possibly more work if you use phpMyAdmin, since you have the added step of generating a WHERE condition that will uniquely match the set of data you're trying to modify).

              It would most likely be something that happens weely/monthly? I havent got a problem with uploading the CSV, I just wanted to know the quickest and least stressful way of doing it without causing performance issues on the server.

              Do you think it would be worth me trying to write a stored procedure and using that instead of doing it through PHP?

              Thanks,
              Chris

                include_me wrote:

                Do you think it would be worth me trying to write a stored procedure and using that instead of doing it through PHP?

                Not really, no, because there's no need to do it the way you're wanting to do it presently.

                It would be a lot easier (IMHO) and simpler to simply use a PHP script to read and process the CSV file. That way, you can read in a row from the CSV file (using [man]fgetcsv/man), do your keyword matching to populate a "category" field, and then perform an INSERT for that row (containing the correct category).

                  An alternative would (I suspect) be to import the CSV directly into MySQL (I recall it does have a command to import CSV files .... ah, "LOAD DATA INFILE").

                  If you write a PHP script that constructs the appropriate import statement and sends it to the MySQL, then MySQL can load and import the file, returning to PHP whatever it returns when it's finished.

                  Then PHP can issue update commands to the database to set the "Category" fields of records that contain the appropriate keywords in their "Name" fields.

                  For added performance, the initial load and category update could be done in a temporary table (with the same structure as the real table): partly to take some of the load off the table's indexes (the temp table probably doesn't need any unless they'd help aid the "Name" column search), partly to avoid the updates wasting time on the thousands of records already present, and partly to avoid locking the table too much while performing the category updates. When the new records have been knocked into shape, they can then be copied across by an INSERT ... SELECT statement.

                  The main thing is that importing CSV in bulk is pretty much always much faster than a series of INSERT statements (as it happens, I think phpMyAdmin parses the CSV and issues multiple INSERTs as well). As it happens, it also sidesteps having PHP read the file and pass the contents to MySQL. The principal drawback is that MySQL needs read access to the file in question.

                  Actually, if you are using a temp table to load the CSV into, and depending on how complicated it would be to search the "Name" fields, you could have your script make a request to select all of the distinct names from the temp table ("Select distinct name from temptable"). The script goes through the list and pairs names with categories in whatever way it's supposed to, and then it can send commands to update the temp table ("update temptable set category='colour' where name='maroon'" or whatever).

                    Write a Reply...