Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

array not returning expected results

Posted on 2011-09-16
7
Medium Priority
?
229 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 500 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 500 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 111

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to count occurrences of each item in an array.

704 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