Jump to content

If statements problem


DarkPrince2005

Recommended Posts

I have two pages, one with a form that has a collection of checkboxes and textboxes to define the extent of a search, and the second page that depending on which checkboxes are checked executes a query, but i'm having  trouble on how to modify the if statements to ignore other if statements. eg. I want the script to ignore the if statement for checkbox a when both checkbox a and b are checked.

 

<html>
<head>
<SCRIPT LANGUAGE="JavaScript"><!--
function codename() {

if(document.form.a1.checked)
{
document.form.a.disabled=false;
document.form.a.focus();
}

else
{
document.form.a.disabled=true;
}
if(document.form.b1.checked)
{
document.form.b.disabled=false;
document.form.b.focus();
}

else
{
document.form.b.disabled=true;
}}


//-->
</SCRIPT>
</head>
<body><form name="form" method="post" action="1.php"> 
a<input type="checkbox" name="a1" value="a" onclick="codename()"><br/>
<input type="text" disabled name="a" size="25">
b<input type="checkbox" name="b1" value="b" onclick="codename()"><br/>
<input type="text" disabled name="b" size="25">
<input type="submit" value="submit" /> 
</form> 

 

 

<?php 

if (isset($_POST['a'])) {
mysql_connect("localhost","root","");

mysql_select_db("a");

$sql=mysql_query ("select * from a where id like '$_POST[a]'");
while ($row = mysql_fetch_array($sql))
{
echo "$row[id]<br>";
};}; 

if (isset($_POST['b'])) {
mysql_connect("localhost","root","");

mysql_select_db("a");

$sql=mysql_query ("select * from a where id like '$_POST[b]'");
while ($row = mysql_fetch_array($sql))
{
echo "$row[id]<br>";
};}; 

if (isset($_POST['a1']) && isset($_POST['b1'])) {
mysql_connect("localhost","root","");

mysql_select_db("a");

$sql=mysql_query ("select * from a where id like '$_POST[a]' or id like '$_POST[b]'");
while ($row = mysql_fetch_array($sql))
{
echo "$row[id]<br>";
};}; 
?>

Link to comment
https://forums.phpfreaks.com/topic/95848-if-statements-problem/
Share on other sites

You might want to consider using formatting within your code (i.e. indenting) and comments to help make it easier to understand and follow the logic.

 

<?php 

//At least one checkbox selected
if (isset($_POST['a']) || isset($_POST['b'])) {

    //Connect to database
    mysql_connect("localhost","root","");
    mysql_select_db("a");

    if (isset($_POST['a1']) && isset($_POST['b1']))
    {
        //Both checkboxes selected
        $query = "select * from a where id like '$_POST[a]' or id like '$_POST[b]'";
    }    
    else if (isset($_POST['a'])
    {
       //Only A checkbox selected
        $query = "select * from a where id like '$_POST[a]'";
    } 
    else
    {
        //Only B checkbox selected
        $query = "select * from a where id like '$_POST[b]'";
    } 

    //Query & Display the results
    $result = mysql_query ($query) or die (mysql_error());
    while ($row = mysql_fetch_array($result))
    {
        echo "$row[id]<br>";
    }

}

?>

Link to comment
https://forums.phpfreaks.com/topic/95848-if-statements-problem/#findComment-490686
Share on other sites

would something like this work? would it save the problem?

 

<?php 

if (isset($_POST['a1']) && !isset($_POST['b'])) {
mysql_connect("localhost","root","");

mysql_select_db("a");

$sql=mysql_query ("select * from a where id like '$_POST[a]'");
while ($row = mysql_fetch_array($sql))
{
echo "$row[id]<br>";
};}; 

if (!isset($_POST['a1']) && isset($_POST['b'])) {
mysql_connect("localhost","root","");

mysql_select_db("a");

$sql=mysql_query ("select * from a where id like '$_POST[b]'");
while ($row = mysql_fetch_array($sql))
{
echo "$row[id]<br>";
};}; 

if (isset($_POST['a1']) && isset($_POST['b'])) {
mysql_connect("localhost","root","");

mysql_select_db("a");

$sql=mysql_query ("select * from a where id like '$_POST[a1]' or id like '$_POST[b]'");
while ($row = mysql_fetch_array($sql))
{
echo "$row[id]<br>";
};}; 
?>

Link to comment
https://forums.phpfreaks.com/topic/95848-if-statements-problem/#findComment-491955
Share on other sites

That code you posed may work, but it is inefficient and doesn't follow a logical flow. For instance you have several places where you have code to connect to the database. That is a waste of code. You just need to check if at least one checkbox is checked - and then connect to the db. That code also has the same problem with the code to display the results.

 

By "copying and pasting" the same code to different places the code will be difficult to debug and will be error prone. For example, if you discovered an error in the display code it might get fixed in one place and not another.

Link to comment
https://forums.phpfreaks.com/topic/95848-if-statements-problem/#findComment-492241
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.