WordPress plugin adds the number 1 to content if activated - php

My WP plugin bizarely adds the numeral 1 to the content if the plugin is active.
add_filter( 'the_content', 'poc_shop_cart_button_filter' );
function poc_shop_cart_button_filter($the_content) {
$new_content = $the_content;
$new_content .= include(WP_CONTENT_DIR . "/custom_php/shop-cart-button.php");
return $new_content;
}
This is the page I am including with the plugin:
if(isset($_COOKIE['_abs_34287GjNW'])){
$cookieID = htmlspecialchars($_COOKIE['_abs_34287GjNW'], ENT_QUOTES, 'UTF-8');
} else {
$cookieID = '';
}
$supplierID = get_bloginfo('name');
require(ABSPATH . 'wp-content/custom_php/includes/connectDB.php');
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$conn->exec("SET CHARACTER SET utf8");
$sql = "SELECT COUNT(*) AS count
FROM `shoppingCart`
WHERE supplierID=:supplierID
AND cookieID = :cookieID";
$sqlprep = $conn->prepare($sql);
$ar_val = array(':supplierID'=>$supplierID,
':cookieID'=>$cookieID);
if($sqlprep->execute($ar_val)) {
while($row = $sqlprep->fetch(PDO::FETCH_OBJ)){
if($row->count=='' || $row->count==0){
$count='0';
} else{
$count = $row->count;
}
}
}
if($count == 1){
$items = "item";
} else {
$items = "items";
}
if(is_page(array(48)) || $count < 1){
echo '<div class="shopCartNoButton">';
echo "<img src='" . get_template_directory_uri() . "/images/cart-icon.png' alt=''>";
echo " <b>$count $items </b>";
echo '</div><div class="clearFix"></div>';
} else {
echo '<div class="shopCartButton">';
echo "<img src='" . get_template_directory_uri() . "/images/cart-icon.png' alt=''>";
echo " <b>$count $items </b>";
echo " <a href='office-seating/checkout/' class='btn btn-warning'> Checkout >></a>";
echo '</div><div class="clearFix"></div>';
}
} catch(PDOException $e) {
echo $e->getMessage();
}
So the solution to this after MUCH Googling is as follows:
$filename = get_include_contents('somefile.php');
function get_include_contents($filename) {
if (is_file($filename)) {
ob_start();
include $filename;
return ob_get_clean();
}
return false;
}
The code:
include( $filename );
returns the included file + the value 1 because the include was successful.
One must use output buffering to include a PHP file into a string and not return the value 1 with the returned file contents.

I had similar problem. It is because include returns value of the call, i.e. "true" or 1 when WordPress render data via echo. Just use file_get_contents it will help.

Related

Order Custom Taxonomy by Slug in Wordpress Plugin

