PHP开关-仅查看一种情况?

PHP开关-仅查看一种情况?,php,joomla,switch-statement,case,Php,Joomla,Switch Statement,Case,目前,我有一个表单,它有两个选择选项,一个可以查看案例0,另一个可以查看案例4 我有一个标签,所以我不能使用表单,因为当我选择一个选项时,网站会刷新。然后显示两个选项卡中的选项 我试着只有一个选择。但这对我不起作用 这是选项卡/表单代码: <div class="tab1"> <form action="<?php echo JRoute::_('index.php'); ?>" method="post"> <input type="hidden"

目前,我有一个表单,它有两个选择选项,一个可以查看案例0,另一个可以查看案例4

我有一个标签,所以我不能使用表单,因为当我选择一个选项时,网站会刷新。然后显示两个选项卡中的选项

我试着只有一个选择。但这对我不起作用

这是选项卡/表单代码:

<div class="tab1">
<form action="<?php  echo JRoute::_('index.php'); ?>" method="post">
<input type="hidden" name="option" value="com_yoflash" />
<input type="hidden" name="view" value="category" />
<input type="hidden" name="mochicat" value="<?php echo $this->cat->catid; ?>" />
<select name="order" size="1" onchange="submit();">
  <option value="0" <?php if($this->order=="0") echo "selected=\"selected\""; ?>><?php echo JText::_('ORDER_DATE_NEWEST'); ?></option>
  <option value="4" <?php if($this->order=="4") echo "selected=\"selected\""; ?>><?php echo JText::_('ORDER_POPULAR'); ?></option>
</select>
<input type="hidden" name="Itemid" value="<?php echo $this->Itemid; ?>" />
</form>
</div>

EWW,开关有点难看,这里有一个更好的版本:

$sortMap = array(
"ORDER BY date_added DESC",
"ORDER BY date_added ASC",
"ORDER BY name ASC",
"ORDER BY name DESC",
"ORDER BY stat_clicked DESC"
);

$str = isset($sortMap[$order]) 
    ? $sortMap[$order]
    : "ORDER BY date_added ASC";
由于$order在0到4之间,您还可以利用它将映射到数组索引这一事实,避免丑陋的切换,并在这个过程中为自己节省大量浪费、杂乱的行


注意:如果$type不符合正常的数字数组索引,您也可以使用关联数组执行此操作,您只需在排序映射数组中手动指定每个元素中的数组索引,如“cat”=>“ORDER BY…”,它的工作方式应该相同。

除了我上面的答案之外,我想您还需要显示一个[select]html下拉列表,您可以在其中“选择”排序顺序,这将从数组中提取适当的SQL并使用它。所以我的建议是这样的

$sortIndex = $_POST["sort"];

$sortMap = array(
"ORDER BY date_added DESC",
"ORDER BY date_added ASC",
"ORDER BY name ASC",
"ORDER BY name DESC",
"ORDER BY stat_clicked DESC"
);
$sortDesc = array(
"Sort by date added in descending order",
"Sort by date added in ascending order",
"Sort by name in ascending order",
"Sort by name in descending order",
"Sort by number of clicks in desending order"
);

$sortType = isset($sortMap[$sortIndex]) 
    ? $sortMap[$sortIndex] 
    : "ORDER BY date_added ASC";

<select name="sort">
    <?php foreach($sortDesc as $k=>$text): ?>
    <option value="<?=$k?>" <?=$sortType==$k?"selected='selected'":""?> ><?=$text?></option>
    <?php endforeach?>
</select>
$sortIndex=$\u POST[“排序”];
$sortMap=array(
“按日期订购\u添加说明”,
“按日期订购\u添加ASC”,
“按名称订购ASC”,
“按名称订购描述”,
“按状态下单\u单击说明”
);
$sortDesc=数组(
“按降序添加的日期排序”,
“按日期按升序添加排序”,
“按名称升序排序”,
“按名称降序排序”,
“按删除顺序中的单击次数排序”
);
$sortType=isset($sortMap[$sortIndex])
? $sortMap[$sortIndex]
:“订单按日期添加ASC”;

