I have a json file data, cache.txt. When I try to write the data I retrieved from it I get this error Illegal string offset for all fields.
I get the data but I cannot read fields, I don't know why
The file Data
s:729:"[{"id":"787","name":"XXY","release":"2007","Language":"Spanish","cover":"upload\/photos\/2019\/06\/sKRTzoEjRPCeHWsCrcHi_19_c12db8f07c6acb75a430a0c63c4f7e8d_image.jpg"},{"id":"638","name":"Barfi","release":"2012","Language":"Hindi","cover":"upload\/photos\/2019\/04\/uJD7t2q3XQcsOjSORv1c_25_eff4e484426302484d8de738dcb99470_image.jpg"},{"id":"937","name":"Secretary","release":"2002","Language":"English","cover":"upload\/photos\/2019\/01\/Secretary.jpg"},{"id":"829","name":"Love Meet Hope","release":"2016","Language":"English","cover":"upload\/photos\/2019\/01\/Love-Meet-Hope.jpg"},{"id":"412","name":"Daughters Of Darkness","release":"1971","Language":"English","cover":"upload\/photos\/2019\/01\/Daughters-Of-Darkness.jpg"}]";
<?php
WritePopularMovies();
function WriteData()
{
$movies = json_decode(GetData(),true);
for($i=0;$i < 5;$i++)
{
echo "<div class='col-exs-6 col-xs-4 col-sm-3 col-md-3'>";
echo "<figure>";
echo "<a href='" . $movies[$i]['id'] . "'>";
echo "<img src='" . $movies[$i]['cover'] ."' alt='".$movies[$i]['name'] .
"' class='responsive-img' style='height:250px;width:170px'>";
echo "</a>";
echo "<h6 title=". $movies[$i]['name'] ."><a href='". $movies[$i]['id']."'>".
$movies[$i]['name']."</a></h6>";
echo "<p>". $movies[$i]['genre']. "(".$movies[$i]['language'].")| ".
$movies[$i]['release'] ."</p>";
echo "</figure>";
echo "</div>";
}
}
function GetData() {
$data = array();
$file = $_SERVER['DOCUMENT_ROOT'] .'/cache.txt';
$data = unserialize(file_get_contents($file));
return json_encode($data,true);
}
?>
I tried to solve it but with no luck, so I will appreciate your help.
Many thanks
In your GetData() you are reading the data and then encoding it...
return json_encode($data,true);
this isn't necessary and stops the next part working, just return the raw data
return $data;
Related
Forgive me if this is simple but I have a for each loop that searches JSON data for results from a search. I then have some preg_match statements that will look at some of the tags within the JSON and if their is a match display the thumbnail in a div. and currently this all works. But it currently displays every result in its own Div and i want just five divs with multiple images within if there is a match.
foreach ($hits as $hit)
{
$target = $hit->metadata->baseName;
$target1 = $hit->metadata->cf_imageType;
if(preg_match("/_cm/i",$target)) {
echo '<div id="div5">';
echo '<h2>Creative</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
if(preg_match("/_SS/i",$target)) {
echo '<div id="div6">';
echo '<h2>Styled</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
if(preg_match("/_SH/i",$target)) {
echo '<div id="div7">';
echo '<h2>Group</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
if(preg_match("/still life/i",$target1) && preg_match("/_sm_00|_sd_00/i",$target)) {
echo '<div id="div8">';
echo '<h2>Cutout</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
if(preg_match("/worn/i",$target1)) {
echo '<div id="div9">';
echo '<h2>Worn</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
}
I cant quite figure out how to accomplish this, would it be the case of putting the results into an array and then displaying the results within the div?
Any help would be greatly appreciated.
You are right and answered the question yourself :)
Collect the results in a first step and create the markup in a second step. Something like this will do it:
$creative = [];
$styled = [];
/* ... */
function getHitInfos($theHit)
{
return [
"url" => $theHit->thumbnailUrl,
"name" => $theHit->metadata->baseName
];
}
function printResults($results, $title) {
echo '<div>';
echo '<h2>'.$title.'</h2>';
foreach ($results as $key => $val) {
echo "<img src='" . $val["url"] . "' alt='error'>";
echo $val["name"];
}
echo '</div>';
}
foreach ($hits as $hit)
{
$target = $hit->metadata->baseName;
$target1 = $hit->metadata->cf_imageType;
echo '<div>';
if(preg_match("/_cm/i",$target)) {
$creative[] = getHitInfos($hit)
}
if(preg_match("/_SS/i",$target)) {
$styled[] = getHitInfos($hit)
}
/* ... */
}
printResults($creative, "Creative");
printResults($styled, "Styled");
/* ... */
Disclaimer: My last contact with php is some years ago, but I hope you will see the point here.
(I also created some helping functions to DRY the code)
I'd like to add an active class with PHP instead of using javascript.
But I probably broke the code somewhere.
I'm using bootstrap.js version 3 and I'm building my tabs using PHP because I need to use my tabs on the control panel in my app.
So essentially I didn't add any javascript or jquery so far, I'm just using the native functions for tabs in bootstrap.js (and jquery):
<?php foreach ($database as $item): ?>
<?php
if (isset($item['associations'])) {
foreach ($item['associations'] as $value) {
echo "<div class='modal' id='des_" . $value['des_id'] . "'>";
echo "<div class='modal-sandbox'></div>";
echo "<div class='modal-box'>";
echo "<div class='modal-body'>";
echo "<div class='close-modal'>✖</div>";
echo "<h3>Credit and values</h3>";
echo "<h5>" . $value["designation_name"] . " - Credit: " . $value["designation_credits"] . "</h5>";
echo "<ul class='nav nav-pills'>";
if (isset($value['credits']) && !is_string($value['credits'])) {
foreach ($value['credits'] as $credits) {
$first_element = reset($value["credits"]);
if (isset($value['credits'])) {
if ($first_element[4] == $credits[4]) {
echo "<li class='active'><a href='tab_" . $credits[4] . "' data-toggle='tab'>Identifier: " . $credits[0] . "</a></li>";
} else {
echo "<li><a href='tab_" . $credits[4] . "' data-toggle='tab'>Identifier: " . $credits[0] . "</a></li>";
}
}
}
}
echo "</ul>";
echo "<div class='tab-content clearfix'>";
if (isset($value['credits']) && !is_string($value['credits'])) {
foreach ($value['credits'] as $credits) {
if (isset($value['credits'])) {
if ($first_element[4] == $credits[4]) {
echo "<div class='tab-pane active' id='tab_" . $credits[4] . "'>";
} else {
echo "<div class='tab-pane' id='tab_" . $credits[4] . "'>";
}
echo "<div class='single-credit'>";
?>
<?=form_open();?>
<?php
echo "<input type='hidden' name='id' value='" . $credits[4] . "'>";
echo "<div>Identifier: <span>" . $credits[0] . "</span></div>";
echo "<input name='edit_credits' type='submit' value='Edit' class='submit'>";
echo "</div>";
echo "</div>";
echo "</div>";
?>
<?=form_close()?>
<?php
}
}
}
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
}
}
?>
<?php endforeach;?>
To explain my code I will start from the list:
Since it's a really complex array, I'm taking the first element with the reset function to add the active class only on that very element.
Then I added the href element to point to the id of the matching div later on, and then I close my list.
Later on, I create another loop and I use the id to match both the list and the div and I used the trick that I used before to add the class active only on my first element of my array.
Everything works except for the fact that, if I click another element of the list, the matching div won't work, the very first element of the array will keep the class active, so I can't switch beteween element.
I need to spot the error.
UPDATE:
The problem that I have with jquery, in this case, is that if I use a toogleClass for example like that:
$(".nav-pills li").click(function(e) {
e.preventDefault();
tab = $(this).find('a').attr('href');
$("#" + tab).toggleClass('active');
});
It won't be able to toggle properly the class on the first element, that one to which I added the class active with php.
I think you miss to change the URL when click it, in the backend (PHP) you need to know where the user is, so, you check the "parameter" and mark active or not the current tab.
For example:
printf("<div class='tab-pane %s' id='...'>", $credits[4] == $_GET['tab'] ? 'active':'');
I need to do something like
$var = "Hello World ..the value is <?php echo 'XYZ' ?>";
Any help will be highly useful. I am stuck in this from the very long time but not able to acheive this. please help
Below is my PHP code that I need to assigned again to php variable (this variable will be passed to return of my ajax call)
<div class="photo-post">
<h4 class="h4-1">Some data...</h4>
<?php
$i=0;
$unique_seq="888094499";
$pic_counter=1;
foreach (glob("target/*".$unique_seq."*") as $filename)
{
$i++;
}
$total_pics=$i;
echo "<div id='post-photo-slider'>";
if($i>1)
{
echo "<a href='#' class='control_next_photo'>></a>";
echo "<a href='#' class='control_prev_photo'><</a>";
}
echo "<ul>";
foreach (glob("target/*".$unique_seq."*") as $filename)
{
echo "<li>";
echo "<div class='photos-slide-container'>";
echo "<img src='".$filename."' width='540px' height='225px'>";
echo "<span class='count-span-photos'>".$pic_counter." of ".$total_pics."</span>";
echo "</div>";
echo "</li>";
$pic_counter++;
}
if ($i==1)
{
echo "<li></li>";
}
echo "</ul>";
echo "</div><!-- post-photo-slider END -->";
echo "<hr class='hr4'>";
?>
</div><!-- photo end -->
you cannot do it in this way
$var = "Hello World ..the value is <?php echo 'XYZ' ?>";
when you return this string to your javascript php code cannot be execute on client side!!!!
why do not return do this simple way?
$var = "Hello World ..the value is " . "XYZ";
Its been a while since the last time I was coding, and I am a little rusty. I am trying to parse an xml file and having a problem with the condition. Spent hours trying to figure out where is the problem, but without success...
PHP CODE:
<div id="gallery">
<?php
$cat_img="images/cat.png";
$logo_path="logos/";
$file = simplexml_load_file('stores.xml');
$old_cat="";
foreach($file->store as $store){
if($store->cat != $old_cat){
echo "<img id='cat' src='".$cat_img."'><div id='cat_text'>".$store->cat."</div><br>";
$old_cat=$store->cat;
}
echo "<div id='store'><img id='store_img' src='".$logo_path.$store->logo."' alt='logo'><br>";
echo "<store_name>".$store->name."</store_name>";
echo "<phone>".$store->phone."</phone><phone>טל: </phone></div>";
}
?>
</div>
STORES.XML
<?xml version="1.0" encoding="UTF-8"?>
<stores>
<store><logo>renuar.jpg</logo>
<name>renuar</name>
<phone>052-6059962</phone>
<cat>clothing</cat></store>
<store><logo>yoop.jpg</logo>
<name>YOOP</name>
<phone>08-6601451</phone>
<cat>clothing</cat></store>
....
</stores>
The idea is to print every store, when category changes it should print the category divider and keep printing the stores. Somehow the condition is always true and I have no idea why...
p.s
If anyone has a better idea on how to divide the categories, a different method, I would be happy to hear.
Thanks!!!
$store->cat will be SimpleXMLElement object and two object will not be same even with same category name. You need to cast it to string for comparison
Like (string) $store->cat, try this:
<?php
$cat_img = "images/cat.png";
$logo_path = "logos/";
$file = simplexml_load_file('test.xml');
$old_cat = "";
foreach ($file->store as $store) {
if ((string) $store->cat != $old_cat) {
echo "<img id='cat' src='" . $cat_img . "'><div id='cat_text'>category: " . $store->cat . "</div><br>";
$old_cat = (string) $store->cat;
}
echo "<div id='store'><img id='store_img' src='" . $logo_path . $store->logo . "' alt='logo'><br>";
echo "<store_name>" . $store->name . "</store_name>";
echo "<phone>" . $store->phone . "</phone><phone>טל: </phone></div>";
}
?>
Alright, so I am trying to figure this out. Basically I have everything working well except for adding the cover photos to each of the DIVS. Basically I have 2 queries using FQL, and here is my code.
$query =
'{"query1":"SELECT aid, cover_pid, name, description FROM album WHERE owner=$fbowner",
"query2":"SELECT src FROM photo WHERE pid IN (SELECT cover_pid FROM #query1)"}';
$fqlResult = $facebook->api(array(
'method' => 'fql.multiquery',
'queries' => $query));
$fql = $fqlResult[0]['fql_result_set'];
$covers = $fqlResult[1]['fql_result_set'];
$the_count = count($fql);
$i = 0;
foreach($fql as $value) {
$album_cover = $value['src'];
echo "<div class='fb_block'>";
echo "<a href='" . $url . "?action=list_pics&aid=" . $value['aid'] . "&album_name=" . $value['name'] . "'>";
echo "<img src='{$covers[$i]['src']}' border='1'>";
echo "</a>";
echo "<h3>".$value['name']."</h3>";
echo "<p>".$value['description']."</p>";
echo "</div>";
$i++;
}
Now that works fine, but what if I put this in the mix, the cover photos are all off unless I make $i = 1.
$i = 0;
foreach($fql as $value) {
if($value['name'] != 'Wall Photos'){
$album_cover = $value['src'];
echo "<div class='fb_block'>";
echo "<a href='" . $url . "?action=list_pics&aid=" . $value['aid'] . "&album_name=" . $value['name'] . "'>";
echo "<img src='{$covers[$i]['src']}' border='1'>";
echo "</a>";
echo "<h3>".$value['name']."</h3>";
echo "<p>".$value['description']."</p>";
echo "</div>";
$i++;
}
}
and if I stick another foreach in the mix, it grabs all the cover photos and sticks them into each div. So what is the easiest solution to just go in order and grab the cover photo associated with the album node?
Thanks in advance!
I tried this, too. You can't rely on the indexes being consistent between these two queries.
To make it work, first modify query2 to also return pid, then replace this line in your code:
echo "<img src='{$covers[$i]['src']}' border='1'>";
with this:
foreach($covers as $cover) {
if ($value['cover_pid'] === $cover['pid']) {
echo "<img src='{$cover['src']}' border='1'>";
break;
}
}