CONTEXT
We have created a custom taxonomy that is to be displayed as a search filter parameter in third party plugin (in the form of a checkbox). However, we want the list of taxonomies to be displayed ordered by slug.
This is the function that generates the checkbox and outputs the taxonomy - the part that we customized starts at " //create the attributes" .
public function checkbox($args)
{
//init defaults & vars
$input_args = $this->prepare_input_args($args, "checkbox");
$input_name = $input_args['attributes']['name'];
unset($input_args['attributes']['name']);
//filter the input arguments before the html is generated - allowing almost all options to be modified
if(has_filter('sf_input_object_pre')) {
$input_args = apply_filters('sf_input_object_pre', $input_args, $this->sfid);
}
//prepare html
$attibutes_html = $this->convert_attributes_to_html($input_args['attributes']);
$open_child_count = 0;
ob_start();
?>
<ul<?php echo $attibutes_html; ?>>
<?php
$last_option_depth = 0;
$option_count = count($input_args['options']);
$current_depth = 0;
$is_li_open = array();
//echo "<ul>";
for($i=0; $i<$option_count; $i++)
{
$option = &$input_args['options'][$i];
if(!isset($option->attributes))
{
$option->attributes = array(
'class' => '',
'id' => ''
);
}
//check a default has been set and set it
$option->attributes['type'] = "checkbox";
$option->attributes['value'] = $option->value;
$option->attributes['name'] = $input_name;
$option->attributes['html'] = '';
$container_attributes = array();
if($this->is_option_selected($option, $input_args['defaults']))
{
$option->attributes['checked'] = 'checked';
$option->attributes['class'] = trim($option->attributes['class']).' sf-option-active';
}
else
{
if(isset($option->attributes['checked']))
{
unset($option->attributes['checked']);
}
}
//now we want to put the class attribute on the LI, and remove from input
$option_class = $option->attributes['class'];
$option->attributes['class'] = $input_args['input_class'];
$input_id = $this->generate_input_id($input_name."_".$option->value);
$option->attributes['id'] = $input_id;
$container_attibutes_html = "";
if(isset($option->count))
{
$container_attributes['data-sf-count'] = $option->count;
$container_attibutes_html = $this->convert_attributes_to_html($container_attributes);
}
//create the attributes
$input_attibutes_html = $this->convert_attributes_to_html($option->attributes);
$option_label = $option->label;
$option_html = $option->attributes['value'];
if($input_name == "_sft_module_authors[]"){
$tax_name = "module_authors";
} else if ($input_name == "_sft_module_mindset[]") {
$tax_name = "module_mindset";
} else if ($input_name == "_sft_module_language[]") {
$tax_name = "module_language";
} else if ($input_name == "_sft_management_levels[]") {
$tax_name = "management_levels";
} else{
$tax_name = "wpdmcategory";
}
// Get the ID of a given category
$my_term = get_term_by('slug',$option->value,$tax_name);
$category_id = $my_term->term_id;
$category_desc = $my_term->description;
$category_name = $my_term->name;
$meta_image = get_wp_term_image($category_id);
// Get the URL of this category
$category_link = get_category_link( $category_id );
if ($input_name == "_sft_module_authors[]") {
echo '<li class="accordion-content '.$option_class.'"'.$container_attibutes_html.'><input '.$input_attibutes_html.'><label class="sf-label-checkbox" for="'.$input_id.'">' . $category_name . '</label><a class="eModal-post-'.$category_id.'" href="'.$category_link.'"><i class="fa fa-info-circle"></i></a>';
echo do_shortcode("[modal id='post-". $category_id ."' size='small' title='". $category_name ."']". "<div class='col-md-8'>" . $category_desc . "</div>" . "<div class='col-md-4'>" . "<div class='img-wrap'><img src='".$meta_image."'></div></div>" . "[/modal]");
} else if ($input_name == "_sft_module_mindset[]") {
echo '<li class="accordion-content '.$option_class.'"'.$container_attibutes_html.'><input '.$input_attibutes_html.'><label class="sf-label-checkbox" for="'.$input_id.'">' . $category_name . '</label><a class="eModal-post-'.$category_id.'" href="'.$category_link.'"><i class="fa fa-info-circle"></i></a>';
echo do_shortcode("[modal id='post-". $category_id ."' size='small' title='". $category_name ."']". "<div class='col-md-12'>" . $category_desc . "</div>" . "[/modal]");
} else if ($input_name == "_sft_module_language[]") {
echo '<li class="accordion-content '.$option_class.'"'.$container_attibutes_html.'><input '.$input_attibutes_html.'><label class="sf-label-checkbox" for="'.$input_id.'">' . $category_name;
} else if ($input_name == "_sft_management_level[]") {
echo '<li class="accordion-content '.$option_class.'"'.$container_attibutes_html.'><input '.$input_attibutes_html.'><label class="sf-label-checkbox" for="'.$input_id.'">' . $category_name;
} else {
echo '<li class="accordion-content '.$option_class.'"'.$container_attibutes_html.'><input '.$input_attibutes_html.'><label class="sf-label-checkbox" for="'.$input_id.'">' . $category_name . '</label><a class="eModal-post-'.$category_id.'" href="'.$category_link.'"><i class="fa fa-info-circle"></i></a>';
echo do_shortcode("[modal id='post-". $category_id ."' size='small' title='". $category_name ."']". "<div class='col-md-12'>" . $category_desc . "</div>" . "[/modal]");
}
if(isset($option->depth))
{//then we do depth calculations
$current_depth = $option->depth;
$close_li = true;
$open_child_list = false;
$close_ul = false;
$next_depth = -1;
if(isset($input_args['options'][$i+1]))
{
$next_option = $input_args['options'][$i+1];
$next_depth = $next_option->depth;
}
if($next_depth!=-1)
{
if($next_depth!=$current_depth)
{//there is a change in depth
if($next_depth>$current_depth)
{//then we need to open a child list
//and, not close the current li
$open_child_list = true;
$close_li = false;
}
else
{
$close_ul = true;
}
}
}
if($open_child_list)
{
$open_child_count++;
echo '<ul class="children">';
}
if($close_li)
{
echo "</li>";
}
if($close_ul)
{
$diff = $current_depth - $next_depth;
$open_child_count = $open_child_count - $diff;
$str_repeat = str_repeat("</ul></li>", $diff);
echo $str_repeat;
}
}
}
$str_repeat = str_repeat("</ul></li>", (int)$open_child_count);
echo $str_repeat;
?>
</ul>
<?php
$output = ob_get_clean();
return $output;
}