嗯,这就是我想要的

首先,我正在编辑一个组件。最初,它是一个表单,用于查看我希望查看游戏的顺序列表

但是我想要一个标签菜单。一个是新游戏,另一个是流行游戏

所以我创建了标签菜单/系统。然后把代码添加到每个div中,这样它们就有了相同的代码和表单。但当我在ex.tab 1(新游戏)中并从所选列表中按日期顺序选择时。它可以观看所有最新的游戏。但是当我打开其他标签(热门游戏)时,它就变成了,查看最新的游戏。它就像是一个互相继承的人

以下是选项卡的全部代码:

    <div class="tab">
    <h2><a name="newgames" id="newgames">a</a></h2>

<form action="<?php  echo JRoute::_('index.php'); ?>" method="post">
<input type="hidden" name="option" value="com_yoflash" />
<input type="hidden" name="view" value="category" />
<input type="hidden" name="mochicat" value="<?php echo $this->cat->catid; ?>" />
<select name="order" size="1" onchange="submit();">
<option value="0" <?php if($this->order=="0") echo "selected=\"selected\""; ?>><?php echo JText::_('ORDER_DATE_NEWEST'); ?></option>
<option value="4" <?php if($this->order=="4") echo "selected=\"selected\""; ?>><?php echo JText::_('ORDER_POPULAR'); ?></option>
</select>
<input type="hidden" name="Itemid" value="<?php echo $this->Itemid; ?>" />
</form>



 <?php
