对于elseif条件中的每个循环不起作用

时间:2015-08-24 05:50:51

标签: php

在我的索引文件项目中,我有以下代码。

<?php
include "app/config.php";
$browser_t="web";

if ($page_name=='') {
    include $browser_t.'/index.php';
}
elseif ($page_name=='index.php') {
    include $browser_t.'/index.php';
}
elseif ($page_name=='about?aboutus=aboutus') {
    include $browser_t.'/about/index.php';
}
elseif ($page_name=='about?aboutus=aboutus&aboutus_id=1') {
    include $browser_t.'/about/index.php';
}
elseif ($page_name=='about?aboutus=aboutus&aboutus_id=2') {
    include $browser_t.'/about/index.php';
}
elseif ($page_name=='about?aboutus=aboutus&aboutus_id=3') {
    include $browser_t.'/about/index.php';
}
elseif ($page_name=='about?aboutus=aboutus&aboutus_id=4') {
    include $browser_t.'/about/index.php';
}
elseif ($page_name=='about?aboutus=aboutus&aboutus_id=5') {
    include $browser_t.'/about/index.php';
}
?>

我想让它充满活力。所以,我正在使用以下查询。

<?php
include "app/config.php";
$browser_t="web";

if ($page_name=='') {
    include $browser_t.'/index.php';
}
elseif ($page_name=='index.php') {
    include $browser_t.'/index.php';
}
elseif ($page_name=='about?aboutus=aboutus') {
    include $browser_t.'/about/index.php';
}
elseif ($query="SELECT id FROM aboutus_tbl";
$result=mysql_query($query);
    foreach ($result as $value) {
        echo $page_name=='about?aboutus=aboutus&aboutus_id='.$value;
    }) {  
    include $browser_t.'/about/index.php';
}

它显示错误,我不知道怎么做?如何在这里使用foreach循环或while循环?请帮帮我。

<?php 

include "app/config.php";
$browser_t="web";

if ($page_name=='') {
    include $browser_t.'/index.php';
}
elseif ($page_name=='index.php') {
    include $browser_t.'/index.php';
}
elseif ($page_name=='about?aboutus=aboutus') {
    include $browser_t.'/about/index.php';
}
elseif( mysql_num_rows( $result =   mysql_query( "SELECT id FROM aboutus_tbl;" ) ) > 0 )
{
    $result_data    =   mysql_fetch_row($result);
    foreach($result_data as $value) {
        $page_name ='about?aboutus=aboutus&aboutus_id='.$value;
    }  
    include $browser_t.'/about/index.php';    
}
elseif ($page_name=='courses?cosoff=cosoff') {
    include $browser_t.'/courses/index.php';
    }
elseif( mysql_num_rows( $result2 =   mysql_query( "SELECT id FROM courses;" ) ) > 0 )
{
    $result_data2    =   mysql_fetch_row($result2);
    foreach($result_data2 as $value) {
        $page_name ='courses?cosoff=cosoff&cors_id='.$value;
    }  
    include $browser_t.'/courses/index.php';    
}
elseif ($page_name=='admission?admi=admi') {
    include $browser_t.'/admission/index.php';
    }
elseif( mysql_num_rows( $result3 =   mysql_query( "SELECT id FROM admission;" ) ) > 0 )
{
    $result_data3    =   mysql_fetch_row($result3);
    foreach($result_data3 as $value) {
        $page_name ='admission?admi=admi&admi_id='.$value;
    }  
    include $browser_t.'/admission/index.php';    
}
elseif ($page_name=='facelities?face=face') {
    include $browser_t.'/facelities/index.php';
    }
elseif( mysql_num_rows( $result4 =   mysql_query( "SELECT id FROM facelities;" ) ) > 0 )
{
    $result_data4    =   mysql_fetch_row($result4);
    foreach($result_data4 as $value) {
        $page_name ='facelities?face=face&face_id='.$value;
    }  
    include $browser_t.'/facelities/index.php';    
}
elseif ($page_name=='events?eve=eve') {
    include $browser_t.'/events/index.php';
    }
