Solved

array not returning expected results

Posted on 2011-09-16
7
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 12

Expert Comment

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

Accepted Solution

by:
jastacdoss earned 125 total points
ID: 36549463
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
ID: 36549487
>>$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
Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36552099
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
ID: 36554041
sorry for the delay guys. Internet was down. shall look at your answers now and report back. thanks
0
 

Author Comment

by:peter_coop
ID: 36554063
@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
ID: 36555944
thank you
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

628 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