Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-14-2006, 06:41 AM   #1 (permalink)
transfield
Code Monkey
 
Join Date: Mar 2006
Posts: 35
transfield is on a distinguished road
Switch / Array Help

Hello,
I'm using a form to query a Mysql database. This form contains 1 group of radio buttons called R1. The code for the form(form1) & the form processor is below. Everything is working fine.

Now, I want to add another 2 groups of radio buttons to my form called radiobutton1 and radiobutton2. The code for the new form(form2) is below. My problem is I don't know how to write the code for the form processor. I believe that I must change the switch in the form processor to an array in order to simplify the code. I don't know how to write the array.

Additional info - the databases cannot be consolidated into 1 database with many tables because they(the databases) are very big. My webhost does not allow any individual database to exceed 50 MB.

Thanks a lot for your help.

Code for Form1:-
PHP Code:
<form name="form1" method="POST" action=""
<
tr
   <
td><div align="center"
     <
input type="radio" value="kl06" name="R1"
     
Kuala Lumpur 2006<strong> or 
     <
input name="R1" type="radio" value="sl06"
     </
strong>Selangor 2006<strong> or 
     <
input name="R1" type="radio" value="kl07"
     </
strong>Kuala Lumpur 2007<strong> or 
     <
input name="R1" type="radio" value="sl07" checked
     </
strong>Selangor 2007 </div></td
</
tr
<
tr
   <
td><div align="center">Key in Your Search Criteria Below </div></td
</
tr
<
tr
   <
td><div align="center"
     <
input type="text" name="Text_Box_1" size="20" tabindex="6"
     <
strong>or</strong
     <
input type="text" name="Text_Box_2" size="20" tabindex="7"
     <
strong>or</strong
     <
input type="text" name="Text_Box_3" size="20" tabindex="8"
     <
strong>or</strong
     <
input type="text" name="Text_Box_4" size="20" tabindex="9"
   </
div></td
</
tr
<
tr
   <
td><div align="center"
     <
input type="text" name="Text_Box_5" size="20" tabindex="10"
     <
strong>or</strong
     <
input type="text" name="Text_Box_6" size="20" tabindex="11"
     <
strong>or</strong
     <
input type="text" name="Text_Box_7" size="20" tabindex="12"
     <
strong>or</strong
     <
input type="text" name="Text_Box_8" size="20" tabindex="13"
   </
div></td
</
tr
<
tr
   <
td><div align="center"
     <
input name="submit" type="submit" id="submit2" value="Submit" tabindex="14"
     <
input name="reset2" type="reset" id="reset2" value="Reset" tabindex="15"
   </
div></td
</
tr
</
form
Code for Form Processor:-
PHP Code:
switch($_POST['R1']) 

