Come visualizzare i dati dalla tabella personalizzata nel database wordpress?
4 risposta
- voti
-
- 2012-06-09
Ecco un codice diesempio che otterrài datie poi li visualizzerà:
global $wpdb; // this adds the prefix which is set by the user upon instillation of wordpress $table_name = $wpdb->prefix . "your_table_name"; // this will get the data from your table $retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" ); ?> <ul> foreach ($retrieve_data as $retrieved_data){ ?> <li><?php echo $retrieved_data->column_name;?></li> <li><?php echo $retrieved_data->another_column_name;?></li> <li><?php echo $retrieved_data->as_many_columns_as_you_have;?></li> <?php } ?> </ul> <?php
Èbuonanorma usarenomi univociper variabilie funzioni,quindipotresti voler aggiungere unprefisso univoco atutte letue variabili ofunzioni IE: ($prefix_table_name dove "prefix" sarebbe qualcosa di unico come l'abbreviazione deltuotema oplugin.)
Riferimento - wpdb - codex
Here is an example code that will get the data and then display it:
global $wpdb; // this adds the prefix which is set by the user upon instillation of wordpress $table_name = $wpdb->prefix . "your_table_name"; // this will get the data from your table $retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" ); ?> <ul> foreach ($retrieve_data as $retrieved_data){ ?> <li><?php echo $retrieved_data->column_name;?></li> <li><?php echo $retrieved_data->another_column_name;?></li> <li><?php echo $retrieved_data->as_many_columns_as_you_have;?></li> <?php } ?> </ul> <?php
It's good practice to use unique names for variables and functions, so you may want to add a unique prefix to all your variables or functions IE: ($prefix_table_name where "prefix" would be something unique such as the abbreviation of your theme or plugin.)
Reference - wpdb - codex
-
- 2017-01-18
Prova questo codiceper visualizzaretuttii record dal databasein wordpress. Per questo deviprima creare unfile.php all'interno della cartella wordpress selezionatae quindi utilizzare questofile comemodello. E questo codicefunzioneràperfettamente Grazie atutti.
<?php /* Template Name: your template name */ ?> <?php get_header(); ?> <table border="1"> <tr> <th>ID</th> <th>FULL NAME</th> <th>BRANCH NAME</th> <th>E-MAIL ID</th> <th>Mobile Number</th> <th>Course</th> <th>Address</th> <th>City</th> <th>Zip Code</th> </tr> <?php global $wpdb; $result = $wpdb->get_results( "SELECT * FROM wp_example"); foreach ( $result as $print ) { ?> <tr> <td> <?php echo $print->id; ?> </td> <td><?php echo $print->firstname; ?> </td> <td> <?php echo $print->branch ; ?> </td> <td> <?php echo $print->email; ?> </td> <td><?php echo $print->mobile; ?> </td> <td> <?php echo $print->course; ?> </td> <td> <?php echo $print->address; ?> </td> <td><?php echo $print->city; ?> </td> <td> <?php echo $print->zip ; ?> </td> </tr> <?php } ?> </table> <?php get_header(); ?>
Please try this code for display all the records from database in wordpress. For this firstly need to create a file.php inside your selected wordpress folder and then use this file as a template. And this code will work perfectly Thank you all.
<?php /* Template Name: your template name */ ?> <?php get_header(); ?> <table border="1"> <tr> <th>ID</th> <th>FULL NAME</th> <th>BRANCH NAME</th> <th>E-MAIL ID</th> <th>Mobile Number</th> <th>Course</th> <th>Address</th> <th>City</th> <th>Zip Code</th> </tr> <?php global $wpdb; $result = $wpdb->get_results( "SELECT * FROM wp_example"); foreach ( $result as $print ) { ?> <tr> <td> <?php echo $print->id; ?> </td> <td><?php echo $print->firstname; ?> </td> <td> <?php echo $print->branch ; ?> </td> <td> <?php echo $print->email; ?> </td> <td><?php echo $print->mobile; ?> </td> <td> <?php echo $print->course; ?> </td> <td> <?php echo $print->address; ?> </td> <td><?php echo $print->city; ?> </td> <td> <?php echo $print->zip ; ?> </td> </tr> <?php } ?> </table> <?php get_header(); ?>
-
Questonon risponde alla domanda.Perfavore rileggilo.That does not answer the question. Please re-read it.
- 1
- 2017-01-18
- kaiser
-
- 2012-05-12
Sembra chetu stia cercando
$wpdb
.Avraibisogno di scriveretutte letuefunzionie simili.Consiglio vivamente di attenersi alle convenzioni di denominazione stabilite (cose comethe_blah
eget_blah
,magari con unprefisso)perfacilità di leggibilitàe coerenza.Sounds like you're looking for
$wpdb
. You'll need to write all your own functions and such. I strongly recommend sticking to the established naming conventions (stuff likethe_blah
andget_blah
, maybe with a prefix) for ease of readability and for consistency. -
- 2018-05-03
Modifica della risposta di @Kirill Fuchs. Se utilizzi questo codice sullo shortcode,potresti creare unproblema. Potrebbe visualizzare le uscitenell'ordine sbagliato. Perevitareil ritorno ho usatoinvece dieco. Prova afarlo su shortcodein questomodo:
add_shortcode('custom_db', function(){ global $wpdb; $table_name = $wpdb->prefix . 'liveshoutbox'; // this will get the data from your table $retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" ); foreach ($retrieve_data as $retrieved_data){ $f_name = $retrieved_data->column_name; $f_text = $retrieved_data->another_column_name; } $output = '<div class="wrap"> <h2>Table of clients.</h2> <table> <tr> <th>First Name</th> <th>Last Name</th> <th>Email</th> </tr> <tr> <td>'. $f_name .'</td> <td>'. $f_text .'</td> </tr> </table> </div>'; return $output; } );
Modification of @Kirill Fuchs's answer. If you use this code on shortcode this may create a problem. It may display the outputs in wrong order. To avoid the I used return instead of echo. Try to do this on shortcode this way:
add_shortcode('custom_db', function(){ global $wpdb; $table_name = $wpdb->prefix . 'liveshoutbox'; // this will get the data from your table $retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" ); foreach ($retrieve_data as $retrieved_data){ $f_name = $retrieved_data->column_name; $f_text = $retrieved_data->another_column_name; } $output = '<div class="wrap"> <h2>Table of clients.</h2> <table> <tr> <th>First Name</th> <th>Last Name</th> <th>Email</th> </tr> <tr> <td>'. $f_name .'</td> <td>'. $f_text .'</td> </tr> </table> </div>'; return $output; } );
-
Èinoltrepossibile utilizzareilbuffer di outputper raccogliere l'htmlper la restituzione.Usa ob_start ();all'inizioe ritorna ob_get_clean ();You can also use output buffer to collect the html for return. Use ob_start(); at the beginning and return ob_get_clean();
- 0
- 2018-05-04
- Bikash Waiba
-
Non c'èbisogno di scriveremolte righein virgolette singole.No need to write many lines in single quote.
- 0
- 2018-05-04
- Bikash Waiba
Voglio recuperarei dati dallatabellapersonalizzata,che ho creato all'interno del database di wordpresse visualizzarliin unapagina di wordpress,comei post
Graziein anticipo