Solved --- Problem with loop if field is 0

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dmacman1962
Forum Newbie
Posts: 14
Joined: Wed Mar 10, 2004 2:58 pm

Solved --- Problem with loop if field is 0

Post 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
Last edited by dmacman1962 on Fri Mar 12, 2004 10:06 am, edited 3 times in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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') {
dmacman1962
Forum Newbie
Posts: 14
Joined: Wed Mar 10, 2004 2:58 pm

Sorry, but that didn't work.

Post by dmacman1962 »

Thanks for responding, Pickle. I tried what you suggested, but it still did not work. Any other ideas.

Thanks,

Dmacman
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

What does it do?
What does var_dump($list); show?
dmacman1962
Forum Newbie
Posts: 14
Joined: Wed Mar 10, 2004 2:58 pm

Post 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>";
}
}
?>
rajivkrishnang
Forum Newbie
Posts: 5
Joined: Tue Mar 23, 2004 12:18 am

Post 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>";
rajivkrishnang
Forum Newbie
Posts: 5
Joined: Tue Mar 23, 2004 12:18 am

Post by rajivkrishnang »

"regarding my above message"

its not eval()

but intval() sorry
Post Reply