case 
'sl06'
$username="something1"
$password="samestuff"
$database="database1"
$host="localhost"
mysql_connect ("$host","$username","$password"); 
mysql_select_db($database) or die( "Where's the database man?"); 
$query="SELECT * FROM selangor WHERE (address like '%$Text_Box_1%') OR (address like '%$Text_Box_2%') OR (address like '%$Text_Box_3%') OR (address like '%$Text_Box_4%') OR (address like '%$Text_Box_5%') OR (address like '%$Text_Box_6%') OR (address like '%$Text_Box_7%') OR (address like '%$Text_Box_8%') OR (name like '%$Text_Box_1%') OR (name like '%$Text_Box_2%') OR (name like '%$Text_Box_3%') OR (name like '%$Text_Box_4%') OR (name like '%$Text_Box_5%') OR (name like '%$Text_Box_6%') OR (name like '%$Text_Box_7%') OR (name like '%$Text_Box_8%') OR (phone like '%$Text_Box_1%') OR (phone like '%$Text_Box_2%') OR (phone like '%$Text_Box_3%') OR (phone like '%$Text_Box_4%') OR (phone like '%$Text_Box_5%') OR (phone like '%$Text_Box_6%') OR (phone like '%$Text_Box_7%') OR (phone like '%$Text_Box_8%') LIMIT 0, 15"
break; 
case 
'kl06'
$username="something2"
$password="samestuff"
$database="database2"
$host="localhost"
mysql_connect ("$host","$username","$password"); 
mysql_select_db($database) or die( "Where's the database man?"); 
$query="SELECT * FROM wilayah WHERE (address like '%$Text_Box_1%') OR (address like '%$Text_Box_2%') OR (address like '%$Text_Box_3%') OR (address like '%$Text_Box_4%') OR (address like '%$Text_Box_5%') OR (address like '%$Text_Box_6%') OR (address like '%$Text_Box_7%') OR (address like '%$Text_Box_8%') OR (name like '%$Text_Box_1%') OR (name like '%$Text_Box_2%') OR (name like '%$Text_Box_3%') OR (name like '%$Text_Box_4%') OR (name like '%$Text_Box_5%') OR (name like '%$Text_Box_6%') OR (name like '%$Text_Box_7%') OR (name like '%$Text_Box_8%') OR (phone like '%$Text_Box_1%') OR (phone like '%$Text_Box_2%') OR (phone like '%$Text_Box_3%') OR (phone like '%$Text_Box_4%') OR (phone like '%$Text_Box_5%') OR (phone like '%$Text_Box_6%') OR (phone like '%$Text_Box_7%') OR (phone like '%$Text_Box_8%') LIMIT 0, 15"
break; 
case 
'sl07'
$username="something3"
$password="samestuff"
$database="database3"
$host="localhost"
mysql_connect ("$host","$username","$password"); 
mysql_select_db($database) or die( "Where's the database man?"); 
$query="SELECT * FROM selangor WHERE (address like '%$Text_Box_1%') OR (address like '%$Text_Box_2%') OR (address like '%$Text_Box_3%') OR (address like '%$Text_Box_4%') OR (address like '%$Text_Box_5%') OR (address like '%$Text_Box_6%') OR (address like '%$Text_Box_7%') OR (address like '%$Text_Box_8%') OR (name like '%$Text_Box_1%') OR (name like '%$Text_Box_2%') OR (name like '%$Text_Box_3%') OR (name like '%$Text_Box_4%') OR (name like '%$Text_Box_5%') OR (name like '%$Text_Box_6%') OR (name like '%$Text_Box_7%') OR (name like '%$Text_Box_8%') OR (phone like '%$Text_Box_1%') OR (phone like '%$Text_Box_2%') OR (phone like '%$Text_Box_3%') OR (phone like '%$Text_Box_4%') OR (phone like '%$Text_Box_5%') OR (phone like '%$Text_Box_6%') OR (phone like '%$Text_Box_7%') OR (phone like '%$Text_Box_8%') LIMIT 0, 15"
break; 
case 
'kl07'
$username="something4"
$password="samestuff"
$database="database4"
$host="localhost"
mysql_connect ("$host","$username","$password"); 
mysql_select_db($database) or die( "Where's the database man?"); 
$query="SELECT * FROM wilayah WHERE (address like '%$Text_Box_1%') OR (address like '%$Text_Box_2%') OR (address like '%$Text_Box_3%') OR (address like '%$Text_Box_4%') OR (address like '%$Text_Box_5%') OR (address like '%$Text_Box_6%') OR (address like '%$Text_Box_7%') OR (address like '%$Text_Box_8%') OR (name like '%$Text_Box_1%') OR (name like '%$Text_Box_2%') OR (name like '%$Text_Box_3%') OR (name like '%$Text_Box_4%') OR (name like '%$Text_Box_5%') OR (name like '%$Text_Box_6%') OR (name like '%$Text_Box_7%') OR (name like '%$Text_Box_8%') OR (phone like '%$Text_Box_1%') OR (phone like '%$Text_Box_2%') OR (phone like '%$Text_Box_3%') OR (phone like '%$Text_Box_4%') OR (phone like '%$Text_Box_5%') OR (phone like '%$Text_Box_6%') OR (phone like '%$Text_Box_7%') OR (phone like '%$Text_Box_8%') LIMIT 0, 15"

Code for Form2:-
PHP Code:
<form name="form2" method="post" action=""
<
tr
   <
td><div align="center"
     <
input type="radio" value="kl06" name="R1"
Kuala Lumpur 2006<strong> or 
<
input name="R1" type="radio" value="sl06"
</
strong>Selangor 2006<strong> or 
<
input name="R1" type="radio" value="kl07"
</
strong>Kuala Lumpur 2007<strong> or 
<
input name="R1" type="radio" value="sl07" checked
</
strong>Selangor 2007</div></td
</
tr
<
tr
   <
td><div align="center">Key in Your Search Criteria Below</div></td
</
tr
<
tr
   <
td><div align="center">Search by 
       
<input name="Text_Box_1" type="text" id="Text_Box_1" tabindex="6" value="Name" size="15"
       <
label
       <
input name="radiobutton1" type="radio" value="and"
       <
strong>and</strong></label
       <
input name="radiobutton1" type="radio" value="or" checked
       <
strong>or </strong
       <
input name="Text_Box_2" type="text" id="Text_Box_2" tabindex="7" value="Phone" size="15"
       <
label
       <
input name="radiobutton2" type="radio" value="and"
       <
strong>and</strong></label
       <
input name="radiobutton2" type="radio" value="or" checked
       <
strong>or </strong
       <
