lundi 24 juin 2019

Use if statement or while statement to check current item value against database?

Trying to check if a row has a certain string and execute something if it does.

I've tried to use AND OR && || in the if statements to combine checks and that failed then tried each one. code follow below.

//QUERY ARRAY//
<?php
$sarray = array("first", "second", "third", "fourth", "fifth", "sixth");
$sArray2 = '"' . implode('","', $sarray) . '"';
$query = "SELECT positions, id FROM tableone WHERE positions IN ($sArray2) ORDER BY id ASC";
// id's are numbers from 1 - ~ number of rows ///
$result = mysqli_query($con,$query);

if (!$result) {
    die("Connection Error: " . mysqli_connect_error());
}
$count = mysqli_num_rows($result);
$i = 1;

while ($row = mysqli_fetch_assoc($result)) 
{
    if($i%1==1) 

    if ($row['positions'] = "first")
    {
    echo $row['first'];
    }
        else if ($row['positions'] == "second")
        {
        echo $row['second'];
        }
            else if ($row['positions'] == "third")
            {
            echo $row['third'];
            }
                else if ($row['positions'] == "fourth")
                {
                echo $row['fourth'];
                }
                    else if ($row['positions'] == "fifth" || "sixth")
                    {
                    echo $row['fifth'];
                    echo $row['sixth'];
                    }
?>

However I also tried to do something like this :

while ($row['positions'] == "first")
        {
        echo $row['first'];
        }
        while ($row['positions'] == "second")
        {
        echo $row['second'];    
        }
        while ($row['positions'] == "third")
        {
        echo $row['third'];    
        }
        while ($row['positions'] == "fourth")
        {
        echo $row['fourth'];    
        }
        while ($row['positions'] == "fifth" || "sixth")
        {
        echo "$row['fifth']<br>";
        echo $row['sixth'];
        }

I'd expect thi to check if the row contains the string and come back true however only the last with fifth and sixth shows. is there a better way to do this?

Aucun commentaire:

Enregistrer un commentaire