Angeleyezz Posted February 16, 2012 Share Posted February 16, 2012 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 https://forums.phpfreaks.com/topic/257134-if-else-issue/ Share on other sites More sharing options...
smerny Posted February 16, 2012 Share Posted February 16, 2012 if ($row = mysql_fetch_array($deposit_query2)==0 ) this is actually checking if "mysql_fetch_array($deposit_query2)==0" and saving the result to $row you can just remove the ==0 part if you're wanting it to return false if there is no matching row Link to comment https://forums.phpfreaks.com/topic/257134-if-else-issue/#findComment-1318141 Share on other sites More sharing options...
Angeleyezz Posted February 16, 2012 Author Share Posted February 16, 2012 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 https://forums.phpfreaks.com/topic/257134-if-else-issue/#findComment-1318142 Share on other sites More sharing options...
Angeleyezz Posted February 16, 2012 Author Share Posted February 16, 2012 yeah i had it right the first time, just needed to take that out like you said, that works fine so far, now to add 1 more thing to the invoice and im finished with it, thank god...... lol Link to comment https://forums.phpfreaks.com/topic/257134-if-else-issue/#findComment-1318143 Share on other sites More sharing options...
livethedead Posted February 16, 2012 Share Posted February 16, 2012 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 https://forums.phpfreaks.com/topic/257134-if-else-issue/#findComment-1318164 Share on other sites More sharing options...
smerny Posted February 17, 2012 Share Posted February 17, 2012 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 https://forums.phpfreaks.com/topic/257134-if-else-issue/#findComment-1318216 Share on other sites More sharing options...
livethedead Posted February 17, 2012 Share Posted February 17, 2012 I realise that but I was trying to show her a few things by coding it like that. Can't hurt... Link to comment https://forums.phpfreaks.com/topic/257134-if-else-issue/#findComment-1318218 Share on other sites More sharing options...
Angeleyezz Posted February 17, 2012 Author Share Posted February 17, 2012 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 https://forums.phpfreaks.com/topic/257134-if-else-issue/#findComment-1318230 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.