简体   繁体   English

如何基于一些数据动态添加字体真棒图标

[英]How to add font awesome icon dynamically based on some data

Is it possible to change/add a font awesome icon based on the fetched data? 是否可以根据获取的数据更改/添加字体超赞图标? If the type is .pdf the font awesome will be <i class="fa fa-file-pdf-o"> else if type is .docx it will be <i class="fa fa-file-word-o"></i> . 如果type.pdf的字体真棒将<i class="fa fa-file-pdf-o">否则,如果type.docx这将是<i class="fa fa-file-word-o"></i> Can someone give me ideas how to do it? 有人可以给我想法怎么做吗? What will i use? 我会用什么? Javascript or PHP? JavaScript还是PHP?

here's some of my code. 这是我的一些代码。

 <div class="col-sm-6 col-md-6">
    <?php
              $sql ="SELECT * FROM lectures WHERE subj_descr = '$subj'";
               $result = mysqli_query($con, $sql);


               while($row = mysqli_fetch_array($result)){
                $file = $row['file'];
                $type = $row['type'];

        ?>

    <a style="display:block; margin-top:5px;" href="uploads/<?php echo $file ?>" target="_blank"><i class="fa fa-file-word-o"></i> <?php echo $file; ?></i></a>

         <?php
          }  
         ?>
    </div>

Try something like this: 尝试这样的事情:

<?php
if($type = $row['type'] == 'pdf')   // make <i> tag on behalf of condition
{
    echo '<i class="fa fa-file-pdf-o">'. your text here .'</i>';
}
else if($type = $row['type'] == 'txt')
{
    echo '<i class="fa fa-file-word-o">'. your text here .'</i>';
}
?>

First of all, best practice is to separate your presentation from your logic. 首先,最佳实践是将您的演示文稿与逻辑分开。 Having a mysql query right smack in the middle of your HTML is not a good idea. 在HTML中间插入mysql查询权限不是一个好主意。 Instead put your query before your HTML and loop over the results in the HTML. 而是将查询放在HTML之前,然后遍历HTML中的结果。

Put something like this before your HTML: 在HTML之前放置以下内容:

$files = [];
while($row = mysqli_fetch_array($result)){
    $file['filename'] = $row['file'];
    $file['type'] = $row['type'];

    switch($file['type']) {
        case 'pdf':
            $file['fa'] = 'fa-file-pdf-o';
            break;
        case 'docx':
        case 'doc':
            $file['fa'] = 'fa-file-word-o';
            break;
        default:
            throw new Exception('Invalid file type'); // Handle this how you want
    }
    $files[] = $file;
}

Then within your HTML loop over the result: 然后在HTML循环中遍历结果:

<div class="col-sm-6 col-md-6">
    <?php
        foreach ($files as $file) {
            ?>
            <a style="display:block; margin-top:5px;" href="uploads/<?php echo $file['filename'] ?>" target="_blank">
                <i class="fa <?php echo $file['fa'];?>"></i> <?php echo $file['filename']; ?></i>
            </a>
            <?php
        }
    ?>
</div>

You can also use a switch here. 您也可以在此处使用开关。

switch(strtolower($row['type'])){
    case 'pdf':
        $icon = "fa-file-pdf-o";
        break;
    default:
        $icon = "fa-file-word-o";
        break;
}

echo "<i class='fa ". $icon ."'>". your text here ."</i>";

Note: You can use a variable value to your text as well. 注意:您也可以在文本中使用变量值。 Which will then also go inside the switch case. 然后它们也将进入开关盒内部。

you can also do like this.put less code in html. 您也可以像this.put那样减少html中的代码。

<?php
$row['type']=='pdf'?'pdf':'txt';
?>
<i class="fa fa-file-<?=$row['type']?>-o"></i>;

Add the following function to your page. 在页面上添加以下功能。 If you need to add additional file types/icons you can add more elements to the array $fa in "new_file_type" => "fa-icon-name" . 如果需要添加其他文件类型/图标,则可以在"new_file_type" => "fa-icon-name"向数组$ fa添加更多元素。 Just don't forget the commas . 只是不要忘记逗号。 Arrays 数组

<?php
    function getFa($type){

        $fa = array(
            "pdf" => "fa-file-pdf-o",
            "docx" => "fa-file-word-o"
            );

        return isset($fa[$type]) ? $fa[$type] : '';
    }
?>

Then in the HTML it can be used like this: 然后在HTML中可以这样使用:

<i class="fa <?php echo getFa($type); ?>"></i> <?php echo $file; ?></i>

The function is not necessary but it'll prevent errors in case the $type variable is empty or the file type is not contained in the array. 该函数不是必需的,但是在$ type变量为空或数组中不包含文件类型的情况下,它将防止错误。

It is also possible to just create an array and call it from your code if you're certain that the file type will always exist and it will have an element associated in the array. 如果您确定文件类型将始终存在并且在数组中具有关联的元素,则也可以只创建一个数组并从代码中调用它。

Example: 例:

<?php
    $fa = array(
        "pdf" => "fa-file-pdf-o",
        "docx" => "fa-file-word-o"
        );
?>

In HTML: 在HTML中:

<i class="fa <?php echo $fa[$type]; ?>"></i> <?php echo $file; ?></i>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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