UPDATE:
Thanks for all the suggestions (what did I do to get -3? I don't understand that)
I found that the data field was set as VARCHAR and truncated the JSON off at 255 characters, making it invalid. I changed that, and the JSON is now valid according to JSONlist.com
But it still refuses to chow, so at the risk of getting more demerits, here is the current code, is there anything I am doing wrong?
JSON:
[{"name":"images/front_8mhztpaj.jpg","usrName":"GEDC0041.JPG","size":805229,"type":"image/jpeg","thumbnail":"images/front_8mhztpaj.jpg","thumbnail_type":"image/jpeg","thumbnail_size":10490,"searchStr":"front_8mhztpaj.jpg,!:sStrEnd"}]
New Code:
<?php
$json = $DETAILS->getColumnVal("IMAGE");
//
json_decode($json, true);
?>
<?php if ($DETAILS->getColumnVal("IMAGE") != "") { ?>
<?php $imgsrc = "input/".$myImage = $myArray[0]['name']; ?>
<img src="<?php echo $imgsrc ?>" class="img-responsive center-block" alt="<?php echo $DETAILS->getColumnVal("TITLE"); ?>"> <?php echo $imgsrc; ?>
<?php } ?>
</div>
</div>
<?php
$DETAILS->moveNext();
}
$DETAILS->moveFirst(); //return RS to first record
?>
The query is still working correctly, when I ask it to echo "IMAGE" it shows the json string I have posted here, but in the page where the image data should be it just shows "input/" and no text afterwards.
What am I doing wrong?
The JSON you provided is misformated, there's missing closing tags. Seems the backslashes are mandatory, you can remove them if you have no need to escape your paths. (#alex-t)
Also, a good practice is to avoid PHP opening/closing tags in the middle of your code. That'll make it less messy.
<?php
$json = $DETAILS->getColumnVal("IMAGE");
// Stripslashes() is mandatory
$myArray = json_decode(stripslashes($json), true);
// Security check in case json_decode returns false.
if ($myArray) {
$myImage = $myArray[0]["name"];
$myTitle = $DETAILS->getColumnVal("TITLE");
// Into a double quoted string, you can 'inject' php vars.
// just put it like $myVar or wrap it with {$myVar}
echo "<img src=\"input/{$myImage}\" class=\"img-responsive center-block\" alt=\"{$myTitle}\">";
}
SOLVED
The main issue I had (thanks to you guys) was that the data field containing the JSON was set as a VARCHAR, and truncated to 255 characters.
The secondary issue I had was a php error
json_decode($json, true);
should have read:
$myArray = json_decode($json, true);
Thanks to all who helped
You need to remove the slashes from the JSON string first:
$myArray = json_decode(stripslashes($json), true);
Related
I have code like this ;
$Photo = filtering($row['FileIcon128']);
<div class="image" style="background:url(/images/imgs/<?php echo $Photo ?>)"></div>
I tried everything
filtering($row['FileIcon128']); , echo ''.$Photo.'';
instead of
$Photo
But i cant get picture. Any idea?
By the way, i have to use
<div class="image"
Give this a shot:
<?php
$Photo = filtering($row['FileIcon128']);
$string = "/images/imgs/" . $Photo;
?>
<div class="image" style="background-image:url('<?php echo $string; ?>')"></div>
Explanation
I saw 3 possible bugs in your code, which were:
1) <?php echo $Photo ?> without a semicolon after $Photo. I don't know if that's a syntax error or not in inline php, however I added the semicolon to make sure and concatenated the static string with the dynamic variable to make for a less messy inline PHP like this: <?php echo $string; ?>.
2) background:url() is not the right way to do this. You want to be as explicit as you can, so background-image:url().
3) background-image:url() without '' (apostrophes) surrounding the parameter passed. background-image:url('') fixed that. I just echoed the code in between the two apostrophes.
I thought this would have been simple enough using strpos, so I must be oblivious to a horribly obvious mistake. I have this code here:
<div class="text" style="text-align:center;border:2px solid #bbb;font-size:12px;padding:0.2%;">
<?php
$root = realpath($_SERVER['DOCUMENT_ROOT']);
include "$root/scripts/display.php";
?>
</div>
Within the included file is a simple array that displays a random string from it:
$data=array("random string","something","something else","and so on");
$info=$data[rand(0,count($data)-1)];
echo $info;
What I want to do is to display an image that is relevant to a few of the random strings that are echoed. I thought I could do this with an if check for strpos as, for example "random" in $info, such that:
...
?>
</div>
<?php if(strpos($info,"random") !== false){?>
<img src=...>
<?php } ?>
Problem is, this displays the image, even if "random" was not echoed.
I have a feeling this might have something to do with $info being checked, right?
can you please check with the following if it solves you problem
$data=array("1","2","3","4","5");
$info = array_rand($data, count($data));
print_r($info);
<?php if(in_array("4", $info)){?>
<img src=...>
<?php } ?>
I am trying to check to make sure a custom-field is not blank before echoing the custom-field.
This is what I have
<?php
$key = 'one_line_summary';
$themeta = get_post_meta($post->ID, $key, TRUE);
if($themeta != '') {
echo '<blockquote><?php echo get_post_meta($post->ID, one_line_summary, true); ?></blockquote>';
}
?>
But it out puts the "get_post_meta($post->ID, one_line_summary, true);" literally rather than the contents of the variable one_line_summary.
I am a beginner but I feel like I need to either use nested echo's somehow or change the second echo all together?
Thanks in advance.
You have nested <?php ?> inside an existing set of PHP tags, which is not allowed. Remove those, and concatenate in the function call to get_post_meta(). What happened here is that the inner <?php ?> tags were output as strings to the browser, but not rendered onscreen (since the browser treated them as unknown HTML tags).
echo '<blockquote>' . get_post_meta($post->ID, one_line_summary, true) . '</blockquote>';
As a note, these kinds of issues are considerably easier to spot with proper code indentation as was done when your post was edited above.
xxxi have the_title() that returns some text, in this case Blue & Whiny
the problem as we can see is that the & character looks different
how do i turn Blue & Whiny into Blue & Whiny i tryed: htmlspecialchars_decode(the_title()), html_entity_decode(the_title()),htmlspecialchars(the_title()) and nothing.
i want to convert & to &
there is not much code to share, I just do this: <?php the_title() ?> and i get Blue & Whiny. If i use get_the_title() it wont display anything
Any ideas?
Thanks
edit1. ill share some code:
<script type="text/javascript">
function showShareUI() {
var act = new gigya.services.socialize.UserAction();
act.setUserMessage("Check out this article.");
act.setTitle("Trends on Explore Talent - <?php the_title(); ?>");
act.setDescription("<?php get_the_content(); ?>");
act.setLinkBack("<?php the_permalink(); ?>");
act.addActionLink("Check out this article", "<?php the_permalink(); ?>");
var image = {
src: 'http://xxx.com/wp-content/uploads/2011/05/BOTTOM_BANNER.jpg',
href: '<?php the_permalink();?>',
type: 'image'
}
act.addMediaItem(image);
var params =
{
userAction: act, // The UserAction object enfolding the newsfeed data.
onError: onError, // onError method will be summoned if an error occurs.
onSendDone: onSendDone // onError method will be summoned after
,showEmailButton: true
// Gigya finishes the publishing process.
};
gigya.services.socialize.showShareUI(conf, params);
}
function onError(event) {
alert('An error has occured' + ': ' + event.errorCode + '; ' + event.errorMessage);
}
function onSendDone(event)
{
document.getElementById('status').style.color = "green";
document.getElementById('status').innerHTML = 'The newsfeed has been posted to: ' + event.providers;
}
</script>
I've tried everything. This starts to annoy me...
html_entity_decode() is the correct way to do it.
html_entity_decode("Blue & Whiny");
Will produce:
Blue & Whiny
If it's not working, make sure you don't have another issue - such as passing a string to it that is double encoded, or running htmlentities() on the string again later.
Demo: http://codepad.org/BHXGWXJi
Double check with a literal string and var_dump() the output, you should see the decoded version. Then var_dump(the_title()), to make sure you are actually passing what you think you are to html_entity_decode().
html_entity_decode should do the trick. If not, try to specify the third parameter $charset.
Something like:
echo html_entity_decode(the_title(), ENT_QUOTES, 'UTF-8');
the_title() directly prints the title, so adding html_entity_decode() directly around that won't work. You can, however, stop it from printing with its third function argument. E.g.
<?php echo html_entity_decode(the_title('', '', false)) ?>
There's also get_the_title(), which doesn't directly print the title, but it requires the ID of the post you want the title of, in contrast with the_title, which prints the title of the current post in The Loop. So you need to do something like this:
<?php echo html_entity_decode(get_the_title($post->ID)) ?>
And actually, you should be able to simply do:
<?php echo $post->post_title ?>
The only reason these utility functions are there is to escape things for you and add tags and stuff. If you just want the raw input, you can print it directly.
This won't fix all of your issues, though, because you're echoing it inside a JavaScript string, so you need to escape certain characters. json_encode() should do the trick, but see the question "Pass a PHP string to a Javascript variable (including escaping newlines)" for more details.
Try this:
echo(mb_convert_encoding(the_title(), "UTF-8", "HTML-ENTITIES"));
see if this works for ya
$convmap = array (0x0, 0xffff, 0, 0xffff);
//$str = mb_decode_numericentity (the_title(), $convmap, 'UTF-8' );
$str = mb_decode_numericentity ("&", $convmap, 'UTF-8' );
echo $str;
http://www.php.net/manual/en/function.mb-decode-numericentity.php
I haven't found anytihng in Google or the PHP manual, believe it or not. I would've thought there would be a string operation for something like this, maybe there is and I'm just uber blind today...
I have a php page, and when the button gets clicked, I would like to change a string of text on that page with something else.
So I was wondering if I could set the id="" attrib of the <p> to id="something" and then in my php code do something like this:
<?php
$something = "this will replace existing text in the something paragraph...";
?>
Can somebody please point me in the right direction? As the above did not work.
Thank you :)
UPDATE
I was able to get it working using the following sample:
Place this code above the <html> tag:
<?php
$existing = "default message here";
$something = "message displayed if form filled out.";
$ne = $_REQUEST["name"];
if ($ne == null) {
$output = $existing;
} else {
$output = $something;
}
?>
And place the following where ever your message is to be displayed:
<?php echo $output ?>
As far as I can get from your very fuzzy question, usually you don't need string manipulation if you have source data - you just substitute one data with another, this way:
<?php
$existing = "existing text";
$something = "this will replace existing text in the something paragraph...";
if (empty($_GET['button'])) {
$output = $existing;
} else {
$output = $something;
}
?>
<html>
<and stuff>
<p><?php echo $output ?></p>
</html>
but why not to ask a question bringing a real example of what you need? instead of foggy explanations in terms you aren't good with?
If you want to change the content of the paragraph without reloading the page you will need to use JavaScript. Give the paragraph an id.<p id='something'>Some text here</p> and then use innerHTML to replace it's contents. document.getElementById('something').innerHTML='Some new text'.
If you are reloading the page then you can use PHP. One way would be to put a marker in the HTML and then use str_replace() to insert the new text. eg <p><!-- marker --></p> in the HTML and $html_string = str_replace('<!-- marker -->', 'New Text', $html_string) assuming $html_string contains the HTML to output.
If you are looking for string manipulation and conversion you can simply use the str_replace function in php.
Please check this: str_replace()
If you're using a form (which I'm assuming you do) just check if the variable is set (check the $_POST array) and use a conditional statement. If the condition is false then display the default text, otherwise display something else.