cron jobs doesn't work with some php function

when i set some cron jobs for loading a php file cron doesn't work and it seams some php function have problem with cron job.
i know my cron command is true because i tested my cron job working true with simple php code that put date to a text file so my cron command is true i tested all ways of command like : wget, crul , cd, php , /user/local/bin/php and another but i don't know why my php code doesn't work and too i test that codes working very well when i loading php files with my browser.
my php file code:
<?php
header('Content-Type: text/html; charset=utf-8');
include ('simple_html_dom.php');
$mycache_url = 'http://example3.com';
$mybziran_url = 'http://example2.com';
function addhttp($url)
{
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
global $mybziran_url;
$url = ltrim($url, '/');
$url = $mybziran_url . '/' . $url;
}
return $url;
}
function urlencodeproblem($badurl)
{
$badurl = urlencode($badurl);
$badchar = array('%3A', '%2F');
$truechar = array(':', '/');
$badurl = str_replace($badchar, $truechar, $badurl);
return $badurl;
}
$url_html = #file_get_html($mycache_url);
$bziran_url = '';
$bziran_title = '';
foreach (#$url_html->find('a') as $elements) {
$bziran_url[] = urlencodeproblem($elements->href);
$bziran_title[] = $elements->innertext;
}
$myi = count($bziran_url);
for ($i = 0; $i < $myi; ++$i) {
$post_title = $bziran_title[$i];
$post_url = $bziran_url[$i];
$html = #file_get_html($post_url);
foreach ($html->find('div.price') as $myhtml_price_adelete) {
echo '######' . $myhtml_price_adelete->innertext . '######';
}
$bad_title_my = '';
foreach ($html->find('h1 a') as $myhtml_price_adelete) {
$bad_title_my .= $myhtml_price_adelete->innertext;
}
if (empty($bad_title_my)) {
echo $post_url;
echo 'prob';
} else {
$kalame = urlencode($post_title);
$A2_html = 'ok';
foreach ($html->find('a') as $myhtml_a_code) {
$e_ahref = addhttp($myhtml_a_code->href);
$myhtml_a_code->href = $e_ahref;
$myhtml_a_code->target = '_blank';
}
$html->save();
foreach ($html->find('img') as $myhtml_img_code) {
if (strpos($myhtml_img_code->src, 'base64') === false) {
$e_imgsrc = addhttp($myhtml_img_code->src);
$myhtml_img_code->src = $e_imgsrc;
}
}
$html->save();
$mymeta_keyword = '';
foreach ($html->find('meta[name=keywords]') as $myhtml_keyword) {
$mymeta_keyword[] = $myhtml_keyword->content;
}
foreach ($html->find('p') as $mytagdelete) {
if (strpos($mytagdelete->innertext, 'tag :') !== false) {
$mytagdelete->outertext = '';
}
}
$html->save();
foreach ($html->find('h1 a') as $myadelete) {
$myadelete->outertext = $myadelete->innertext;
}
$html->save();
$a3_href = '';
$a2_href = $html->find("img[alt=buy]");
foreach ($a2_href as $a2_href) {
$a2_href->outertext =
'<br><p align="center"><img alt="pay-download" src="http://exam.com/tmp_files/01-pay-download.png"></p>';
}
$html->save();
echo '<br>buy : ' . $a3_href . '<br>';
$myhtmlcode3 = '';
foreach ($html->find('div.prod') as $myhtmlcode) {
$myhtmlcode3 .= $myhtmlcode->outertext;
}
$html->save();
echo '<br>*** title ***' . $post_title . '<br>';
echo $post_url . '<br>';
$i_t = mt_rand(1, 34);
$mysaier_mahsolat = '<br>
<a target="_blank" href="http://ayta.ir/index.php?page=' . $i_t . '"> click </a>
<br>';
echo $mysaier_mahsolat;
echo $A2_html . $myhtmlcode3 . '<br>';
echo 'keyword :' . $mymeta_keyword[0];
}
}
?>
<?
$crontest = date("Y-m-d - h:i:s") . "\n" . file_get_contents(dirname(__file__) .
DIRECTORY_SEPARATOR . "cron.txt");
echo $crontest;
file_put_contents("cron.txt", $crontest);
?>

