lundi 28 décembre 2020

IF always returns FALSE after comparing data of a database

I'm trying to create a website that has a login page. I had connected a database which contains everyone name and passwords (we are 10 people, so I put all data on a database). After the login it should redirect you to another page, but it always returns false; despite this, I inserted all data correctly. Here's the code of the "if", thanks in advance for the help.

session_start(); 
                  if ($ArrRow["NomeUtente"]==$_POST["NomeUtente"] and $ArrRow["Password"]==$_POST["Password"]) {
                  $_SESSION["login"]=true;
                  echo "Login corretto";
                  echo "<meta http-equiv=refresh content=2;url=https://tpvgruppo.altervista.org/turni-settimanali/>";
                } else {
                  $_SESSION["login"]=false;
                  echo "Login non corretto, riprova.";
                  echo "<meta http-equiv=refresh content=2;url=https://tpvgruppo.altervista.org/area-riservata/>";
             } 

Show values based on dropdown selection on the same page

Is there a way for me to show the values based on selection of dropdown using if statements in Django.

The pricing output value depend on the contract selected and if it is a half day or full day, if it is a half day I just take the outputted price and divide by 2. Below is my model and form, I have been told that I need to do this ideally in the model but cannot figure out the code for a method to work this out or if I need to do it in the template or view, keep hitting brick walls if you have any documents you recommend I should read please let me know.

The way I am looking at solving this is that the dropdown lets say 1-Year Weekly will take the day rate x the number of days until the end date based on:

   @property
   def day_rate_year1(self):
     return self.year1 / weeksinyear / 5

Ideally I would like to have the results of the dropdown shown on the same pages, as I plan to use another button to generate a PDF. I am guessing from my research that this would be done using AJAX

Model

    class AdminData(models.Model):
        year1 = models.IntegerField()
        year3 = models.IntegerField()
        year1_fortnight = models.IntegerField()
        year3_fortnight = models.IntegerField()

    @property
    def fortnight_dayrate_year1(self):
        return self.year1_fortnight / weeksinyear / 5

    @property
    def fortnight_dayrate_year3(self):
        return self.year3_fortnight / weeksinyear / 5

    @property
    def day_rate_year1(self):
        return self.year1 / weeksinyear / 5

    @property
    def day_rate_year3(self):
        return self.year3 / weeksinyear / 5


class Price(models.Model):
        year_choice = Choices('1-Year Weekly', '3-Year Weekly','1-Year Fortnightly', '3-Year 
        Fortnightly')
        day_choice = Choices('Full Day', 'Half Day')
        name = models.CharField(max_length=100)
        contract = StatusField(choices_name='year_choice')
        time_daily = StatusField(choices_name='day_choice')
        start_date = models.DateField(default=datetime.now)
        end_date = models.DateField(default=datetime(2021,3,31))
        epoch_year = date.today().year
        year_start = date(epoch_year, 1, 4)
        year_end = date(epoch_year, 3 , 31)


        def __str__(self):
            return self.name

Please let me know if you need further code.

Thanks for the help, sorry for the stupid question.

Why is my code returning two results instead of one

I am writing a simple code for a password controller which should return false if the string contains less than 8 characters and false if the string contains 8 or more characters. but i am getting two response one is either true or false, and the other result is <function password at 0x00000272BA8B7F70>.

Here is the code I have written:

def password(str):
    if len(str) >= 8:
        print("true")
    else:
        print("false")
    return password
print(password("gtbd"))

Problem on adding quantity in existing item in DataGridView - vb.net

I am creating a shopping system as my university mini project, where the user can enter the barcode of an item they wanted and the entered item will be added to the grid on the right and also as a new item to the grid on the left. Each item has a unique barcode and can have the same product code. When the user enters an item with the same product code then quantity will be added on the grid on the left. I am having a problem where when the second item quantity need to be added, the system is detecting it as a new item instead of adding the quantity

eg: Example of products and items

    Public Sub addRecordTo2ndDGV()
    Try
        dbconnection()
        sql = "SELECT * FROM items_database WHERE Item_Barcode = @ItemBarcode;"
        cmd = New MySqlCommand
        With cmd
            .Connection = conn
            .CommandText = sql
            .Parameters.Clear()
            .Parameters.AddWithValue("@ItemBarcode", formUser.barcodeTB.Text)
        End With
        da = New MySqlDataAdapter
        dt = New DataTable
        da.SelectCommand = cmd
        da.Fill(dt)
        If dt.Rows.Count > 0 Then
            Dim ItemProductCode, ItemBarcode As String
            Dim repeated As Boolean = False
            ItemProductCode = dt.Rows(0).Item(1)
            ItemBarcode = dt.Rows(0).Item(2)
            formUser.itemproductcodeTB.Text = ItemProductCode
            formUser.itembarcodetb.Text = ItemBarcode
            
            For Each row2 As DataGridViewRow In formUser.ItemBarcodeDGV.Rows
                If Convert.ToString(row2.Cells(1).Value) = formUser.itembarcodetb.Text AndAlso Convert.ToString(row2.Cells(0).Value) = formUser.itemproductcodeTB.Text Then
                    MsgBox("QUANTITY WILL MINUS")
                    minusQuantity()
                    formUser.ItemBarcodeDGV.Rows.Remove(row2)
                    MsgBox("QUANTITY HAS MINUS")
                    Exit For

                ElseIf Convert.ToString(row2.Cells(1).Value) <> formUser.itembarcodetb.Text AndAlso Convert.ToString(row2.Cells(0).Value) = formUser.itemproductcodeTB.Text Then
                    MsgBox("QUANTITY WILL ADD")
                    formUser.ItemBarcodeDGV.Rows.Add(New String() {formUser.itemproductcodeTB.Text, formUser.itembarcodetb.Text})
                    addQuantity()
                    MsgBox("QUANTITY HAS ADDED")
                    Exit For

                ElseIf Convert.ToString(row2.Cells(1).Value) <> formUser.itembarcodetb.Text AndAlso Convert.ToString(row2.Cells(0).Value) <> formUser.itemproductcodeTB.Text Then
                    MsgBox("NEW ITEM")
                    formUser.ItemBarcodeDGV.Rows.Add(New String() {formUser.itemproductcodeTB.Text, formUser.itembarcodetb.Text})
                    addRecord()
                    MsgBox("REGISTERED SUCCESSFULL")
                    Exit For
                End If
                Exit For
            Next
        Else
            MsgBox("Barcode not registered!")
            formUser.barcodeTB.Text = ""
            formUser.barcodeTB.Focus()
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        conn.Close()
        da.Dispose()
        formUser.barcodeTB.Text = ""
        clearUserForm()
        retrieveSubTotal()
    End Try
End Sub

Public Sub addRecord()
    If formUser.itemproductcodeTB.Text = "" Then
        MsgBox("Scan a barcode")
    Else
        Try
            dbconnection()
            sql = "SELECT * FROM products_database WHERE Product_Code = @ProductCode;"
            cmd = New MySqlCommand
            With cmd
                .Connection = conn
                .CommandText = sql
                .Parameters.Clear()
                .Parameters.AddWithValue("@ProductCode", formUser.itemproductcodeTB.Text)
            End With
            da = New MySqlDataAdapter
            dt = New DataTable
            da.SelectCommand = cmd
            da.Fill(dt)
            If dt.Rows.Count > 0 Then
                Dim productcode, itemdescription, unitprice As String
                Dim repeated As Boolean = False
                productcode = dt.Rows(0).Item(1)
                itemdescription = dt.Rows(0).Item(3)
                unitprice = dt.Rows(0).Item(4)
                formUser.productcodeTB.Text = productcode
                formUser.itemdescriptionTB.Text = itemdescription
                formUser.unitpriceTB.Text = unitprice
                formUser.unitQuantityTB.Text = "1"

                For Each row As DataGridViewRow In formUser.ProductAddToCartDGV.Rows
                    If Convert.ToString(row.Cells(0).Value) = formUser.productcodeTB.Text AndAlso Convert.ToString(row.Cells(2).Value) = formUser.unitpriceTB.Text Then
                        row.Cells(4).Value = Convert.ToString(row.Cells(3).Value * row.Cells(2).Value)
                    End If
                Next
                formUser.ProductAddToCartDGV.Rows.Add(New String() {formUser.productcodeTB.Text, formUser.itemdescriptionTB.Text, formUser.unitpriceTB.Text, formUser.unitQuantityTB.Text, formUser.unitpriceTB.Text})
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            conn.Close()
            da.Dispose()
        End Try
    End If
End Sub

Public Sub addQuantity()
    If formUser.itemproductcodeTB.Text = "" Then
        MsgBox("Scan a barcode")
    Else
        Try
            dbconnection()
            sql = "SELECT * FROM products_database WHERE Product_Code = @ProductCode;"
            cmd = New MySqlCommand
            With cmd
                .Connection = conn
                .CommandText = sql
                .Parameters.Clear()
                .Parameters.AddWithValue("@ProductCode", formUser.itemproductcodeTB.Text)
            End With
            da = New MySqlDataAdapter
            dt = New DataTable
            da.SelectCommand = cmd
            da.Fill(dt)
            If dt.Rows.Count > 0 Then
                Dim productcode, itemdescription, unitprice As String
                Dim repeated As Boolean = False
                productcode = dt.Rows(0).Item(1)
                itemdescription = dt.Rows(0).Item(3)
                unitprice = dt.Rows(0).Item(4)

                formUser.productcodeTB.Text = productcode
                formUser.itemdescriptionTB.Text = itemdescription
                formUser.unitpriceTB.Text = unitprice
                formUser.unitQuantityTB.Text = "1"

                For Each row As DataGridViewRow In formUser.ProductAddToCartDGV.Rows
                    If Convert.ToString(row.Cells(0).Value) = formUser.productcodeTB.Text AndAlso Convert.ToString(row.Cells(2).Value) = formUser.unitpriceTB.Text Then
                        row.Cells(3).Value = Convert.ToString(Convert.ToInt16(row.Cells(3).Value + 1))
                        row.Cells(4).Value = Convert.ToString(row.Cells(3).Value * row.Cells(2).Value)
                    End If
                Next
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            conn.Close()
            da.Dispose()
        End Try
    End If
End Sub

Public Sub minusQuantity()
    If formUser.itemproductcodeTB.Text = "" Then
        MsgBox("Scan a barcode")
    Else
        Try
            dbconnection()
            sql = "SELECT * FROM products_database WHERE Product_Code = @ProductCode;"
            cmd = New MySqlCommand
            With cmd
                .Connection = conn
                .CommandText = sql
                .Parameters.Clear()
                .Parameters.AddWithValue("@ProductCode", formUser.itemproductcodeTB.Text)
            End With
            da = New MySqlDataAdapter
            dt = New DataTable
            da.SelectCommand = cmd
            da.Fill(dt)
            If dt.Rows.Count > 0 Then
                Dim productcode, itemdescription, unitprice As String
                Dim repeated As Boolean = False
                productcode = dt.Rows(0).Item(1)
                itemdescription = dt.Rows(0).Item(3)
                unitprice = dt.Rows(0).Item(4)

                formUser.productcodeTB.Text = productcode
                formUser.itemdescriptionTB.Text = itemdescription
                formUser.unitpriceTB.Text = unitprice
                formUser.unitQuantityTB.Text = "1"

                For Each row As DataGridViewRow In formUser.ProductAddToCartDGV.Rows
                    If Convert.ToString(row.Cells(0).Value) = formUser.productcodeTB.Text AndAlso Convert.ToString(row.Cells(2).Value) = formUser.unitpriceTB.Text Then
                        row.Cells(3).Value = Convert.ToString(Convert.ToInt16(row.Cells(3).Value - 1))
                        row.Cells(4).Value = Convert.ToString(row.Cells(3).Value * row.Cells(2).Value)
                    End If
                Next
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            conn.Close()
            da.Dispose()
        End Try
    End If
End Sub

Here is the output image

How to filter a list based on multiple other lists in Python?

I have 3 lists:

minimal_values = ['0,32', '0,35', '0,45']
maximal_values = ['0,78', '0,85', '0,72']

my_list = [
    ['Morocco', 'Meat', '190,00', '0,15'], 
    ['Morocco', 'Meat', '189,90', '0,32'], 
    ['Morocco', 'Meat', '189,38', '0,44'],
    ['Morocco', 'Meat', '188,94', '0,60'],
    ['Morocco', 'Meat', '188,49', '0,78'],
    ['Morocco', 'Meat', '187,99', '0,101'],
    ['Spain', 'Meat', '190,76', '0,10'], 
    ['Spain', 'Meat', '190,16', '0,20'], 
    ['Spain', 'Meat', '189,56', '0,35'],
    ['Spain', 'Meat', '189,01', '0,40'],
    ['Spain', 'Meat', '188,13', '0,75'],
    ['Spain', 'Meat', '187,95', '0,85'],
    ['Italy', 'Meat', '190,20', '0,11'],
    ['Italy', 'Meat', '190,10', '0,31'], 
    ['Italy', 'Meat', '189,32', '0,45'],
    ['Italy', 'Meat', '188,61', '0,67'],
    ['Italy', 'Meat', '188,01', '0,72'],
    ['Italy', 'Meat', '187,36', '0,80']]

I want to filter my_list based on index [-1] and the values in minimal_values and maximal_values. So like this:

  1. For Morocco I only want the rows where index[-1] is between 0,32 and 0,78
  2. For Spain I only want the rows where index[-1] is between 0,35 and 0,85
  3. For Italy I only want the rows where index[-1] is between 0,45 and 0,72

I ultimately want my_list to look like this:

my_list = [
    ['Morocco', 'Meat', '189,90', '0,32'], 
    ['Morocco', 'Meat', '189,38', '0,44'],
    ['Morocco', 'Meat', '188,94', '0,60'],
    ['Morocco', 'Meat', '188,49', '0,78'],
    ['Spain', 'Meat', '189,56', '0,35'],
    ['Spain', 'Meat', '189,01', '0,40'],
    ['Spain', 'Meat', '188,13', '0,75'],
    ['Spain', 'Meat', '187,95', '0,85'],
    ['Italy', 'Meat', '189,32', '0,45'],
    ['Italy', 'Meat', '188,61', '0,67'],
    ['Italy', 'Meat', '188,01', '0,72']]

This is the code I tried:

for l in my_list:
    if l[-1] >= [x for x in minimal_values] and <= [x for x in maximal_values]:
        print(l)

I received the following output:

SyntaxError: invalid syntax

Code is not working, and is not doing directed task

The code is self explanatory, but I need help because, when I use it, it skips all the "If" cases and jumps to the next loop directly. The "scanf" works correctly; the variable "order" receives input correctly too. If you figure out how to fix it, Let me know! Have a great day!

#include <stdio.h>
#include <string.h>

int main(){
    int side [3]; // Fries(0), Fried Squid(1), Cheese Fries(2)
    int combo [3]; // Spicy Chicken(0), Whopper(1), Double Whopper(2)
    int drinks[3]; // Cola(0), Vanilla Shake(1), Lemonade(2)
    int amount;
    char order [20];
    int total = 0;

    for(int i = 0; i < 3; ++i){
        combo[i] = 20;
        total = total + 20;
    }
     for(int i = 0; i < 3; ++i){
        side[i] = 20;
        total = total + 20;
    }
    for(int i = 0; i < 3; ++i){
        drinks[i] = 20;
        total = total + 20;
    }

    while (total > 0){
        printf("Make your order here: \n");
        scanf("%s %d", order, &amount);
        printf("%s\n", order);
        if(order == "Fries"){
            printf("A\n");A
            if(side[0] < amount){
                printf("Your order is invalid, and you are stinky.\n");
            }
            else{
                printf("B\n");
                printf("your order is here: %s x %d", order, amount);
                total = total - amount;
            }
            printf("C\n");
        }
        printf("D\n");
    }
    return 0;
}



dimanche 27 décembre 2020

if & else conditios not working in javascript [closed]

<script>
    document.addEventListener('DOMContentLoaded', function() {

        // by default the submit button shd be disabled 

        document.querySelector('#submit').disabled = true;

        document.querySelector('#task').onkeyup = () => {
            if (document.qerySelector('#task').value.length > 0) {
                document.querySelector('#submit').disabled = false;
            } else {
                document.querySelector('#submit').disabled = true;
            }

        }