flyersman Posted October 7, 2008 Share Posted October 7, 2008 Im trying to do a simple... if aff in the users table == 0 print this elseif == 1 print that No matter what, its prints this. Heres my code, please see if im doing anything wrong. Thanks!! <? mysql_connect("localhost", "xxxx_xxxxx", "xxxxxxxxx") or die(mysql_error()); mysql_select_db("xxxx_xxxxxxxx") or die(mysql_error()); $query = "SELECT aff, w9, incentive FROM users"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) ?> And then later in the code... <? if ( $row['aff'] == 0 ) { echo "this"; } elseif ( $row['aff'] == 1 ) { echo "that"; } ?> But when aff is == 1 for the user im using, it still prints this. Anybdy know why? Thanks! Quote Link to comment Share on other sites More sharing options...
R0bb0b Posted October 7, 2008 Share Posted October 7, 2008 try using: $row['aff']+0 == 0 or trim($row['aff']) == "0" or (int)$row['aff'] == 0 Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 7, 2008 Share Posted October 7, 2008 I'm pretty sure that when you pull information out of a database, it comes out as a string, so you need to convert it into an integer or wrap the value that you're comparing it to in quotes (both as R0bb0b showed you). Personally I'd use his 3rd suggestion. Quote Link to comment Share on other sites More sharing options...
flyersman Posted October 7, 2008 Author Share Posted October 7, 2008 I tried them all, and only the second one had any affect. When I changed it to if ( trim($row['aff']) ) { echo "this"; } elseif ( $row['aff'] == 1 ) { echo "yo"; } Nothing showed up. Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 7, 2008 Share Posted October 7, 2008 if( (int)$row['aff'] == 1 ) echo "value is 1"; else if( (int)$row['aff'] == 0 ) echo "value is 0"; Quote Link to comment Share on other sites More sharing options...
flyersman Posted October 7, 2008 Author Share Posted October 7, 2008 It says the value is 0, when for just that user the value is 1 Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 7, 2008 Share Posted October 7, 2008 $aff = (int)$row['aff']; if( $aff == 1 ) echo "value is 1"; else if( $aff == 0 ) echo "value is 0"; else echo "value is other: " . $aff; Try it like that. Quote Link to comment Share on other sites More sharing options...
flyersman Posted October 7, 2008 Author Share Posted October 7, 2008 Agh still showing the value is 0. Maybe its not pulling it just for that user loading the page? Im not sure, im a "newb" to php and tried to make this for my website because I thought it was simple... Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 7, 2008 Share Posted October 7, 2008 Let's see your current code. Quote Link to comment Share on other sites More sharing options...
flyersman Posted October 7, 2008 Author Share Posted October 7, 2008 <? $log="Logout"; if($login_page) $log="Login"; $filearray=explode("/",$_SERVER['PHP_SELF']); $file=$filearray[count($filearray)-1]; switch($file) { case 'dashboard.php': $file_name="Dashboard";break; case 'campaigns.php': $file_name="Campaigns";break; case 'reports.php': $file_name="Reports";break; case 'tools.php': $file_name="Tools";break; case 'profile.php': $file_name="Profile";break; case 'payments.php': $file_name="Payments";break; case 'support.php': $file_name="Support";break; case 'terms.php': $file_name="TOS";break; default:$file_name="";break; } ?> <? mysql_connect("localhost", "xxxx_xxxxx", "xxxxxxxx") or die(mysql_error()); mysql_select_db("xxxxxxxxxxxxxxxx") or die(mysql_error()); $query = "SELECT aff, w9, incentive FROM users"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) ?> <? $aff = (int)$row['aff']; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="Author" content="DJBrewardsATGMAILDOTCOM"> <meta http-equiv="Keywords" content="DJBrewards,freebie,incentive"> <meta name="Description" content="DJBrewards freebie scipt"> <link rel="icon" href="<?=$admin_ext . $config['image_dir']?>favicon.ico" type="image/x-icon" /> <link rel="shortcut icon" href="<?=$admin_ext . $config['image_dir']?>favicon.ico" type="image/x-icon" /> <?php /*?><link rel="stylesheet" href="<?=$admin_ext?>data/template/style.css" type="text/css" /><?php */?> <script language="Javascript" src="<?=$admin_ext?>include/AJAX.js"></script> <script defer type="text/javascript" src="pngfix.js"></script> <title>Express Revenue - <?=$file_name?></title> <!-- this style sheet in main dir, because this file is called by a file in main dir--> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="basic"> <div id="logo"> <table width="706" border="0"> <tr> <td width="19"> </td> <td width="597"> <div align="left"><img src="images/logo.png" width="280" height="36" /></div></td> <td><div align="center"><a href="logout.php"><img src="images/logout.png" width="42" height="14" border="0" /></a></div></td> </tr> </table> </div> <div id="menu"> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="720" height="40"> <param name="movie" value="images/user_menu.swf" /> <param name="quality" value="high" /> <param name="WMODE" value="Transparent" /> <embed src="images/user_menu.swf" width="720" height="40" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="Transparent"></embed> </object> </div> </div> <div id="stat"><img src="images/stats_box.png" /><div id="data" class="style8"><strong><? if(!($admin_page || $login_page)) { load_status_new(); }?></strong></div> </div> <div id="aff"> <img src="images/aff_mngr_box.png" width="285" height="110" /> <div class="style8" id="aff_data"> <strong>Your Affiliate Manager: <? if( $aff == 1 ) echo "value is 1"; else if( $aff == 0 ) echo "value is 0"; else echo "value is other: " . $aff; ?></div> </div> <div id="background"><img src="images/body_bg_shad.png" width="768" height="18" /><br /> <table width="729" border="0" align="center" cellpadding="0" cellspacing="0" class="style9"> <tr> <td align="left" valign="top"><p class="style8" > <div id="heading_back" class="style8"><strong> <?=$file_name?></strong></div><br /> <? echo '<p align="center" >'.page_description($this_page).'</p>'; //this function is saved in include/function.php ?> Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 7, 2008 Share Posted October 7, 2008 Are there multiple records in the users table? Quote Link to comment Share on other sites More sharing options...
flyersman Posted October 7, 2008 Author Share Posted October 7, 2008 Yeah, there are multiple users. Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 7, 2008 Share Posted October 7, 2008 That's your problem then. You need to pass the ID of the user through the URL somehow and then do: $query = 'SELECT * FROM users WHERE id=' . $id; // $id is the ID being passed through the URL $result = mysql_query($query); if(mysql_num_rows($result) == 1) { $row = mysql_fetch_assoc($result); $aff = (int)$row['aff']; // ... } As of now, with your current code, it's probably pulling information from the last user in the database. I bet if you changed the last user's "aff" value, you'd notice a difference. Quote Link to comment Share on other sites More sharing options...
R0bb0b Posted October 7, 2008 Share Posted October 7, 2008 probably not a problem, but what does this <?php while($row = mysql_fetch_array($result, MYSQL_ASSOC)) ?> do without an opening and closing curly brace? Does it just loop through until the last line so $row = last row, or is it the same as $row = mysql_fetch_array($result, MYSQL_ASSOC)? I'm guessing that Brandon Jaeger is right, you need an id in the query otherwise you are just getting the last row. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.