MySQL result data as link - php

I'm not really a good web programmer but I wanted to try.
I want to make my result from database as a link for example
I want "click here" as click would redirect to a new page or something.
and I think the idea is like.. a href"this.webpage.com">click here /a
my code is.
add_filter( 'the_content', 'get_scrapy_scraped' );
function get_scrapy_scraped( $content )
{
global $wpdb;
if ( ! is_page( 'jobs' ) )
return $content;
$table_name = $wpdb->prefix . "careers";
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
if ( ! $retrieve_data )
return $content;
$table = '<table><tr>
<th>Job Name</th>
<th>Location/Dept</th>
<th>Complete Info</th>
<th>Application Link<th>
</tr>';
foreach ( $retrieve_data as $row )
{
$table .= "<tr>
<td>{$row->Job_Name}</td>
<td>{$row->Job_Department}</td>
// I want this Job_Link_Info and Job_Link_Apply to be links naming Click for Details and Click to Apply respectively
<td>{$row->Job_Link_Info}</td>
<td>{$row->Job_Link_Apply}</td>
</tr>";
}
$table .= "</table>";
return $table . $content;
}

Assuming that $row->Job_Link_Info and $row->Job_Link_Apply are destinations for some sort you can just add an a tag and use that as the href attribute.
For example:
$table .= '<tr>
<td>{' . $row->Job_Name . '}</td>
<td>{' . $row->Job_Department . '}</td>
// I want this Job_Link_Info and Job_Link_Apply to be links naming Click for Details and Click to Apply respectively
<td>Click for info</td>
<td>Click to apply</td>
</tr>';

Related

Unable to retrieve status count after passing Mysql query

Currently I'm using CodeIgniter to retrieve my data in a particular timeframe. All these entries have a status. I am trying to get a count of all the data that are present in the particular time frame. Currently this is my model class where I have the following entry to return all the entries in a particular date range:
public function get_records($st_date,$end_date){
$this->db->select('*');
$this->db->from('crm_listings');
$this->db->where('cast(added_date as date) BETWEEN "' . $st_date . '" AND "' . $end_date . '" AND status= "D"');
return $this->db->count_all_results();
}
And my controller class:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
class Testcontroller extends CI_Controller
{
public function index()
{
$this->load->view('crm/test');
}
function fetch_status(){
$output ='';
$startDate = '';
$endDate = '';
$this->load->model('crm/user_model');
if($this->input->post('startDate')){
$startDate = $this->input->post('startDate');
}
if($this->input->post('endDate')){
$endDate = $this->input->post('endDate');
}
$data = $this->user_model->get_records($startDate,$endDate);
$output .= '
<div class="table-responsive">
<table class="table table-bordered table-striped">
<tr>
<th>Draft</th>
</tr>
';
if($data->num_rows() > 0)
{
$output .= '
<tr>
<td>'.$data.'</td>
</tr>
';
}
else
{
$output .= '<tr>
<td colspan="7">No Data Found</td>
</tr>';
}
$output .= '</table>';
echo $output;
}
}
?>
So as of now when I comment everything in my controller class after $data and just make a statement echo $data; I get the output I want. But when I'm wrapping it around <td>'.$data.'</td>, it doesn't give an output.
Edit your function at line echo $this->db->count_all_results();
function get_records($st_date,$end_date){
echo $this->db->count_all_results();// change this code
// to
return $this->db->get();
}
for make sure there is any data, try to print_r($data) after you call that method/function
$data = $this->user_model->get_records($startDate,$endDate);
print_r($data) // check data

Render table data to pdf in php

