Solved

array not returning expected results

Posted on 2011-09-16
7
222 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 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