简体   繁体   中英

Get data if the database is empty?

Pay attention to the options array this array holds an array of properties on the database, there is a call for the database in the script it's not nessecary to add here, it retrieves the data because it's already on there...

$meta_boxes[] = array(
    'id'         => 'page_metabox',
    'fields'     => array(

        array(
            'name' => ''.$slider['properties']['title'].'',
            'desc' => 'Upload an image/pattern for the static area.',
            'id' => $prefix . 'text_BOMB',
            'type' => 'sortable',
            'options' => $slider['properties'],               
            'multiple' => true,
        ),
 );

The data HTML is outputted using a foreach and an input...

 <?php foreach ( $field['options'] as $value => $name ) : ?>
    <input type="text" name="<?php echo $field['id'].'[]' ?>" id="<?php echo $field['id'] ?>" value="<?php echo $value; ?>"  />      
 <?php endforeach; ?>

This is exactally what I want, I have a list of inputs for each property... then it hit me, what if its a new user and the data isnt on their database yet the foreach wont output anything and then there is no way to write it to the database... this is pretty noobish but that means an easier answer for you guys :p

You could use empty - function:

<?php 
if (empty($field['options'])) {
    //do something when options are empty
}
else { //the array $field['options'] has something in it
    foreach ( $field['options'] as $value => $name ) : ?>
    <input type="text" name="<?php echo $field['id'].'[]' ?>" id="<?php echo $field['id'] ?>" value="<?php echo $value; ?>"  />      
    <?php endforeach;
}
?>

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