I am writing a plugin where you put in a shortcode in a page and it displays a table of data.
However, the page content prior to the shortcode is being inserted randomly into the middle of the table. When I refresh the page the content typed above the shortcode randomly moves within the table produced by the shortcode.
Content under the shortcode does not appear within the shortcode return.
Does anyone have any idea why this might be happening. This is super strange.
------------------ wordpress page edit ---------------
Here is some content.
Here is another paragraph.
[view_contributions]
End of page content.
------------------end of wordpress page edit ----------------------------
It then produces
------ displays --------
[Shortcode data table with "Here is some content. Here is another paragraph." randomly inserted into a cell somewhere. Then more data table]
End of page content.
-------- end of display -----
This is so odd. It's as if shortcode is rendering first but then WordPress injects the page content into whatever the shortcode is rendering. Any ideas what could cause this?
EDIT: Added entirety of code in case something really strange is happening...
function soco_view_contributions_shortcode() {
$view_contributions = Soco_Contributions::soco_display_contributions();
return $view_contributions;
}
add_shortcode( 'view_contributions', 'soco_view_contributions_shortcode');
public function soco_display_contributions() {
$contribution_results = Soco_Contributions::soco_get_contributions_view();
ob_start;
?>
<div name="div-output-container">
<form name="frm-search-contributions">
<table width="100%" border="0">
<tbody>
<tr>
<th scope="col">Start Date</th>
<th scope="col">End Date</th>
<th scope="col">Minimum</th>
<th scope="col">Maximum</th>
<th scope="col">Name</th>
<th scope="col">Event</th>
</tr>
<tr>
<td><input type="date" name="start-date"></td>
<td><input name="end-date" type="date" ></td>
<td><input type="number" name="low-number"></td>
<td><input type="number" name="high-number"></td>
<td><text name="txt-auto-name"> </textarea></td>
<td><select> </select></td>
</tr>
</tbody>
</table>
<input type="submit">
<input type="reset">
</form>
<table width="100%" border="0">
<tbody>
<tr>
<th scope="col">Date</th>
<th scope="col">Amount</th>
<th scope="col">Cycle</th>
<th scope="col">Name</th>
<th scope="col">Event</th>
</tr>
<?php foreach ($contribution_results as $cr) { ?>
<tr>
<td><?php echo $cr->contribution_date ?></td>
<td><?php echo $cr->amount ?></td>
<td><?php echo $cr->cycle_amount ?></td>
<td><?php echo $cr->last_name.', '.$cr->first_name ?></td>
<td> </td>
</tr>
<?php } ?>
</tbody>
</table>
<button name="btnDownload" id="btnDownload" title="Click this button to download the above dataset." >Download CSV File</button>
</div>
<?php
$contribution_output = ob_get_clean();
return $contribution_output;
}
It seems that you are attempting to return an output buffer as string. When ob_start() is called, all output is suppressed until ob_end(), ob_end_flush() or ob_end_clean() is called so logically, you can't return it. Simply call them within the function itself and return the contents of the output buffer (ob_get_contents()) as a string:
add_shortcode('view_contributions','soco_view_contributions_shortcode');
function soco_view_contributions_shortcode( ) {
ob_start();
?>
<h1>Shortcode Output</h1>
<p><?php echo "Some other output" ?></p>
<?
return ob_end_clean();
}
Why PHP doesn't throw a fatal error when attempting to return an entire buffer from a function surprises me.
The problem turned out to be mixing ECHO and RETURN in the output buffer. To fix this I turned the whole thing into a concatenated string to output instead.
The echos that are in the foreach loop I think were messing up the output buffer. So I removed ob_start all together and will just output a final HTML string.
Not a great solution but at least it is working now and not producing random results. If anyone has suggestions or examples on how to mix ob_start with php logic inside of it that would be great. It seems weird to me that ob_start() has issues with this.
$contribution_output = '<div name="div-output-container">
<form name="frm-search-contributions">
<table width="100%" border="0">
<tbody>
<tr>
<th scope="col">Start Date</th>
<th scope="col">End Date</th>
<th scope="col">Minimum</th>
<th scope="col">Maximum</th>
<th scope="col">Name</th>
<th scope="col">Event</th>
</tr>
<tr>
<td><input type="date" name="start-date"></td>
<td><input name="end-date" type="date" ></td>
<td><input type="number" name="min-amount"></td>
<td><input type="number" name="max-amount"></td>
<td>
<input type="text" name="donor_name">
<input type="hidden" name="hdn-donor-id" value="">
</td>
<td><select> </select></td>
</tr>
</tbody>
</table>
<input type="submit">
<input type="reset">
</form>
<div name="contribution-results-container" >
<table width="100%" border="0">
<tbody>
<tr>
<th scope="col">Date</th>
<th scope="col">Amount</th>
<th scope="col">Cycle</th>
<th scope="col">Name</th>
<th scope="col">Event</th>
</tr>';
foreach ($contribution_results as $cr) {
$contribution_output .= '
<tr>
<td>'.$cr->contribution_date.'</td>
<td>'.$cr->amount.'</td>
<td>'.$cr->cycle_amount.'</td>
<td>'.$cr->last_name.', '.$cr->first_name.'</td>
<td> </td>
</tr>';
}
$contribution_output .= '</tbody>
</table>
<button name="btnDownload" id="btnDownload" title="Click this button to download the above dataset." >Download CSV File</button>
</div>
</div>';
Related
I am updating a plugin for WordPress using .php files. I have an update that requires a table and pagination. I'm trying to use jQuery for this.
I followed the datatable instructions but I am not seeing any changes applied. What am I doing wrong? I have added the jQuery to use the correct table id. Nothing seems to work doing this.
I am attempting to run through the data and allow the page to paginate. I do not care about having the option for user to select number of items on page at given time.
I am looking for an easy way to paginate a HTML table. Nothing I've tried seems to work
jQuery(document).ready(function() {
jQuery('#engine-search-table').DataTable();
});
<div class="parts-table">
<table id="engine-search-table" class="display">
<thead>
<tr id="header-row">
<th class="table-header">
<h4>Manufacturer</h4>
</th>
<th class="table-header">
<h4>Model</h4>
</th>
<th class="table-header">
<h4>Description</h4>
</th>
<th class="table-header">
<h4>Series</h4>
</th>
<th class="table-header">
<h4>Engine</h4>
</th>
<th class="table-header">
<h4>Years</h4>
</th>
<th class="table-header">
<h4>Begin Serial</h4>
</th>
<th class="table-header">
<h4>Engine Serial</h4>
</th>
</tr>
</thead>
<tbody>
<?php
if (!empty($parts)) {
foreach ($parts as $part) {
$meta = get_post_meta($part->ID); ?>
<tr id="data" class="parts-row">
<td class="part-container-title">
<?php printCFTVal($meta, 'manufacturer');?>
</td>
<td class="part-container-manufacturer">
<a href='<?php echo ($part->guid); ?>'>
<?php printCFTVal($meta, 'model'); ?>
</a>
</td>
<td class="part-container-category">
<?php printCFTVal($meta, 'description'); ?>
</td>
<td class="part-container-excerpt">
<?php echo str_replace('OVERVIEW', '', get_the_excerpt($part)); ?>
</td>
<td class='part-container-button'>
<?php echo ($part->guid); ?>'>
<?php printCFTVal($meta, 'enginemodel'); ?>
</td>
<td>
<?php printCFTVal($meta, 'years');?>
</td>
<td>
<?php printCFTVal($meta, 'beginSerial');?>
</td>
<td>
<?php printCFTVal($meta, 'engineserial');?>
</td>
</tr>
<?php }
} else {
echo '<p>No parts found.</p>';
} ?>
</tbody>
</table>
</div>
Trying to use a Bootstrap 5 Collapse. This same code has been copied from a similar page, but the rows do not expand to show the collapsed (hidden) row. Instead I get the above error on the browser console.
The browser's console has these lines at the bottom ( I can click the top row to expand the remaining rows, which looks like the attached pic):
SyntaxError: The string did not match the expected pattern.
querySelector - index.js:64
e - index.js:64
(anonymous function) - collapse.js:317
If I tap any of the lines after the f symbols, the browser redirects me to the "Sources" tab to the relevant JS file for Bootstrap.
The code is - not the full page, this is exerted:
<?php
$x = 1;
$leadingScore = -100;
if (isset($leaderboard)) {
foreach ($leaderboard as $score) {
$pid = (string) $score['playerId'];
?>
<div class="collapse">
<tr data-bs-toggle='collapse' data-bs-target='#<?=$pid?>'>
<th scope="row">
<?php
if ($leadingScore != $score['score'])
echo $x;
?>
</th>
<td style="text-align: left;">
<?php echo $score['playerName']." (".$score['hcap'].")"; ?>
</td>
<td>
<?php
echo $score['score'];
$leadingScore = $score['score'];
?>
</td>
<td>
<?php
echo $score['thru'];
?>
</td>
</tr>
<tr id='<?=$pid?>' class='collapse'>
<td colspan=4>
<table class='table'>
<tr>
<th class='sub-th' scope='col'></th>
<th class='sub-th' scope='col'>1</th>
<th class='sub-th' scope='col'>2</th>
<th class='sub-th' scope='col'>3</th>
<th class='sub-th' scope='col'>4</th>
<th class='sub-th' scope='col'>5</th>
<th class='sub-th' scope='col'>6</th>
<th class='sub-th' scope='col'>7</th>
<th class='sub-th' scope='col'>8</th>
<th class='sub-th' scope='col'>9</th>
<th class='sub-th' scope='col'>TOTAL</th>
</tr>
</table>
</td>
</tr>
</div>
<?php
$x++;
}
}
?>
The output in the browser source, the primary table row HTML is:
<div class="collapse">
<tr data-bs-toggle='collapse' data-bs-target='#609d0993906429612483cf49'>
The collapsable row HTML is:
<tr id='609d0993906429612483cf49' class='collapse'>
<td colspan=4>
<table class='table'>
...
So it has the target and id tags populated from the DB.
As stated by #johansenja, starting the div id with character not integer resolved the issue.
I have a query that returns me a long series of questions and answers. in my html page I have div that alternate with slider effect. so far everything is fine. at the moment I try to correctly display a question from the db bumps everything. why?
<div align=Center>
<br/>
<h1><?php echo $rowfirst['Cognome']." ".$rowfirst['Nome']; ?></h1>
<br/>
<div class="nivo-slider">
<div class="navigation"></div>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<div id="nivo">
<div class="element" align=Center><h3>Anamnesi pregressa</h3>
<table class="w3-table w3-bordered " style="width:400px;padding:10px;" align="center">
<tbody>
<tr>
<th scope="row"><?php echo $row['testo'];}?></th>
<td>risposta 1</td>
</tr>
</tbody>
</table>
</div>
<div class="element" align=Center><h3>Scrittura</h3>
<table class="w3-table w3-bordered" style="width:400px;padding:10px;" align="center">
<tbody>
<tr>
<th scope="row">domanda 1</th>
<td>risposta 1</td>
</tr>
</tbody>
</table>
</div>
<div class="element"><h3>Motricità</h3>
<table class="w3-table w3-bordered " style="width:400px;padding:10px;" align="center">
<tbody>
<tr>
<th scope="row">domanda 1</th>
<td>risposta 1</td>
</tr>
</tbody>
</table>
</div>
</div>
<br/>
</div>
</div>
The problem is the while. cycle. how can I solve it?
What you have will create malformed HTML.
In order to prevent malformed HTML, you should place your while () { and } where the tags matches like I have done with <tr> and </tr> shown below:
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<th scope="row"><?php echo $row['testo'];?></th>
<td>risposta 1</td>
</tr>
<?php } ?>
That will fix your malformed HTML but the while statement may or may not be in the right place and without knowing exactly what you are trying to achieve you'll have to correct it from there. As for the slider you mentioned, you probably need to supply more information if this answer doesn't solve your issue.
I am trying to get checked values in unos.php file. I get empty post but could not figure it out where is mistake.
I want to check just one checkbox and according to post value received I will perform results.
<form method="post" action="unos.php">
<button type="submit" class="btn btn-primary" name="izmjena">Izmjena</button>
<table>
<thead>
<tr>
<th data-field="artikal" data-checkbox="true" > </th>
<th data-field="id" data-sortable="true">Id</th>
<th data-field="sifra" data-sortable="true">Sifra</th>
<th data-field="name" data-sortable="true">Naziv</th>
<th data-field="mjera" data-sortable="true">Jed. mjere</th>
<th data-field="stanjesa" data-sortable="true">Stanje Sa</th>
<th data-field="stanjebl" data-sortable="true">Stanje BL</th>
<th data-field="minkolicina" data-sortable="true">Min kolicina</th>
<th data-field="kategorija" data-sortable="true">Kategorija</th>
<th data-field="status" data-sortable="true">Status</th>
<th data-field="print" data-sortable="true">P</th>
</tr>
</thead>
<tbody>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><input type="checkbox" name="artikli" value="<?.$r['ArtId'].?>"></td>
<td><?=$r['ArtId'] ?></td>
<td><?=$r['ArtSifra'] ?></td>
<td><?=$r['ArtNaziv'] ?></td>
<td><?=$r['JmNaziv'] ?></td>
<td><?=$r['ArtStanje']?></td>
<td><?=$r['ArtStanjeMo'] ?></td>
<td><?=$r['ArtMinKolic'] ?></td>
<td><?=$r['KatNaziv'] ?></td>
<td><?=$r['ArtAktivan'] ?></td>
<td><a onClick="window.open('kartica-artikla.php?id= <?=$r['ArtKategorija']?>','Websoft','width=800,height=500,left=0,top=100,screenX=0,screenY=100,menubar=yes,scrollbars=yes')" href "">
<img src="../../img/kartica.png" width="20"></td>
</tr>
<?php endwhile; ?>
</tbody>
<tfoot>
</tfoot>
</table>
file unos.php.. In this file I want to post value of checkbox
<?php
if (isset($_POST["izmjena"])) {
$id=$_POST["artikli"];
echo $id;
var_dump($id);
}
else{
}
Change:
<td><input type="checkbox" name="artikli" value="<?.$r['ArtId'].?>"></td>
To:
<td><input type="checkbox" name="artikli" value="<?php echo $r['ArtId']; ?>"></td>
Dont forget to close your form either :)
If you're using several checkboxes, you should be using an input checkbox with the following "name": "artikli[]".
The "[]" will send an array of values instead of sending a string.
Code:
<input type="checkbox" name="artikli[]" value="<?.$r['ArtId'].?>" />
checkbox's value will be present if it is checked. Do this -
$id=(!empty($_POST["artikli"])) ? $_POST["artikli"] : false;
and you are not echoing the whole html so no need to concate. Change -
<td><input type="checkbox" name="artikli" value="<? echo $r['ArtId'];?>"></td>
I have this code:
<?php
$id_sent = $_POST['id'];
echo $id_sent;
include ($_SERVER['DOCUMENT_ROOT']."/upload/upload_class.php");
$max_size = 1024*250*500;
$my_upload = new file_upload;
$my_upload->upload_dir = $_SERVER['DOCUMENT_ROOT']."/uploads/";
$my_upload->extensions = array(".pdf");
$my_upload->max_length_filename= 50;
$my_upload->rename_file = true;
$my_upload->id_search = $id_sent;
if(isset($_POST['Submit'])) {
$my_upload->the_temp_file = $_FILES['upload']['tmp_name'];
$my_upload->the_file = $_FILES['upload']['name'];
$my_upload->http_error = $_FILES['upload']['error'];
if ($my_upload->upload()) {
mysql_query(sprintf("UPDATE psi_avize SET pdf = 'T' WHERE id = '%s'", $my_upload->id_search));?>
<table width="800" border="0">
<tr>
<th width="167" rowspan="2" scope="col"><img src="images/figure_check_mark_celebrate_anim_md_wm.png" width="129" height="142"></th>
<th width="471" height="29" scope="col"><div align="left">Succes!</div></th>
<th width="148" scope="col"> </th>
</tr>
<tr>
<td height="104"> </td>
<td> </td>
</tr>
</table><?php
echo $my_upload->show_error_string();
}
}
else {?> <strong>Insert file!</span></p>
</strong>
<table width="800" border="0">
<tr>
<th width="167" rowspan="2" scope="col"><img src="images/document.png" width="150" height="152"></th>
<th width="471" height="29" scope="col"><div align="left"></div></th>
<th width="148" scope="col"> </th>
</tr>
<tr>
<td height="104">Max = 5 MB.</td>
<td> </td>
</tr>
</table>
<p> </p>
Load file <form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>"
enctype="multipart/form-data" method="post">
<div align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<th width="11%" scope="col"> </th>
<th width="26%" scope="col"> </th>
<th width="51%" scope="col"><div align="left">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_size; ?>" />
<?php
echo $my_upload->create_file_field("upload", "Select file...", 25, false); ?></div></th>
<th width="12%" scope="col"> </th>
</tr>
<tr>
<td> </td>
<td> </td>
<td><div align="left">
<input name="Submit" type="submit" id="Submit" value="Upload" />
</div></td>
<td> </td>
</tr>
</table>
</div>
</form> <?php }
?>
My problem is that somehow I loose the variable $id_sent (it is sent using POST from other page) and I am not able to query correctly using id.
When I echo the variable in line 3 is working but after that somehow I am losing that variable and I am not able to use it.
Thank you!
Looks like the problem you are having is that the $id_sent is coming from another page's post. This is the reason you are able to echo it correctly in the first place.
After the current page is loaded, and you click on submit button of the current page's form, the POST values from the previous page is overwritten by the POST values from this page.
In order to retain the value across this page's post as well, store it in a hidden field like so
<input type ='hidden' name='id' value='<? php echo $id_sent; ?>'>
So here, the first time around the $id_sent is set as the value of id from previous page post. It is also set into a hidden field named id in this page's form, which will then be considered for subsequent form submissions