简体   繁体   中英

PHP: If MYSQL Result greater than 0, add to array?

I have a MYSQL table with a list of services that user's provide:

The Enum values in these columns can be 0 or 1. 0 represents a service not offered and 1 represents a service offered.

Cleaning   Tour Guide    Cooking    Parties
0          1             0          1

I am then running the following query In MYSQL to fetch my rows in the table:

 <?php $myBio = $conn->query("SELECT * FROM user_data, user_services WHERE user_data.user_id = user_services.user_id AND user_id = $p_id");
    if ($myBio->num_rows > 0) {
    $row = $myBio->fetch_assoc();?>

I want to generate a list of the services the user provides (where the service has a value greater than 0) - and separate the list with commas like so:

Tour Guide, Parties

I am trying to do this by using an array:

$os = array(if($row['cleaning'] > 0) { echo 'cleaning';}, if($row['tour'] >0) { echo 'Tour Guide'; });

I am trying to use PHP if statements to decipher if a service is 0 or 1 before adding it to my list.

I do not believe it is possible to combine php if statements within an array.

Please can someone show me how I can achieve my desired result? Thanks in advance

Use array_keys() with the optional search parameter:

$services = array_keys($row, 1);

Example:

$row = [
    'Cleaning'   => 0,
    'Tour Guide' => 1,
    'Cooking'    => 0,
    'Parties'    => 1,
];

$services = array_keys($row, 1);

var_export($services);

Result:

array (
  0 => 'Tour Guide',
  1 => 'Parties',
)

Demo

Would something like this achieve what you want?

$results=array();
if ($row['cleaning']>0) $results[]='Cleaning';
if ($row['tour']>0) $results[]='Tour Guide';
// ...

Also, please heed @tadman's comment about prepared statements!

If your database columns have a speaking name, you can do it like this:

<?php
    $arrayServices = array();
    $arrayAllServices = array('Cleaning','Tour Guide','Cooking','Parties');
    foreach($arrayAllServices as $service) {
        if($row[$service] > 0) {
            $arrayServices[] = $service;
        }
    }

    $sServices = join(', ', $arrayServices);

    echo $sServices;

If the speaking names are different from the column names, you need a second array to look up translations.

<?php 
    $myResults = array();
    $myBio = $conn->query("SELECT * FROM user_data, user_services WHERE user_data.user_id = ? AND user_id = ?");
    $stmt->bind_param("ss",$user_services.user_id,$p_id);
    $stmt->execute();
    $result = $stmt->get_result();
    if($result->num_rows === 0) exit('No rows');
    while($row = $result->fetch_assoc()) 
    {
        $tempArray = array();
        $Cleaning = $row['Cleaning'];
        $Tour_Guide = $row['TourGuide'];
        $Cooking = $row['Cooking'];
        $Parties = $row['Parties'];
        if($Cleaning == 1)
            array_push($tempArray,$Cleaning)
        if($Cleaning == 1)
            array_push($tempArray,$Cleaning)
        if($Cooking == 1)
            array_push($tempArray,$Cooking )
        if($Parties == 1)
            array_push($tempArray,$Parties )
        array_push($myResults,$tempArray);
    }
?>

You will then get the myResult array which will be an array of arrays, you can then loop over the sub arrays to check values and construct the strings you intend to make.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM