Solved

array not returning expected results

Posted on 2011-09-16
7
218 Views
Last Modified: 2012-05-12
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
Comment
Question by:peter_coop
7 Comments
 
LVL 12

Expert Comment

by:jet-black
Comment Utility
What is your full code?
What do you expect and what you get?
0
 
LVL 1

Accepted Solution

by:
jastacdoss earned 125 total points
Comment Utility
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
 
LVL 82

Assisted Solution

by:hielo
hielo earned 125 total points
Comment Utility
>>$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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 

Author Comment

by:peter_coop
Comment Utility
sorry for the delay guys. Internet was down. shall look at your answers now and report back. thanks
0
 

Author Comment

by:peter_coop
Comment Utility
@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
 

Author Closing Comment

by:peter_coop
Comment Utility
thank you
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now