2

i have a array from oher file php and show in select data html

this is my array

array.php

<?php
    $category_attachment = array(       "SPK" ,    
                                        "Justifikasi" ,  
                                        "PR" ,  
                                        "RAB" ,  
                                        "Proc" ,  
                                        "PO" ,  
                                        "Notulen" ,  
                                        "Sertifikat" ,  
                                        "BAUT" ,  
                                        "BAST" ,  
                                        "Tagihan" ,
                                        "Other"
                                ); ?>

and this myfile.php

<?php include "array.php" ?>

<select name="category" id="category">
<option value="">Choose Category</option>
  // I want array data in here
</select>

Help Me Thank's

5 Answers 5

1

Try this

<select name="category" id="category">
    <option value="">Choose Category</option>
    <?php
    foreach ($category_attachment as $value) {
        ?>
        <option value="<?php echo $value ?>"><?php echo $value ?></option>
        <?php
    }
    ?>
</select>
Sign up to request clarification or add additional context in comments.

1 Comment

@MuhamadRidwansyah happy to help :)
1

just try print_r($category_attachment); in your myfile.php and see what happens. you should be able to access the variable after you included the file

Also, you should disable the first option by using the disabled attribute ;)

<?php 
    include "array.php" 
    print_r($category_attacment);
?>

<select name="category" id="category">
    <option disabled>Choose Category</option>
<?php
foreach($category_attachment as $cat) { 
    echo "<option value='" . $cat . "'>" . $cat . "</option>";
}
?>
</select>

This example won't work:

<?php 
    print_r($category_attacment);
    include "array.php" 
?>

you have to include the file before you can access the variable.

Comments

1

You could do the following :

<select name="category" id="category">
<option value="">Choose Category</option>
    <?php
      foreach($category_attachment as $category){
      echo '<option value="'.$category'">'.$category.'</option>'; 
     }
     ?>
</select>

Give a look at foreach doc, which is very useful when using array :

http://php.net/manual/en/control-structures.foreach.php

Comments

1

You can do this:

<?php include "array.php" ?>
<select name="category" id="category">
    <option value="">Choose Category</option>
    <?php foreach ($category_attachment as $category) {?>
        <option value="<?php echo $category; ?>"><?php echo $category; ?></option>  
    <?php } ?>
</select>

Comments

1

As simple as this,

<select name="category" id="category">
    <?php
    foreach ($category_attachment as $value) {
        ?>
        <option value="<?= $value ?>"><?= $value ?></option>
        <?php
    }
    ?>
</select>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.