• I have a custom page template with html table.

    I wrote a PHP for numeric pagination (e.g. 1 2 3 4 … 9 10) in a html table. But the snippet is not working. I got only a blank table and no pagination.

    Here’s the snippet:

    $rows_per_page = 2;
    $current = (intval(get_query_var(‘paged’))) ? intval(get_query_var(‘paged’)) : 1;

    $pagination_args = array(
    ‘base’ => @add_query_arg(‘paged’,’%#%’),
    ‘format’ => ”,
    ‘total’ => ceil(sizeof($rows)/$rows_per_page),
    ‘current’ => $current,
    ‘show_all’ => false,
    ‘type’ => ‘plain’,
    );

    if($wp_rewrite->using_permalinks())
    $pagination_args[‘base’] = user_trailingslashit(trailingslashit(remove_query_arg(‘s’,get_pagenum_link(1))) . ‘page/%#%/’, ‘paged’);

    if(!empty($wp_query->query_vars[‘s’]))
    $pagination_args[‘add_args’] = array(‘s’=>get_query_var(‘s’));

    echo paginate_links($pagination_args);

    $start = ($current – 1) * $rows_per_page;
    $end = $start + $rows_per_page;
    $end = (sizeof($rows) < $end) ? sizeof($rows) : $end;

    if (isset($_POST[‘list_position’]) && $_POST[‘list_position’] != ‘Select by Position’){
    $list_position= $_POST[‘list_position’];
    $result_position= $wpdb->get_results($wpdb->prepare(“SELECT DISTINCT id, submit_time, last_name, first_name, middle_name, mobile_number, email, location, position, process_resume, message, attachment_resume_id FROM resume_databank WHERE position= ‘” . $list_position . “‘ ORDER BY position ASC”, OBJECT));
    echo ‘<table>’;
    echo ‘<tr>’;
    $dir=””;
    $file=””;
    $optionId = 0;
    echo ‘<th>Submit Time</th>’;
    echo ‘<th>Last Name</th>’;
    echo ‘<th>First Name</th>’;
    echo ‘<th>Middle Name</th>’;
    echo ‘<th>Mobile Number</th>’;
    echo ‘<th>Email</th>’;
    echo ‘<th>Location</th>’;
    echo ‘<th>Position</th>’;
    echo ‘<th>Message</th>’;
    echo ‘<th>Resume</th>’;
    echo ‘<th>Processed?</th>’;
    foreach ($result_position as $record_s){
    $optionId++;
    for ($i=$start;$i < $end ;++$i){
    $record_s = $result_position[$i];
    //$optionId = $record_s[‘id’];
    echo ‘<tr>’;
    echo ‘<td id=”submit_time”>’ . $record_s->submit_time . ‘</td>’;
    echo ‘<td id=”last_name”>’ . $record_s->last_name . ‘</td>’;
    echo ‘<td id=”first_name”>’ . $record_s->first_name . ‘</td>’;
    echo ‘<td id=”middle_name”>’ . $record_s->middle_name . ‘</td>’;
    echo ‘<td id=”mobile_number”>’ . $record_s->mobile_number . ‘</td>’;
    echo ‘<td id=”email”>’ . $record_s->email . ‘</td>’;
    echo ‘<td id=”location”>’ . $record_s->location . ‘</td>’;
    echo ‘<td id=”position”>’ . $record_s->position . ‘</td>’;
    echo ‘<td id=”message”>’ . $record_s->message . ‘</td>’;
    echo ‘<td id=”resumeFile’.$optionId.'”>attachment_resume_id) . ‘>Download Resume</td>’;
    echo ‘<td id=”radioOption><label for=”Yes”>Yes</label>
    <input type=”radio” id=”processedOptionYes’.$optionId.'” name=”processedOption” value=”Yes” onclick=”proccessedCheck(‘.$optionId.’,\’Yes\’)”/>
    <label for=”No”>No</label>
    <input type=”radio” id=”processedOptionNo’.$optionId.'” name=”processedOption” value=”No” onclick=”proccessedCheck(‘.$optionId.’,\’No\’)”/></td>’;
    echo ‘</tr>’;
    } }
    echo ‘</table>’;
    }

Viewing 1 replies (of 1 total)
  • Thread Starter UserDG

    (@tenkepadu)

    I update the PHP snippet, but still not working, it displayed the duplicate of the data from the database instead of display a numeric pagination

    Snippet update:

    <?php
    $per_page = 2;
    
    $row_count = $wpdb->get_var("SELECT COUNT(*) FROM resume_databank");
    $total_results = $wpdb->get_var($row_count);
    $total_pages = ceil($total_results / $per_page);
    
    if (isset($_GET['page'])) {
    $show_page = $_GET['page'];
    	if ($show_page > 0 && $show_page <= $total_pages){
    		$start = ($show_page - 1) * $per_page;
    		$end = $start + $per_page;
    	} else {
    		$start = 0;
    		$end = $per_page;
    	}
    } else {
    	$start = 0;
    	$end = $per_page;
    }
    	$page = intval($_GET['page']);
    	$tpages=$total_pages;
    	if ($page <= 0)
    	$page = 1;
    
    $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
    echo '<div class="pagination"><ul>';
    if ($total_pages > 1){
    	echo paginate($reload, $show_page, $total_pages);
    }
    	echo "</ul></div>";
    
    if (isset($_POST['list_position']) && $_POST['list_position'] != 'Select by Position'){
    	$list_position= $_POST['list_position'];
    	$result_position= $wpdb->get_results($wpdb->prepare("SELECT DISTINCT id, submit_time, last_name, first_name, middle_name, mobile_number, email, location, position, message, attachment_resume_id FROM resume_databank WHERE position= '" . $list_position . "' ORDER BY position ASC", OBJECT));
    		echo '<table>';
    		echo '<tr>';
    		$dir="";
    		$file="";
    		$optionId = 0;
    		echo '<th>Submit Time</th>';
    		echo '<th>Last Name</th>';
    		echo '<th>First Name</th>';
    		echo '<th>Middle Name</th>';
    		echo '<th>Mobile Number</th>';
    		echo '<th>Email</th>';
    		echo '<th>Location</th>';
    		echo '<th>Position</th>';
    		echo '<th>Message</th>';
    		echo '<th>Resume</th>';
    		echo '<th>Processed?</th>';
    	for ($i = $start; $i < $end; $i++){
    		foreach ($result_position as $record_s){
    			$optionId++;
    			//$optionId = $record_s->id;
    			echo '<tr>';
    			echo '<td id="submit_time">' . $record_s->submit_time . '</td>';
    			echo '<td id="last_name">' . $record_s->last_name . '</td>';
    			echo '<td id="first_name">' . $record_s->first_name . '</td>';
    			echo '<td id="middle_name">' . $record_s->middle_name . '</td>';
    			echo '<td id="mobile_number">' . $record_s->mobile_number . '</td>';
    			echo '<td id="email">' . $record_s->email . '</td>';
    			echo '<td id="location">' . $record_s->location . '</td>';
    			echo '<td id="position">' . $record_s->position . '</td>';
    			echo '<td id="message">' . $record_s->message . '</td>';
    			echo '<td id="resumeFile'.$optionId.'"><a href=' . wp_get_attachment_url($record_s->attachment_resume_id) . '>Download Resume</a></td>';
    			echo '<td id="radioOption><label for="Yes">Yes</label>
    					  <input type="radio" id="processedOptionYes'.$optionId.'" name="processedOption" value="Yes" onclick="proccessedCheck('.$optionId.',\'Yes\')"/>
    					  <label for="No">No</label>
    					  <input type="radio" id="processedOptionNo'.$optionId.'" name="processedOption" value="No" onclick="proccessedCheck('.$optionId.',\'No\')"/></td>';
    			echo '</tr>';
    		}
    			echo '</table>';
    	}
    }
    
    function paginate($reload, $page, $tpages){
    $adjacents = 2;
    $prevlabel = "? Prev";
    $nextlabel = "Next ?";
    $out = "";
    // previous
    if ($page == 1){
    	$out.= "<span>".$prevlabel."</span>\n";
    } elseif ($page == 2){
    	$out.="<li><a href=\"".$reload."\">".$prevlabel."</a>\n</li>";
    } else {
    	$out.="<li><a href=\"".$reload."&page=".($page - 1)."\">".$prevlabel."</a>\n</li>";
    }
    
    $pmin=($page>$adjacents)?($page - $adjacents):1;
    $pmax=($page<($tpages - $adjacents))?($page + $adjacents):$tpages;
    
    for ($i = $pmin; $i <= $pmax; $i++){
    	if ($i == $page) {
    		$out.= "<li class=\"active\"><a href=''>".$i."</a></li>\n";
    	} elseif ($i == 1) {
    		$out.= "<li><a href=\"".$reload."\">".$i."</a>\n</li>";
    	} else {
    		$out.= "<li><a href=\"".$reload. "&page=".$i."\">".$i. "</a>\n</li>";
    	}
    }
    
    if ($page<($tpages - $adjacents)){
    	$out.= "<a style='font-size:11px' href=\"" . $reload."&page=".$tpages."\">" .$tpages."</a>\n";
    }
    
    if ($page < $tpages){
    	$out.= "<li><a href=\"".$reload."&page=".($page + 1)."\">".$nextlabel."</a>\n</li>";
    } else {
    	$out.= "<span style='font-size:11px'>".$nextlabel."</span>\n";
    }
    	$out.= "";
    	return $out;
    }
    ?>

    [closed as duplicate of https://www.ads-software.com/support/topic/how-to-add-a-pagination-properly-using-php-2?replies=2 – please start only one topic per problem]

Viewing 1 replies (of 1 total)
  • The topic ‘How to add a pagination properly using PHP?’ is closed to new replies.