?
Solved

query and then query again

Posted on 2009-02-13
2
Medium Priority
?
164 Views
Last Modified: 2013-12-12
I need to take the results from a query and  explode it and then use each item in the array in a new query.

 my code is wrong but maybe this will help explain


$myarray = explode(',', $zipcodes);
       foreach($myarray as $value) {
     //   echo "$value<br>\n";
       }
 
 
$sql4 ="SELECT d.zip  FROM db_publication_report d 
  WHERE d.`zip` LIKE '%$value%' AND d.`id_publisher` = '$Publisher_id'";
$result4 = mysql_query($sql4) or die(mysql_error());
echo"ZIPCODE:$sql4 <br><br>";
 
if(mysql_num_rows($result) > 0) 
{
  while($row = mysql_fetch_assoc($result4)) 
  {
    $zip = $row['zip'];
 
    echo "&nbsp;&nbsp;<b>Zip Code:</b>$zip<br>&nbsp;&nbsp;";
  }
} else {
  // Print an error if no user is found.
  echo "No zip was found in that zipcode";
}

Open in new window

0
Comment
Question by:MasCon
2 Comments
 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 200 total points
ID: 23636752
try
$myarray = explode(',', $zipcodes);
       foreach($myarray as $value) {
     //   echo "$value<br>\n";
 
 
$sql4 ="SELECT d.zip  FROM db_publication_report d 
  WHERE d.`zip` LIKE '%$value%' AND d.`id_publisher` = '$Publisher_id'";
$result4 = mysql_query($sql4) or die(mysql_error());
echo"ZIPCODE:$sql4 <br><br>";
 
if(mysql_num_rows($result) > 0) 
{
  while($row = mysql_fetch_assoc($result4)) 
  {
    $zip = $row['zip'];
 
    echo "  <b>Zip Code:</b>$zip<br>  ";
  }
} else {
  // Print an error if no user is found.
  echo "No zip was found in that zipcode";
}
 
 
 
       }
 
 

Open in new window

0
 

Accepted Solution

by:
MasCon earned 0 total points
ID: 23652291
that didnt work to well becasue both the zip fields in the tables have comma separated values.

here is my rewrite and what I am trying to do is:

1st query gets the publisher id and zipcodes they have license to that matched a form submission.
(there may be more than one publisher)

2nd query gets there name and address

3rd query takes the publisher ID and looks up the zipcodes they published into


from there I want to take the 2 arrays (one from the 1st query called $Owned_Zip and one from the 3rd Query called $Pub_Zip) and compare them to the zipcode entered. if the zip code is on bot lists it prints out OK if they are not in both arrays then it prints out their is a license violation.
$zipcode = ($_POST['zip']);
 
echo "<br>Here are the results for zip code: <div class=seek>$zipcode</div><br>
  <br><font color=#0072bc size= 3><b><u>ZIP CODE RESULTS</u></b></font><br><br>";
$Publisher_id= '';
$Owned_Zip= '';
$Pub_Zip = '';
 
//Get the Pulishers ID , Zip Codes they own
//echo" All Publisher ID's with that Zip Code:<br>";
$dbname = 'adnet';
mysql_select_db($dbname);
$sql = "SELECT p.`pdata_id_publisher`, p.`pdata_zipcodes` 
  FROM publisher_data p where `pdata_zipcodes` LIKE '%$zipcode%';";
//	echo"$sql";
$result = mysql_query($sql) or die("Couldn't execute query");
 
while($row = mysql_fetch_assoc($result)) 
{
	$Publisher_id =  $row['pdata_id_publisher'];
  	$Owned_Zip =  $row['pdata_zipcodes'];
  
//  echo"$Publisher_id <br>";
 
//Get Publishers Name, Phone and Job Number
  $dbname = 'adnet';
  mysql_select_db($dbname);
$sql2 = "SELECT d.`jobno`, d.`contact`, d.`phone` FROM db_publishers d  
    WHERE d.`id_publisher` = '$Publisher_id'";
//	echo"$sql2";
$result2  = mysql_query($sql2) or die(mysql_error());
 
while($row = mysql_fetch_assoc($result2)) 
{
	$contact=$row['contact'];
    $phone=$row['phone'];
    $jobno=$row['jobno'];
 
 echo "<br><font color=#0072bc size= 3><b><u>License Info:</u></b></font><br><BR>&nbsp;&nbsp;<b>Publisher:&nbsp;</b>$contact<br>&nbsp;&nbsp;<b>Zip Codes Owned:&nbsp;</b>$Owned_Zip<br>&nbsp;&nbsp;<b>Phone:&nbsp;</b>$phone<br>&nbsp;&nbsp;<b>Job No.:&nbsp;</b>$jobno<br><br>";
// print_r (explode(" ",$Owned_Zip));
 
 
  $dbname = 'adnet2010';
  mysql_select_db($dbname);
$sql3 = "SELECT d.community,d.code,d.zip, d.inactive_edition  FROM db_publication_report d 
  WHERE d.`id_publisher` = '$Publisher_id'";  
//echo"$sql3<br>";
  
$result3  = mysql_query($sql3) or die(mysql_error());
while($row = mysql_fetch_assoc($result3)) 
{
    $community=$row['community'];
	$Pub_Zip=$row['zip'];
    $active=$row['inactive_edition'];
	$code=$row['code'];
		
//echo "<br><font color=#0072bc size= 3><b><u>Publishing Info:</u></b></font><br><br>&nbsp;&nbsp;<br>&nbsp;&nbsp;<b>Published Zip Codes:</b>$Pub_Zip&nbsp;";
 
//	echo"OWNED:$Owned_Zip<BR><BR>";
//echo"Published Zips:$Pub_Zip";
 
 
 
		}		
	}
}
//echo"Searched for:<b>$zipcode</b>";
//echo"<br>in the Array of:<b>$Owned_Zip</b>";
 
$Owned = array('$Owned_Zip');
 
if (in_array("$zipcode", $Owned, true)) {
    echo "The Zip Code is Owned\n";
}
 
else {
echo"There is a violation for $zipcode";
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.
Suggested Courses

609 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