In my experience with PHP and MySQL, being a bit of a purist, I have tried to use MySQL to store pictures for a website. This, I discovered, was an exercise in futility. It can be done, but it is FAR more trouble than it is worth. In addition, if you did store the pictures to MySQL (using a BLOB type), modifying it or changing it is even more trouble.
So, the best way it to simply make a directory (or directories), rename the file using a unique name, and put the images in the desired directory. Then, tell MySQL the name of the path and file name. Then, when we need the image, we can query the database for the path, and retrieve the image.
Let's divide it up into steps:
Step 1:
Wednesday, May 21, 2008
Friday, May 9, 2008
MySQL - find only words in a field
This snippet here is pure gold! When making a keyword search, I searched high and low for a method to use MySQL to find an individual word.
I even asked my professor, who taught the databases course at Cal State San Marocs(a credible Computer Science department), and he is to be respected, for he knows a great deal about Computer Science. He told me he would get back to me, but either he didn't try to find the answer or he tried and couldn't figure it out!
Here's an example of the problem: suppose I want to search for a book title, which requires querying title fields in a table called 'Books'. Now suppose I want to find books with the word 'hot' in their title. But I don't want to return books that contain the words 'shot', 'hotel', 'thot', etc.
The key is to use the REGEXP operator. And there are a series of symbols you can use. I will post the site listing the symbols, but the main symbol you need to know is the [[:<:]] [[:>:]] symbols. This locates words in a field that either have nothing next to it or a space, comma, or period next to it. Observe:
Wow, what a simple solution to such a long-sought and puzzling question. Here is the website for the other metacharacters. Also, I might add that after I found the answer, I never told my professor :).
I even asked my professor, who taught the databases course at Cal State San Marocs(a credible Computer Science department), and he is to be respected, for he knows a great deal about Computer Science. He told me he would get back to me, but either he didn't try to find the answer or he tried and couldn't figure it out!
Here's an example of the problem: suppose I want to search for a book title, which requires querying title fields in a table called 'Books'. Now suppose I want to find books with the word 'hot' in their title. But I don't want to return books that contain the words 'shot', 'hotel', 'thot', etc.
The key is to use the REGEXP operator. And there are a series of symbols you can use. I will post the site listing the symbols, but the main symbol you need to know is the [[:<:]] [[:>:]] symbols. This locates words in a field that either have nothing next to it or a space, comma, or period next to it. Observe:
SELECT *
FROM books
WHERE title REGEXP '[[:<:]]hot[[:>:]]';
Wow, what a simple solution to such a long-sought and puzzling question. Here is the website for the other metacharacters. Also, I might add that after I found the answer, I never told my professor :).
Wednesday, April 23, 2008
C language: Process Control Block Queue
This is a basic program that consists of a Process Control Block (PCB), and a Queue (which is a vector). I won't display the whole program, but I will show the structure. First, the PCB class definition. I only need a header file for the PCB class, because the class only needs variables, not methods:
So, there it is, now you simply need a main function that calls the ReadyQ methods, and you are good to go!
// PCB.h
#ifndef PCB_H
#define PCB_H
Class *PCB {
int ID;
string STATE;
};
#endif
Now, here is the ReadyQ class, that uses a vector. The ReadyQ class's methods manipulate the PCB. I will only list the class source file, and not bother displaying the header:
//*************************************************************************************
// Name: Queue.C
// By: Phillip McCubbin
// Description:
// This file contains the implementation of Queue class which is
// used to store the data in the order of first in first out (FIFO)
//*************************************************************************************
#include
#include"PCB.h"
#include "ReadyQ.h"
using namespace std;
//*************************************************************************************
// Method: Queue::Queue
// Description:
// Constructor. Initializes the data members to 0 and allocates
// memory to storage.
// No arguments, no returns
//*************************************************************************************
ReadyQ::ReadyQ() {
*PCB = new int
}
//*************************************************************************************
// Method: Queue::~Queue
// Destructor
//*************************************************************************************
ReadyQ::~ReadyQ(){
m_data.clear();
}
//*************************************************************************************
// Method: Queue::isEmpty
// Description:
// Check if the queue is empty
// Returns:
// true - if the queue is empty
// false - otherwise
//*************************************************************************************
bool ReadyQ::isEmpty(){
return m_data.empty();
}
//*************************************************************************************
// Method: Queue::size
// Description:
// Returns the size of the queue
// Returns:
// m_total - size of the queue
//*************************************************************************************
int ReadyQ::size() {
return m_data.size();
}
//*************************************************************************************
// Method: Queue::enqueue_proc
// Description:
// This pushes the data onto the queue
// Arguments:
// value - data to be inserted
//*************************************************************************************
void ReadyQ::enqueue_proc(int value){
m_data.push_back(value);
}
//*************************************************************************************
// Method: Queue::dequeue_proc
// Description:
// Pop an element from the queue
//*************************************************************************************
void ReadyQ::dequeue_proc() {
if ( isEmpty() ){
cout << "Fatal Error - Queue is empty!!" << endl;
return;
}
m_data.erase(m_data.begin());
}
//*************************************************************************************
// Method: Queue::front
// Returns the first/top element of the queue.
// Returns:
// int - top element
//*************************************************************************************
int ReadyQ::front(){
return m_data[0];
}
//*************************************************************************************
// Method: Queue::displayAll
// Description:
// Displays the entire queue to stdout.
//*************************************************************************************
void ReadyQ::displayAll(){
for ( int i = 0; i < m_data.size(); i++ ) {
cout << m_data[i] << " ";
}
cout << endl;
}
So, there it is, now you simply need a main function that calls the ReadyQ methods, and you are good to go!
Tuesday, April 15, 2008
C++: Using pointers
This is a very basic use of C++ pointers, but it is really essential to know how pointers work.
Thursday, April 10, 2008
C++: Find 1st non-repeating character
Here is a function that I enjoyed solving. You have to write a function that takes a string input and finds the first non-repeating character in a string (eg. “ABCA” -> B). This code runs in about O(n). Enjoy!
/*FUNCTION*/ //*FIND 1ST NON-REPEATING****************************************
char find_nonrepeat_char(strLetters) {
//make ascii array to store letter count, then count
{
int countArray[25];
for (int i =0; i < 26; i++) countArray[i] = 0;
for (int i = 0; i < length(strLetters); i++) {
countArray[(int)strLetters[i]-65]++;
}
for (i = 0; i < length(strLetters); i++) {
if (countArray[(int)strLetters[i]-65)] == 1) return strLetters[i];
}
}
Saturday, April 5, 2008
CORRECTION php: Cleaning user input text
After reviewing my last post, I came to the conclusion that code should not be bunched together as I did. Sure, it may make the code look sharp and concise, but clarity and user understandability is our second priority (efficiency is our first). So, here is my revision of the code in my last post. I have made it a function, so you can simply call it each time you need to clean a string. Put it in a common_functions.php (or whatever you want to name it) file in directory named "include".
//*function:CLEAN STRING*******************************************************
function clean_string($text) {
$text =
ereg_replace("[,]"," ",$text); //replace , with space
$text = strtolower($text); //lower case
$text = eregi_replace("[^a-z0-9 ']", "", $text); //keep a-z0-9 space '
$text = preg_replace('/[\s\s+]/', ' ', $text); //multiple spaces -> 1
$text = trim($text); //trim
$text_arr = explode(' ',$text); //make into an array
return $text_arr;
}
Thursday, April 3, 2008
php: Cleaning user input text (some text manipulation)
There are many times you will want to ask a user for text input. One of the more common usages is in a search. And usually you will take the word and use it to make a query. But for now, it is important that the code be "cleaned" so that frivolous mistakes go unnoticed by the system (e.g. the user puts a space between words). Also, we are going to take words separated by spaces and make them their own words by putting them in an array.
This snippet does the following:
1) ereg_replace - replaces all the commas with spaces in the $text variable
2) strtolower - makes the whole string lowercase,
3) eregi_replace - replaces anything that isn't a number, letter, space, or apostrophe
4) preg_replace - replaces multiple spaces with 1 space
5) trim - trims the spaces from the sides
6) explode - "explodes" the string into an array delimited by spaces
//*MANIPULATE STRING****************************************************
$cleaned_text = explode(' ',trim(preg_replace('/[\s\s+]/', ' ', eregi_replace("[^a-z0-9 ']", "", strtolower(ereg_replace("[,]"," ",$text))))));
This snippet does the following:
1) ereg_replace - replaces all the commas with spaces in the $text variable
2) strtolower - makes the whole string lowercase,
3) eregi_replace - replaces anything that isn't a number, letter, space, or apostrophe
4) preg_replace - replaces multiple spaces with 1 space
5) trim - trims the spaces from the sides
6) explode - "explodes" the string into an array delimited by spaces
//*MANIPULATE STRING****************************************************
$cleaned_text = explode(' ',trim(preg_replace('/[\s\s+]/', ' ', eregi_replace("[^a-z0-9 ']", "", strtolower(ereg_replace("[,]"," ",$text))))));