开发者社区 问答 正文

检查PHP中其他文件的值

我试图弄清楚如何比较2个不同文件中的2个不同值。我想从一侧到另一侧比较资源。如果我的资源超出了我的需要,那么我想显示“提交”按钮。谢谢你们!在这两个代码上,我都将字符串从Int转换为一个更容易的比较。我的问题是在A代码的末尾,我在该代码中发出“ IF”语句,并且仅当所有条件都满足时,我才希望它有效。

文件A中的代码,我的升级成本在哪里?数据库将返回所有建筑物以及以下升级成本:

  <tr>
    <td colspan="2"><b><center>Actual</center></b></td>
    <td colspan="7"><b><center>Recursos per passar al proper nivell</center></b></td>
  </tr>
  <tr>
    <td><b><center>Edifici</center></b></td>
    <td><b><center>Nivell Actual</center></b></td>
    <td><b><center>Seguent Nivell</center></b></td>
    <td><b><center>Cristall</center></b></td>
    <td><b><center>Fusta</center></b></td>
    <td><b><center>Marbre</center></b></td>
    <td><b><center>Raim</center></b></td>
    <td><b><center>Sofre</center></b></td>
  </tr>
<?php
include("connexio.php");
  $consulta_edificis="SELECT nom_edifici,nivell FROM edificis ORDER BY id_edifici ASC"; //consultem el nom i el nivell acual del edifici
  $resultat_edificis=mysqli_query($connect,$consulta_edificis);
  while($consulta_nivell=mysqli_fetch_array($resultat_edificis)) //Treiem el nom i nivell actual.
  {
    $nivell_actual=(int)$consulta_nivell['nivell']; //convertim el string a numero enter
    $nivell_actual=$nivell_actual+1; //sumem 1 ja que a la taula de nivells també tenim el nivell 0.
    $consulta_seguent_nivell="SELECT id_nivell,cost_upgrade_cristall,cost_upgrade_fusta,cost_upgrade_marbre,cost_upgrade_raim,cost_upgrade_sofre FROM nivells LIMIT 1 OFFSET $nivell_actual"; //consulta per saber el que ens costarà pujar al seguent nivell.
    $resultat_pujar_nivell=mysqli_query($connect,$consulta_seguent_nivell);
    while($consulta_cost_seguent_nivell=mysqli_fetch_array($resultat_pujar_nivell))
    { //imprimim resultats
      ?>
      <tr>
        <td><?php echo $consulta_nivell['nom_edifici']?></td>
        <td><?php echo $consulta_nivell['nivell']?></td>
        <td><?php echo $consulta_cost_seguent_nivell['id_nivell']?></td>
        <td><?php echo $consulta_cost_seguent_nivell['cost_upgrade_cristall']?></td>
        <td><?php echo $consulta_cost_seguent_nivell['cost_upgrade_fusta']?></td>
        <td><?php echo $consulta_cost_seguent_nivell['cost_upgrade_marbre']?></td>
        <td><?php echo $consulta_cost_seguent_nivell['cost_upgrade_raim']?></td>
        <td><?php echo $consulta_cost_seguent_nivell['cost_upgrade_sofre']?></td>
        <?php
          $cost_cristall=(int)$consulta_cost_seguent_nivell['cost_upgrade_cristall'];
          $cost_fusta=(int)$consulta_cost_seguent_nivell['cost_upgrade_fusta'];
          $cost_marbre=(int)$consulta_cost_seguent_nivell['cost_upgrade_marbre'];
          $cost_raim=(int)$consulta_cost_seguent_nivell['cost_upgrade_raim'];
          $cost_sofre=(int)$consulta_cost_seguent_nivell['cost_upgrade_sofre'];
        ?>
        <?php include("materials.php"); ?>
        <td><?php if ($cristall>$cost_cristall && $fusta>$cost_fusta && $marbre>$cost_marbre && $raim>$cost_raim && $sofre>$cost_sofre): ?>
            <input type="submit" value="enviar">
            <?php else: ?>
            <p>no</p>
            <?php endif; ?></td>
          </tr>
      <?php
    }
  }
mysqli_close($connect);
?>
</table>

来自文件B的代码,这里有我的资源:

include("connexio.php");
$consulta_valors="SELECT * FROM materials LIMIT 1";
$resultat_valors=mysqli_query($connect,$consulta_valors);
while($consulta=mysqli_fetch_array($resultat_valors))
{
$consulta['cristall'];
$consulta['fusta'];
$consulta['marbre'];
$consulta['raim'];
$consulta['sofre'];
$cristall=(int)$consulta['cristall'];
$fusta=(int)$consulta['fusta'];
$marbre=(int)$consulta['marbre'];
$raim=(int)$consulta['raim'];
$sofre=(int)$consulta['sofre'];
}
mysqli_close($connect);
?>
</table>

展开
收起
社区秘书 2019-12-09 17:01:59 512 分享 版权
阿里云 AI 助理回答

为了帮助您比较两个文件中的值并根据条件显示“提交”按钮,我建议使用以下步骤。这里假设您正在使用PHP作为后端语言,但方法可以调整以适应其他编程语言。

文件A中的代码解析

首先,确保您从数据库获取的数据已正确处理,并且能够进行比较。您的描述中提到了升级成本的计算,但未提供具体代码。基于您的描述,一个简化的示例可能是这样的:

// 假设这是从数据库获取的数据
$buildingData = [
    // 示例数据结构
    [
        'Edifici' => 'Building1',
        'Nivell Actual' => 1,
        'Seguent Nivell' => 2,
        'Cristall' => 100,
        'Fusta' => 50,
        // ... 其他资源
    ],
    // 更多建筑...
];

// 您拥有的资源(示例)
$yourResources = [
    'Cristall' => 150,
    'Fusta' => 75,
    // ... 其他资源
];

// 遍历每个建筑,检查升级条件
foreach ($buildingData as $building) {
    // 计算升级到下一等级需要的总资源
    $totalCost = array_sum(array_slice($building, 3)); // 从'Cristall'开始计算总和

    // 检查您是否有足够的资源进行升级
    if ($totalCost <= array_sum($yourResources)) {
        echo "您可以升级此建筑: {$building['Edifici']}<br>";
        // 在满足所有条件时显示"提交"按钮
        echo "<button type='submit'>提交</button>";
    } else {
        echo "资源不足以升级此建筑: {$building['Edifici']}<br>";
    }
}

关键点说明

  • 上述代码遍历了从数据库获取的每个建筑及其升级成本。
  • 使用array_sum()函数计算升级单个建筑所需的总资源量。
  • 将您的现有资源总量与升级所需资源进行比较,如果您的资源足够,则允许升级并显示“提交”按钮。
  • 这里简化了逻辑,实际应用中可能需要更复杂的条件判断,比如针对特定建筑类型的特殊规则等。

请根据您的实际项目需求调整上述代码。如果您使用的是其他编程语言或框架,实现逻辑会有所不同,但核心思想是类似的:获取数据、计算成本、比较资源、根据结果展示UI元素。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答