In WordPress, I have a table called wp_users
and I want to show all of them in a custom html table. So, my query goes like this
<table>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td> Email </td>
</tr>
<?php
global $wpdb;
$rows = $wpdb->get_results( "SELECT * FROM wp_users");
foreach ( $rows as $row ) ?>
<tr>
<td>echo $row->first_name;</td>
<td>echo $row->first_name;</td>
<td> echo $row->first_name;</td>
</tr>
<?php
}
?>
This one is giving result for all the first_name inside the first table like this
<tr>
<td>Test 1Test 2</td>
<td>Test 12Test 2</td>
<td> test#test.comtest2#test.com</td>
</tr>
but I want the result be like this
<table>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td> Email </td>
</tr>
<tr>
<td>Test 1</td>
<td>Test 12</td>
<td> test#test.com</td>
</tr>
<tr>
<td>Test 2</td>
<td>Test 2</td>
<td> test2#test.com</td>
</tr>
</table>
So, Can someone tell me how to do this?
Any help and suggestions wll be really appreciable. Thanks
The answer is simple you just have to give the correct columnname inside foreach loop.
Right now you are just trying to print the firstname. And there is a bracket"{" missing after the foreach loop
<table>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td> Email </td>
</tr>
<?php
global $wpdb;
$rows = $wpdb->get_results( "SELECT * FROM wp_users");
foreach ( $rows as $row )
{
?>
<tr>
<td>echo $row->first_name;</td>
<td>echo $row->Lastname;</td>
<td> echo $row->Email;</td>
</tr>
<?php }
?>
Related
I want to print a section of a table if the data is available in JSON file for it. And if the data is not there and it shouldn't do anything.
So, far what I am doing is as below. Checking is the account_type is parent and then generate the table rows with corresponding data.
<?php
$str = file_get_contents('test.json');
$json = json_decode($str, true);
$parent= strtr('<tr>
<td colspan="2" style="color:#263a80;font-size:19px;padding-bottom:20px;padding-top:20px">Parent Information</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">First Name</td>
<td>#fname</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">Last Name</td>
<td>#lname</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">Mobile Number</td>
<td>#mobile</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">Email</td>
<td><a #email target="_blank" rel="noreferrer">#email</a></td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">Country</td>
<td>#country</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">State</td>
<td>#state</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">City</td>
<td>#city</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">Postal Code</td>
<td>#pcode</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">Full Home Address</td>
<td>#address</td>
</tr>', ["#fname"=>$json["json"]["register_session_1"]["first_name"],
"#lname"=>$json["json"]["register_session_1"]["last_name"],
"#mobile"=>$json["json"]["register_session_1"]["mobile"],
"#email"=>$json["json"]["register_session_1"]["email"],
"#country"=>$json["json"]["register_session_1"]["country"],
"#state"=>$json["json"]["register_session_1"]["state_online"],
"#city"=>$json["json"]["register_session_1"]["city"],
"#pcode"=>$json["json"]["register_session_1"]["postal_code"],
"#address"=>$json["json"]["register_session_1"]["address"]
]);
if ($json["json"]["register_session_1"]["account_type"]==="parent"){
echo $parent;
}
else {
echo "hahahah";
}
?>
Problem is that it printing the table data anyway. I don't know what I am doing wrong. The if-else condition is not working.
I have this code and I can't seem to figure out why. I have added this code in between the <table></table> tag.
I have a table named 'entries' with the columns: originator, collaborator, engagement_partner, niche, and fee_potential. Some of the rows in the table have the same originator. I have a form that someone fills out, submits it to the database. I have tried using GROUP BY, but need some assistance on this.
My WordPress SQL statement: This will be used in the WordPress function file.
global $wpdb;
$rows = $wpdb->get_results( "SELECT * FROM entries group by originator");
I need to display the data in a table format below. Basically, I need to group each entry based on Originator that are the same.
<table class="table">
<thead>
<tr>
<th>Originator</th>
<th>Collaborator</th>
<th>engagement_partner</th>
<th>Niche</th>
<th>Fee Potential</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>First Originator</b></td>
<td>Company Name</td>
<td>Engagement Partner</td>
<td>Web</td>
<td>$40000</td>
</tr>
<tr>
<td><b>First Originator</b></td>
<td>Company Name</td>
<td>Engagement Partner</td>
<td>Web</td>
<td>$40000</td>
</tr>
<tr>
<td><b>First Originator</b></td>
<td>Company Name</td>
<td>Engagement Partner</td>
<td>Web</td>
<td>$40000</td>
</tr>
</tbody>
</table>
<br>
<br>
<table class="table">
<thead>
<tr>
<th>Originator</th>
<th>Collaborator</th>
<th>engagement_partner</th>
<th>Niche</th>
<th>Fee Potential</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>Second Originator</b></td>
<td>Company Name</td>
<td>Engagement Partner</td>
<td>Web</td>
<td>$40000</td>
</tr>
<tr>
<td><b>Second Originator</b></td>
<td>Company Name</td>
<td>Engagement Partner</td>
<td>Web</td>
<td>$40000</td>
</tr>
</tbody>
</table>
I suggest you fetch all the entries and group them with php like so;
global $wpdb;
$rows = $wpdb->get_results( "SELECT * FROM entries");
$originators = [];
foreach($rows as $row)
{
$originators[$row->originator][] = $row;
}
then loop through the originators to fill the table, something like this:
<?php
foreach($originators as $originator => $entries){
?>
<table class="table">
<thead>
<tr>
<th>Originator</th>
<th>Collaborator</th>
<th>engagement_partner</th>
<th>Niche</th>
<th>Fee Potential</th>
</tr>
</thead>
<tbody>
<?php foreach($entries as $entry){ ?>
<tr>
<td><b> <?php echo $entry->originator; ?></b></td>
<td><?php echo $entry->collaborator; ?></td>
<td><?php echo $entry->engagement_partner; ?></td>
<td><?php echo $entry->niche; ?></td>
<td><?php echo $entry->fee_potential;?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php}?>
It doesn't looks like a GROUP BY statement is what you are looking for.
Seeing your example HTML structure, the thing you are looking for is an ordered list of rows. You can then use php to sort the results and then use them
global $wpdb;
$rows = $wpdb->get_results( "SELECT * FROM `entries`");
$sortedRows = [];
foreach($rows as $row) {
if (!isset($sortedRows[$row->originator])) {
$sortedRows[$row->originator] = [];
}
$sortedRows[$row->originator][] = $row;
}
a different approach is using ORDER BY in the query, which i won't fully work out since i dont know what your approach is on creating the HTML structure:
global $wpdb;
$rows = $wpdb->get_results( "SELECT * FROM `entries` ORDER BY `originator` ASC");
$currentOriginator = NULL;
foreach ($rows as $row) {
if ($row->originator !== $currentOriginator) {
//create table
$currentOriginator = $row->originator;
}
//add the row to the table
}
I need some help with how I can soft query from sql.
I have a php script with an html page who is showing the result from the query.
I have this query:
$ready_orders = DB::query( 'SELECT * FROM ordertable where orderid is NOT null ORDER by id DESC')->fetchAll( DB::FETCH_ASSOC );
From the HTML I have this;
<tal:block tal:condition="exists:orders">
<table>
<tr>
<td>amount</td>
<td> want counter here</td>
<td>result 1</td>
<td>result 2</td>
<td>result 3</td>
<td>result 4</td>
</tr>
<tr tal:repeat="order orders">
<td tal:content="">
....
</td>
<td tal:content="order/id">
....
</td>
<td tal:content="order/orderid">
....
</td>
<td tal:content="order/productid">
....
</td>
<td tal:content="order/processed">
....
</td>
<td>
</td>
</tr>
</table>
The query is doing all I want, but need the result to be in a listing.
Like a counter 1,2,3,4,5 etc
Is it possible?
include a
select count * from ordertable
Then insert this result in the first
SELECT COUNT(orderid) AS Order_Count
FROM ordertable
WHERE orderid IS NOT NULL
using foreach loop
<tal:block tal:condition="exists:orders">
<table>
<tr>
<td>amount</td>
<?php
$i = 1;
foreach($ready_orders as $result){ ?>
<td>result <?php echo $i++; ?></td> //your counter
<?php } ?>
</tr>
<tr tal:repeat="order orders">
<td tal:content="">
....
</td>
<td tal:content="order/id">
....
</td>
<td tal:content="order/orderid">
....
</td>
<td tal:content="order/productid">
....
</td>
<td tal:content="order/processed">
....
</td>
<td>
</td>
</tr>
I'm making a brochure generator based on inventory in a database. Due to the way the on-paper brochure looks, the order of items display will not match the way mysql_fetch_array works. Each user chosen category holds four items in a 2X2 grid.
Category 1 ---------Category 2
[1] [2] ---------[5] [6]
[3] [4] ---------[7] [8]
Category 3 ---------Category 4
[9] [10] ---------[13] [14]
[11] [12] ---------[15] [16]
I've already created a basic table as a placeholder for the items in this pattern. This is my query to retrive the items.
$query = "SELECT t1.*, image_path FROM flyer_item AS t1
LEFT JOIN product_images AS t2 ON t1.product_id = t2.product_id WHERE id_page = '".$id_page."'";
echo $query."<br>";
$result = mysql_query($query);
echo '<h3 class = "splitter">Items</h3>';
//-create while loop and loop through result set
//Due to the unique item arrangement patter, we fill
//an array with the fetched array results.
$iArray = array();
while($row=mysql_fetch_array($result))
{
$iArray[] = $row;
$square = $row['square'];
$item_name = $row['item_name'];
$sales_info = $row['sales_info'];
$link = $row['image_path'];
$sku_item_number = $row['sku_item_number'];
if (empty($link))
{
$link = '../imagen/no_imagen.gif';
}
}
One page can hold up to a maximum of 24 items, all following the pattern mentioned above to make sure items are in their respective categories. I'm contemplating making another database table called Categories that will store its four items, but is there a way I can use the second array, iArray, to store the number indicating where the items should go?
The actual table looks like this, to match the original document.
<table width="100%" border="1" bordercolordark="#000000" bordercolorlight="#000000">
<tr>
<td> </td>
<td colspan="2" align="center"><?php echo $category1; ?></td>
<td> </td>
<td colspan="2" align="center"><?php echo $category2; ?></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>Item 1</td>
<td>Item 2</td>
<td> </td>
<td>Item 5</td>
<td>Item 6</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>Item 3</td>
<td>Item 4</td>
<td> </td>
<td>Item 7</td>
<td>Item 8</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td colspan="2" align="center"><?php echo $category3; ?></td>
<td> </td>
<td colspan="2" align="center"><?php echo $category4; ?></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>Item 9</td>
<td>Item 10</td>
<td> </td>
<td>Item 13</td>
<td>Item 14</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>Item 11</td>
<td>Item 12</td>
<td> </td>
<td>Item 15</td>
<td>Item 16</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td colspan="2" align="center"><?php echo $category5; ?></td>
<td> </td>
<td colspan="2" align="center"><?php echo $category6; ?></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>Item 17</td>
<td>Item 18</td>
<td> </td>
<td>Item 21</td>
<td>Item 22</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>Item 19</td>
<td>Item 20</td>
<td> </td>
<td>Item 23</td>
<td>Item 24</td>
<td> </td>
</tr>
</table>
I've been struggling with this for around 4 hours now...
What I'm trying to establish is pretty simple, I have a news table, I want to display the title of the news, the content, and a read more link, I know how to loop through a table and force , but this won't work in my case, the table should look like this in the end:
<table>
<tr>
<td>header one</td>
<td>header 2</td>
<td>header 3</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td>text one</td>
<td>text two</td>
<td>text 3</td>
</tr>
<tr>
<td>read more</td>
<td>read more </td>
<td>read more</td>
</tr>
</table>
What I have so far in my php is a code that will generate the rows and columns, but I want them to be distributed just like the sample table above in order not to mess the alignment of the text and the read more link ...
Here's my php code :
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<?php while ($record = mysql_fetch_assoc($result)): ?>
<?php $style++ ?>
<td width="33%" valign="top">
<h6><?php echo $record['title'] ?></h6>
<div class="service-sum"><?php echo $record['content'] ?></div>
<div class="findout">> find out more</div>
</td>
<?php if ($style == 3): ?>
</tr>
<?php $style = 0; ?>
<tr>
<td>
<div style="height:30px;"></div>
</td>
</tr>
<tr>
<?php endif ?>
<?php endwhile ?>
This one is working fine, but i'm displaying the title and the content and the read more link in one column, these should be distributed into 3 for design purposes...
Any help would be much appreciated, I looked all over the net and I can't find a solution for that!
try using this alignment. The design is almost same as yours.
<table>
<tr>
<td>
<table>
<tr>
<td>header one</td>
</tr>
<tr>
<td>text one</td>
</tr>
<tr>
<td>read more</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>header 2</td>
</tr>
<tr>
<td>text 2</td>
</tr>
<tr>
<td>read more</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>header 3</td>
</tr>
<tr>
<td>text 3</td>
</tr>
<tr>
<td>read more</td>
</tr>
</table>
</td>
</tr>
</table>
if there are only 3 records, the following should be fine. if there are more, some changes should be made. Please check your php code.
<table>
<tr>
<?php while ($record = mysql_fetch_assoc($result)) { ?>
<td>
<table>
<tr>
<td><?php echo $record['title'] ?></td>
</tr>
<tr><td height = "30px;"></td></tr>
<tr>
<td><?php echo $record['content'] ?></td>
</tr>
<tr>
<td>> find out more</td>
</tr>
</table>
</td>
<?php } ?>
</tr>
</table>