• Resolved agenciaklox

    (@agenciaklox)


    Hi,

    I need a little help, i’m developing my first plugin, and i need to push some data from database using wpdb class and populate a select with this data.

    So i start to push it, but i don’t know how to do the loop with the data.

    <select name='wcwp_settings[wcwp_select_state]' id="cod_estados">
    		<option value='0'>Selecione uma Op??o</option>
    		<?php
    			global $wpdb;
    			$states = $wpdb->get_col("SELECT nome FROM estados");
    			$i = 0;
    			while($i <= '27'){
    		?>
    		<option value='0'><?php echo $states[$i]; ?></option>
    		<?php
    				$i++;
    			}
    		?>
    	</select>

    Do you know how to do it?

    Thanks ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • Dion

    (@diondesigns)

    Assuming your $wpdb->get_col function call is correct, the following code should work.

    I noticed that you were setting the same value fo revery option, which made no sense to me, so I gave them an incremented value. If you really wanted them all to be zero, then change ‘{$i}’ to ‘0’.

    <?php
    global $wpdb;
    $i = 1;
    $options = '';
    $states = $wpdb->get_col("SELECT nome FROM estados");
    if (!empty $states) {
    	foreach ($states as $state) {
    		$options .= "<option value='{$i}'>{$state}</option>";
    		$i++;
    	}
    }
    ?>
    
    <select name='wcwp_settings[wcwp_select_state]' id="cod_estados">
    	<option value='0'>Selecione uma Op??o</option>
    	<?php echo $options; ?>
    </select>
    Thread Starter agenciaklox

    (@agenciaklox)

    Thank you very much DioDesigns, it work very well ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Loop with wpdb class’ is closed to new replies.