is there better way of doing it?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You're welcome.
rami
Forum Contributor
Posts: 217
Joined: Thu Sep 15, 2005 8:55 am

Post by rami »

by the way i made the code worked
but after i thought and thought
after all it is doing same thing making nested if selection
only way of making query is different
i am afarid that this may put significant effect on performance and database

and may be pros will not consider its good coding ...may be will be embrassed later

so ok fine its doing job
arent there any pro way of doing it...

Note by the way the solved code by Everah is logically incorrect but it make me orrect ways though syntax...
i have done it
so once again thanks but how does this pro site does it
for instance this site has numerous such fields with lots of all condition
http://www.jeevansathi.com/

any such ways
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

rami wrote:Note by the way the solved code by Everah is logically incorrect...
I took that straight from your code. I only cleaned up redundancy and use of code that was duplicated one line up. I didn't really change anything.
rami
Forum Contributor
Posts: 217
Joined: Thu Sep 15, 2005 8:55 am

Post by rami »

Everah wrote:
rami wrote:Note by the way the solved code by Everah is logically incorrect...
I took that straight from your code. I only cleaned up redundancy and use of code that was duplicated one line up. I didn't really change anything.
i have no complains for that
it was my fault and u gave me way of doing it in better way

what about thinking of latter part
as i say it is not actually increasing efficiency but just doing thing in some what managed way...
so is there any organized way of doing it

i tried switch but same thing i need query in each case so same thing....
rami
Forum Contributor
Posts: 217
Joined: Thu Sep 15, 2005 8:55 am

Post by rami »

if i search for it in google what keyword will get me to tutorials for this kind of search project
i am not getting idea
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

rami wrote:i tried switch but same thing i need query in each case so same thing....
This is your problem. You do NOT need a seperate query for each case. You need to build a query from different parts. This was shown at the beginning of the thread.
rami
Forum Contributor
Posts: 217
Joined: Thu Sep 15, 2005 8:55 am

Post by rami »

onion2k wrote:
rami wrote:i tried switch but same thing i need query in each case so same thing....
This is your problem. You do NOT need a seperate query for each case. You need to build a query from different parts. This was shown at the beginning of the thread.
can you show me how
i didnt realy got it
i would be very very g8ful for that
this is really taking my lot of time
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

I'll try and explain.

In your original code, you had a separate query for each case - if the user clicks the first checkbox, a certain query was executed. If the user clicked the second checkbox, then a different query was executed.

What onion2k was trying to show was that instead of coding all these separate queries, you can create your query based on the user input.

Simple database SELECT queries are typically made up of four distinct sections:

- the SELECT section, where you list the fields you need to be returned.
- the FROM section, where you list the tables to search in
- the WHERE section, where you put the code that limits/defines the records to be returned
- the part at the end of the query, where you put your order bys, group bys and limit instructions.

Because of this, one query is very similar to another query - this structure doesn't tend to change.

So... why is this important? It means that you can "create" queries while the script is processing, rather than hard-coding the queries into the script.

A simple example: Imagine a form with two radio buttons. If the user selects radio button #1, the query must return all records with id less than or equal to 10. If the user selects radio button #2, the query must return all records with id greater than 10.

One way to do this, is to create two queries: "SELECT * FROM table_name WHERE id <= 10" and "SELECT * FROM table_name WHERE id > 10"

A better way to do it is to create the query from pieces, and build it up into a full query:

Code: Select all

$radio_selected = $_POST['radio'];

$sql = "SELECT first_field, second_field, third_field FROM table_name ";

switch ($radio_selected) {
    case 1:
        $sql .= "WHERE id <= 10";
    case 2:
        $sql .= "WHERE id > 10";
    default:
        die('invalid radio button choice');
}

$sql .= " ORDER BY id";

$result_set = mysql_query($sql);
Here you can see the first part of the query just deals with the SELECT and FROM sections (which do not change depending on the user's selection). Inside the switch, we add the WHERE part of the query - which changes depending on the user's choice. We also include a default case, for any values that were not specifically dealt with before. In this case, the only accepted values are 1 and 2, so if we receive another value, we handle the error (in this simple case, we just die and tell the user that something's wrong). At the end, we add the ORDER BY part, which again is independant of the user's choice.

In this way, we have built up a complete SQL query, and we can execute it to return the result set without ever hard-coding the query into the code.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

As an FYI, I posted a recode of the originally posted snippet with that same logic applied to it.
rami
Forum Contributor
Posts: 217
Joined: Thu Sep 15, 2005 8:55 am

Post by rami »

thanks for such comprehensive reply
any way
first thing i have already got it correct all the code using style of everah...
as suggested new concept is rather than using whole "select query....now we are concatenating select query according to condition

mr everah as i have alrady said that i have no compalins about that logical error (it was posted incorrectly by me as well,one if($dist==0) in both (if and else condition) which cannot happen either it can be 0 or 1)


my headache is nested ifs...can it be improved

when we use switch it seems very simple with out if ...but what if there were four condition or colums to check(each with "all" or some drop down option as shown in image)
i think that time the switch also will be nested which would bring us to begining of our discussion.


for eg as suggested
SELECT * FROM table_name WHERE id <= 10" and "SELECT * FROM table_name WHERE id > 10
if each case there would have been
property type filed with drop down option "all" ,hire or sale
so i think it would be nested or switch will need if condition in it
is't it...?

any way thanks for reply...
i think the switch case condition seems simple for one condition
by the way it is getting data from drop down not radio..just a info...
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

rami wrote:by the way it is getting data from drop down not radio..just a info...
A little off-topic, but you're actually wrong. Sort of. The data is coming from a user's browser .. you have no way to tell what they used to input that data. In 99.9% of cases it'll be your radio buttons, but if someone is trying to hack into your application that information could be anything. Don't assume that just because you only gave them a specific set of options that the incoming data will definitely be one of them. Always sanitise and validate any incoming form fields.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Something I like to do when I know there is going to be a mess of possible ifs, etc, is to write out in English what the script will be doing, then code to that. Sometimes getting the logic down before the code makes the code flow easier.
Post Reply