Jump to content

if value is 0 do this w/MySql


Lamez

Recommended Posts

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!

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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!]";
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

alright I got the code, I wrote it myself :D

 

<?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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.