So simply put, I have my PHP code generating a bunch of HTML. I then want to update a database entry like so
$wpdb->update("wp_table",
array( 'content' => $html ),
array( 'id' => 1 ),
array( '%s' ),
array( '%d' )
);
This simply does not work. Nothing in the database changes.
To debug, I placed the exact same code right above with one minor change, and it DOES WORK:
$wpdb->update("wp_sn_cached_popular_display",
array( 'content' => "hello" ),
array( 'id' => 1 ),
array( '%s' ),
array( '%d' )
);
(I swapped out the $html for a direct string.)
I can't even begin to comprehend why, and I've played around with it a lot. I've even done this and it DOES WORK too:
$string = "hello";
$wpdb->update("wp_sn_cached_popular_display",
array( 'content' => $string),
array( 'id' => 1 ),
array( '%s' ),
array( '%d' )
);
It's just the one $html variable is causing this function to not run, or something?
Compiling the $html variable is a bunch of stuff like this:
$html .= '<li>';
$html .= '<div class="upcoming-left">';
$html .= '<time datetime="' . $date . '" class="icon">';
$html .= '<em>' . $date_day.'</em>';
$html .= '<strong>' . $date_month . '</strong>';
$html .= '<span>' . $date_num . '</span>';
$html .= '</time>';
$html .= '</div>';
$html .= '<div class="upcoming-right">';
$html .= '<span class="upcoming-title">' . $upcoming_title . '</span>';
$html .= '<span class="upcoming-desc">' . $upcoming_desc . '</span>';
$html .= '<div class="clear"></div>';
$html .= '</div>';
$html .= '</li>';
//...
Any ideas why this is?
Bonus fun time: This appears to work on my local xampp insall, not the live site.
This could be an issue with table datatypes
https://www.w3schools.com/sql/sql_datatypes.asp
depending on what the table is set to it may not support the string length of your html variable or certain chars in it.
It looks like you concat your .html var multiple times. It's a pretty long string in the end. to test this try to save a very long manually entered string. try with special chars. look at the table structure. This is just some ideas and where my thoughts go.
Related
I want to combine the output of an acf field from my page with my shortcode. The text should be underlined with the color set via an acf field.
I tried to call the field color and set the text-decoration via an inline style. But this is not working. Any ideas what I am doing wrong?
function quote_func($atts, $content = null){
$color = get_field('color');
$output = '<div>';
$output .= '<span style="text-decoration-color:' . echo the_field('color'); . '">' . $content . '</span>';
$output .= '</div>';
return $output;
}
add_shortcode( 'quote', 'quote_func' );
You should echo the variable you set in the beginning of your function.
function quote_func($atts, $content = null){
$color = get_field('color');
$output = '<div>';
$output .= '<span style="text-decoration-color:' . $color . '">' . $content . '</span>';
$output .= '</div>';
return $output;
}
add_shortcode( 'quote', 'quote_func' );
get_field('color') isn't enough to get the value if you're not inside a post, you need a second parameter. You are in a shortcode then you need to use:
get_field('color', $postId);
To get the id of the post from within shortcode you can use:
global $post;
$postId = $post->ID;
If you use the same color for each post you may have the option page and in that case you need to use:
get_field('color', 'option');
I tried to parse the php query into html, but on the html page it shows array.
My query looks like this: $comments = get_comments( array('post_id' => $post->ID, 'status' => 'approve') );
I tried to print it on the html page using this: $html .= $comments;
As you can see from the examples.
And as Tom J Nowell commented, you need to loop through each comment and append the comment to the $html.
$args = array(
'status' => 'approve',
'post_id' => $post->ID,
);
$comments = get_comments( $args );
foreach ( $comments as $comment ) :
$html .= $comment->comment_author . '<br />' . $comment->comment_content . '<br />';
endforeach;
I am trying to align contents of two arrays in a tabular format and send an email. But the second column in the table doesn't align as desired. The content of the second column appears as a single row.
I'm attaching output table as well:
#!/usr/bin/perl
use strict;
use warnings;
use MIME::Lite;
use HTML::Entities;
my $msg;
my #Sucess = qw(s1 s2 s3 s4 s5 s6);
my #Failed = qw(f1 f2 f3 f4);
my $html = '<table style="width:600px;margin:0 100px" border="1" BORDERCOLOR="#000000">
<thead><th bgcolor="#9fc0fb">Successful</th><th bgcolor="#9fc0fb">Failed</th></thead>
<tbody>';
$html .= "<tr><td>$_</td>" for #Sucess;
$html .= "<td>$_</td>" for #Failed;
$html .= " </tr>";
$msg = MIME::Lite->new(
from => 'foo#abc.com',
To => 'foo#abc.com',
Subject => 'Status of Update',
Type => 'multipart/related'
);
$msg->attach(
Type => 'text/html',
Data => qq{
<body>
<html>$html</html>
</body>
},
);
MIME::Lite->send ('smtp','xyz.global.abc.com' );
$msg->send;
You need to replace your code that builds up the table with something that works in a logical order. HTML tables need to be defined row by row. You can't process all of the successes and then all of the failures.
I'd replace your middle section of code with something like this:
use List::Util qw[max];
my $max = max($#Sucess, $#Failed);
for (0 .. $max) {
$html .= '<tr><td>';
$html .= $Sucess[$_] // '';
$html .= '</td><td>';
$html .= $Failed[$_] // '';
$html .= "</td></tr>\n";
}
But actually, I would never put raw HTML in a Perl program. Use a templating system instead.
First you loop over each item in #Sucess and for each one you:
Create a new row
Create a cell in that row
$html .= "<tr><td>$_</td>" for #Sucess;
Then you look over each item in #Failed and for each one you:
Create a cell in the last row you created (for the most recent #Sucess)
$html .= "<td>$_</td>" for #Failed;
Finally, you explicitly close the last table row you created:
$html .= " </tr>";
To get the layout you desire (which is distinctly non-tabular) you need to work row by row. You can't deal with all the #Sucess and then all the #Failed.
my #Sucess= qw(s1 s2 s3 s4 s5 s6);
my #Failed= qw(f1 f2 f3 f4);
my $html = "<table>\n";
do {
my $s = shift #Sucess;
my $f = shift #Failed;
$html .= sprintf("<tr> <td> %s </td> <td> %s </td> </tr> \n", map { $_ // '' } $s, $f);
} while ( #Sucess or #Failed );
$html .= "</table>";
print $html;
I'm trying to get the sum for all the meta values of one custom field. I now get the sum for the metavalues of the whole site. I want it to be restricted to the category you're in.
The code now looks like this:
<?php
$thevariable = $wpdb->get_var($wpdb->prepare("
SELECT SUM($wpdb->postmeta.meta_value)
FROM $wpdb->postmeta, $wpdb->posts
WHERE $wpdb->postmeta.meta_key='mycustomfield'
AND $wpdb->postmeta.post_id=$wpdb->posts.id
"));
echo '<h1>' . $thevariable . '</h1>';
?>
Can somebody help me to filter out one category?
Would be amazing!
M
Inside your WP files, you can add this lines; I guess It will do what you are looking for.
<?php
$MySum = 0;
$args = array(
'category_name' => 'MyCategory',
'meta_key' => 'mycustomfield',
'posts_per_page' => '-1' );
// The Query
$the_query = new WP_Query( $args);
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
$MySum += get_post_meta($post_id, $key, true);
endwhile;
echo '<h1>' . $MySum . '</h1>';
// Reset Post Data
wp_reset_postdata();
?>
I want to convert my sql data to csv files while clicking on a button. The code fragments I found for sql to CSV conversion were in PHP, and I'm trying to convert it to CakePHP since I'm working in CakePHP.
Here is the PHP code I'm tring to convert:
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field']."; ";
$i++;
}
}
$csv_output .= "\n";
$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j]."; ";
}
$csv_output .= "\n";
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
SOLUTION
Function in the Controller:
function exporttocsv()
{
$this->set('headers',$this->Result->find('all',array('fields'=>'Result.label')));
$this->set('values',$this->Result->find('all',array('fields'=>'Result.value')));
}
exporttocsv.ctp file:
<?php
foreach($headers as $header):
$csv_output .=$header['Result']['label'].", ";
endforeach;
$csv_output .="\n";
if(!empty($values)){
foreach($values as $value):
$csv_output .=$value['Result']['value'].", ";
endforeach;
$csv_output .="\n";
}
else{
echo "There is no data to export.";
}
$filename = "export_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>
First of all, you don't do queries and output in the same file in Cake. You query the data as usual in the Controller, $this->set() the result to the view, and in the view you do something like this:
foreach ($results as $result) {
echo join(', ', $result['COLUMNS']);
echo "\n";
}
Outputs something like this:
value, varchar(25), NO, , ,
submitter, int(11), NO, , ,
...
Since Cake automatically wraps a layout around your view, you'll have to set the layout to something different, like 'ajax' (which is simply an empty layout).
deceze is correct about outputting the results from the view file. You'll just need to set some headers so that it appears as a file download on the client side. You can simply put these 2 calls in the top of your view:
header("Content-type:application/vnd.ms-excel");
header("Content-disposition:attachment;filename=\"{$filename}\"" );
If you plan on doing csv downloads in more than one place in your application, I'd recommend this helper:
http://bakery.cakephp.org/articles/view/csv-helper-php5
I use it and it works well.