I want to show a title with the H2 tag

Could someone look at this.
I want to show echo htmlspecialchars($title) in <h2> but everything I tried I get a blank page.
<div class="adsmanager_ads_desc">
<?php $strtitle = "";
if (#$this->positions[5]->title) {
$strtitle = JText::_($this->positions[5]->title);
}
echo "<h2>" . #$strtitle . "</h2>";
if (isset($this->fDisplay[6]))
{
foreach($this->fDisplay[6] as $field) {
$c = $this->field->showFieldValue($this->content,$field);
if ($c != "") {
$title = $this->field->showFieldTitle(#$this->content->catid,$field);
if ($title != "") {
echo htmlspecialchars($title).": ";
echo "$c<br/>";
}
}
}
}
?>
</div>
Any suggestions?
Try the following, and if it throws errors say what they are
<div class="adsmanager_ads_desc">
<?php
$strtitle = (is_string($this->positions[5]->title)) ? $this->positions[5]->title : '';
echo "<h2>" . $strtitle . "</h2>";
if (isset($this->fDisplay[6]) && is_array($this->fDisplay[6]))
{
foreach($this->fDisplay[6] as $field) {
$field_value = $this->field->showFieldValue($this->content, $field);
if (is_string($field_value) && strlen($field_value) > 0) {
$title = $this->field->showFieldTitle($this->content->catid,$field);
if (is_string($title) && strlen($title) > 0) {
echo htmlspecialchars($title).": " . $field_value . "<br/>";
}
}
}
}
?>
</div>
I also added some is_string etc, this is not neccessary but can help debuging (although if it is a int|double it should pass too probably
You are suppressing errors and wondering what is wrong. In development, error reporting is your friend.
Instead of this
if (#$this->positions[5]->title) {
$strtitle = JText::_($this->positions[5]->title);
}
Try this
$strtitle = JText::_($this->positions[5]->title) || "Error bro";
Do you mean this
<h2><?php echo htmlspecialchars($title).": "; ?></h2>
Can you try this also
<div class="adsmanager_ads_desc">
<?php $strtitle = "";
if (#$this->positions[5]->title) {
$strtitle = JText::_($this->positions[5]->title);
}
echo "<h2>" . #$strtitle . "</h2>";
if (isset($this->fDisplay[6]))
{
foreach($this->fDisplay[6] as $field) {
$c = $this->field->showFieldValue($this->content,$field);
if ($c != "") {
$title = $this->field->showFieldTitle(#$this->content->catid,$field);
if ($title != "") {
echo '<h2>'.htmlspecialchars($title).':'.'</h2>';
echo "$c<br/>";
}
}
}
}
?>
</div>

foreach loop does not work with include

Would someone of you know why I'm not able to use a (long)piece of code within a foreach loop?
The code in the foreach loop is only executed once.
This code at topictweets.php works fine on its own but I want to repeat it for each forum.
The foreach loop works fine without the include. I also tried to have the code from topic tweets.php plainly in the foreach loop, this didn't work either of course.
The code it includes is used to get topics of a forum from the database and find related tweets, and save those in the database.
Is there some other way to do this?
foreach ($forumlist as $x => $fID) {
echo 'id:'.$fID.'<br>';
include 'topictweets.php';
/////////
////////
}
online version: http://oudhollandsedrop.nl/webendata/FeedForum/fetchtweets.php
bunch of code in topic tweets.php
<?php
//?/ VVVV ---- SELECT TOPICS FOR CURRENT FORUM ----- VVVV ////
echo $fID;
$sql = "SELECT Topics_TopicID
FROM Topics_crosstable
WHERE Forums_ForumID = '$fID'";
$result = mysql_query($sql);
if (!$result) {
//echo 'The topiclist could not be displayed, please try again later.';
} else {
if (mysql_num_rows($result) == 0) {
// echo 'This topic doesn′t exist.';
} else {
while ($row = mysql_fetch_assoc($result)) {
//display post data
// echo $row['Topics_TopicID'];
// echo': ';
$topic = "SELECT Name
FROM Topics
WHERE TopicID = " . mysql_real_escape_string($row['Topics_TopicID']);
$topicname = mysql_query($topic);
if (!$topicname) {
// echo 'The topic could not be displayed, please try again later.';
} else {
if (mysql_num_rows($topicname) == 0) {
// echo 'This topic doesn′t exist.';
} else {
while ($row = mysql_fetch_assoc($topicname)) {
//display post data
// echo $row['Name'];
// echo'<br>';
$topiclist[] = $row['Name'];
}
}
}
}
}
}
foreach ($topiclist as $key => $value) {
$terms .= "" . $value . ",";
}
//echo'<p>';
//echo rtrim($terms, ",");
//echo'<p>';
//echo'<p>';
//echo $terms;
//$terms="vintage";
//Twitter account information
$username = "Username";
$password = "Password";
while (true) {
//$terms="vintage";
//echo "search terms: " . substr_replace($terms, "", -1) . "\n";
$url = "https://stream.twitter.com/1/statuses/filter.json";
$cred = sprintf('Authorization: Basic %s', base64_encode("$username:$password"));
$param = "track=" . urlencode(substr_replace($terms, "", -1));
$opts = array(
'http' => array(
'method' => 'POST',
'header' => $cred,
'content' => $param,
'Content-type' => 'application/x-www-form-urlencoded'),
'ssl' => array('verify_peer' => false)
);
$ctx = stream_context_create($opts);
$handle = fopen($url, 'r', false, $ctx);
//var_dump($handle);
$content = "";
$flag = true;
while ($flag) {
$buffer = fread($handle, 100);
//$buffer = stream_get_line($handle, 1024, "\n");
$a = explode("\n", $buffer, 2);
$content = $content . $a[0];
#var_dump($a);
if (count($a) > 1) {
#echo $content;
#echo "\n";
$r = json_decode($content, true);
#var_dump($r);
// echo '<p>';
// echo "text: " . $r["text"];
// echo '<br>';
// echo "\nrceated_at: " . $r["created_at"];
// echo '<br>';
// echo "\nuser screen name: " . $r["user"]["screen_name"];
// echo '<br>';
// echo "\nuser id: " . $r["user"]["id"];
// echo '<br>';
// echo "\nid : " . $r["id"];
// echo '<br>';
// echo "\nin_reply_to_status_id: " . $r["in_reply_to_status_id"];
// echo '<p>';
// echo "\n\n";
$created_at = $r["created_at"];
$created_at = strtotime($created_at);
$mysqldate = date('Y-m-d H:i:s', $created_at);
//
// echo'<p>';
foreach ($topiclist as $key => $value) {
// echo'getshere!';
//$whichterm = $r["text"];
$whichterm = '"' . $r["text"] . '"';
//echo $whichterm;
if (stripos($whichterm, $value) !== false) {
// echo 'true:' . $value . '';
//find topicid
$whattopic = "SELECT TopicID
FROM Topics
WHERE Name = '$value'";
//var_dump($whattopic);
$tID = mysql_query($whattopic);
//var_dump($tID);
if (!$tID) {
// echo 'topic id not found.';
} else {
if (mysql_num_rows($tID) == 0) {
// echo 'This topic doesn′t exist.';
} else {
while ($rec = mysql_fetch_assoc($tID)) {
$inserttweets = "INSERT INTO
Tweets(Topics_TopicID, AddDate, Tweetcontent)
VALUES('" . mysql_real_escape_string($rec['TopicID']) . "',
'" . mysql_real_escape_string($mysqldate) . "',
'" . mysql_real_escape_string($r["text"]) . "')";
//WHERE TopicID = " . mysql_real_escape_string($row['Topics_TopicID'])
}
}
$addtweet = mysql_query($inserttweets);
if (!$addtweet) {
//something went wrong, display the error
//echo 'Something went wrong while adding tweet.';
//echo mysql_error(); //debugging purposes, uncomment when needed
} else {
echo 'Succesfully added tweet';
}
}
}
}
die();
$content = $a[1];
}
}
fclose($handle);
}
?>
"Pasting" a bunch of code inside a loop isn't a great practice. In fact, what you're looking for is a function or the use of a defined class. So, if you can, define a function in your topictweets.php that will contain your code and use it in your loop:
include 'topictweets.php';
foreach ($forumlist as $x => $fID) {
echo 'id:'.$fID.'<br>';
processYourForums($fID);
/////////
////////
}
try include_once()
however, why not have a loop within topictweets.php?
you can do the query, etc.. in this page, but then loop through it in the include
This should work fine:
include 'topictweets.php';
foreach ($forumlist as $x => $fID) {
echo 'id:'.$fID.'<br>';
}
You only need to include once.

php report is very slow and crashes in firefox

I have a report that runs and returns 366 records, each containing a thumbnail that is 104 x 80 px. The issue is that the report runs very slowley even though I increased the memory size.
ini_set('memory_limit', '128M');
ini_set('max_execution_time','600');
After writing the SQL query I generate the table items here
generate_table_items($query_all_items);
This then runs through and checks for the image in the columns
function generate_table_items($query){
$columns = array();
$resultset = array();
$scriptname = array();
$scriptname[0] = "/reports/all_items.php";
$scriptname[1] = "/reports/all_items_by_value.php";
$columncount = 0;
$rowcost = 0;
$rowsale = 0;
while ($row = mssql_fetch_assoc($query)) {
if (empty($columns)) {
$columns = array_keys($row);
echo '<tr><th scope="col" >'.implode('</th><th scope="col" >',get_column_name($columns)).'</th></tr>';
$columncount = sizeof(array_keys($row));
}
$resultset[] = $row;
echo '<tr><td>'.implode('</td><td>',report_image_check($row)).'</td></tr>';
if(in_array($_SERVER['SCRIPT_NAME'],$scriptname)){
$colspan = (count($columns)-2);
echo "<tr><th scope='row'>Documents</th><td colspan='$colspan' >";
$PKID = $row['ID'];
if($row['SumOfTotalCost'] || $row['SumOfSalePrice']){
$rowcost += $row['SumOfTotalCost'];
$rowsale += $row['SumOfSalePrice'];
$get_total = true;
}
$query_docs = mssql_query("select documents.* from dbo.documents where documents.Antiquities_id = $PKID") or die ('get docs query failed ' . mssql_get_last_message());
while($row_docs = mssql_fetch_assoc($query_docs)){
$document = "../documents/" . $row_docs['document'];
echo "<a href='$document' title='opens in a new window' target='_blank' >" . $row_docs['document'] . "</a> | ";
} // End while (list docs)
mssql_free_result($query_docs);
echo "</td></tr>";
myflush();
} // End if all items and all items by value report
} // End While
echo '<tr>';
for($i=0;$i < $columncount-4;$i++){
echo '<td> </td>';
}
echo '<td>Total Cost '. $rowcost.'</td>';
echo '<td>Total Sale '. $rowsale.'</td>';
echo '<td>Total Calculated Difference '. ($rowsale-$rowcost).'</td></tr>';
} // End function
function get_column_name($columns){
$newcol = array();
$scriptname = array();
$scriptname[0] = "/reports/all_items.php";
$scriptname[1] = "/reports/all_items_by_value.php";
$thecount = 0;
foreach($columns as $col) {
if($thecount == 1 && in_array($_SERVER['SCRIPT_NAME'],$scriptname)) {
// Don't list the PK
} else {
$newcol[] = '<img id="'.$col.'" src="../images/icons/arrow_down.png" alt="click to sort by" onclick="sortby(\''.$col.'\');" />' . $col;
}
$thecount++;
}
/*if(in_array($_SERVER['SCRIPT_NAME'],$scriptname)){
$newcol[] = "documents";
}*/
return $newcol;
}
function report_image_check($row){
global $base_url, $uploaded_images_folder;
$newrow = array();
$imageext = array();
$imageext[0] = ".jpg";
$imageext[1] = ".png";
$imageext[2] = ".gif";
$imageext[3] = ".tiff";
$scriptname = array();
$scriptname[0] = "/reports/all_items.php";
$scriptname[1] = "/reports/all_items_by_value.php";
$PKID = 0;
$thecount = 0;
foreach($row as $rn) {
if(in_array(strtolower(substr($rn,-4)),$imageext)){
$small_img_ext = substr($rn,-4);
$small_img = substr($rn,0,strripos($rn,"."));
$small_img = $small_img . '_140_105' . $small_img_ext;
$newrow[] = '<a href="' . $base_url . $uploaded_images_folder . '/' . $small_img . '" title="click to zoom on image" target="_blank" ><img src="' . $base_url . $uploaded_images_folder . '/' . $rn . '" alt="" width="50px" height="50px" /></a>';
} elseif($thecount == 1 && in_array($_SERVER['SCRIPT_NAME'],$scriptname)) {
$PKID = $rn;
} elseif($thecount == 2 && in_array($_SERVER['SCRIPT_NAME'],$scriptname)) {
$newrow[] = "<a href='../index.php?template=10&PKID=$PKID' target='_blank' >$PKID (click to view)</a>";
} else {
$newrow[] = $rn;
}
$thecount++;
myflush();
}
/*if (in_array($_SERVER['SCRIPT_NAME'],$scriptname)) {
$newrow[] = "<a href='#&PKID=$PKID' target='_blank' >Documents (click to view)</a>";
}*/
return $newrow;
} // End function
//// Flushing function
function myflush(){
ob_implicit_flush();
ignore_user_abort();
}
Can anyone see an issue with this code or see why it would take so long or why it crashes firefox? Would printing to pdf function work better?
It'll take a long time because you're nesting SQL queries... executing a second SQL query for every result that has been returned by the first query.... Doing a single query with a JOIN should help performance significantly.
Printing to PDF would almost certainly be slower: you'd eithe rneed a lot of code to position everything correctly in the report yourself, or to use one of the libraries that can take HTML and render it to a PDF (as you're already generating HTML anyway at the moment, this would be additional processing)

Categories