使用具有开关功能的foreach

时间:2015-02-03 06:24:17

标签: php mysql arrays

我的数据库中有一个表从网站返回的部分问题没有返回数据库中的第一个注册表这里是代码:

$mysqli = $conexion_temporal; //connect to mysqli
$queryn = $mysqli->query('select * from sections'); //we get the content
$dataset = array(); //we declare an array
while ($data = $queryn->fetch_array()) {    
   $dataset['seo'] = $data['seo'];
 }



switch ($pagina) {  //switch the sections
    case 'compra_completa':
        echo $plantilla->SuccessPurchase();
        break;
    case $dataset['seo']: //the seo column of the database
        echo $plantilla->album($dataset['seo'],$subcat);
        $subcat = $_GET['subcat']; //if we request category we show ..
        break;
    }

问题是没有获得数据库的第一列 http://prntscr.com/60gjzd

编辑:仅适用于最后插入的数据

4 个答案:

答案 0 :(得分:1)

当有单行时,尝试使用MYSQLI_ASSOC常量到fetch_array()函数,如下所示:

while ($data = $queryn->fetch_array(MYSQLI_ASSOC)) {    
   $dataset[] = $data['seo'];
 }

对于多个部分的切换案例,请使用foreach:

foreach ($dataset as $key => $value) {
    switch ($key) {
        case $value : 
            // do something
            break ;

     }
 }

答案 1 :(得分:1)

$ dataset [' seo']将始终被$ data [' seo']覆盖,这就是为什么你只获得最后插入的数据

$dataset['seo'] = $data['seo'];

如果您需要将所有数据存储到数据集,则应使用array_push

while ($data = $queryn->fetch_array()) {    
    array_push($dataset,$data['seo']);
}

根据我的理解,这些是你想要的吗?

$mysqli = $conexion_temporal; //connect to mysqli
$queryn = $mysqli->query('select * from sections'); //we get the content
$dataset = array(); //we declare an array

//find section
$matched_section = false;

while ($data = $queryn->fetch_array()) {
    array_push($dataset,$data['seo']);

    //if section match store as $matched_section
    if($data['seo'] == $pagina)
        $matched_section = $data['seo'];
}

if($pagina == 'compra_completa'){
    echo $plantilla->SuccessPurchase();
}else if( matched_section ){
    echo $plantilla->album(matched_section,$subcat);
    $subcat = $_GET['subcat']; //if we request category we show ..
}else{
    ........
}

答案 2 :(得分:0)

while ($data = $queryn->fetch_array(MYSQLI_ASSOC)) {    
   $dataset['seo'] = $data['seo'];
 }

这会一直用它读取的每个值重写$ dataset ['seo'],直到它在最后一个值结束。

要么你可以像之前的建议那样使用array_push,然后再为你的交换机再次读取它,或者你可以将它全部放在第一个循环中,如下所示:

    while ($data = $queryn->fetch_array()) {    
    switch ($pagina) {  //switch the sections
        case 'compra_completa':
            echo $plantilla->SuccessPurchase();
            break;
        case $data['seo']: //the seo column of the database
            echo $plantilla->album($data['seo'],$subcat);
            $subcat = $_GET['subcat']; //if we request category we show ..
            break;
        }
}

这将为列seo上的每一行执行你的开关块,如果这就是你所要求的。

答案 3 :(得分:0)

在您的下方,我们会发现您的代码经过一些编辑,以使其有效。

$mysqli = $conexion_temporal; //connect to mysqli
$queryn = $mysqli->query('select * from sections'); //we get the content
$dataset = array(); //we declare an array

// I made a fix here because the $data['seo'] was not
// added to the array correctly.
// I also added the MYSQLI_ASSOC parameter
while ($data = $queryn->fetch_array(MYSQLI_ASSOC)) {    
   $dataset[] = $data['seo'];
}

// I added a foreach to loop through all your 'paginas'
foreach ($dataset as $pagina) {
    switch ($pagina) {  //switch the sections
        case 'compra_completa':
            echo $plantilla->SuccessPurchase();
            break;
        case $dataset['seo']: //the seo column of the database
            echo $plantilla->album($dataset['seo'],$subcat);
            $subcat = $_GET['subcat']; //if we request category we show ..
            break;
    }
}