Shortcode won't render in correct place - php

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.

Related

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;
}

How to pass values to $_GET from a drop-down list from a table

the situation I am facing is like that:
I read data from a database and dump them into a table in a webpage. And then, according to a column's value, show a drop-down list or not. And write the value from the drop-down list back into the database.
I use ...Submit, but the $_GET has no values about the drop-down list.
I list the whole code set here.
Any help is highly appreciated!
$GCdataSQL = "select * from table_name";
$GCdata = new Query($GCdataSQL);
$table = "<form method='GET'><table class='datatable table table-condensed ' id='query-data'><thead><tr>";
$ColName = array('col1','col2','col3','col4', 'col5', 'col6', 'col7', 'col8');
foreach ($ColName as $Name){
$table .= "<th>$Name</th>";
}
$table .= "</tr></thead><tbody>";
while ($GCdataRow = $GCdata->fetchRow()){
$emplid = $GCdataRow['emplid'];
$term = $GCdataRow['term'];
$handled = $GCdataRow['handled'];
if ($GCdataRow['code'] == 'UNKNOW' || $GCdataRow['code'] == 'Not Repeated'){$GCdataRow['code']= '';}
if ($GCdataRow['local_action'] !== $GCdataRow['new_action']){
$table .= "<tr bgcolor= 'LightPink'>";
}
else {
$table .= "<tr>";
}
foreach($GCdataRow as $key=>$value){
if ($key == 'emplid'){
$table .="<td><a href='link' target = '_blank'>$value</a></td>";
}
else if ($key == 'new_action'){
$table .="<td><a href='link' target = '_blank'>$value</a></td>";
}
else if ($key == 'handled' && $handled == 'Not Reviewed'){
$table .="<td><select name = 'handlecode'><option value = 'Not Reviewed'>Not Reviewed</option><option value = 'Handled by Script'>Handled by Script</option><option value = 'Handled Manually'>Handled Manually</option><option value = 'Leave It'>Leave It</option></select></td>";
}
else {
$table .= "<td>".htmlentities($value)."</td>";
}
}
$table .= "</tr>";
$loopcount++;
}
if(isset($table)) {$table .= "</tbody></table>";}
echo $table;
echo "<button type='submit' class = 'btn btn-primary' style = 'position: absolute; left:50%;' >Submit</button></form>";
finally I changed the form method to POST, and everything is fine! Now the values are in $_POST, and I could use them to insert into the database!
Thank you!

Wrap div if MYSQL column same

I've created a pretty standard output script that spits out a list from my database WHERE bv_category="Smart" and they are also ordered by subcategory+id.
All is well and good, but I'd like to further enhance this by splitting this up into separate divs so as to provide more grouping -- "visually". It's a pretty standard table at this point.
So for those with the same bv_subcategory would be placed into a separate div container. I'd style accordingly each one. Any tips on achieving this?
I tried an if within the for each, but that would insert a div for each one where as I'd want to wrap each one that has the same bv_subcategory value.
$query = "SELECT * FROM wp_products_table_ba_BV_unique";
if (! empty ( $sql_filter )) {
$query .= ' WHERE bv_category="Smart" AND ' . implode ( ' AND ', $sql_filter ) . ' ORDER BY bv_subcategory ASC, id ASC';
}
else {
$query = "SELECT * FROM wp_products_table_ba_BV_unique WHERE bv_category='Smart' ORDER BY bv_subcategory ASC, id ASC";
}
echo "SQL been Called : <br/>";
echo "<b>" . $query . "</b>";
$products = $wpdb->get_results ( $query );
$prodTable_Build = "<table id='myTable' class='display' cellspacing='0' width='100%'>";
$prodTable_Build .= "<thead class='productTableHead'>
<tr>
<th>Product Code</th>
<th>Product Name</th>
<th>Pack Size</th>
<th>Exhibit A Grain<br />OZ. Equivalent</th>
<th>Grain OZ.<br />Equivalent</th>
<th> </th>
</tr>
</thead>";
foreach ( $products as $product ) {
$bv_product_num = stripslashes ($product->bv_product_num);
$bv_product_num_fix = str_pad($bv_product_num, 5, '0', STR_PAD_LEFT);
$bv_product_iw_bulk = stripslashes ($product->bv_product_iw_bulk);
if($bv_product_iw_bulk == 1) {
$bv_product_iw_bulk = "Bulk";
}
else
$bv_product_iw_bulk = "IW";
$bv_id = stripslashes ($product->id);
$bv_wg = stripslashes ($product->bv_wg);
$bv_rf = stripslashes ($product->bv_rf);
$bv_lf = stripslashes ($product->bv_lf);
$bv_product_name = stripslashes ($product->bv_product_name);
$bv_product_flavor = stripslashes ($product->bv_product_flavor);
$bv_catering = stripslashes ($product->bv_catering);
$bv_commodity_processed = stripslashes ($product->bv_commodity_processed);
$bv_ex_a_equiv = stripslashes ($product->bv_ex_a_equiv);
$bv_ex_a_equiv = number_format((float)$bv_ex_a_equiv, 2, '.', '');
$bv_grain_equiv = stripslashes ($product->bv_grain_equiv);
$bv_grain_equiv = number_format((float)$bv_grain_equiv, 2, '.', '');
$bv_category = stripslashes ($product->bv_category);
$bv_subcategory = stripslashes ($product->bv_subcategory);
$bv_alliance = stripslashes ($product->bv_alliance);
$upload_image1 = stripslashes ($product->upload_image1);
$bv_pack_oz = (float)stripslashes ($product->bv_pack_oz);
$bv_pack_quant = stripslashes ($product->bv_pack_quant);
$bv_attached_pdf = stripslashes ($product->bv_attached_pdf);
$bv_image_main = stripslashes ($product->bv_image_main);
$bv_image_extra = stripslashes ($product->bv_image_extra);
$prod_desc = stripslashes ($product->prod_desc);
if($bv_alliance == 1) {
$alianFix = "<img src='".WP_PLUGIN_URL."/bv-product-crud/icons/alliance-icon.png' />";
}
else{
$alianFix = "";
}
if ( is_user_logged_in() ) {
$uploadimageFix = "<a href='$bv_attached_pdf' target='_blank'>PDF</a>";
} else {
$uploadimageFix = "<a href='wp-login.php'>Log In</a>";
}
$prodTable_Build .= "<tr>";
$prodTable_Build .= "<td>$bv_product_num_fix $bv_product_iw_bulk</td>";
$prodTable_Build .= "<td><b>$alianFix $bv_wg $bv_rf $bv_lf $bv_product_name</b> <!--- $bv_subcategory--></td>";
$prodTable_Build .= "<td>$bv_pack_oz oz./$bv_pack_quant</td>";
$prodTable_Build .= "<td>$bv_ex_a_equiv</td>";
$prodTable_Build .= "<td>$bv_grain_equiv</td>";
$prodTable_Build .= "<td>$uploadimageFix</td>";
$prodTable_Build .= "</tr>";
}
$prodTable_Build .= "</tbody></table>";
$prodTable_Build .= "</div>";
echo $prodTable_Build;
You get the idea
$query = "SELECT * FROM ORDER BY `bv_subcategory`";
$temp = row['bv_subCategory'];
<div>
//in loop
if($temp != row['bv_subcategory'])
{
echo "</div><div>"
$temp = $row['bv_subcategory'];
}
//after loop
</div>