I have a an html form with a data table which has a pdf button on every row. I want to click that button and data for that particular row from mysql database is fetched and rendered into a table in pdf i.e. I want to render data array fetched from a table with a set id into a table in pdf. The code I wrote is rendering the first row incorrectly outside the table in pdf but all other rows correctly under their respective column (see below image)
Here is my code in view_payment_request.php;
<?php
if
(
isset($_GET["pdf"]) &&
isset($_GET['payment_request_id'])
)
{
require_once 'pdf.php';
include('database_connection.php');
include('function.php');
//-------------------------------------------------------------------------------------------------------------------------------
$output = '';
$statement = $connect->prepare
(
"
SELECT
*
FROM
tbl_payment_request
WHERE
payment_request_id = :payment_request_id
LIMIT 1
"
)
;
$statement->execute
(
array
(
':payment_request_id' => $_GET["payment_request_id"]
)
)
;
$result = $statement->fetchAll();
foreach($result as $row)
{
$output .=
'
<table width="100%" border="1" cellpadding="5" cellspacing="0">
<tr>
<th rowspan="2">Count</th>
<th rowspan="2">CRS/Site</th>
<th rowspan="2">Protocol</th>
<th rowspan="2">Budget Class</th>
<th rowspan="2">Amount</th>
</tr>
'
;
$statement = $connect->prepare
(
"
SELECT
*
FROM
tbl_cost_sharing
WHERE
payment_request_id = :payment_request_id
"
)
;
$statement->execute
(
array
(
':payment_request_id' => $_GET["payment_request_id"]
)
)
;
$cost_sharing_result = $statement->fetchAll();
$count = 0;
foreach($cost_sharing_result as $sub_row)
{
$count = $count + 1;
$output .=
'
<tr>
<td>'.$count.'</td>
<td>'.$sub_row["crs_site_id"].'</td>
<td>'.$sub_row["protocol_id"].'</td>
<td>'.$sub_row["budget_class_id"].'</td>
<td>'.$sub_row["cost_sharing_amount"].'</td>
</tr>
'
;
}
// Added Table Closing Tag
$output .=
'
</table>
'
;
}
//-------------------------------------------------------------------------------------------------------------------------------
$pdf = new Pdf();
$file_name = 'PRF#'.$row["payment_request_id"].'.pdf';
$pdf->loadHtml($output);
$pdf->render();
$pdf->stream($file_name, array("Attachment" => false));
}
?>
I have solved it by removing rowspans from the tags.

Return instead of echo in WP plugin

This is probably a stupid question, but I'm new to coding so here goes :-).
I'm trying to create a simple plugin for WordPress. The plugin gets data from a MySQL database and echos out a table with the results. My problem is when I use echo the plugin is places first on the page even if i put the shortcode in the middle of the page. I understand that is because I use echo instead of return. I just don't get how to use return in my case. Any help would be much appreciated :-).
Here's my code:
$get_runners = $connection->prepare('SELECT first_name, last_name, nick_name, FROM database WHERE status = :status ORDER BY first_name ASC');
$get_runners->execute([status=>'success']);
// Create the table
echo '
<table id="Table" class="start-list-table">
<thead>
<tr class="start-list-tr">
<th scope="col">Name</th>
<th scope="col">Club</th>
</tr>
</thead>
<tbody>
';
// Get the runner object:
$runners = $get_runners->fetchAll();
foreach($runners as $runner){
if($runner->nick_name)
{
$runner_name = $runner->first_name.' "'.$runner->nick_name.'" '.$runner->last_name;
}
else
{
$runner_name = $runner->first_name.' '.$runner->last_name;
}
echo '
<tr class="start-list-tr">
<td data-label="Name">'.$runner_name.'</td>
<td data-label="Club">'.$runner->club.'</td>
</tr>';
}
echo '</tbody>
</table>';
}
add_shortcode( 'startlist', 'create_startlist' );
You want to assign your output to a variable, instead of echoing:
$get_runners = $connection->prepare('SELECT first_name, last_name, nick_name, FROM database WHERE status = :status ORDER BY first_name ASC');
$get_runners->execute([status=>'success']);
// Create the table
$output = '
<table id="Table" class="start-list-table">
<thead>
<tr class="start-list-tr">
<th scope="col">Name</th>
<th scope="col">Club</th>
</tr>
</thead>
<tbody>
';
// Get the runner object:
$runners = $get_runners->fetchAll();
foreach($runners as $runner){
if($runner->nick_name)
{
$runner_name = $runner->first_name.' "'.$runner->nick_name.'" '.$runner->last_name;
}
else
{
$runner_name = $runner->first_name.' '.$runner->last_name;
}
$output .= '
<tr class="start-list-tr">
<td data-label="Name">'.$runner_name.'</td>
<td data-label="Club">'.$runner->club.'</td>
</tr>';
}
$output .= '</tbody>
</table>';
return $output;
}
add_shortcode( 'startlist', 'create_startlist' );
This uses concatenation to continue to fill the variable through your function. You then set the return to the $output variable.
Firstly read more about Shortcodes Output : https://codex.wordpress.org/Shortcode_API#Output
There are two ways that I can think of at this moment.
Using ob_start... basically you need to wrap you code in ob_start()
function create_startlist() {
ob_start();
/* CODE HERE */
return ob_get_clean();
}
Second is to use a concatenation operator
function create_startlist() {
$output = '';
$output .= 'OUTPUT HERE';
return $output;
}

Trying to retrieve data from wp DB

