Page 1 of 1

[Solved] Problem getting values in foreach statement

Posted: Fri Feb 23, 2007 12:51 pm
by Wade
Hi there, I'm trying to construct a page that will print labels out depending on the selected criteria

Here's the code:

Code: Select all

<?php
include 'config.php';
include 'opendb.php';
date_default_timezone_set("America/Edmonton");

require_once('PDF_Label.php');

$pdf = new PDF_Label('5160', 'mm', 1, 2);

$pdf->Open();
$pdf->AddPage();

$action=$_GET['action'];

switch ($action) {
case "Cal":
   $searchStr = "xCalendar";
	 break;
case "iGift":
   $searchStr = "xIndivGift";
	 	 break;
case "GShirt":
   $searchStr = "gShirt";
	 	 break;
case "GBalls":
   $searchStr = "gBalls";
	 	 break;
case "SShirt":
   $searchStr = "sShirt";
	 	 break;
case "BLI":
   $searchStr = "xBuilderLunchInvite";
	 	 break;
case "DJD":
   $searchStr = "DJDInvite";
	 	 break;
case "SAM":
   $searchStr = "SAM";
	 	 break;
default :
	 $searchStr = "";				
}

$query = mysql_escape_string("SELECT ContactID FROM anualevents WHERE " . $searchStr . "=1");
$result = mysql_query ($query)or die ('I cannot connect to the database because: ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$total = mysql_num_rows($result);

foreach($row as $i => $v){
    $query1 = mysql_escape_string("SELECT * FROM _contact WHERE ContactID = " . $v['ContactID'] );
    $result1 = mysql_query ($query1)or die ('I cannot connect to the database because: ' . mysql_error());
    $row1 = mysql_fetch_array($result, MYSQL_ASSOC);

    $fullName = $row1['Prefix'] . ". " . $row1['FirstName'] . " " . $row1['LastName'];
    $address = $row1['City'] . " " . $row1['Province']. " " . $row1['Postal'];
    echo("<br>Fullname: " . $fullName . "<br>Address: ". $row1['StreetAddress'] . "<br>" . $address);

	//Print labels
       //$pdf->Add_PDF_Label(sprintf("%s\n%s\n%s", $fullName, $row1['StreetAddress'], $address));
       //$pdf->Output();
}
include 'closedb.php';
?>
What's happening is that I'm only getting one record to 'print' and I know there 2. Thanks to everyone who looks, i've been kicking this around all morning.

Cheers,

Wade

Posted: Fri Feb 23, 2007 1:14 pm
by shiznatix

Code: Select all

$row1 = mysql_fetch_array($result, MYSQL_ASSOC);
shouldnt that line be:

Code: Select all

$row1 = mysql_fetch_array($result1, MYSQL_ASSOC);  //notice the $result and the number 1

Posted: Fri Feb 23, 2007 2:47 pm
by Wade
HA! That was the other part I was working on, yes that helps display the results properly, but it's still only showing 1 record instead of 2... any thoughts?

Good eye!!

Posted: Fri Feb 23, 2007 2:58 pm
by Mordred

Code: Select all

foreach($row as $i => $v){
write

Code: Select all

var_dump($row);
foreach($row as $i => $v){
instead and think what and why happens.

This:

Code: Select all

default : 
         $searchStr = "";
is not wise, it will break your SQL. Try a default value of "0" if you want nothing to be shown (... WHERE 0=1)

Posted: Fri Feb 23, 2007 3:17 pm
by Wade
Mordred wrote:

Code: Select all

foreach($row as $i => $v){
write

Code: Select all

var_dump($row);
foreach($row as $i => $v){
instead and think what and why happens.

This:

Code: Select all

default : 
         $searchStr = "";
is not wise, it will break your SQL. Try a default value of "0" if you want nothing to be shown (... WHERE 0=1)
Hmmm still not working properly... here's the changed code:

Code: Select all

<?php
include 'config.php';
include 'opendb.php';
date_default_timezone_set("America/Edmonton");

//define('FPDF_FONTPATH','font/');
require_once('PDF_Label.php');

$pdf = new PDF_Label('5160', 'mm', 1, 2);

$pdf->Open();
$pdf->AddPage();

$action=$_GET['action'];

switch ($action) {
case "Cal":
   $searchStr = "xCalendar";
	 break;
case "iGift":
   $searchStr = "xIndivGift";
	 	 break;
case "GShirt":
   $searchStr = "gShirt";
	 	 break;
case "GBalls":
   $searchStr = "gBalls";
	 	 break;
case "SShirt":
   $searchStr = "sShirt";
	 	 break;
case "BLI":
   $searchStr = "xBuilderLunchInvite";
	 	 break;
case "DJD":
   $searchStr = "DJDInvite";
	 	 break;
case "SAM":
   $searchStr = "SAM";
	 	 break;
default :
	 $searchStr = "0";				
}

$query = mysql_escape_string("SELECT ContactID FROM anualevents WHERE " . $searchStr . "=1");
$result = mysql_query ($query)or die ('I cannot connect to the database because: ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$total = mysql_num_rows($result);
echo $total . "<br>";

var_dump($row);
foreach($row as $i => $v){
    $query1 = mysql_escape_string("SELECT * FROM _contact WHERE ContactID = " . $v['ContactID'] );
    $result1 = mysql_query ($query1)or die ('I cannot connect to the database because: ' . mysql_error());
    $row1 = mysql_fetch_array($result1, MYSQL_ASSOC);
   echo "<br><br>" . $query1;
    $fullName = $row1['Prefix'] . ". " . $row1['FirstName'] . " " . $row1['LastName'];
    $address = $row1['City'] . " " . $row1['Province']. " " . $row1['Postal'];
    echo("<br>Fullname: " . $fullName . "<br>Address: ". $row1['StreetAddress'] . "<br>" . $address);

		//Print labels
		//$pdf->Add_PDF_Label(sprintf("%s\n%s\n%s", $fullName, $row1['StreetAddress'], $address));
	  //$pdf->Output();
}
include 'closedb.php';
?>
and here's the output:
2
array(1) { ["ContactID"]=> string(1) "8" }

SELECT * FROM _contact WHERE ContactID = 8
Fullname: <Name>
Address: <Address>
<Address cont>
The values are displaying properly, but only for the first record, the second is not being processed...

Posted: Fri Feb 23, 2007 4:45 pm
by Wade
Resolved with the following Info:

mysql_fetch_array() only returns one result row from the query. The most common idiom for looping through all the results is:

Code: Select all

while($row = mysql_fetch_array($result))
{
   // do whatever needs doing for this result row
}