Jump to content

if else issue


Angeleyezz

Recommended Posts

Greetings =), seems i'm stumped again. lol

 

What I want to do is check to see if a row exists if it does assign the variable to the $deposit variable to $row['price_each']

If it does not exist, i want it to assign the variable $deposit = 0

 

Here is my code, I think I am doing something wrong, becuase it keeps giving me the undefined variable for $deposit, when the statment i wrote should make it work. I think my syntax is off, or my ==0 is in the wrong place.  I'm not too expierenced with php and i'm still learning.  Any help would be greatly appreciated!

 

$deposit_query2 = mysql_query("SELECT * FROM invoice_entries, sku_codes WHERE invoice_entries.invoice_number = $invoice_number AND invoice_entries.sku_number = sku_codes.sku AND sku_codes.sku = 01-00")
or die(mysql_error());
if ($row = mysql_fetch_array($deposit_query2)==0 )
{
$deposit = $row['price_each'];
} else {
$deposit = 0;
}

Link to comment
Share on other sites

I changed it up a bit with a different method, but its giving me a parse error unexpected T_Variable, I dont see why, all my syntax looks right.

 

$deposit_query2 = mysql_query("SELECT * FROM invoice_entries, sku_codes WHERE invoice_entries.invoice_number = $invoice_number AND invoice_entries.sku_number = sku_codes.sku AND sku_codes.sku = 01-00")
or die(mysql_error());

if (mysql_num_rows($deposit_query2)==0)
{
$row = mysql_fetch_array($deposit_query2)
$deposit = $row['price_each'];

} else {

$deposit = 0;

}

Link to comment
Share on other sites

I think this is what you were trying to do, I'm a newbie myself but I'm trying to contribute back haha.

If mysql_num_rows returns 0 theres nothing to put in the array.

 

<?php

//you forget to concenate $invoice number
$deposit_query2 = "SELECT * FROM invoice_entries, sku_codes WHERE 
invoice_entries.invoice_number = {$invoice_number} AND invoice_entries.sku_number =
sku_codes.sku AND sku_codes.sku = 01-00"

$sql_deposist2 = mysql_query($despost_query2);