elseif( mysql_num_rows( $result5 =   mysql_query( "SELECT id FROM events;" ) ) > 0 )
{
    $result_data5    =   mysql_fetch_row($result5);
    foreach($result_data5 as $value) {
        $page_name ='avrodh.php?eve=eve&evt_id='.$value;
    }  
    include $browser_t.'/events/index.php';    
}
elseif ($page_name=='career.php?car=car') {
    include $browser_t.'/career.php';
    }
elseif ($page_name=='contact.php?con=con') {
    include $browser_t.'/contact.php';
    }
elseif ($page_name=='config.php') {
    include $browser_t.'/config.php';
    }
elseif ($page_name=='campus_tour.php?nohead=nohead') {
    include $browser_t.'/campus_tour.php';
    }
elseif ($page_name=='gallery.php?nohead=nohead') {
    include $browser_t.'/gallery.php';
    }
elseif ($page_name=='stud_corner.php?nohead=nohead') {
    include $browser_t.'/stud_corner.php';
    }
elseif ($page_name=='404.php') {
    include $browser_t.'/404.php';
}
else
{
    include $browser_t.'/404.php';
}

?>

5 个答案:

答案 0 :(得分:1)

1)您需要在if条件之外写入查询。

2)$result=mysql_query($query)不用于从数据库中获取数据!!。

3)停止使用mysql,不推荐使用它。您可以使用mysqli或PDO

4)检查页面使用错误

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

使用

更改代码
<?php
include "app/config.php";
$browser_t="web";
$query="SELECT id FROM aboutus_tbl";
$result=mysql_query($query);
$row = mysql_fetch_assoc($result);

 $page_name1=='about?aboutus=aboutus&aboutus_id='.$row['id'];// assing to anothere variable

if ($page_name=='') {
    include $browser_t.'/index.php';
}
elseif ($page_name=='index.php') {
    include $browser_t.'/index.php';
}
elseif ($page_name=='about?aboutus=aboutus') {
    include $browser_t.'/about/index.php';
}
elseif ($page_name==$page_name1) {  
    include $browser_t.'/about/index.php';
}

答案 1 :(得分:0)

