简体   繁体   中英

Print the value of a variable from an IF/ELSEIF/ELSE statement within another variable's value?

I am trying to print the value of a variable from an IF/ELSEIF/ELSE statement within another variable's value. I need to check for the $organize variable; if set to 1, 2, 3 or 4 it will show a special bullet; otherwise, it will show a default bullet. Regardless, I am trying to insert the value of the variable $bullet into the variable $dataset which will be further manipulated down the road (not shown here).

I tried setting the $dataset variable as shown below in the commented version. It didn't work! Any ideas?

// $dataset will contain array( 'Category1' => array('Item 1', 'Item2'), ... )

$dataset = array();

while($row = mysql_fetch_array($result)) {
    if (!$row['CategoryID']) {
        $row['CategoryName'] = 'Sort Me';
    }

    if (((isset($organize)) && ($organize=="1"))) { $bullet = "<div class=\"bullet1\">"; } 
    elseif (((isset($organize)) && ($organize=="2"))) { $bullet = "<div class=\"bullet2\">"; } 
    elseif (((isset($organize)) && ($organize=="3"))) { $bullet = "<div class=\"bullet3\">"; } 
    elseif (((isset($organize)) && ($organize=="4"))) { $bullet = "<div class=\"bullet4\">"; } 
    else  { $bullet = "<div class=\"bullet0\">"; }

    // $dataset[$row['CategoryName']][] = 'print $bullet . "<a target=\"_blank\" href=\"" . $row['ItemAddress'] . "\">" . $row['ItemTitle'] . "</a>" . $row['ItemNote'] . "</div>"';
    $dataset[$row['CategoryName']][] = "<div class=\"bullet0\"><a target=\"_blank\" href=\"" . $row['ItemAddress'] . "\">" . $row['ItemTitle'] . "</a>" . $row['ItemNote'] . "</div>";

    $num_articles++;
}

Try this:

$dataset[$row['CategoryName']][] = $bullet . "<a target=\"_blank\" href=\"" . $row['ItemAddress'] . "\">" . $row['ItemTitle'] . "</a>" . $row['ItemNote'] . "</div>"';

Furthermore, you can clean up you if/elseif/else to show this:

if (isset($organize)) {
    $org_num = (int) $organize;
    $bullet = "<div class=\"bullet{$org_num}\">";
} else {
    $bullet = "<div class=\"bullet0\">";
}

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