input name="Text_Box_3" type="text" id="Text_Box_3" tabindex="8" value="Address" size="15"
</
div></td
</
tr
<
tr
   <
td><div align="center"
     <
input name="submit" type="submit" id="submit" value="Submit" tabindex="14"
     <
input name="reset22" type="reset" id="reset22" value="Reset" tabindex="15"
</
div></td
</
tr
</
form
transfield is offline   Reply With Quote
Old 09-14-2006, 07:00 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
your question is very vague, but I'll try to give some advice.

1. get a new web host. you should never have to deal with 2 databases if you don't want to. i think most web hosts just include your database size into your web space limit.

2. you don't need 2 forms just becuase you are using 2 databases. you can put the additional controls in the same form and just handle the processing logic in your php code.

generally when you are working with 2 databases, you would just do everything you need to with the first database, close it, connect to the second database, and then process the additional fields there. you really should not be working with 2 databases because of web host restrictions though.
__________________
Mike
sde is offline   Reply With Quote
Old 09-14-2006, 10:38 AM   #3 (permalink)
transfield
Code Monkey
 
Join Date: Mar 2006
Posts: 35
transfield is on a distinguished road
Thanks for your reply sde.
Quote:
1. get a new web host.
I'll look into this later. While webhosting is cheap, the inconvenience of transferring the site over to the new server & the time taken to set things up all over again is not worth the trouble. Furthermore, there are always uncertainties about whether the existing codes will work in exactly the same way on the new server.

Quote:
2. You don't need 2 forms just because you are using 2 databases.
I am not using 2 forms. I am upgrading my existing form(form1 to form 2) with more options to query the existing databases in different ways.

Quote:
generally when you are working with 2 databases, you would just do everything you need to with the first database, close it, connect to the second database, and then process the additional fields there. you really should not be working with 2 databases because of web host restrictions though.
I am not trying to work with more than 1 database at the same time. I'm trying to query a single database in different ways.

Thank you for seeking further clarification.
transfield is offline   Reply With Quote
Old 09-18-2006, 11:11 AM   #4 (permalink)
losthitchhiker
Registered User
 
Join Date: May 2006
Posts: 2
losthitchhiker is on a distinguished road
Are you asking how to convert your existing code for form1 to be functional with your new form2?

Below might help. Doing all the switching might prove cumbersome if the sql or db's ever get changed.

I did not test any syntax though but you can get an idea. I use an array to store the different configurations for each main option available in the form. Build the SQL from the config and the foreach loop

FYI, the foreach code, is based on the radio buttons be named (rdo_name, rdo_address, rdo_phone). The same for the corresponding text fields (txt_name ... etc.)

Code:
$cfg = array (

's106' => array(
'db' => 'database1',
'pw' => 'password1',
'usr' => 'username1',
'tbl' => 'selangor'
),

'k106' => array(
'db' => 'database2',
'pw' => 'password2',
'usr' => 'username',
'tbl' => 'wilayah'
)

);

$ar = $cfg[$_POST['R1']];

if (count($ar) > 0)
{

$host="localhost"; 
mysql_connect ($host,$ar['usr'],$ar['pw']); 
mysql_select_db($ar['db']) or die( "Where's the database man?"); 

$arfld = array (
'name',
'address',
'phone'
);

foreach ($arfld as $fld)
{
$rdo = $_POST['rdo_' . $fld];
$txt = $_POST['txt_' . $fld];

switch ($rdo)
{
case 'or':
case 'and':
if (!empty($txt)) $rdo_sql .= " $rdo $fld LIKE '%$txt%' "
}

}

$query = "SELECT * FROM " . $ar['tbl'] . " WHERE " . $rdo_sql . "LIMIT 0, 15; ";

/* perform query */

}else{

/* option is not part of configuration */
echo 'error'; exit();

}
losthitchhiker is offline   Reply With Quote
Old 09-19-2006, 10:11 PM   #5 (permalink)
transfield
Code Monkey
 
Join Date: Mar 2006
Posts: 35
transfield is on a distinguished road
Thank you for your generous help, losthitchhiker. I've followed your instructions to the best of my understanding. I'm getting an error message which I don't know how to debug. I would appreciate your help on this matter.
Quote:
Are you asking how to convert your existing code for form1 to be functional with your new form2?
Yes.
Quote:
radio buttons be named (rdo_name, rdo_address, rdo_phone)
I'm not sure I understand this correctly because I only have 2 new groups of radio buttons. The 1st group was called radiobutton1 which I have now re-named to rdo_name. The 2nd group was called radiobutton2 which I have now re-named to rdo_address. So what about rdo_phone?

The error message:-
Code:
Parse error: syntax error, unexpected '}' in /home/.../public_html/metrohomes1/Copy of metro_query.php on line 161
The code for my new form:-
PHP Code:
<form name="form2" method="post" action="">  
<
tr>  
   <
td><div align="center">  
     <
input type="radio" value="kl06" name="R1">  
Kuala Lumpur 2006<strong> or  
<
input name="R1" type="radio" value="sl06">  
</
strong>Selangor 2006<strong> or  
<
input name="R1" type="radio" value="kl07">  
</
strong>Kuala Lumpur 2007<strong> or  
<
input name="R1" type="radio" value="sl07" checked>  
</
strong>Selangor 2007</div></td>  
</
tr>  
<
tr>  
   <
td><div align="center">Key in Your Search Criteria Below</div></td>  
</
tr>  
<
tr>  
   <
td><div align="center">Search by  
       
<input name="txt_Box_1" type="text" id="txt_Box_1" tabindex="6" value="Name" size="15">  
       <
label>  
       <
input name="rdo_name" type="radio" value="and">  
       <
strong>and</strong></label>  
       <
input name="rdo_name" type="radio" value="or" checked>  
       <
strong>or </strong>  
       <
input name="txt_Box_2" type="text" id="txt_Box_2" tabindex="7" value="Phone" size="15">  
       <
label>  
       <
input name="rdo_address" type="radio" value="and">  
       <
strong>and</strong></label>  
       <
input name="rdo_address" type="radio" value="or" checked>  
       <
strong>or </strong>  
       <
input name="txt_Box_3" type="text" id="txt_Box_3" tabindex="8" value="Address" size="15">  
</
div></td>  
</
tr>  
<
tr>  
   <
td><div align="center">  
     <
input name="submit" type="submit" id="submit" value="Submit" tabindex="14">  
     <
input name="reset22" type="reset" id="reset22" value="Reset" tabindex="15">  
</
div></td>  
</
tr>  
</
form
The code for my form processor:-
PHP Code:
$cfg = array (

's106' => array(
'db' => 'o2',
'pw' => 'aaa',
'usr' => 't2',
'tbl' => 'selangor'
),

'k106' => array(
'db' => 'o3',
'pw' => 'aaa',
'usr' => 't3',
'tbl' => 'wilayah'
),

's107' => array(
'db' => 'o4',
'pw' => 'aaa',
'usr' => 't4',
'tbl' => 'selangor'
),

'k107' => array(
'db' => 'o5',
'pw' => 'aaa',
'usr' => 't5',
'tbl' => 'wilayah'
)

);

$ar $cfg[$_POST['R1']];

if (
count($ar) > 0)
{

$host="localhost"
mysql_connect ($host,$ar['usr'],$ar['pw']); 
mysql_select_db($ar['db']) or die( "Where's the database man?"); 

$arfld = array (
'name',
'address',
'phone'
);

foreach (
$arfld as $fld)
{
$rdo $_POST['rdo_' $fld];
$txt $_POST['txt_' $fld];

switch (
$rdo)
{
case 
'or':
case 
'and':
if (!empty(
$txt)) $rdo_sql .= " $rdo $fld LIKE '%$txt%' "
}

}

$query "SELECT * FROM " $ar['tbl'] . " WHERE " $rdo_sql "LIMIT 0, 15; ";

/* perform query */

}else{

/* option is not part of configuration */
echo 'error'; exit();


transfield is offline   Reply With Quote
Old 09-20-2006, 05:58 AM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
php error reporting is pretty good. do you have line numbers in your text editor? if not, you should get an editor that does.

Quote:
Copy of metro_query.php on line 161
go to line 161 and perhaps some lines above it to see what type of syntax error you have made.

posting all that code isn't gonna help when you have a parse error. if you can't find the error, just post the lines near 161 and specify which line is 161 for us.
__________________
Mike
sde is offline   Reply With Quote
Old 09-20-2006, 07:53 AM   #7 (permalink)
losthitchhiker
Registered User
 
Join Date: May 2006
Posts: 2
losthitchhiker is on a distinguished road
I forgot the ; at the end of the following line. I believe this is the error as the } wasn't being recognized. As mentioned, I didn't test syntax.

Code:
if (!empty($txt)) $rdo_sql .= " $rdo $fld LIKE '%$txt%' ";
My bad though, I thought you had a phone option. Just remove it from the array.

Code:
$arfld = array (
'name',
'address'
);
Also, don't forget to rename the text fields to txt_name and txt_address as the code checks this.

You should also take the time to understand the coding used so you can debug in the future or build upon. I was only giving some code to help. There are many ways to code the same thing. Codenewbie has a good array tutorial by the way (I used associative arrays if you want to google further)...

Code Newbie Array
losthitchhiker is offline   Reply With Quote