【问题标题】:Print or echo a selected option in php, without a form在 php 中打印或回显选定的选项,无需表单
【发布时间】:2013-05-03 00:40:14
【问题描述】:

这是一个配送模块,允许您选择分店进行本地取货。 分支列在 csv 中,而不是在数据库中。

<?php
        if ( ($n > 1) || ($n2 > 1) ) {
         if ($quotes[$i]['id'] == 'localdelivery') {
          if (file_exists($quotes[$i]['pricesL'])) {
           echo '<td class="main" style="padding-right:15px;"><select name="locdeliv">';
           $file_handle = fopen($quotes[$i]['pricesL'], "r");
           while (!feof($file_handle)) {
            $line_of_text = fgetcsv($file_handle, 1024);
            echo '<option value="' . $line_of_text[1] . '">' . $line_of_text[0] . '</option>';
                                       }
            fclose($file_handle);
                                                     }
          else {
           echo '<td class="main"><select name="locdeliv"><option value="0">file missing</option>';
               }
          echo '</select></td>';
                                                }
         //else {
?>

效果很好。 下面的代码显示了用户所看到的运输描述:

    function quote($method = '') {
  global $order;

  $this->quotes = array('id' => $this->code,
                        'module' => MODULE_SHIPPING_LOCALDELIVERY_TEXT_TITLE,
                        'pricesL' => MODULE_SHIPPING_LOCALDELIVERY_PRICESFILE,
                        'methods' => array(array('id' => $this->code,
                                                 'title' => print $line_of_text[1],
                                                 'cost' => MODULE_SHIPPING_LOCALDELIVERY_COST)));

  if ($this->tax_class > 0) {
    $this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
  }

  if (tep_not_null($this->icon)) $this->quotes['icon'] = tep_image($this->icon, $this->title);

  return $this->quotes;
}

这部分

'title' => print $line_of_text[1],

我需要回显用户从下拉列表中选择的任何内容。我尝试了各种方法,例如 $file_handle 等,但结果总是得到“1”。 有没有一种简单的方法可以将选定的下拉值放入“标题”? 这允许稍后将标题保存在数据库中。在其他模块中,“标题”取自语言文件,因此它永远不会来自数据库,并且结帐页面“不在乎”该行是什么,它将其保存到数据库中,因此我可以看到选择了哪个选项在行政上。 使用 CSV 的原因是公司经常更新分支机构,这是经常更新的最简单方法。

【问题讨论】:

  • 我看到的是三重还是有3 if statements可以组合
  • PHP 在服务器端执行,因此服务器无法获知用户从客户端下拉列表中选择的值,直到将其以&lt;form&gt; 发回服务器,当然,除非你使用 AJAX。
  • 我会调查一下,因为结帐页面已经在表单的其余部分使用了 AJAX,只需要在其中实现它。
  • 发回您的发现 - 很乐意为您提供帮助

标签: php csv printing echo option


【解决方案1】:

您在定义变量时尝试使用programming construct (print) - 这是不正确的语法。

您需要做的是使用函数Quote,并从返回的数组中获取值。

$result = $obj->quote();
$title  = $result['methods'][0]['title'];
print $title;

编辑详细说明:

您的函数quote 引用$this,这意味着它是class 中的method。 为了能够使用此方法,您需要在类本身的外部 调用它。我们称之为客户端代码。

class Foo
{
  public function quotes() {
    // ** all your code **
    return $this->quotes;
  }
}

// outside the class, so client code
$foo = new Foo();
$result = $foo->quote();
$title  = $result['methods'][0]['title'];
print $title;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 2018-01-24
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    相关资源
    最近更新 更多