if (!$sql_deposit2) {
// added some error handling, checks if the query failed
die("Error executing the query" . mysql_error() . "fml");
}	else {
	$row = mysql_fetch_row($despost_query2);
}	if ($row == 0) {
		echo "no rows returned";
	}	else {
			$deposit_2_data = mysql_fetch_array($sql_deposit2);
			$deposit = $deposit_2_data['price_each'];
		}	else {
				$deposit = 0;

?>

Link to comment
Share on other sites

livethedead, this was already taken care of with this code

 

$deposit_query2 = mysql_query("SELECT * FROM invoice_entries, sku_codes WHERE invoice_entries.invoice_number = $invoice_number AND invoice_entries.sku_number = sku_codes.sku AND sku_codes.sku = 01-00")
or die(mysql_error());

if ($row = mysql_fetch_array($deposit_query2))
{
  $deposit = $row['price_each'];
} else {
  $deposit = 0;
}

Link to comment
Share on other sites

This is how I wrote the whole script if anyone wanted to see it, maybe some other noob like me could refrence to it if they need help with what I was trying to achieve with anything.

 

 

$invoice_number = $_GET['id'];

// Select MYSQL Database
mysql_select_db("terra_elegante_operations", $con);

// ALL QUERIES FOR CREDIT THAT NEEDS TO BE APPLIED 

// Query the deposit
$deposit_query2 = mysql_query("SELECT * FROM invoice_entries, sku_codes WHERE invoice_entries.invoice_number = $invoice_number AND invoice_entries.sku_number = sku_codes.sku AND sku_codes.sku = 01-00")
or die(mysql_error());

if ($row = mysql_fetch_array($deposit_query2))
{
$deposit = $row['price_each'];
} else {
$deposit = 0;
}

// Query the (Senior Citizen Discount - 01-02)
$senior_discount_query_2 = mysql_query("SELECT * FROM invoice_entries, sku_codes WHERE invoice_entries.invoice_number = $invoice_number AND invoice_entries.sku_number = sku_codes.sku AND sku_codes.sku = 01-02")
or die(mysql_error());

if ($row = mysql_fetch_array($senior_discount_query_2))
{
$senior_discount = $row['price_each'];
} else {
$senior_discount = 0;
}

// / END ALL QUERIES FOR CREDIT THAT NEEDS TO BE APPLIED

// Query invoice number
$query_client_invoice = mysql_query("SELECT * from client_invoices where invoice_number = $invoice_number");

// Assign Variables from client_invoices table.
$row = mysql_fetch_array($query_client_invoice);
$account_number = $row['account_number'];
$invoice_date = $row['invoice_date'];
$invoice_status = $row['invoice_status'];
$due_date = $row['due_date'];


// Query Client Information
$query_client_information = mysql_query("SELECT * FROM client_information where account_number = $account_number");

// Assign Variables from client_information table.
$row = mysql_fetch_array($query_client_information);
$name_first = $row['name_first'];
$name_last = $row['name_last'];
$address = $row['address'];
$city = $row['city'];
$state = $row['state'];
$zipcode = $row['zipcode'];
$telephone = $row['telephone'];
$telephone_alt = $row['telephone_alt'];
$email = $row['email'];
?>

<table border="0" cellspacing="0" cellpadding="0" width="100%" bordercolor="#000000">
  <tr>
    <td><font face="verdana" size="2" color="#000000"><b><?php echo "<a class='bubble_nav' href='edit_invoice.php?id=$invoice_number'>EDIT INVOICE</a>"; ?>  |  <?php echo "<a class='bubble_nav' href='print_invoice.php?id=$invoice_number'>PRINT INVOICE</a>"; ?></b></font></td>
  </tr>
</table>
<!-- Invoice Heading - Account #, Invoice #, Name, Address, Telephone #'s -->
<br />
<table border="0" cellspacing="0" cellpadding="2" width="100%" bordercolor="#000000">
  <tr>
    <td colspan="4" align="left"><font face="verdana" size="3" color="#000000"><b><u>Invoice Date: <?php echo $invoice_date; ?></u></b></font></td>
  </tr>
    <td width="20%"><font face="verdana" size="2" color="#000000">Account Number:</font></td>
    <td width="40%"><font face="verdana" size="2" color="#000000"><?php echo "<a class=\"bubble_nav\" href='show_client.php?id=$account_number'>$account_number</a\>"; ?></font></td>
    <td width="20%"><font face="verdana" size="2" color="#000000">Telephone</font></td>
    <td width="20%"><font face="verdana" size="2" color="#000000"><?php echo $telephone; ?></font></td>
  </tr>
  <tr>
    <td><font face="verdana" size="2" color="#000000">Invoice Number:</font></td>
    <td><font face="verdana" size="2" color="#000000"><?php echo $invoice_number; ?></font></td>
    <td><font face="verdana" size="2" color="#000000">Telephone Alt</font></td><td><font face="verdana" size="2" color="#000000"><?php echo $telephone_alt ?></font></td>
  </tr>
  <tr>
    <td colspan="2"><font face="verdana" size="2" color="#000000"><br /><?php echo $name_last; ?> , <?php echo $name_first; ?></font></td>
    <td align="left" valign="bottom" colspan="2" rowspan="3"><font face="verdana" size="3" color="#000000"><b>Status: <?php echo $invoice_status ?></b><br />Due Date:<?php echo $due_date; ?></font></td>
  </tr>
  <tr>
    <td colspan="2"><font face="verdana" size="2" color="#000000"><?php echo $address ?></font></td>
  </tr>
  <tr>
    <td colspan="2"><font face="verdana" size="2" color="#000000"><?php echo $city; ?>, <?php echo $state; ?> <?php echo $zipcode; ?></font></td>
  </tr>
</table>
<!-- / End Invoice Heading -->

<!-- Begin Invoice Entries SQL & Table Structure -->

<br /><br /><br />
<!-- Out of the while loop / Top of table -->
<table bgcolor="#000000" border="0" cellspacing="1" cellpadding="2" width="100%" bordercolor="#000000">
  <tr bgcolor="#CCCCCC">
    <td align="center" width="5%"><font face="verdana" size="2" color="#000000"><b>QTY</b></font></td>
    <td align="center" width="10%"><font face="verdana" size="2" color="#000000"><b>SKU</b></font></td>
    <td align="center" width="30%"><font face="verdana" size="2" color="#000000"><b>DESCRIPTION</b></font></td>
    <td align="center" width="25%"><font face="verdana" size="2" color="#000000"><b>LOCATION</b></font></td>
    <td align="center"width="15%"><font face="verdana" size="2" color="#000000"><b>EACH</b></font></td>
    <td align="center" width="15%"><font face="verdana" size="2" color="#000000"><b>TOTAL</b></font></td>
  </tr>
<?php
// Query the deposit
$deposit_query = mysql_query("SELECT * FROM invoice_entries, sku_codes WHERE invoice_entries.invoice_number = $invoice_number AND invoice_entries.sku_number = sku_codes.sku AND sku_codes.sku = 01-00")
or die(mysql_error());

while($row = mysql_fetch_array($deposit_query))
{
$deposit			 = $row['price_each'];
$deposit_location	 = $row['location'];
$deposit_description = $row['sku_description'];
$deposit_quantity	 = $row['quantity'];
$deposit_sku		 = $row['sku'];

?>
  <tr bgcolor="#c2f48c">
<td align="center"><font face="verdana" size="1" color="#000000"><i><b><?php echo $deposit_quantity; ?></b></i></font></td>
<td align="center"><font face="verdana" size="1" color="#000000"><i><b><?php echo $deposit_sku ?></b></i></font></td>
<td><font face="verdana" size="1" color="#000000"><i><b><?php echo $deposit_description; ?></b></i></font></td>
<td align="center"><font face="verdana" size="1" color="#000000"><i><b><?php echo $deposit_location; ?></b></i></font></td>
<td align="right"><font face="verdana" size="1" color="#000000"><i><b>$<?php echo $deposit; ?></b></i></font></td>
<td align="right"><font face="verdana" size="1" color="#000000"><i><b>$<?php echo $deposit; ?></b></i></font></td>
  </tr>
<?php

}
// Query the Senior Discount
$senior_discount_query = mysql_query("SELECT * FROM invoice_entries, sku_codes WHERE invoice_entries.invoice_number = $invoice_number AND invoice_entries.sku_number = sku_codes.sku AND sku_codes.sku = 02-00")
or die(mysql_error());

while($row = mysql_fetch_array($senior_discount_query))

{
$senior_discount			 = $row['price_each'];
$senior_discount_location	 = $row['location'];
$senior_discount_description = $row['sku_description'];
$senior_discount_quantity	 = $row['quantity'];
$senior_discount_sku		 = $row['sku'];

?>
  <tr bgcolor="#ffffcc">
<td align="center"><font face="verdana" size="1" color="#000000"><i><b><?php echo $senior_discount_quantity; ?></b></i></font></td>
<td align="center"><font face="verdana" size="1" color="#000000"><i><b><?php echo $senior_discount_sku ?></b></i></font></td>
<td><font face="verdana" size="1" color="#000000"><i><b><?php echo $senior_discount_description; ?></b></i></font></td>
<td align="center"><font face="verdana" size="1" color="#000000"><i><b><?php echo $senior_discount_location; ?></b></i></font></td>
<td align="right"><font face="verdana" size="1" color="#000000"><i><b>%<?php echo $senior_discount; ?></b></i></font></td>
<td align="right"><font face="verdana" size="1" color="#000000"><i><b>%<?php echo $senior_discount; ?></b></i></font></td>
  </tr>
<?php
}


// Query Invoice records from tables : invoice_entries, sku_codes
$query_invoice_entries = mysql_query("SELECT * FROM invoice_entries, sku_codes WHERE invoice_entries.invoice_number = $invoice_number AND invoice_entries.sku_number = sku_codes.sku AND sku_codes.sku != 01-00 AND invoice_entries.sku_number = sku_codes.sku AND sku_codes.sku != 02-00")
or die(mysql_error());

// Math variable assignments
$price_each_subtotal = 0;

// Begin While Loop of invoice records
while($row = mysql_fetch_array($query_invoice_entries))

{

$quantity = $row['quantity'];
$sku_number = $row['sku_number'];
$location = $row['location'];
$price_each = $row['price_each'];
$sku_description = $row['sku_description'];

// Math, Number Format Variables
$price_each_subtotal += ($quantity * $price_each);
$price_each_subtotal_self = ($quantity * $price_each);

?>
  <tr bgcolor="#ffffff">
     <td align="center"><font face="verdana" size="1" color="#000000"><?php echo $quantity; ?></font></td>
     <td align="center"><font face="verdana" size="1" color="#000000"><?php echo $sku_number; ?></font></td>
     <td align="left"><font face="verdana" size="1" color="#000000"><?php echo $sku_description; ?></font></td>
     <td align="center"><font face="verdana" size="1" color="#000000"><?php echo $location; ?></font></td>
     <td align="right"><font face="verdana" size="1" color="#000000">$<?php echo number_format($price_each,$decimals = 2, $dec_point = '.', $thousands_sep = ','); ?></font></td>
     <td align="right"><font face="verdana" size="1" color="#000000">$<?php echo number_format($price_each_subtotal_self,$decimals = 2, $dec_point = '.', $thousands_sep = ','); ?></font></td>
   </tr>
<?php 
} 

// Total Mathematics




// Subtotal Math
$subtotal_no_deposit = ($price_each_subtotal);



// Discounts
$discount_1 = ($subtotal_no_deposit * $senior_discount);
$senior_discount_extract = ($subtotal_no_deposit - $discount_1);

$total_discounts = ($senior_discount_extract - $subtotal_no_deposit);

// Tax Math
$subtotal_tax = ($senior_discount_extract * 0.07);
$deposit_tax = ($deposit * 0.07);

// Identify $tax
$tax = ($subtotal_tax);

// Final Total Math
$subtotal = ($senior_discount_extract);

$total = ($subtotal + $tax - $deposit);

?>
<!-- / End invoice Entries SQL & Table Structure -->
<!-- End table outside of the while loop -->
<!-- Close entry table -->
</table>
<br />
<!-- Open Sub Total, Tax, Total Table -->
<table border="0" align="right" cellspacing="1" cellpadding="3" width="30%" bordercolor="#000000" bgcolor="#000000">
  <tr bgcolor="#ffffff">
    <td width="15%" align="right"><font face="verdana" size="1" color="#000000">SUB TOTAL :</font></td>
    <td width="15%" align="right"><font face="verdana" size="1" color="#000000">$<?php echo number_format($subtotal,$decimals = 2, $dec_point = '.', $thousands_sep = ','); ?></font></td>
  </tr>
  <tr bgcolor="#ffffff">
    <td align="right"><font face="verdana" size="1" color="#000000">TAX :</font></td>
    <td align="right"><font face="verdana" size="1" color="#000000">$<?php echo number_format($tax,$decimals = 2, $dec_point = '.', $thousands_sep = ','); ?></font></td>
  </tr>
  <tr bgcolor="#ffffff">
    <td align="right"><font face="verdana" size="2" color="#000000"><b>TOTAL DUE :</b></font></td>
    <td align="right"><font face="verdana" size="2" color="#000000"><b>$<?php echo number_format($total,$decimals = 2, $dec_point = '.', $thousands_sep = ','); ?></b></font></td>
  </tr>
</table>

<br /><br /><br /><br />

<!-- / End Invoice Entries SQL & Table Structure ->

<?php
include("includes/footer.php");
?>

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.