Display data from database inside <table> using wordpress $wpdb - php

Can someone help me, how to code the logic to print the data from my database into a <table>?
<table border="1">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM myTable" );
foreach ( $result as $print ) {
echo '<td>' $print->firstname.'</td>';
}
?>
</tr>
</table>
I know this is so basic, but I'm having a hard time making this work.

Try this:
<table border="1">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM myTable" );
foreach ( $result as $print ) {
?>
<tr>
<td><?php echo $print->firstname;?></td>
</tr>
<?php }
?>

You're missing . in echo '<td>' $print->firstname.'</td>';
Try this
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM myTable" );
foreach ( $result as $print ) {
echo '<tr>';
echo '<td>' . $print->firstname.'</td>';
echo '<td>' . $print->lastname.'</td>';
echo '<td>' . $print->points.'</td>';
echo '</tr>';
}
?>

Try this:
$result = $wpdb->get_results("SELECT * FROM myTable" , ARRAY_A); //get result as associative array
Then the usual cycle:
//spare memory
$count = count($result);
//fastest way to perform the cycle
for ($i = $count; $i--;) {
echo '<td>'. $print[$i]['firstname'].'</td>';
}

You just need to put <tr> inside your foreach loop, and add . concatenation operator in your line, you need also try this :
You need to wrap <td></td> inside <tr></tr> in foreach loop
You need to add . concatenation operator in line that contains firstname variable.
If you have duplicate values, then add this parameter ARRAY_A to your query
$result = $wpdb->get_results ( "SELECT * FROM myTable",ARRAY_A );.
<table border="1">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM myTable" );
foreach ( $result as $print ) {
echo '<tr>';
echo '<td>' . $print->firstname .'</td>';
echo '<td>' . $print->lastname .'</td>';
echo '<td>' . $print->points .'</td>';
echo '</tr>';
}
?>
</table>

Related

How to list PHP table query as non repeatable heading

I have a PHP table which consists of three catagories:
finit "material"
disc "discription"
size "dimension"
Similar 'finit' comes in different 'disc' and 'size'
I would like to display the results in a way that 'finit' are displayed only once depending upon the quantity while 'disc' and 'size' are listed within the table under their associated 'finit'
I am able to display 'finit' only once if it contains multiple discriptions etc.
But the listings are not properly set within the table.
they are listed directly under 'finit' and horizontally listed.
<?php
include("connect.php");
$query = "SELECT * FROM prime order by finit asc";
$info = mysqli_query($conn, $query);
$finit = $rows['finit'];
?>
<table class="table table-striped">
<tr>
<th>Material</th>
<th>Discription</th>
<th>Dimension</th>
</tr>
<?php
while($rows = mysqli_fetch_assoc($info)):
if($rows['finit'] != $finit) {
echo '<tr><td>'.$rows['finit'].'</td></tr>';
$finit = $rows['finit'];
}
echo'<td>'.$rows['disc'].'</td>';
echo'<td>'.$rows['size'].'</td>';
endwhile;
?>
</table>
</div>
</body>
</html>
Your code was not generating correct HTML which may well explain the presentation issues. You would also have lost the first finit value. See comments in code
<?php
include("connect.php");
$query = "SELECT * FROM prime order by finit asc";
$info = mysqli_query($conn, $query);
// this will cause the first finit to be completely lost
//$finit = $rows['finit'];
$finit = NULL;
?>
<table class="table table-striped">
<tr>
<th>Material</th>
<th>Discription</th>
<th>Dimension</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($info)):
echo '<tr>';
if($row['finit'] != $finit) {
echo '<td>'.$row['finit'].'</td>';
$finit = $row['finit'];
} else {
// when you dont print a finit, you need to output something in that column
echo '<td> </td>';
}
echo '<td>' . $rows['disc'] . '</td>';
echo '<td>' . $rows['size'] . '</td>';
echo '</tr>';
endwhile;
?>
</table>
UPDATE
To format the table so that the finit value is always on its own row followed by rows containing only the other 2 columns, you could try.
All you have to remember is that you have to output the same number of <td>'s on each line, so in this case 3, unless you use the colspan="2" attribute, but try that once you have the simple route working.
<?php
include("connect.php");
$query = "SELECT * FROM prime order by finit asc";
$info = mysqli_query($conn, $query);
// this will cause the first finit to be completely lost
//$finit = $rows['finit'];
$finit = NULL;
?>
<table class="table table-striped">
<tr>
<th>Material</th>
<th>Discription</th>
<th>Dimension</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($info)):
if($row['finit'] != $finit) {
echo '<tr><td>'.$row['finit'].'</td><td> </td><td> </td></tr>';
$finit = $row['finit'];
}
echo '<tr><td> </td>';
echo '<td>' . $rows['disc'] . '</td>';
echo '<td>' . $rows['size'] . '</td>';
echo '</tr>';
endwhile;
?>
</table>

display data in tabular format from database in wordpress page

I have look all day the whole internet and didn't find fetching data in tabular format. I have my short code and everything is ready, I have this code which is display data but not in good format as you can see in the screen shot I want data like this in WordPress page.
<?php
?>
<table border="1">
<tr>
<th>Char Item</th>
<th>Item Id</th>
<th>Item Description</th>
</tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM wp_orderlist" );
foreach ( $result as $print ) {
echo '<td>'. $print->char_item.'</td>';
echo '<td>'. $print->item_id.'</td>';
echo '<td>'. $print->Item_Description.'</td>';
}
?>
</table>
You forget to add tr tag. Try this:
echo '<tr>';
foreach ( $result as $print ) {
echo '<td>'. $print->char_item.'</td>';
echo '<td>'. $print->item_id.'</td>';
echo '<td>'. $print->Item_Description.'</td>';
}
echo '</tr>';

PHP - Inputting data into MySQL cell

I am trying put row['user'] into cell but it doesn't work.
When I uncomment "echo" it works fine.
PHP:
function mod_Something($database)
{
$sql = "SELECT user FROM table_name";
if ($result = mysqli_query($database, $sql)) {
while ($row = mysqli_fetch_assoc($result)) {
$html = $html . '<tr><td>' . $row['user'] . '</td></tr>';
// echo $row['user'];
}
return $html;
}
}
I also have a HTML view file where I have:
<table id="data-table-basic" class="table table-striped">
<thead>
<tr>
<th>user</th>
</tr>
</thead>
<tbody>
%mod_Something%
</tbody>
</table>
I know that HTML isn't a function but I must return it because there is a script which allows to return "view".
try this:
function mod_Something($database)
{
$sql = "SELECT user FROM table_name";
if ($result = mysqli_query($database, $sql)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr><td>' . $row['user']; . '</td></tr>';
}
return $html;
}
}
<table id="data-table-basic" class="table table-striped">
<thead>
<tr>
<th>user</th>
</tr>
</thead>
<tbody>
<?php mod_Something(); ?>
</tbody>
</table>
You are not getting anything from the function mod_Something because you are not building & returning HTML properly.
See the below code.
function mod_Something($database)
{
$html = "";
$sql = "SELECT user FROM table_name";
if ($result = mysqli_query($database, $sql)) {
while ($row = mysqli_fetch_assoc($result)) {
$html .= '<tr><td>' . $row['user'] . '</td></tr>';
}
}
return $html;
}
Hope this works for you!

html table mixed up when echo from while loop

HTML Table getting mixed up when having echo in while loop. Bellow attached the output image for that. How can i get normal table with loop result? Please note i am getting table data by searching with manual date.
<?php
if (isset($_POST['submit_date'])) {
if (empty($_POST['m_date'])) {
echo '<div class="alert alert-danger">Error: Select date then search</div>';
}else{
$m_date = $_POST['m_date'];
$q = mysqli_query($conn, "SELECT * FROM bazar_dor WHERE m_date='$m_date'");
echo '<table style="width:100%">
<tr>
  <th>Category Name</th>
  <th>Price</th>
  </tr>
<tr>
';
while ($row=mysqli_fetch_array($q)) {
$cat_name = $row['cat_name'];
$price = $row['price'];
echo '<td>'.$cat_name.'</td><td>'.$price.'</td>';
}
echo '</tr></table>';
}
}
?>
the output i am getting from this code
It looks like you'll need to include table rows (<tr>) inside your loop, as well. Your current code outputs all data on the same row, which undoubtedly breaks the table.
echo '<table style="width:100%">
<tr>
<th>Category Name</th>
<th>Price</th>
</tr>';
while ($row=mysqli_fetch_array($q)) {
$cat_name = $row['cat_name'];
$price = $row['price'];
echo '<tr><td>'.$cat_name.'</td><td>'.$price.'</td></tr>';
}
echo '</table>';
Replace your cede with this
<?php if (isset($_POST['submit_date'])) { if (empty($_POST['m_date'])) { echo '<div
class="alert alert-danger">Error: Select date then search</div>'; }else{ $m_date = $_POST['m_date'];
$q = mysqli_query($conn, "SELECT * FROM bazar_dor WHERE m_date='$m_date'");
echo '<table style="width:100%"> <tr>
  <th>Category Name</th>   <th>Price</th>   </tr> ';
while ($row=mysqli_fetch_array($q)) { $cat_name = $row['cat_name'];
$price = $row['price']; echo '<tr><td>'.$cat_name.'</td><td>'.$price.'</td><\tr>';
} echo '</table>';
} } ?>

php Multiple looping problem

Im writing a function to get the parent and child ids but for the third loop there is a
problem the loop gets even the previous loops id also .
How can i avoid it?
<?
$results = '
<table>
<thead>
<tr >
<td id="ticket" align="center" ><b>Task<br />ID</b></td>
<td id="ticket" align="center" ><b>col1</td>
<td id="ticket" align="center" ><b>col2</td>
</tr>
</thead>
<tbody>';
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{
$results .='
<tr >
<td align="center">
'.$row['Task_id'].'
</td>';
$results .= '<td align="center">';
$gg = mysqli_query($dbc,"select * from Tasks where ParentTask_Id='".$row['Task_id']."'");
echo "<br>";
while ($rowdd = mysqli_fetch_assoc($gg))
{
$results .= $rowdd['Task_id']."<br><br>";
$gg2 = mysqli_query($dbc,"select * from Tasks where ParentTask_Id='".$rowdd['Task_id']."'");
while ($rowdd2 = mysqli_fetch_assoc($gg2))
{
$results2 = $rowdd2['Task_id']."<br><br>";
}
echo "<br>";
}
// $results .= $car ;
// $results .= $t;
$results .='</td>';
$results .=' <td align="left" >'?>
<?
$results .= $results2;
$results .='</td>';
$results .='
</tr>';
}
?>
Is the $results variable empty? I only see it being concatenated.
Also, on your table you have multiple ids that are the same. You either need to change that to a class or have a unique value for each id.

Categories