Re: Removing first word from a string?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Chris,

I'll try to explain the basics of a technique I often use:

imagine you have 0 or more possible where clauses. start of with an empty array e.g.

$whereClauses = array();

now for each clause you need add the relevant subclause as a seperate item to the array (DO NOT include the AND or WHERE string). e.g.

if ($foo) {
// it up to you to make sure $foo is okay for adding to a query string
	$whereClauses[] = " foofield='$foo'";
}

once you have gone thru all the possible subclauses that could exist do something like this (I'll assume $sql contains the base query):

if (count($whereClauses)) {
	// we have a where statement to make
	$sql = $sql . ' WHERE ' . join(' AND ', $whereClauses);
}

I leave it up to your imagination how you apply this idea!

greets,
Jochem

Bastien Koert wrote:
one simple trick is to use a clause that always evaluates correctly. For example, in my site I don't delete data, but flag it with a field (record_deleted) which defaults to 'No' (actually 0) and can be set to 'Yes' (again 1) so that all queries are built with the requirement to use AND

ie

$sql = "select * from tablename where record_deleted = 0 "

//code to delevop additional where paramters goes here
if (!empty($_POST['city'])){
  $sql .= " AND city like '{$_POST['city']}%' ";
}//end if


you could also use the same mechanism that PHPMYAdmin does which is to 1

select * from tablename where 1

since one always evaluates to true and does not affect the query, its a simple way of doing the same as above



bastien

From: "Chris Payne" <cjp@xxxxxxxxxxxxxxxxx>
To: <php-db@xxxxxxxxxxxxx>
Subject:  Removing first word from a string?
Date: Mon, 3 Jan 2005 21:46:03 -0500

Hi there everyone,



I am building a dynamic MySQL query for a project I am working on, but there
are instances where it products WHERE AND instead of WHERE city etc ….. due
to the nature of the system I am developing. My question is, how can I
remove the FIRST “ AND” from a string if it is present, but leave all other
AND statements in the string?




I would really appreciate any help on this. I can do a find and replace on
a string no problem, but just want it to be removed IF it is the FIRST word
in the string.




Oh and Happy New Year everyone.



Chris


-- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.296 / Virus Database: 265.6.7 - Release Date: 12/30/2004



-- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux