简体   繁体   English

如何访问此数组的内部元素?

[英]How to access this array inner elements?

EDIT: 编辑:

I'm trying to display category names of my categories table inside select HTML dropdown, but I'm getting 我正在尝试在select HTML下拉列表中显示我的类别表的类别名称,但我正在

Illegal string offset 非法字符串偏移

error all the time, no matter if I use $value['category_name'] or $key['category_name'] . 始终出错,无论我使用$value['category_name']还是$key['category_name']

Categories table just have category_id and category_name columns with two rows. Categories表只有具有两行的category_idcategory_name列。

How to access category_id in this array? 如何访问此数组中的category_id

array(6) {
  ["Field"]=>
  string(11) "category_id"
  ["Type"]=>
  string(7) "int(11)"
  ["Null"]=>
  string(2) "NO"
  ["Key"]=>
  string(3) "PRI"
  ["Default"]=>
  NULL
  ["Extra"]=>
  string(14) "auto_increment"
}

My Code right now: 我的代码现在:

$q = "SELECT DISTINCT categories.category_name FROM products LEFT JOIN categories ON (products.category_id = categories.category_id)";

$result = mysqli_query($dbc, $q);
$sql = "SHOW COLUMNS FROM `categories`";
$columns = mysqli_query($dbc,$sql);

$arr = [];
while($row = mysqli_fetch_array($columns, MYSQLI_ASSOC)){
    $arr[] = $row;
}

<fieldset>
        <legend>Add Product</legend>
        <p>Product name: <input name="product" type="text" size="60" maxlength="100" required></p>
        <p>Category: <select name="category_id"><?php $i = 1;foreach ($arr as $entry)
                if($i > 1) {continue;}
                { ?><option value="<?php echo $entry['Field'];?>"><?php foreach($entry as $key=>$value) {var_dump($arr);} ?></option> <?php $i++; }?></select></p>
        <p>Price: <input name="price"></p>
        <p>Amount: <input name="amount"></p>
        <p><input name="submit" type="submit" value="Add This Product"></p>
    </fieldset>

Thanks. 谢谢。

The actual code part of the PHP seems to be of the form: PHP的实际代码部分似乎具有以下形式:

$q = "SELECT DISTINCT c.category_name 
        FROM products p
        LEFT
        JOIN categories c
          ON c.category_id = p.category_id";
$result = mysqli_query($dbc, $q);

// the code below ignores result of the preceding query

$sql = "SHOW COLUMNS FROM `categories`";
$columns = mysqli_query($dbc,$sql);

$arr = [];
while( $row = mysqli_fetch_array($columns, MYSQLI_ASSOC) ) {
   $arr[] = $row;
}

$i = 1;
foreach( $arr as $entry ) if( $i > 1 ) { continue; }

// the preceding foreach loop is ended, so the following is not part of that loop
{
  echo $entry['Field'];
  foreach( $entry as $key=>$value ) {
    var_dump($arr);
  }
  $i++;
}

(There's no reference to $result , the result set from that first query just gets discarded. It's hard to determine what it is this is supposed to be accomplished. why is the variable $i even needed? What is the deal with looping through every element of the array, and doing a "continue" on a condition that won't ever be satisfied? And why do we need to do a SHOW COLUMNS query? (没有引用$result ,第一个查询的结果集将被丢弃。很难确定这应该完成什么。为什么还要使用变量$i ?遍历每个变量的处理方式是什么数组的元素,并在永远不会满足的条件下执行“继续”操作;为什么我们需要执行SHOW COLUMNS查询?

It's just an incoherent rigmarole. 这只是一个不连贯的琐事。

I'm just guessing, but it seems like the job we want to accomplish is retrieve a list of category_id and category_name values, and decorate that list as 我只是在猜测,但似乎我们要完成的工作是检索category_idcategory_name值的列表,并将其装饰为

 <select ... > 
   <option value="42">Computers     </option>
   <option value="43">Smartphpones  </option>
 </select>

But again, I'm just guessing. 但是,我只是在猜测。

It seems like we could retrieve that with a query, and store that in an array: 似乎我们可以通过查询来检索它,并将其存储在数组中:

$sql = "SELECT c.category_id
             , c.category_name
          FROM products p
          LEFT
          JOIN categories c
            ON c.category_id = p.category_id
         GROUP
            BY c.category_id
             , c.category_name
         ORDER
            BY c.category_name";
$result = mysqli_query($dbc, $sql);
$arr = [];
while( $row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
   $arr[] = $row;
}

And once we have that done in the initialization, as then as we crank out the html page, when we get to the point we want to dump the list into the html, we would do that in a loop something like this: 初始化完成后,当我们打开html页面时,当我们想要将列表转储到html时,我们将在循环中执行以下操作:

  foreach( $arr as $val ) {
      echo $val['category_id'];
      echo $val['category_name'];
  }

You have some errors in code. 您在代码中有一些错误。

Your foreach loop in your HTML code has the bracket in the wrong place: HTML代码中的foreach循环将括号放在错误的位置:

<p>Category: <select name="category_id">
<?php 
    $i = 1;
    foreach ($arr as $entry) { //<-- You was missing this
        if($i > 1) continue;
?>

Since your foreach loop is only populating a single value on each iteration, there would be no key. 由于您的foreach循环在每次迭代中仅填充一个值,因此没有键。

<option value="<?php echo $entry; ?>">

Completed code: 完成的代码:

$q = "SELECT DISTINCT categories.category_name FROM products LEFT JOIN categories ON (products.category_id = categories.category_id)";

$result = mysqli_query($dbc, $q);
$sql = "SELECT * FROM `categories`";
$columns = mysqli_query($dbc,$sql);

$arr = [];
while($row = mysqli_fetch_array($columns, MYSQLI_ASSOC)){
    $arr[] = $row;
}

<fieldset>
    <legend>Add Product</legend>
    <p>Product name: <input name="product" type="text" size="60" maxlength="100" required></p>
    <p>Category: 
        <select name="category_id">    
            <?php 
                foreach ($arr as $entry) {
                    echo '<option value="' . $entry['category_name']. '"></option>
                }
            ?>
        </select>
    </p>
    <p>Price: <input name="price"></p>
    <p>Amount: <input name="amount"></p>
    <p><input name="submit" type="submit" value="Add This Product"></p>
</fieldset>

Sources: PHP-foreach 资料来源: PHP-foreach

For getting arrays content from your sql result just use below syntax 为了从SQL结果中获取数组内容,只需使用以下语法

So instead 所以与其

<option value="<?php echo $entry['Field'];?>">

please use 请用

<option value="<?php echo $entry['category_id'];?>">

and instead 而是

<?php foreach($entry as $key=>$value) {var_dump($arr);} ?>

use 采用

<?php echo $entry['category_name'] ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM