简体   繁体   中英

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

Illegal string offset

error all the time, no matter if I use $value['category_name'] or $key['category_name'] .

Categories table just have category_id and category_name columns with two rows.

How to access category_id in this array?

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:

$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?

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

 <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:

  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:

<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.

<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

For getting arrays content from your sql result just use below syntax

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'] ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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