Table function edit

beaphoenix

New member
Hi guys,

I leave here a code and the code image. What i need. If a user buy something from my website, his username it will be shown in a table. But i want to change this, and i want in the table to be shown the First and last name, not the username. Please help me. Thank you!
Code:
<table>
	<thead>
		<tr>
			<th><?php _e('Date', 'wc-lottery-pn') ?></th>
			<th><?php _e('Prenume', 'wc-lottery-pn') ?></th>
			<th><?php _e('Nume', 'wc-lottery-pn') ?></th>
			<?php if ($use_ticket_numbers === 'yes' ) :?>
				<th><?php _e('Ticket number', 'wc-lottery-pn') ?></th>
			<?php endif; ?>
			<?php if ($use_answers === true && 'yes' === get_option('lottery_answers_in_history', 'yes')  && ( 'no' === get_option('lottery_answers_in_history_finished', 'no') || $product->is_closed() === TRUE ) ) :?>
				<th><?php _e('Answer', 'wc-lottery-pn') ?></th>
				
			<?php endif; ?>
		</tr>
	</thead>
	<?php 
		$lottery_history = $product->lottery_history();
		
		if( $lottery_history ) {		 
		
			foreach ($lottery_history as $history_value) {

				echo "<tr>";
				echo "<td class='date'>".date_i18n( $date_format, strtotime( $history_value->date )).' '.date_i18n( $time_format, strtotime( $history_value->date ))."</td>";
				echo $history_value->userid ? "<td class='username'>". ( get_userdata($history_value->userid) ? get_userdata($history_value->userid)->display_name : '' ) ."</td>" : '';
				echo $history_value->userid ? "<td class=''>". ( get_userdata($history_value->userid) ? get_userdata($history_value->userid)->display_name : '' ) ."</td>" : '';

				if ($use_ticket_numbers === 'yes' ) {
					echo "<td class='ticket_number'>" . apply_filters( 'ticket_number_display_html' , $history_value->ticket_number, $product ) . "</td>";
				}
				
				if ( $use_answers === true && 'yes' === get_option('lottery_answers_in_history', 'yes')  && ( 'no' === get_option('lottery_answers_in_history_finished', 'no') || $product->is_closed() === TRUE ) ){
					$answer = isset( $answers[$history_value->answer_id] ) ? $answers[$history_value->answer_id] : false;
 

Attachments

  • siteimage.jpg
    siteimage.jpg
    29.2 KB · Views: 350
To get the user data you are using:
Code:
get_userdata($history_value->userid)
To make your changes you need to read this: https://developer.wordpress.org/reference/classes/wp_user/

Additionaly you can get the user once, because the code get it twice
 
Here's the updated code :

<table>
<thead>
<tr>
<th><?php _e('Date', 'wc-lottery-pn') ?></th>
<th><?php _e('Prenume', 'wc-lottery-pn') ?></th>
<th><?php _e('Nume', 'wc-lottery-pn') ?></th>
<?php if ($use_ticket_numbers === 'yes') : ?>
<th><?php _e('Ticket number', 'wc-lottery-pn') ?></th>
<?php endif; ?>
<?php if ($use_answers === true && 'yes' === get_option('lottery_answers_in_history', 'yes') && ( 'no' === get_option('lottery_answers_in_history_finished', 'no') || $product->is_closed() === TRUE )) : ?>
<th><?php _e('Answer', 'wc-lottery-pn') ?></th>
<?php endif; ?>
</tr>
</thead>
<?php
$lottery_history = $product->lottery_history();

if ($lottery_history) {
foreach ($lottery_history as $history_value) {
echo "<tr>";
echo "<td class='date'>".date_i18n($date_format, strtotime($history_value->date)).' '.date_i18n($time_format, strtotime($history_value->date))."</td>";
if ($history_value->userid) {
$user = get_userdata($history_value->userid);
$first_name = $user ? $user->first_name : '';
$last_name = $user ? $user->last_name : '';
echo "<td class=''>". $first_name ."</td>";
echo "<td class=''>". $last_name ."</td>";
}
if ($use_ticket_numbers === 'yes') {
echo "<td class='ticket_number'>" . apply_filters('ticket_number_display_html', $history_value->ticket_number, $product) . "</td>";
}
if ($use_answers === true && 'yes' === get_option('lottery_answers_in_history', 'yes') && ('no' === get_option('lottery_answers_in_history_finished', 'no') || $product->is_closed() === TRUE)) {
$answer = isset($answers[$history_value->answer_id]) ? $answers[$history_value->answer_id] : false;
// Display the answer if needed
}
echo "</tr>";
}
}
?>
</table>
 
Well, there are some logical issues with your code:
Please try this code once, it may resolve your error:

<table>
<thead>
<tr>
<th><?php _e('Date', 'wc-lottery-pn') ?></th>
<th><?php _e('Prenume', 'wc-lottery-pn') ?></th>
<th><?php _e('Nume', 'wc-lottery-pn') ?></th>
<?php if ($use_ticket_numbers === 'yes' ) :?>
<th><?php _e('Ticket number', 'wc-lottery-pn') ?></th>
<?php endif; ?>
<?php if ($use_answers === true && 'yes' === get_option('lottery_answers_in_history', 'yes') && ( 'no' === get_option('lottery_answers_in_history_finished', 'no') || $product->is_closed() === TRUE ) ) :?>
<th><?php _e('Answer', 'wc-lottery-pn') ?></th>

<?php endif; ?>
</tr>
</thead>
<?php
$lottery_history = $product->lottery_history();

if( $lottery_history ) {

foreach ($lottery_history as $history_value) {

echo "<tr>";
echo "<td class='date'>".date_i18n( $date_format, strtotime( $history_value->date )).' '.date_i18n( $time_format, strtotime( $history_value->date ))."</td>";
echo $history_value->userid ? "<td class='first-name'>". ( get_userdata($history_value->userid) ? get_userdata($history_value->userid)->first_name : '' ) ."</td>" : '';
echo $history_value->userid ? "<td class='last-name'>". ( get_userdata($history_value->userid) ? get_userdata($history_value->userid)->last_name : '' ) ."</td>" : '';

if ($use_ticket_numbers === 'yes' ) {
echo "<td class='ticket_number'>" . apply_filters( 'ticket_number_display_html' , $history_value->ticket_number, $product ) . "</td>";
}

if ( $use_answers === true && 'yes' === get_option('lottery_answers_in_history', 'yes') && ( 'no' === get_option('lottery_answers_in_history_finished', 'no') || $product->is_closed() === TRUE ) ){
$answer = isset( $answers[$history_value->answer_id] ) ? $answers[$history_value->answer_id] : false;
}
}
}
?>
</table>

Thanks
 
Nếu bạn cần dịch vụ vận chuyển quốc tế chất lượng, hãy liên hệ với Nhật Minh Express qua số điện thoại 0937 603 702. Tôi đã thử và không thất vọng. Họ là lựa chọn hàng đầu của tôi.
 
Back
Top