Page 1 of 1

Solved --- Problem with loop if field is 0

Posted: Wed Mar 10, 2004 2:58 pm
by dmacman1962
I am having a problem with a loop that I want to show the contents of a field if it is not zero. Here is my code:

Code: Select all

<?
$dbHost = "xxxxx";
$dbName = "xxxxx";
$dbUser = "xxxxx";
$dbPass = "xxxxx";
$connmts = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error(mysql_error(),E_USER_ERROR); 

$link = @mysql_connect($dbHost, $dbUser, $dbPass);
if (!$link){
	$errorMsg = "Could not connect to server";
	print "&result=Fail&errorMsg=$errorMsg";
	exit;
}
if (!@mysql_select_db($dbName)){
  $errorMsg = "Could not select $dbName database";
  print "&result=Fail&errorMsg=$errorMsg";
  exit;
}
$hold = $_REQUEST['merchant'];
$query = "SELECT * FROM coupons WHERE id LIKE '$hold%' AND active = 1 ORDER BY id asc";
$merchants = mysql_query($query, $connmts) or die(mysql_error());
if (mysql_num_rows($merchants) < 1) {
		print "There are no offers at this time. ";
	} else {
while ($row = mysql_fetch_array($merchants, MYSQL_ASSOC)) {
	$list = $row['info'];
	$coupon = $row['coupon'];
	$merchant_name = $row['merchant_name'];
		print "<strong>$merchant_name</strong><br><br>";
	print "<a href='http://www.mytownsavings.com/proxy.asp?action=clip&coupon=$coupon'>";
		if ($list != 0) {
			 print "<strong>$list</strong>";
		} else {
			 print "<br>";
		}
	print "<br>";
}
}
?>
I believe the problem lies in this code:

Code: Select all

<?
if ($list != 0) {
			 print "<strong>$list</strong>";
		} else {
			 print "<br>";
?>
I am fairly new to this, a few months, so I appreciate the help in advance.

Thanks,
Dmacman

Posted: Wed Mar 10, 2004 2:59 pm
by pickle
Here's your code, put within PHP tags. Take a look at the sticky at the top of the forum for more guidlines (Highlighting makes it infinitely easier to look at code).

Code: Select all

<? 
$dbHost = "subdomain.server.com"; 
$dbName = "database_name"; 
$dbUser = "database_user"; 
$dbPass = "database_password"; 
$connmts = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error(mysql_error(),E_USER_ERROR); 

$link = @mysql_connect($dbHost, $dbUser, $dbPass); 
if (!$link){ 
$errorMsg = "Could not connect to server"; 
print "&result=Fail&errorMsg=$errorMsg"; 
exit; 
} 
if (!@mysql_select_db($dbName)){ 
$errorMsg = "Could not select $dbName database"; 
print "&result=Fail&errorMsg=$errorMsg"; 
exit; 
} 
$hold = $_REQUEST['merchant']; 
$query = "SELECT * FROM coupons WHERE id LIKE '$hold%' AND active = 1 ORDER BY id asc"; 
$merchants = mysql_query($query, $connmts) or die(mysql_error()); 
if (mysql_num_rows($merchants) < 1) { 
print "There are no offers at this time. "; 
} else { 
while ($row = mysql_fetch_array($merchants, MYSQL_ASSOC)) { 
$list = $row['info']; 
$coupon = $row['coupon']; 
$merchant_name = $row['merchant_name']; 
print "<strong>$merchant_name</strong><br><br>"; 
print "<a href='http://www.address.com/proxy.asp?action=clip&coupon=$coupon'>"; 
if ($list != 0) { 
print "<strong>$list</strong>"; 
} else { 
print "<br>"; 
} 
print "<br>"; 
} 
} 
?>
I'm thinking your problem is that nothing is being echoed at all? If that's the case, it's due to the fact that you're comparing $list, a string, with 0, a number. Put 0 in quotes and it'll probably work:

Code: Select all

if ($list != '0') {

Sorry, but that didn't work.

Posted: Wed Mar 10, 2004 11:37 pm
by dmacman1962
Thanks for responding, Pickle. I tried what you suggested, but it still did not work. Any other ideas.

Thanks,

Dmacman

Posted: Wed Mar 10, 2004 11:40 pm
by markl999
What does it do?
What does var_dump($list); show?

Posted: Fri Mar 12, 2004 10:06 am
by dmacman1962
I got it to work. Here is the code tht worked correctly.

Thanks everyone!!

Dmacman

Code: Select all

<?
$dbHost = "xxx.xxx.net";
$dbName = "xxx";
$dbUser = "xxx";
$dbPass = "xxx";
$connmts = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error(mysql_error(),E_USER_ERROR); 

$link = @mysql_connect($dbHost, $dbUser, $dbPass);
if (!$link){
	$errorMsg = "Could not connect to server";
	print "&result=Fail&errorMsg=$errorMsg";
	exit;
}
if (!@mysql_select_db($dbName)){
  $errorMsg = "Could not select $dbName database";
  print "&result=Fail&errorMsg=$errorMsg";
  exit;
}
$hold = $_REQUEST['merchant'];
$query = "SELECT * FROM coupons WHERE id LIKE '$hold%' AND active = 1 ORDER BY id asc";
$merchants = mysql_query($query, $connmts) or die(mysql_error());
if (mysql_num_rows($merchants) < 1) {
		print "There are no offers at this time. ";
	} else {
while ($row = mysql_fetch_array($merchants, MYSQL_ASSOC)) {
	$offer = $row['offering'];
	$coupon = $row['coupon'];
	print "<a href='http://www.mytownsavings.com/proxy.asp?action=clip&coupon=$coupon'>$offer";
		if ($list != 0) {
			print $list;
			} else {
			print "<br>"; 
		}
	print "<br>";
}
}
?>

Posted: Tue Mar 23, 2004 12:18 am
by rajivkrishnang
<?
if ($list != 0) {
print "<strong>$list</strong>";
} else {
print "<br>";
?>


solution:

u can chek the value using after convertin it to integer using the funtion eval() or u can check as its a string ,like '0' ( depend on ur database vaue)

then

print "<strong>$list</strong>";

here specify $list as a variable not a string

solution:

print "<strong>".$list."</strong>";

Posted: Tue Mar 23, 2004 12:20 am
by rajivkrishnang
"regarding my above message"

its not eval()

but intval() sorry