switch ($page){
  case ($page ==''):
  case ($page) == 'index.php':
    include $browser_t.'/index.php';

    break;
  // an other case 
  default:
    // add your query here
    $result =   mysql_query( "SELECT id FROM  aboutus_tbl;"
    if( mysql_num_rows($result)> 0 ){
      $result_data = mysql_fetch_row($result);
      foreach($result_data as $value) {
        echo $page_name ='about?aboutus=aboutus&aboutus_id='.$value;
     }  
     include $browser_t.'/about/index.php'; 
    break; 
   }
希望这有帮助。

答案 2 :(得分:0)

你的问题在这里

elseif ($query="SELECT id FROM aboutus_tbl";
$result=mysql_query($query);
    foreach ($result as $value) {
        echo $page_name=='about?aboutus=aboutus&aboutus_id='.$value;
    }) {  
    include $browser_t.'/about/index.php';
}

我不确定您在该代码中尝试实现的目标,但您无法在elseif的括号之间进行SQL查询和foreach循环。您唯一可以拥有的是要评估为TRUEFALSE的表达式。

答案 3 :(得分:0)

<?php
include "app/config.php";
$browser_t="web";

$string = "";
$pg = "page_name";
$query =  "select id from task_assign";//your query goes here......
$result=mysql_query($query);
while($value = mysql_fetch_array($result)) 
{
        $string .= "else if($$pg == 'about?aboutus=aboutus&aboutus_id='.$value[id])
        {
            include $browser_t.'/about/index.php';
        }<br>";
}

if ($page_name=='') {
    include $browser_t.'/index.php';
}
elseif ($page_name=='index.php') {
   include $browser_t.'/index.php';
}
elseif ($page_name=='about?aboutus=aboutus') {
   include $browser_t.'/about/index.php';
}
echo $string

答案 4 :(得分:0)

它像.......

一样完成
<?php 
include "app/config.php";
$browser_t="web";

if ($page_name=='') {
    include $browser_t.'/index.php';
}
elseif ($page_name=='index.php') {
    include $browser_t.'/index.php';
}
elseif ($page_name=='about?aboutus=aboutus') {
    include $browser_t.'/about/index.php';
}
elseif( mysql_num_rows( $result =   mysql_query( "SELECT id FROM aboutus_tbl;" ) ) > 0 && strpos($page_name,'about?aboutus=aboutus&aboutus_id=') !== false)
{
    $result_data    =   mysql_fetch_row($result);
    foreach($result_data as $value) {
        $page_name ='about?aboutus=aboutus&aboutus_id='.$value;
    }  
    include $browser_t.'/about/index.php';    
}
elseif ($page_name=='courses?cosoff=cosoff') {
    include $browser_t.'/courses/index.php';
    }
elseif( mysql_num_rows( $result =   mysql_query( "SELECT id FROM courses;" ) ) > 0 && strpos($page_name,'courses?cosoff=cosoff&cors_id=') !== false)
{
    $result_data    =   mysql_fetch_row($result);
    foreach($result_data as $value) {
        $page_name ='courses?cosoff=cosoff&cors_id='.$value;
    }  
    include $browser_t.'/courses/index.php';    
}
elseif ($page_name=='admission?admi=admi') {
    include $browser_t.'/admission/index.php';
    }
elseif( mysql_num_rows( $result =   mysql_query( "SELECT id FROM admission_tbl;" ) ) > 0 && strpos($page_name,'admission?admi=admi&admi_id=') !== false)
{
    $result_data    =   mysql_fetch_row($result);
    foreach($result_data as $value) {
        $page_name ='admission?admi=admi&admi_id='.$value;
    }  
    include $browser_t.'/admission/index.php';    
}
elseif ($page_name=='facelities?face=face') {
    include $browser_t.'/facelities/index.php';
    }
elseif( mysql_num_rows( $result =   mysql_query( "SELECT id FROM facelities;" ) ) > 0 && strpos($page_name,'facelities?face=face&face_id=') !== false)
{
    $result_data    =   mysql_fetch_row($result);
    foreach($result_data as $value) {
        $page_name ='facelities?face=face&face_id='.$value;
    }  
    include $browser_t.'/facelities/index.php';    
}
elseif ($page_name=='events?eve=eve') {
    include $browser_t.'/events/index.php';
    }
elseif( mysql_num_rows( $result =   mysql_query( "SELECT id FROM event_content;" ) ) > 0 && strpos($page_name,'avrodh.php?eve=eve&evt_id=') !== false)
{
    $result_data    =   mysql_fetch_row($result);
    foreach($result_data as $value) {
        $page_name ='avrodh.php?eve=eve&evt_id='.$value;
    }  
    include $browser_t.'/events/avrodh.php';    
}
elseif ($page_name=='career.php?car=car') {
    include $browser_t.'/career.php';
    }
elseif ($page_name=='contact.php?con=con') {
    include $browser_t.'/contact.php';
    }
elseif ($page_name=='config.php') {
    include $browser_t.'/config.php';
    }
elseif ($page_name=='campus_tour.php?nohead=nohead') {
    include $browser_t.'/campus_tour.php';
    }
elseif ($page_name=='gallery.php?nohead=nohead') {
    include $browser_t.'/gallery.php';
    }
elseif ($page_name=='stud_corner.php?nohead=nohead') {
    include $browser_t.'/stud_corner.php';
    }
elseif ($page_name=='404.php') {
    include $browser_t.'/404.php';
    }
else
    {
        include $browser_t.'/404.php';
    }

?>