mercredi 25 février 2015

One PHP statement to use result of IF statement for assignment

I have a situation where I have a function that does some database retrieval and either returns a value or an error. I'd like to have a single statement that calls the function and depending on result either assigns the returned value or a default value.


Using the tenary operator I could do something like this:



$val=(getVal($param)!='error' ? getVal($param) : "default");


but I don't want to have to call getVal twice because it excutes database queries and is expensive on performance.


I could also do it in two statements, but if possible I just want one. Appreciate any help.


django views if statement not working with a boolean

My if statment always goes to the Else even if the boolean value changes? Working in Django 1.6.5


views.py


def awaiting_email_confirmation(request): confirmed = EmailConfirmed.objects.get(user=request.user) print confirmed if confirmed is False: print "if" template = 'accounts/email_confirmation.html' context = {} return render(request, template, context)

else: print "else" return HttpResponseRedirect(reverse("dashboard"))


My console will print


True else


False else


Android: Compare two values doesn't work

at the moment i have a wierd problem... i can't make out where i did a mistake.


I have two pairs of values of the same type and i want to know if these are equal. When i print the values in my Log the values are equal but my if statement doesn't work :/


The values are from type double, but i reduced the decimal to two digits with BigDecimal, like this...



double a = 50.7739202f;
BigDecimal bdA = new BigDecimal(a);
BigDecimal A = bdA.setScale(2, BigDecimal.ROUND_DOWN);


A and B are 50.77 C and D are 60.07


But he always goes to else



if(A == B && C == D){
...
}
else{
...
}

If Else condition on python

I am trying to place a condition after the for loop. It will print the word available if the retrieved rows is not equal to zero, however if I would be entering a value which is not stored in my database, it will return a message. My problem here is that, if I'd be inputting value that isn't stored on my database, it would not go to the else statement. I'm new to this. What would be my mistake in this function?



def search(title):
query = "SELECT * FROM books WHERE title = %s"
entry = (title,)

try:
conn = mysql.connector.connect(user='root', password='', database='python_mysql') # connect to the database server
cursor = conn.cursor()

cursor.execute(query, entry)
rows = cursor.fetchall()

for row in rows:
if row != 0:
print('Available')
else:
print('No available copies of the said book in the library')


except Error as e:
print(e)

finally:
cursor.close()
conn.close()

def main():
title = input("Enter book title: ")
search(title)

if __name__ == '__main__':
main()

IF (Condition) and (another condition) increment cell

I have 2 spread sheets represent in CSV below:


1)


Type,Quantity


Games,1


Games,1


Games,2


DVD,1


2)


Type, Increment DVD, Games,


I want a nested if statement that IF type is "Games" and Quantity is 1, ++1 in the increment cell.


Any help will be much appreciated!


If statement in VB

I have one simple question.I work some applications for holidays appartmants and I don't know how to make if statement that will give me a true price depending on date that customer came.. One price is from 1.5 to 21.6 and from 6.9 to 30.9 second price is from 21.6 to 6.9


How to update information into the database?

Im having a problem in updating the database,


This is my first page,





<div class="col-md-4">
<div class="createnewbox">
<form name="Edit Admin Infomation" class="form-horizontal" method="post" action="adminUpdateProductDetail.php?cat=<?php echo $product['categoryid']; ?>&code=<?php echo $product['productname']; ?>">
<h2>Edit Product Information</h2>
<div class="form-group">
<label class="col-sm-2 control-label">Name</label>
<br/>
<br/>
<div class="col-md-11">
<input type="text" class="form-control" value="<?php echo $product['productname']; ?>" name="productname">
</div>
</div>

<div class="form-group">
<label class="col-sm-2 control-label">Price</label>
<br/>
<br/>
<div class="col-md-11">
<input type="text" class="form-control" value="<?php echo $product['price']; ?>" name="price">
</div>
</div>

<div class="form-group">
<label class="col-sm-2 control-label">Dimension</label>
<br/>
<br/>
<div class="col-md-11">
<input type="text" class="form-control" value="<?php echo $product['dimension']; ?>" name="dimension">
</div>
</div>

<div class="form-group">
<label class="col-sm-2 control-label">Description</label>
<br/>
<br/>
<div class="col-md-11">
<textarea type="text" class="form-control" rows="8" name="productinfo">
<?php echo $product[ 'productinfo']; ?>
</textarea>
</div>
</div>

<div class="form-group">
<div class="col-md-11">
<input type="submit" value="Update Information" class="btn btn-success">
<script>
function reset() {
location.reload();
}
</script>
<button class="btn btn-info" onclick="reset()">undo</button>
</div>
</div>
<?php ?>
</form>
</div>
</div>



This is my adminUpdateProductDetail.php,





<?php
include 'adminNavBar.php';
require 'dbfunction.php';

$con = getDbConnect();

$price = $_POST['price'];
$name = $_POST['productname'];
$info = $_POST['productinfo'];
$dimension = $_POST['dimension'];
$cat= $_GET['cat'];
$code= $_GET['code'];
?>
<div class="space">
<div class="container">
<div class="row">
<?php
if (!mysqli_connect_errno($con)) {
$sqlQueryStr = "UPDATE product SET price = '$price', productname = '$name', productinfo = '$info', dimension = '$dimension' WHERE categoryid = '$cat' AND productname = '$code'";
if (mysqli_query($con, $sqlQueryStr)) {
$recordid = mysqli_insert_id($con);




mysqli_query($con, $sqlQueryStr);
}
mysqli_close($con);
echo "$name Product details updated.";
} else {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
</div>
</div>
</div>



I gotten no error message, the system echo out success. But my database was not updated. I not sure where when wrong.