We help IT Professionals succeed at work.

user choice test

asaidi
asaidi asked
on
Medium Priority
524 Views
Last Modified: 2012-08-16
Hi
is there something wrong with these test as it takes only the last test
if(!empty($_POST[$netw]) && !empty($_POST[$client])){
         include('full.php');  
         p5($date,$date2,$report,$client=$client,$netw=$netw);
        
   
}

elseif (empty($netw) && !empty($_POST['client'])){
  
         include('full.php');  
         p5($date,$date2,$report,$client=$client,NULL);
        
   

}
elseif (empty($client) && !empty($netw)){
 
   
         include('full.php');  
         p5($date,$date2,$report,NULL,$netw=$netw);
         
   
}
elseif (empty($netw) && empty($client)){
   
  
      include('full.php');  
        p5($date,$date2,$report,null,null);
        
  }

Open in new window

if i choose any of parameters it takes when the two are empties

the calling program
function p5($v1,$v2,$v3,$v4,$v5){  
$auto=$_GET['auto'];
 
	echo $v1, PHP_EOL;
	echo $v2, PHP_EOL;
	echo $v3, PHP_EOL;
	echo $v4, PHP_EOL;
        echo $v5, PHP_EOL;
     $sql="SELECT *
        from count_transactions  
          WHERE DATE(tran_date) BETWEEN '$v1' AND '$v2' and account_no=$auto AND customer_no='$v4' AND unit_serial='$v5'  
          group by customer_no,unit_serial"; 
          $res = mysql_query($sql) or die("FAIL: $sql<br>" . mysql_error());
         $num = mysql_num_rows($result1);
while($rows = mysql_fetch_array($res)) { 
....

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
@asaidi, I think you misinterpreted the answer in http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_27829298.html

You are doing assignment in your call and not setting defaults in your declaration.

In this case using variable parameters is not the answer - what you actually need is a way to set your passing parameters correctly. As I understand from your two posts the only two parameters that are variable are $netw and $client - which you want to set only if these values are posted in.

Try this
include('full.php');
// These two lines test to see if a POST was made and if not tests to see 
// if the variable is empty (exists) and sets accordingly. The inner test could 
// probably be removed if the $netw / $client values are being initialised before this.

$netw = empty($_POST[$netw])?(empty($netw)?null:$netw):$_POST[$netw];
$client = empty($_POST[$client])?(empty($client)?null:$netw):$_POST[$client];
p5($date,$date2,$report,$client,$netw);

function p5($v1,$v2,$v3,$v4,$v5){  
$auto=$_GET['auto'];
 
	echo $v1, PHP_EOL;
	echo $v2, PHP_EOL;
	echo $v3, PHP_EOL;
	echo $v4, PHP_EOL;
        echo $v5, PHP_EOL;
     $sql="SELECT *
        from count_transactions  
          WHERE DATE(tran_date) BETWEEN '$v1' AND '$v2' and account_no=$auto AND customer_no='$v4' AND unit_serial='$v5'  
          group by customer_no,unit_serial"; 
          $res = mysql_query($sql) or die("FAIL: $sql<br>" . mysql_error());
         $num = mysql_num_rows($result1);
while($rows = mysql_fetch_array($res)) { 
...

Open in new window

Author

Commented:
Hi
the same thing
if i choose only customer it prints all customers the if test it is not working...
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
In your p5 function you need to make a conditional where (refer your other question where I posted code on this ( http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_27829298.html)

Don't include filter by customer_no if that field is blank.

$query = "SELECT * FROM count_transactions WHERE DATE(tran_date) BETWEEN '$v1' AND '$v2' and account_no=$auto";
if ($v4 != null) $query .= " AND customer_no='$v4'";
if ($v5 != null) $query .= " AND unit_serial='$v5'";
$query .= " GROUP BY customer_no,unit_serial"; "

Open in new window


Also please post results of this code
echo "<pre>";
print_r($_POST);
echo "<//pre>";
echo "BEFORE: netw: $netw, client: $client<br/>";
$netw = empty($_POST[$netw])?(empty($netw)?null:$netw):$_POST[$netw];
$client = empty($_POST[$client])?(empty($client)?null:$netw):$_POST[$client];
echo "AFTER netw: $netw, client: $client<br/>";

Open in new window

Author

Commented:
Hi
i run the program and did not start waiting for localhost...

Author

Commented:
that what i get after running

Array
(
    [client] =>
    [netw] =>
    [date] => 1-Jul-2012
    [date2] => 7-Jul-2012
    [report] => 1
    [pulse] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

    [submit] => Run
)
BEFORE: netw: , client:
AFTER netw: , client:
2012-07-01
2012-07-07
1
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
Ok, with the above can you add in your p5() function an echo to output the database query

echo "Query: $sql<br/>";

or

echo "Query: $query<br/>"; // depending on whether you are using your original code or the code I posted above.

Author

Commented:
that is what i got
Array
(
    [client] =>
    [netw] =>
    [date] => 1-Jul-2012
    [date2] => 7-Jul-2012
    [report] => 1
    [pulse] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

    [submit] => Run
)
BEFORE: netw: , client:
AFTER netw: , client:
2012-07-01
2012-07-07
1
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
No then you did not do something correctly - if you added the echo above that I recommended you should have seen an SQL statmenet dumped.

I was suggesting something like this

function p5($v1,$v2,$v3,$v4,$v5){  
$auto=$_GET['auto'];
 
	echo $v1, PHP_EOL;
	echo $v2, PHP_EOL;
	echo $v3, PHP_EOL;
	echo $v4, PHP_EOL;
        echo $v5, PHP_EOL;
     $sql="SELECT *
        from count_transactions  
          WHERE DATE(tran_date) BETWEEN '$v1' AND '$v2' and account_no=$auto AND customer_no='$v4' AND unit_serial='$v5'  
          group by customer_no,unit_serial"; 
echo "SQL: $sql<br/>"; // ADD THIS LINE AND RUN AGAIN
          $res = mysql_query($sql) or die("FAIL: $sql<br>" . mysql_error());
         $num = mysql_num_rows($result1);
while($rows = mysql_fetch_array($res)) { 

Open in new window

Author

Commented:
Hi
that is what i got
Array
(
    [client] =>
    [netw] =>
    [date] => 1-Jul-2012
    [date2] => 7-Jul-2012
    [report] => 1
    [pulse] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

    [submit] => Run
)
BEFORE: netw: , client:
AFTER netw: , client:
SQL: SELECT *
        from count_transactions  
          WHERE DATE(tran_date) BETWEEN '2012-07-01' AND '2012-07-07' and account_no=1 AND customer_no='' AND unit_serial=''  
          group by customer_no,unit_serial
Query: SELECT *
        from count_transactions  
          WHERE DATE(tran_date) BETWEEN '2012-07-01' AND '2012-07-07' and account_no=1 AND customer_no='' AND unit_serial=''  
          group by customer_no,unit_serial
 when i do that
$sql="SELECT *
        from count_transactions  
          WHERE DATE(tran_date) BETWEEN '$v1' AND '$v2' and account_no=$auto AND customer_no='$v4' AND unit_serial='$v5'  
          group by customer_no,unit_serial"; 
echo "SQL: $sql<br/>"; // ADD THIS LINE AND RUN AGAIN

Open in new window

Author

Commented:
Hi
it is working
when i choose client empty and unit not empty but when the client is not empty and unit empty it is not working
Query: SELECT * FROM count_transactions WHERE DATE(tran_date) BETWEEN '2012-07-01' AND '2012-07-07' and account_no=1 GROUP BY customer_no,unit_serial

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
Ok can you do your test with client empty and unit not empty and post all the output from above.

We need to see
a) What is posted to the app
b) What is sent to p5
c) What the resulting query is.
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
In your code, you are only ever going to run the last 'if', because $netw and $client are always empty - you're not setting the variables anywhere!

Also, when getting a POST value, the key should be wrapped in quotes - you are using a variable. It should be this:

$_POST['netw']
$_POST['client']

and not this:

$_POST[$netw]
$_POST[$client]

Also confusing is that throughout your code, sometimes you're checking a variable called $netw and sometimes you are checking a variable called $_POST[$netw]. Neither of these have been set anywhere so they will both always be empty. The only exception to this is if you have SUPERGLOBALS turned on in PHP, which is really big NO NO. If you have, turn it off immediately!

Also, when writing functions, it makes like a lot easier if your function and arguments have meaningful names - p5, v1, v2, v3 etc mean nothing. Something like this would be better:

function getRecords($startDate, $endDate, $report, $customer, $serial, $auto) { ... }

Another weird looking thing (sorry) is that you are getting some of the variables from POST and some from GET! You are retrieving the $_GET['auto'] from within the function - why not just pass it to the function as another argument.

Just a few pointers :)

Author

Commented:
empty client

Array
(
    [client] => 
    [netw] => 9
    [date] => 1-Jul-2012
    [date2] => 7-Jul-2012
    [report] => 1
    [pulse] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

    [submit] => Run
)
BEFORE: netw: , client: 
AFTER netw: , client: 
Query: SELECT * FROM count_transactions WHERE DATE(tran_date) BETWEEN '2012-07-01' AND '2012-07-07' and account_no=1 AND unit_serial=9 GROUP BY customer_no,unit_serial order by DATE(tran_date)

Open in new window

empty unit or netw
Array
(
    [client] => 1
    [netw] => 
    [date] => 1-Jul-2012
    [date2] => 7-Jul-2012
    [report] => 1
    [pulse] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

    [submit] => Run
)
BEFORE: netw: , client: 
AFTER netw: , client: 
Query: SELECT * FROM count_transactions WHERE DATE(tran_date) BETWEEN '2012-07-01' AND '2012-07-07' and account_no=1 GROUP BY customer_no,unit_serial order by DATE(tran_date)

Open in new window

when client is empty and netw empty
Array
(
    [client] => 
    [netw] => 
    [date] => 1-Jul-2012
    [date2] => 7-Jul-2012
    [report] => 1
    [pulse] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

    [submit] => Run
)
BEFORE: netw: , client: 
AFTER netw: , client: 
Query: SELECT * FROM count_transactions WHERE DATE(tran_date) BETWEEN '2012-07-01' AND '2012-07-07' and account_no=1 GROUP BY customer_no,unit_serial order by DATE(tran_date)

Open in new window


all not empty
Array
(
    [client] => 1
    [netw] => 33
    [date] => 1-Jul-2012
    [date2] => 7-Jul-2012
    [report] => 1
    [pulse] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

    [submit] => Run
)
BEFORE: netw: , client: 
AFTER netw: , client: 
Query: SELECT * FROM count_transactions WHERE DATE(tran_date) BETWEEN '2012-07-01' AND '2012-07-07' and account_no=1 AND customer_no=33 AND unit_serial=33 GROUP BY customer_no,unit_serial order by DATE(tran_date)

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
You are not pulling the values from the POST array correctly.

As you did a partial post of your code I assumed you were setting the $netw and $client prior to passing them to p5() in other words preserving previous entries that may have been made what you need to do is this

include('full.php');

$netw = empty($_POST['netw'])?null:$_POST['netw'];
$client = empty($_POST['client'])?null:$_POST['client'];

p5($date,$date2,$report,$client,$netw);

function p5($v1,$v2,$v3,$v4,$v5) {  
  $sql = "SELECT * FROM count_transactions WHERE DATE(tran_date) BETWEEN '$v1' AND '$v2' and account_no=$auto";

  if ($v4 != null) $query .= " AND customer_no='$v4'";
  if ($v5 != null) $query .= " AND unit_serial='$v5'";

  $query .= " GROUP BY customer_no,unit_serial"; "
                                            
  $res = mysql_query($sql) or die("FAIL: $sql<br>" . mysql_error());
  $num = mysql_num_rows($result1);
  while($rows = mysql_fetch_array($res))  { 
...

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.