MySQL result data as link

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>';

How to split part of table into 2 columns over specific number of td's with PHP?

The site I'm working on wants data organized in a specific way. I need to split it into two columns if it's over 8 td's long. Here is my code right now. I've put it into an array as I had an idea about doing that and using the count to display data but I haven't figured out how to make that work.
$resultFound = false;
$prevWeek = -1;
$count = 0;
while($row = mysqli_fetch_array($result)) {
$adjustedWeek = dateToWeek($row['date']) - 35;
if($_GET['keywords'] != "") {
$id = search($row, $_GET['keywords']);
if($row['id'] == $id) {
if($validWeek) {
if($week == $adjustedWeek) {
include('test2.php');
}
}
else {
include('test2.php');
}
}
}
foreach($tableArray as $table) {
echo $table;
}
Here is my code for test2.php
$table = "";
if($prevWeek != $adjustedWeek) {
$table .= ('<th colspan=2>Week' . $adjustedWeek . '</th>');
$prevWeek = $adjustedWeek;
}
$table .= '<tr>';
$table .= ('<td colspan=12><a href="details.php?id=' . $row['id'] . '">'
. getGameTitleText($row) . '</a></td>');
$table .= '</tr>';
$resultFound = true;
$tableArray[] = $table;
$count++;
I need the code to do something like this:
If (all items of any week > 8)
write out 8 items to column one
then write the rest to column two
I've accounted for the total number of entries it's searching but not specific to a week, and I don't want to have to make lots of variables for that either to keep track.
How could I get the result like I want?
Warning!! the following code is to illustrate the idea only. Not tested on machine, but I think you can iron the possible error out.
pseudo code example:
$arr=array();
while($row = mysqli_fetch_array($result)) {
$arr[]['data']=$row['data'];
$arr[]['whatever']=$row['whatever'];
}
$columns=floor(count($arr)/8);
$output="<table>";
foreach ($arr as $i=>$a){
$output.="<tr>";
$output.="<td>{$arr[$i]['data']}</td>";
if ($columes>0){
for($j=0;$j<$columes;$j++){
$row_number=$i+($j+1)*8;
$output.="<td>{$arr[$row_number]['data']}</td>";
}
}
$output.="</tr>";
}
$output.="</table>";
This code will continue to add the third column if 2 is not enough.
You may still want to put a test for the end of array, so you don't get a bunch of warning when the $row_number is larger than count($arr)
This is how I ended up doing it. It's a bit "dirty" because it echo's out an extra /table and /tr at the beginning but it works for my purposes and is simpler than the code already posted. That was too complex for me
$table = "";
if($prevWeek != $adjustedWeek) {
$table .= '</tr></table><table class="vault_table">';
$table .= ('<tr><th>Week ' . $adjustedWeek . '</th></tr>');
$table .= '<table class="vault_table">';
$prevWeek = $adjustedWeek;
}
if($count % 2 == 0) {
$table .= '<tr>';
}
$table .= ('<td><a href="details.php?id=' . $row['id'] . '">'
. getGameTitleText($row) . '</a></td>');
$resultFound = true;
$tableArray[] = $table;
$count++;
The class is just for styling the table. Didn't have anything to do with how it's being layed out. other than each side of the table being 50% width of the container

Categories