Lamez Posted February 11, 2008 Share Posted February 11, 2008 Alright, I have redesigned my paid system. Now if they have a 0 in the paid column, then is shows them as not paid, but if they have a 1 then they have paid. However, I do not know how to write a IF statement with a MySQL statement. Could someone post an example thanks! Quote Link to comment Share on other sites More sharing options...
Wolphie Posted February 11, 2008 Share Posted February 11, 2008 <?php $sql = "SELECT `paid` FROM `users` WHERE `paid` = 1"; $sql = mysql_query($sql) or die('Error: ' . mysql_error()); if($sql) { echo 'You have paid'; } else { echo 'You haven't paid'; } ?> Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted February 11, 2008 Share Posted February 11, 2008 try is like this: If($paid == 0 ){ echo "Unpaid"; } elseif($paid == 1 ){ echo "Paid"; } OR If($paid == 0 ){ echo "Unpaid"; } else{ echo "Paid"; } $paid is a variable coming from mysql query. Quote Link to comment Share on other sites More sharing options...
Lamez Posted February 11, 2008 Author Share Posted February 11, 2008 right, but how do I do this with a loop, and it changes the message with different usernames? here is my old code: (now the paid value is in one table w/ the usernames) $qr=mysql_query("select * from users order by first"); $pt=mysql_query("select * from paid order by user"); while ($rs=mysql_fetch_assoc($qr)) { $pu=mysql_fetch_assoc($pt); $table = 'paid'; $field = 'user'; $compared = $pu['user']; $result = mysql_query("SELECT $field FROM $table WHERE $field = '$compared'"); ?> <tr> <td><? echo $rs['first'];?></td> <td><? echo $rs['last'];?></td> <td> <? if(mysql_num_rows($result)==0) { //"Not Paid "; ?> <input type="checkbox" name="checkbox[]" value="<? echo $rs['username'] ?>" /> <?php } else { echo '<a href="pay_dele.php?cmd=delete&user='.$rs['username'].'">Not Paid</a> '; } ?> (<? echo $rs['username']; ?>) </td> Quote Link to comment Share on other sites More sharing options...
Wolphie Posted February 11, 2008 Share Posted February 11, 2008 @ mmarif4u That's not necessary, you could just simply use SQL alone. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted February 11, 2008 Share Posted February 11, 2008 @Wolphie,it is necessary because how is going to show all users who not paid OR want to show username for unpaid. Your this query is just selecting paid users. $sql = "SELECT `paid` FROM `users` WHERE `paid` = 1"; I write that code if he want to show some other details for all users or transactions. Quote Link to comment Share on other sites More sharing options...
Wolphie Posted February 11, 2008 Share Posted February 11, 2008 The code you wrote is precisely the same as mine. It's basically if true do something, if false do something else. Nothing more, nothing less. Besides, you never provided a query with your code. So as far as i'm concerned, mines correct. Quote Link to comment Share on other sites More sharing options...
Lamez Posted February 11, 2008 Author Share Posted February 11, 2008 I get this error: Error: Query was empty here is my PHP code: <table width="63%" border="0"> <tr> <td width="194"><strong>First Name </strong></td> <td width="261"><strong>Last Name </strong></td> <td width="273"><strong>Paid (username) </strong></td> </tr> <?php $paid = "select * from users order by last"; $paid = mysql_query($sql) or die('Error: ' . mysql_error()); while ($rs=mysql_fetch_assoc($paid)) { ?> <tr> <td><? echo $rs['first'];?></td> <td><? echo $rs['last'];?></td> <td> <?php If($paid == 0 ){ echo "Unpaid"; } elseif($paid == 1 ){ echo "Paid"; } echo "(".$rs['username'].")"; } ?> </td> </tr> </table> lol, what does that mean? Quote Link to comment Share on other sites More sharing options...
Wolphie Posted February 11, 2008 Share Posted February 11, 2008 It means nothing was returned, no result. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted February 11, 2008 Share Posted February 11, 2008 Change it: <table width="63%" border="0"> <tr> <td width="194"><strong>First Name </strong></td> <td width="261"><strong>Last Name </strong></td> <td width="273"><strong>Paid (username) </strong></td> </tr> <?php $paid = "select * from users order by last"; $q = mysql_query($paid) or die('Error: ' . mysql_error()); while ($rs=mysql_fetch_array($q)) { ?> <tr> <td><? echo $rs['first'];?></td> <td><? echo $rs['last'];?></td> <td> <?php If($rs['paid'] == 0 ){ echo "Unpaid"; } elseif($rs['paid'] == 1 ){ echo "Paid"; } echo "(".$rs['username'].")"; } ?> </td> </tr> </table> Quote Link to comment Share on other sites More sharing options...
Lamez Posted February 11, 2008 Author Share Posted February 11, 2008 That did, but now I gotta change it in the dashboard. How would I write this code for one individual user? here is my old code: (the paid table is now a column in the users table) $q = mysql_query("Select * from `paid` where user='$session->username'"); if(mysql_num_rows($q)=='0'){ echo "<br><h3>You have not paid yet.</h3>"; }else{ echo "[You have paid!]"; } Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted February 11, 2008 Share Posted February 11, 2008 Post your update code after editing and also post the table structures. And under line it,where you want to change things. Quote Link to comment Share on other sites More sharing options...
Lamez Posted February 11, 2008 Author Share Posted February 11, 2008 I have not written my update code yet. TABLE NAME: users username password userid first last zip yahoo aim paid $session->username that defines the current user that is logged in Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 11, 2008 Share Posted February 11, 2008 Hmmm, seems like you guys are going to a LOT more trouble than needed. Just use a simple IF statement in the SQL query. For all users: SELECT *, IF(paid==1,'Paid','Not Paid') as status FROM users ORDER BY last For one user: SELECT *, IF(paid==1,'Paid','Not Paid') as status FROM users WHERE username = '{$session->username}' ORDER BY last Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 11, 2008 Share Posted February 11, 2008 mjdamato has the best solution, except in MySQL it's a single equal signs, not a double. Quote Link to comment Share on other sites More sharing options...
Lamez Posted February 11, 2008 Author Share Posted February 11, 2008 alright I got the code, I wrote it myself <?php $q = mysql_query("Select * from `users` where username='$session->username'"); $rs=mysql_fetch_array($q); $paid = $rs['paid']; if ($paid === ('0')){ echo "<br><br><h3>You have not paid yet.</h3>"; }else{ echo "[You have paid!]"; } ?> -Topic Solved! 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.