I am trying to retrieve data from wp database here with my plugin code below. I have manually added records in the db but my plugin doesn't want to retrieve my data. I don't know where am I going wrong here.
<?php
/**
* Plugin Name: Member Details
*/
function custom_view() {
global $wpdb;
echo '<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Bank</th>
<th>Account Number</th>
<th>Deposited Amount</th>
<th>Deposit Date</th>
<th>Period [Days]</th>
</tr>';
$results = $wpdb->get_results("select * from users");
foreach( $results as $user_data) {
// $roi = $user_data->amount * $user_data->period;
// $amount_growth = $roi - $user_data->amount;
echo "<tr>
<td>$user_data->user_nicename</td>
<td>$user_data->user_email</td>
<td>$user_data->bank</td>
<td>$user_data->account_num</td>
<td>$user_data->amount</td>
<td>$user_data->deposit_date</td>
<td>$user_data->period</td>
<td></td>
<td></td>
</tr>";
}
echo '</table>';
}
add_shortcode('views', 'custom_view');
?>
Can somebody help me here? Thanks
Two important things:
1° your data was not returning because you did not pass the table prefix, ie the table name was wrong.
Example "users" is correct "wp_users" or the prefix you used when creating in the database.
You can use the $wpdb->users variable which will automatically return it with the prefix you registered in wp
2° shortcodes must always end in a return and not in echo.
I left an example for you to call with php manually if you want to use it. but you can use it directly in the editor just by calling shortcode normally :)
If my answer helps you. Vote and put as right answer to close this question please :)
<?php
/**
* Plugin Name: Member Details
* Plugin URI: https://mysite.co.za
* Description: All Member Details List
* Version: 1.0
* Author: Empire Investment
* Author URI: https://mysite.co.za
*/
function custom_view() {
global $wpdb;
$output .= '<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Bank</th>
<th>Account Number</th>
<th>Deposited Amount</th>
<th>Deposit Date</th>
<th>Period [Days]</th>
</tr>';
$results = $wpdb->get_results("select * from $wpdb->users");
foreach( $results as $user_data) {
// $roi = $user_data->amount * $user_data->period;
// $amount_growth = $roi - $user_data->amount;
$output .= "<tr>
<td>$user_data->user_nicename</td>
<td>$user_data->user_email</td>
<td>$user_data->bank</td>
<td>$user_data->account_num</td>
<td>$user_data->amount</td>
<td>$user_data->deposit_date</td>
<td>$user_data->period</td>
<td></td>
<td></td>
</tr>";
}
$output .= '</table>';
return $output;
}
add_shortcode('views', 'custom_view');
//example usae with php
echo do_shortcode( $content, '[custom_view]' );

Shortcode won't render in correct place

I'm trying to output my shortcode content before the main content body, I simply cannot get it to work and nor can I find the answer.
If I use a simple string or number, it outputs in the correct place but when I try to output the table html it renders below the content.
EDIT: I've just checked the source code - the code renders in the correct order but is shown differently. I'm really confused.
My code is as follows:
/* SHORTCODES */
//inject code on initialization
add_action('init', 'ebs_register_shortcodes');
//register shortcodes
function ebs_register_shortcodes() {
add_shortcode('offer_table', 'ebs_table_shortcode');
}
function ebs_table_shortcode() {
$page_offers = get_post_meta( get_the_ID(), 'offers_on_page', true);
$rank = 1;
//Table Content
$table = '<table>
<thead>
<tr>
<th>Rank</th>
<th>Brands</th>
<th>User Rating</th>
<th>Signup Bonus</th>
<th>Key Features</th>
<th>Play Now</th>
</tr>
</thead>
<tbody>
<tr>';
foreach ($page_offers as $index => $page_id) {
$table .= '<td>'.$rank.'</td>';
$table .= '<td>'.get_field('ebs_brand_logo', $page_id).'</td>';
if ( get_field('ebs_launch_date' !== '') ) {
$table .= '<td>'.get_field('ebs_launch_date', $page_id).'</td>';
}
$table .= '<td>'.get_field('ebs_rating', $page_id).'</td>';
//echo '<td>'.get_field('ebs_review_link', $page_id).'</td>';
$table .= '<td>'.get_field('ebs_signup_bonus', $page_id).'</td>';
$table .= '<td>'.get_field('ebs_key_features', $page_id).'</td>';
$table .= '<td>'.get_field('ebs_clean_link', $page_id).'</td>';
//$table .= '<td>'.get_field('ebs_brand_name', $page_id).'</td>';
$table .= '</tr>';
$rank++;
}
$table .= '</tbody><br>';
//return the table
return $table;
}
Thankyou in advance for any help!
SOLVED!
The issue was a missing tag.

Categories