数据库阵列爆炸

时间:2018-10-19 10:46:43

标签: php

I have this row in a database

,并且我希望能够通过网站上的页面更改每个块(例如[license_civ_driver,1])中的值。可以在数据库here中看到数据库中的完整单元格。

"[[`license_civ_driver`,1],[`license_civ_boat`,0],[`license_civ_pilot`,1],[`license_civ_advpilot`,1],[`license_civ_trucking`,1],[`license_civ_gun`,1],[`license_civ_hunting`,0],[`license_civ_dive`,0],[`license_civ_home`,0],[`license_civ_platinum`,1],[`license_civ_oil`,1],[`license_civ_diamond`,0],[`license_civ_salt`,0],[`license_civ_sand`,0],[`license_civ_iron`,0],[`license_civ_copper`,1],[`license_civ_cement`,0],[`license_civ_rubber`,0],[`license_civ_meth`,0],[`license_civ_cocaine`,0],[`license_civ_heroin`,0],[`license_civ_marijuana`,0],[`license_civ_rebel`,0],[`license_civ_advrebel`,0]]"

(我已经设置了所有数据库连接,我只需要查询和PhP代码即可完成操作)。

我还希望将每个块都翻译成文本(例如,license_civ_driver将是驾驶执照)。

此外,我想将每个块显示为div,并根据块中的数字进行背景更改。

我知道这可能是一个很大的问题,但是我已经尽我所知尝试了所有事情,包括使用网站来帮助我自己做到这一点(this is my attempt)。如果有人可以帮助我做到这一点,我将非常感激。如果有人想为我做,那么我将不胜感激,但是显然我想学习,所以我希望得到帮助。

谢谢!

1 个答案:

答案 0 :(得分:1)

我在您的代码中注意到,您在一个循环中包含许多文件-在其他循环中包含一些文件...这就是我要说的NOT方法。另外,我认为说"I'll sort that ( sql injection ) out later"并考虑"...obviously I want to learn so I'd rather be helped."是错误的 最好早点采用良好做法,正确地做,然后不必回头。

那是说,也许基于问题中的注释,而不是对php进行深入研究,以下内容可能会有用。


更新: 阅读您的评论后,我添加了一些草率编写的javascript代码(并对php生成的html进行了较小的更改)-并没有简单的点击就可以对其进行测试,但是应该提供一种机制,您可以通过该机制来实现所述目标。使用该请求触发一些PHP代码(同一页面或其他脚本...)-验证发送的参数/数据,构造sql prepare语句并执行。

/* some rudimentary styles for demo purposes */
echo "
<style>
    .class_low{padding:1rem;width:100%;background:rgba(255,0,0,0.25);}
    .class_medium{padding:1rem;width:100%;background:rgba(0,0,255,0.25);}
    .class_high{padding:1rem;width:100%;background:rgba(0,255,0,0.25);}
    .unknown{padding:1rem;width:100%;background:yellow}}
</style>
<script>
    const buildparams=function(p){
        if( p && typeof( p )==='object' ){
            p=Object.keys( p ).map(function( k ){
                return typeof( p[ k ] )=='object' ? buildparams( p[ k ] ) : [ encodeURIComponent( k ), encodeURIComponent( p[ k ] ) ].join('=')
            }).join('&');
        }
        return p;
    };

    const ajax=function(url,params,callback){
        let xhr=new XMLHttpRequest();
        xhr.onload=function(){
            if( this.status==200 && this.readyState==4 )callback.call( this, this.response )
        };
        xhr.onerror=function(e){
            alert(e)
        };
        xhr.open( 'POST', url, true );
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xhr.setRequestHeader('X-Requested-With','XMLHttpRequest');
        xhr.send( buildparams( params ) );
    };

    const evtcallback=function(r){
        alert(r);
    };

    const evtbindclicks=function(div){
        div.addEventListener( 'click', evtclickhandler, false );
    };

    const evtclickhandler=function(e){
        let url=document.location.href;
        let params={ licence:this.innerHTML, value:this.dataset.value };

        ajax.call( this, url, params, evtcallback );
    };

    document.addEventListener('DOMContentLoaded',function(){
        Array.prototype.slice.call( document.querySelectorAll('.class_low, .class_medium, .class_high, .unknown') ).forEach( evtbindclicks );
    },false );

</script>";

/* a lookup object to choose correct style based upon integer value */
$matrix=array(
    0   =>  'class_low',
    1   =>  'class_medium',
    2   =>  'class_high'
);

/* fudge around with the database column data. It would be so much easier if the data were originally json in the db!  */
function makejson( $colval, $arr ){
    $colval=str_replace( array( '"', '`','_' ),array('',"#",' ' ), $colval );
    return json_decode( str_replace( $arr,'', str_replace( '#', '"', $colval ) ) );
}


/* words to replace in data */
$arrchrs=array('civ','license');

/* source data - ie: column data from db */
$colval="[[`license_civ_driver`,1],[`license_civ_boat`,2],[`license_civ_pilot`,15],[`license_civ_advpilot`,0],[`license_civ_trucking`,1],[`license_civ_gun`,1],[`license_civ_hunting`,0],[`license_civ_dive`,0],[`license_civ_home`,0],[`license_civ_platinum`,1],[`license_civ_oil`,1],[`license_civ_diamond`,0],[`license_civ_salt`,0],[`license_civ_sand`,0],[`license_civ_iron`,0],[`license_civ_copper`,1],[`license_civ_cement`,0],[`license_civ_rubber`,0],[`license_civ_meth`,0],[`license_civ_cocaine`,0],[`license_civ_heroin`,0],[`license_civ_marijuana`,0],[`license_civ_rebel`,0],[`license_civ_advrebel`,0]]";

/* attempt to make the above usable */
$json=makejson( $colval, $arrchrs );




/* process the data, create a new div per item and assign class using matrix */
foreach( $json as $arr ){
    $class = isset( $matrix[ $arr[1] ] ) ? $matrix[ $arr[1] ] : 'unknown';
    $name = $arr[0];

    printf( '<div class="%s" data-value="%d">%s Licence</div>', $class, $arr[1], ucwords( $name ) );
}

这将产生如下内容: example output

相关问题