Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

array not returning expected results

Am I correct in thinking that once an array has been processed, then watever variable is assigned to the array can be used elsewhere in the code? The reason I ask is because I am trying to use the variable further on in the script but it doesn't appear to be working. It is just returning empty values. I have posted an example of the code and would be grateful if someone could show ne ny error. Many thanks

if (isset($_POST['item']))
{
  $data = split(',',$_POST['item'][0]);
  $duplicates = array();

foreach ($data as $val)
{
if ( $val != "" )
 {
  $sql = "SELECT custref FROM boxes WHERE custref='$val' and status = 'In' Union SELECT item FROM act WHERE item='$val'";
  $qry = mysql_query($sql) or die(mysql_error());

  if(mysql_num_rows($qry))
  {
  $duplicates[] = $val;
  }
 }
}
}

Open in new window


code in a different set of if statements in the same page

elseif($duplicates) {

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'No duplicate boxes. That box is already in the database or awaiting processing.';
    $response_array['duplicates'] = 'duplicates' . $duplicates;

//if no errors
}

Open in new window

0
peter_coop
Asked:
peter_coop
2 Solutions
 
jet-blackCommented:
What is your full code?
What do you expect and what you get?
0
 
jastacdossCommented:
I assume the blank you are receiving is when printing $response_array['duplicates']?? I think you are treating $duplicates as a string instead of an array using the concatenation (.).

try
$response_array['duplicates'] = 'duplicates' . implode(", ", $duplicates);

Open in new window

0
 
hieloCommented:
>>$duplicates = array();
declare it OUTSIDE the if, then check for if( !empty($duplicates) )
$duplicates = array();

if (isset($_POST['item']))
{
  $data = split(',',$_POST['item'][0]);

  foreach ($data as $val)
  {
    if ( $val != "" )
    {
      $sql = "SELECT custref FROM boxes WHERE custref='$val' and status = 'In' Union SELECT item FROM act WHERE item='$val'";
      $qry = mysql_query($sql) or die(mysql_error());

     if(mysql_num_rows($qry))
     {
       $duplicates[] = $val;
     }
    }
  }
}


..
elseif( !empty($duplicates) ) {

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'No duplicate boxes. That box is already in the database or awaiting processing.';
    $response_array['duplicates'] = 'duplicates' . $duplicates;

//if no errors
}

ALSO, IF the first block of statements  you posted is WITHIN some function, then the second set of statements also need to be within that same function.

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Ray PaseurCommented:
If you want to be certain that your script did not accidentally rely on an unset variable or array index, you can add this to the top of your scripts:
error_reporting(E_ALL);

When you do not know what your program is doing to your data, you can find out very easily with this function:
http://php.net/manual/en/function.var-dump.php

If you just do those two things - report the errors and visualize the data, you will have completed nearly everything that is taught in Computer Science 101!
0
 
peter_coopAuthor Commented:
sorry for the delay guys. Internet was down. shall look at your answers now and report back. thanks
0
 
peter_coopAuthor Commented:
@hielo
only checking the first item. if a user selects 2 inputs then the second is ignored. thanks

@jastacdoss
likewise the same result as hielo. thanks
I have included the out of a print_r so you can see what is being echoed. thanks

[item] => Array
        (
            [0] => item1
            [1] => item2
        )
0
 
peter_coopAuthor Commented:
thank you
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now