I made some code updates from 5.6 to 7.4.11 and this section of code stopped working, Its not tossing an error so im a little unclear where I went wrong.
Original code
<?
$mapdesc = get_post_meta( $post->ID, 'the_mapdesc');
if (count($maxmapdesclength) > 0 )
{
if (strlen($mapdesc[0]) > $maxmapdesclength) $mapdesc[0] = substr($mapdesc[0], 0, $maxmapdesclength-3) . '...';
$arrmapdesc = split("\n", $mapdesc[0]);
$mymapdesc = "";
for ($i=0; $i<count($arrmapdesc) && $i<3; $i++)
$mymapdesc .= $arrmapdesc[$i] . '<br />';
print $mymapdesc;
}
Updated code
<?
$mapdesc = get_post_meta( $post->ID, 'the_mapdesc');
// if (count($maxmapdesclength) > 0 )
if (is_countable($maxmapdesclength) && count($maxmapdesclength) > 0 )
{
if (strlen($mapdesc[0]) > $maxmapdesclength) $mapdesc[0] = substr($mapdesc[0], 0, $maxmapdesclength-3) . '...';
$arrmapdesc = explode("\n", $mapdesc[0]);
$mymapdesc = "";
for ($i=0; $i<count($arrmapdesc) && $i<3; $i++)
$mymapdesc .= $arrmapdesc[$i] . '<br />';
print $mymapdesc;
}
I suspect there's something wrong with your usage of $maxmapdesclength.
In the if statement, you are treating it as a Countable|array, and within the substr function you are then expecting it to act like a number.
Related
I would like to make a small change in a code for a table of contents.
I want to add a sign in front of each heading. The character should be recognized as text.
I've tried a few things, but unfortunately I have not found the right variable.
The code comes from a plugin for Wordpress
I have already tried the following variables:
$items
$tic
$find
$replace
$post
Here is the code that prints the list:
if ( $tic->is_eligible($custom_toc_position) ) {
extract( $args );
$items = $tic->extract_headings( $find, $replace,wptexturize($post->post_content) );
$title = ( array_key_exists('title', $instance) ) ? apply_filters('widget_title', $instance['title']) : '';
if ( strpos($title, '%PAGE_TITLE%') !== false ) $title = str_replace( '%PAGE_TITLE%', get_the_title(), $title );
if ( strpos($title, '%PAGE_NAME%') !== false ) $title = str_replace( '%PAGE_NAME%', get_the_title(), $title );
$hide_inline = $toc_options['show_toc_in_widget_only'];
$css_classes = '';
// bullets?
if ( $toc_options['bullet_spacing'] )
$css_classes .= ' have_bullets';
else
$css_classes .= ' no_bullets';
if ( $items ) {
// before widget (defined by themes)
echo $before_widget;
// display the widget title if one was input (before and after titles defined by themes)
if ( $title ) echo $before_title . $title . $after_title;
// display the list
echo '<ul class="toc_widget_list' . $css_classes . '">' . $items . '</ul>';
// after widget (defined by themes)
echo $after_widget;
}
This are the full code of function extract_headings:
public function extract_headings( &$find, &$replace, $content = '' )
{
$matches = array();
$anchor = '';
$items = false;
// reset the internal collision collection as the_content may have been triggered elsewhere
// eg by themes or other plugins that need to read in content such as metadata fields in
// the head html tag, or to provide descriptions to twitter/facebook
$this->collision_collector = array();
if ( is_array($find) && is_array($replace) && $content ) {
// get all headings
// the html spec allows for a maximum of 6 heading depths
if ( preg_match_all('/(<h([1-6]{1})[^>]*>).*<\/h\2>/msuU', $content, $matches, PREG_SET_ORDER) ) {
// remove undesired headings (if any) as defined by heading_levels
if ( count($this->options['heading_levels']) != 6 ) {
$new_matches = array();
for ($i = 0; $i < count($matches); $i++) {
if ( in_array($matches[$i][2], $this->options['heading_levels']) )
$new_matches[] = $matches[$i];
}
$matches = $new_matches;
}
// remove specific headings if provided via the 'exclude' property
if ( $this->options['exclude'] ) {
$excluded_headings = explode('|', $this->options['exclude']);
if ( count($excluded_headings) > 0 ) {
for ($j = 0; $j < count($excluded_headings); $j++) {
// escape some regular expression characters
// others: http://www.php.net/manual/en/regexp.reference.meta.php
$excluded_headings[$j] = str_replace(
array('*'),
array('.*'),
trim($excluded_headings[$j])
);
}
$new_matches = array();
for ($i = 0; $i < count($matches); $i++) {
$found = false;
for ($j = 0; $j < count($excluded_headings); $j++) {
if ( #preg_match('/^' . $excluded_headings[$j] . '$/imU', strip_tags($matches[$i][0])) ) {
$found = true;
break;
}
}
if (!$found) $new_matches[] = $matches[$i];
}
if ( count($matches) != count($new_matches) )
$matches = $new_matches;
}
}
// remove empty headings
$new_matches = array();
for ($i = 0; $i < count($matches); $i++) {
if ( trim( strip_tags($matches[$i][0]) ) != false )
$new_matches[] = $matches[$i];
}
if ( count($matches) != count($new_matches) )
$matches = $new_matches;
// check minimum number of headings
if ( count($matches) >= $this->options['start'] ) {
for ($i = 0; $i < count($matches); $i++) {
// get anchor and add to find and replace arrays
$anchor = $this->url_anchor_target( $matches[$i][0] );
$find[] = $matches[$i][0];
$replace[] = str_replace(
array(
$matches[$i][1], // start of heading
'</h' . $matches[$i][2] . '>' // end of heading
),
array(
$matches[$i][1] . '<span id="' . $anchor . '">',
'</span></h' . $matches[$i][2] . '>'
),
$matches[$i][0]
);
// assemble flat list
if ( !$this->options['show_heirarchy'] ) {
$items .= '<li><a href="#' . $anchor . '">';
if ( $this->options['ordered_list'] ) $items .= count($replace) . ' ';
$items .= strip_tags($matches[$i][0]) . '</a></li>';
}
}
// build a hierarchical toc?
// we could have tested for $items but that var can be quite large in some cases
if ( $this->options['show_heirarchy'] ) $items = $this->build_hierarchy( $matches );
}
}
}
return $items;
}
I tried it like this:
$items = '>'.$items
$tic = '>'.$tic
$find = '>'.$find
.
.
.
Unfortunately, nothing has hit the right place
$ items addressed only the entire list
The other Variables had no effect or led to errors
The extract_headings is creating the list items. This section of the function...
// assemble flat list
if ( !$this->options['show_heirarchy'] ) {
$items .= '<li><a href="#' . $anchor . '">';
if ( $this->options['ordered_list'] ) $items .= count($replace) . ' ';
$items .= strip_tags($matches[$i][0]) . '</a></li>';
}
Should look like this:
// assemble flat list
if ( !$this->options['show_heirarchy'] ) {
$items .= '<li><a href="#' . $anchor . '">>';
if ( $this->options['ordered_list'] ) $items .= count($replace) . ' ';
$items .= strip_tags($matches[$i][0]) . '</a></li>';
}
You can see I've added an extra > inside the hyperlink on the third line. If adding an extra > causes any issues, you can also use >.
I'm new to PHP and I've encountered an issue that is driving me crazy. Perhaps someone here can let me know what I'm doing wrong.
I have a from that a user fills out. The below script is using the date entered into the mysql database to generate json data:
<?php
include("../includes.php");
$sq = new SQL();
$TableName = "permissions";
$Fields = $_POST["Fields"];
$FieldsSTR = "`" . str_replace("*;*","`,`", $Fields) . "`";
$Join = "AND";
$Start = 0;
$Limit = $_POST["Limit"];
if($Limit == "")$Limit = 1000;
$Where = $_POST["Where"];
$WhereSTR = "";
if($Where !== "")$WhereSTR = str_replace("*;*"," $Join ", $Where);
$q = "SELECT $FieldsSTR FROM `$TableName` $WhereSTR";
$data = $sq->sqlQuery($q);
if(sizeof($data)>0)array_shift($data);
header("Content-Type: application/json");
echo "[";
foreach($data as $k=>$line){
echo "[";
echo "\"" . str_replace("*;*","\",\"",$line) . "\"";
echo "]";
if($k < sizeof($data) - 1)echo ",";
}
echo "]";
exit;
?>
The problem that I'm having is that it has stopped working. One day it's fine and the next day it's not working. I'm thinking that maybe the cause of this problem is that crazy user data has been entered into the database. In my foreach statement I tried to replace the ";" with a "" tag, but that didn't work.
Has anyone encountered this issue before? Perhaps someone can point me in the right direction!
Thanks
Jason
Thanks everyone for your input. I was able to stumble on an fix to my immediate problem. I changed my foreach loop to the following:
foreach($data as $k=>$line){
$parts = explode("*;*",$line);
$NEWLINE = array();
for($i = 0;$i < sizeof($parts);$i++){
$value = $parts[$i];
$value = str_replace("\r","<br>",str_replace("\n","<br>",$value));
$NEWLINE[] = "\"" . $value . "\"";
}
$FINDATA[] = "[" . implode(",",$NEWLINE) . "]";
}
But I will now look into into using json_encode() as mentioned in the comments.
Thanks,
Jason
I am getting this error in 3 spots in the function that I pasted below. There is more to this code, but I didn't think it was needed to get this thing figured out.
$count = count($collection);
$i = 1 ;
foreach ($collection as $product)
{
$j = 1 ;
$productId = $product->getDiamondsearchId();
$attributValueOptions = "[" ;
$attributValueOptions .= "'".$productId."', ";
foreach($filterAttributeIds as $filterAttributeId){
$attributValueCollection = Mage::getModel('diamondsearch/diamondsearchattributvalue')->getCollection()->addFieldToFilter('attribut_id',$filterAttributeId)->addFieldToFilter('diamondsearch_id',$productId)->getData();
$attrbutValueId = $attributValueCollection[0]['attrbut_value_id'];
//echo $attrbutValueId."<br>";
$attributValueOptionCollection = Mage::getModel('diamondsearch/diamondsearchattributoptionvalue')->getCollection()->addFieldToFilter('id',$attrbutValueId)->getData();
if($j == 1 && $attributValueOptionCollection[0]['attribut_value'] == ""){
break ;
}
if($j == 15){
$attributValueOptions .= "'".$attributValueOptionCollection[0]['attribut_value']."'";
}else{
$attributValueOptions .= "'".$attributValueOptionCollection[0]['attribut_value']."', ";
}
$j++;
}
if($count == $i ){
$attributValueOptions .= "]";
}else{
$attributValueOptions .= "], ";
}
$i++;
echo $attributValueOptions ;
}
The problem is with this line:
$attributValueCollection = Mage::getModel('diamondsearch/diamondsearchattributvalue')->getCollection()->addFieldToFilter('attribut_id',$filterAttributeId)->addFieldToFilter('diamondsearch_id',$productId)->getData();
$attrbutValueId = $attributValueCollection[0]['attrbut_value_id'];
You're trying to get the first item of array, but this variable is empty. You should verify and then do the stuff you want, like this:
if(!empty(attributValueCollection)){
$attrbutValueId = $attributValueCollection[0]['attrbut_value_id'];
Here's the complete gist:
https://gist.github.com/muriloazevedo/8dc5b11b17d1c4a3a518
But also is important to remember, if you trying to generate a json result, is better to use json_encode for that:
http://php.net/manual/pt_BR/function.json-encode.php
I have been trying to get this code to work for ages now and have looked at other questions but it is not working for me. Any guidance is greatly appreciated.
In context, there is a sql query to bring back all of the results for today's entries. These are converted to a CSV file every day. To import into the software then the end of each row must return "" and a carriage return except the last row which should not add a carriage return.
The code is below:
$xo1 = "\"\"";
$xo2 = "\r\n";
while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$row["$i"].'",';
if(++$i === $columns_total){
$xo1 = "";
$xo2 = "";}
}
$output .= $xo1;
$output .= $xo2;
}
I guess rtrim() should do the job, if I got the question right.
while ( $row = mysql_fetch_array( $sql ) ) {
foreach( $row as $column ) {
$output .= sprintf( '"%s",""' . "\r\n" , $column );
}
}
rtrim( $output, '""' . "\r\n" );
You can also use the index of the loop for this problem. When index is above zero apply the $xo1 and $xo2. For example:
for ($i = 0; $i < $columns_total; $i++) {
if ( $i > 0 ) {
$output .= $xo1 . $xo2;
}
// now apply other workings
$output .='"'.$row["$i"].'",';
}
That might be a fool question, but I need to know how to solve this:
Notice: Array to string conversion in C:\xampp\htdocs\search_view.php on line 248
Why am I getting this message, what can I do to solve it?
echo'<div id="thumb">
'.$ids = array();
$ids[] = $results['idGames'];
for ($i = 0; $i < count($ids); $i++) {
$id = $ids[$i];
$v = $results['total_votes'];
$tv = $results['total_value'];
if ($v)
$rat = $tv / $v;
else
$rat = 0;
$j = $ids[$i];
$id = $ids[$i];
echo '<div class="topcontentstar">
<div id="' . $id . '" class="">';
for ($k = 1; $k < 6; $k++) {
if ($rat + 1 > $k)
$class = "" . $k . " ratings_stars_index ratings_vote";
else
$class = "" . $k . " ratings_stars_index ratings_blank";
echo '<div class="' . $class . '"></div>';
}
echo '
</div>
</div></div>;
Because in this part of code you tried to convert an array to string via concatenation
echo'<div id="thumb">
(line 248) '.$ids = array();
Separate them: $ids = array()
echo'<div id="thumb">
(line 248) ';
$ids = array();
echo'<div id="thumb">
(line 248) '.$ids = array();
You are concatenating a string and an array, just as the errors says. You are echoing the string, and appending the array $ids to that. Because assigning a value gets a higher precedence than concatenating things, $ids is already an array.
You're doing this:
echo'<div id="thumb">
(line 248) '.$ids = array();
Basically, you can't concatenate array with a string and that's why the error appears.
To fix the error, you can separate the array declaration into a separate line:
echo'<div id="thumb">';
$ids = array();
Hope this helps!
As a side note, I can see a problem in your last few lines:
echo '
</div>
</div></div>;
Should be:
echo '</div></div></div>';