///////////////////////////////////////
// GAMES BOXES START
///////////////////////////////////////
  $db =& JFactory::getDBO();

  for($i=0;$i<count($this->games);$i++) {
  $game=$this->games[$i];
  $slug=$game->slug;

  $game->imgname=$this->params->get('dir_swf').$slug.'/'.$game->imgname;
  $game->description=$db->getEscaped($game->description);
  $game->description=str_replace("\\\"","\'",$game->description);
  $game->namett=$db->getEscaped($game->name);
  ?>

<div class="game" style="display:block;">

<a style="border:0;" href="<?php echo JRoute::_('index.php/play?option=com_yoflash&view=game&id='.$slug.'&Itemid='.$this->Itemid); ?>" onmouseover="Tip('<strong><?php echo $game->namett; ?></strong><br/> <?php echo $game->description; ?>',WIDTH,300,FADEIN,0,DELAY,0,BGCOLOR, '<?php echo $this->params->get('ttip_bgcolor'); ?>', BORDERCOLOR,'<?php echo $this->params->get('ttip_bordercolor'); ?>',FONTCOLOR,'<?php echo $this->params->get('ttip_fontcolor'); ?>',FONTSIZE,'<?php echo $this->params->get('ttip_fontsize'); ?>',FONTFACE,'<?php echo $this->params->get('ttip_fontface'); ?>')" onmouseout="UnTip()">
<img src="<?php echo $game->imgname; ?>" width="71" height="56" alt="<?php echo $game->name; ?>"/><br/>
<div class="gameName"><?php echo "<p>$game->name</p>"; ?></div>
</a>

<span class="info"><?php echo $game->description; ?></span>

 <span class="gamePlayed"><?php echo "<p>$game->stat_clicked <br/></p>".JText::_("<p> plays</p>") ;?></span> 

 </div>

在此选项卡(最新游戏)中,我只想查看最新游戏

另一个标签(流行游戏)看起来一模一样


希望描述更多?

确切的问题是什么?有两种选择吗?第二种选择取决于第一种选择?如果用户第一次选择某个内容,web应用程序应该更新第二个?请重新表述您的问题。此答案不适用于该问题。不过,从技术上来说,我喜欢你的建议。谢谢,但是除了有一个表单可供选择之外,我该如何查看该顺序?是的,我意识到我实际上没有回答这个问题,只是清理了代码:/I我只是添加了一个新的答案,考虑到你提出的问题,我添加了一个html选择和一种从数组中选择适当SQL的方法,这样更好吗?在别人说之前,您可以通过放置一个数组(“按名称排序DESC”,“按名称按降序排序”)并使用[0]和[1]将$sortMap与$sortDesc组合起来数组索引表示您是要使用sql还是文本描述。我想现在发生的是,您发送到数据库的排序参数对于两个选项卡都是相同的,因此这两个选项卡中的信息以相同的方式排序。所以你需要的是两个排序参数,比如$sort\u new和$sort\u popular,并使用相同的逻辑分别设置它们,但是从$u GET中的不同参数中读取,这有意义吗?是的,但我的php不是很好,所以我不知道如何编写……(好吧,在我的上一个回答中,我把$sort POST[“sort”]也许您可以使用$\u GET[“sort\u new”]和$\u GET[“sort\u popular”]以及它在不同选项卡中使用其中一个$\u GET参数选择SQL的底部部分进行交换?是的,但是我应该在这些选项卡div中写些什么来获得$order=$\u GET[“sort\u newgames”]嗯,这很复杂,取决于你的具体情况,几乎不可能帮助你在这个网站上发表评论。我建议你学习PHP和HTML如何协同工作来解决这个问题,但这不是一个简单的问题,我可以输入500个字符,加上答案需要你理解PHP/HTML,所以你陷入了困境对不起
    <div class="tab">
    <h2><a name="newgames" id="newgames">a</a></h2>

<form action="<?php  echo JRoute::_('index.php'); ?>" method="post">
<input type="hidden" name="option" value="com_yoflash" />
<input type="hidden" name="view" value="category" />
<input type="hidden" name="mochicat" value="<?php echo $this->cat->catid; ?>" />
<select name="order" size="1" onchange="submit();">
<option value="0" <?php if($this->order=="0") echo "selected=\"selected\""; ?>><?php echo JText::_('ORDER_DATE_NEWEST'); ?></option>
<option value="4" <?php if($this->order=="4") echo "selected=\"selected\""; ?>><?php echo JText::_('ORDER_POPULAR'); ?></option>
</select>
<input type="hidden" name="Itemid" value="<?php echo $this->Itemid; ?>" />
</form>



 <?php
///////////////////////////////////////
// GAMES BOXES START
///////////////////////////////////////
  $db =& JFactory::getDBO();

  for($i=0;$i<count($this->games);$i++) {
  $game=$this->games[$i];
  $slug=$game->slug;

  $game->imgname=$this->params->get('dir_swf').$slug.'/'.$game->imgname;
  $game->description=$db->getEscaped($game->description);
  $game->description=str_replace("\\\"","\'",$game->description);
  $game->namett=$db->getEscaped($game->name);
  ?>

<div class="game" style="display:block;">

<a style="border:0;" href="<?php echo JRoute::_('index.php/play?option=com_yoflash&view=game&id='.$slug.'&Itemid='.$this->Itemid); ?>" onmouseover="Tip('<strong><?php echo $game->namett; ?></strong><br/> <?php echo $game->description; ?>',WIDTH,300,FADEIN,0,DELAY,0,BGCOLOR, '<?php echo $this->params->get('ttip_bgcolor'); ?>', BORDERCOLOR,'<?php echo $this->params->get('ttip_bordercolor'); ?>',FONTCOLOR,'<?php echo $this->params->get('ttip_fontcolor'); ?>',FONTSIZE,'<?php echo $this->params->get('ttip_fontsize'); ?>',FONTFACE,'<?php echo $this->params->get('ttip_fontface'); ?>')" onmouseout="UnTip()">
<img src="<?php echo $game->imgname; ?>" width="71" height="56" alt="<?php echo $game->name; ?>"/><br/>
<div class="gameName"><?php echo "<p>$game->name</p>"; ?></div>
</a>

<span class="info"><?php echo $game->description; ?></span>

 <span class="gamePlayed"><?php echo "<p>$game->stat_clicked <br/></p>".JText::_("<p> plays</p>") ;?></span> 

 </div>