如何从 MySQL 查询创建动态 PHP switch 语句 [英] How to create a dynamic PHP switch statement from MySQL query

查看:30
本文介绍了如何从 MySQL 查询创建动态 PHP switch 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是为了将电子邮件中的图像、类别和评论添加到我正在处理的照片库中.

This is for adding images, categories and comments from an email to a photo gallery I'm working on.

我正在从电子邮件正文中提取前两个找到的类别名称,并且需要将类别 ID 分配给 $cat 变量以插入到数据库中.

I'm pulling the first two found category names out of an email message body and need to assign the category id to $cat variable for insertion into DB.

我让它在硬编码中工作得很好,但想让它动态化,以便对类别名称的更改将贯穿始终.

I have it working fine hardcoded, but would like to make it dynamic so changes to category names will cascade throughout.

我想更改的 switch 语句:

The switch statement I'd like to change:

硬编码:

foreach($foundKeywords as $key => $value) {  
            if($key<=1){

switch($value)
        {
            case 'family':
            $cat='3';
            break;
            case 'friends';
            $cat='4';
            break;
            case 'fun';
            $cat='6';
            break;
            case 'places';
            $cat='5';
            break;
            case 'general';
            $cat='2';
            break;
            case 'henry';
            $cat='7';
            break;
            default;
            $cat='2';
        }

我想从 MySQL 表中提取类别并这样做:

I'd like to pull the categories from a MySQL table and do it this way:

$res = mysql_query('SELECT * FROM gallery_category');
            $cat_switch_list = "";
            while($row = mysql_fetch_array($res)){
                $cat_switch_list .= "case '".$row[1]."':
                                    $cat = ".$row[0].";
                                    break;";
            }
            //////////////////////
            foreach($foundKeywords as $key => $value) {  
                if($key<=1){


                    switch($value)
                    {
                        echo $cat_switch_list;
                        default:
                        $cat='1';
                    }

理论上,我认为这应该可行,但有些地方不对.

In theory, I believe this should work, but something is not right.

有什么建议吗?

推荐答案

最好还是直接使用 mysql 查询:

You'd be better of to either use mysql directly for querying:

mysql_query('select id from gallery_category where name=\''.mysql_real_escape($name).'\'');

或者通过从您的结果生成一个数组:

or by generating an array from your result:

$result = mysql_query('select * from gallery_category');
$categories = array();
while($row = mysql_fetch_num($result))
{
  $categories[$row[1]] = $row[0];
}

然后您只需在类别数组中查找即可找到正确的 id:$id = $categories[$name].

Then you can find the correct id simply be looking it up in your categories array: $id = $categories[$name].

如果你想生成代码并且看起来像,你可以使用 var_export() 将 $categories 变量保存到真正的 php 代码中.您要生成代码吗?

If you want to generate code and it looks like it, you can use var_export() to save the $categories variable to real php code. Do you want to generate code?

这篇关于如何从 MySQL 查询创建动态 PHP switch 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