PHP and JSON entering inside a Div - php

So, I have a JSON array, which parses just fine in the layout view, but the elements from json stack vertically and I need them to be horizontally aligned.
Here is the example code
<div class="swiper-container mySwiper2 swiper-container-h">
<div class="swiper-wrapper">
<div class="swiper-slide swiper-padding">
<?php include_once("team_hq.php"); ?>
</div>
<div class="swiper-slide swiper-padding bkg bkg3">
<h2>Partners</h2>
<?php
showStuff($stuff, "Partners"); ?>
</div>
<div class="swiper-slide swiper-padding bkg bkg12">
<h2>Production</h2>
<?php showStuff($stuff, "Production"); ?>
</div>
<div class="swiper-slide swiper-padding bkg bkg13">
<h2>Sales</h2>
<?php showStuff($stuff, "Sales"); ?>
</div>
<div class="swiper-slide swiper-padding bkg bkg14">
<h2>Support</h2>
<?php showStuff($stuff, "Support"); ?>
</div>
</div>
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-pagination"></div>
</div>
The question I'm asking is how can I make the content which is pulled from the $stuff variable present horizontally instead of vertically as is now.

To position HTML elements horizontal either use:
Flexbox or CSS-Grid or Floating
But assuming your function showStuff just prints a JSON string, you'd have to work with your $stuff variable. E.g. loop over the it's content – with PHP I suppose – and build the HTML-Tags according your needs.

Related

How to change link for each item in foreach loop in PHP?

Totally no experience with PHP and I need to change some little thing by my self, the developer has made a generic html component with a generic link ("href=#"). This should be different for each item in foreach loop. Pretty simple but totally difficult who doesnt know the syntax.
Anyone a quick fix?
<?php foreach ($pricing['price_box'] as $box) :?>
<div class="col-lg-6 mb-4 mb-lg-0">
<div class="p-4 pl-5 pr-5 pb-5">
<h3 class="text-center pt-4 pb-5"><?php echo $box['title'] ?></h3>
<div class="d-md-flex justify-content-md-between mb-5">
<?php echo $box['detail'] ?>
<div class="price-unit align-self-center">
<?php
$priceArray = explode('.', $box['price']);
echo $box['price'] ? $priceArray[0] . ",<span>$priceArray[1]</span>" : ''
?>
<div><?php echo $box['duration'] ?></div>
</div>
</div>
<div class="text-center">
Registreren
</div>
</div>
</div>
<?php endforeach; ?>
Since the anchor tag is inside the foreach loop, you can use the $box variable in the anchor tag.
Also, you can use the php variable inside the html by echo it.
For example, if your link is something like some_php_script.php?id=XXX and you want to replace the xxx with a unique id, you can use it like:
Registreren
assumes that the $box array contains an index called id.
Read more about php echo function.
Read more about php arrays here.
Assuming you need to change the link here :
<div class="text-center">
Registreren
</div>
As it is already in the loop, and you have $box array, you need to find out on which index the link href and text(the one you need to display instead of Registreren ) is available. Suppose you have the link at index link and text is at text, then your code will be like
<div class="text-center">
<?php echo $box['text'];?>
</div>
You can check the array structure using print_r()
Reference
Arrays
foreach

How to change css in php file

I am using post navigation for my blog. For this portion i wrote some css. It's working fine. But the problem is for the very latest post , it's showing blank space for next post link. Same thing is happened with oldest post, it's showing again bank space for previous post link.
I add code snippet for this portion. Can i anyone suggest how can i modify my css for this portion. Following code is embedded in php file.
<div class="row justify-content-center">
<div class="sewl-more-posts">
<div class="previous-post">
<?php If(previous_post_link()){?>
<span class="post-control-link">Previous Article</span>
<span class="post-name"><?php previous_post_link(); ?></span>
<?php } ?>
</div>
<div class="next-post">
<?php If(next_post_link()){?>
<span class="post-control-link">Next Article</span>
<span class="post-name"><?php next_post_link(); ?></span>
<?php } ?>
</div>
</div>
</div>
First, output the divs only if they'll have content:
<div class="sewl-more-posts">
<?php if (previous_post_link()){?>
<div class="previous-post">
<span class="post-control-link">Previous Article</span>
<span class="post-name"><?php previous_post_link(); ?></span>
</div>
<?php } ?>
<?php if (next_post_link()){?>
<div class="next-post">
<span class="post-control-link">Next Article</span>
<span class="post-name"><?php next_post_link(); ?></span>
</div>
<?php } ?>
</div>
This way, if the previous-post or next-post div is missing, you'll be able to target the remaining one with the :only-child pseudo class:
.sewl-more-posts > div:only-child {
text-align: center;
}
You cannot do this with php once the page is generated client-side, therefore you have to use javascript (for example):
onclick = documet.getElementById('the id you have').style.property = "blue";

How to add full html code into an PHP array and echo the array with array_rand function

I am trying to print random slections from an array. However array contains full html and it causes problems, it does not echo the whole html. It just displays the texts within the HTML. I have tried singles quotes but also did not work.
Here is the the thing what i have done;
$items = Array('<div class="custom-top-tags">
<div class="sm-top-tags-title">Öne Çıkanlar:</div>
<div class="sm-top-tags-keywords">
<ul class="sm-top-tags-keywords-list">
<li>Matkaplar</li>
<li>İş Eldivenleri</li>
<li>Mikser & Karıştırıcı</li>
<li>Silikon</li>
<li>Tangitt</li>
<li>Lokma Takımı</li>
<li>Plastik Boru Makası</li>
<li>El Arabası</li>
<li>Vanalar</li>
<li>Boya</li>
<li>Kompresör</li>
</ul>
</div>
</div>',
'<div class="custom-top-tags">
<div class="sm-top-tags-title">Öne Çıkanlar:</div>
<div class="sm-top-tags-keywords">
<ul class="sm-top-tags-keywords-list">
<li>Matkaplar</li>
<li>İş Eldivenleri</li>
<li>Mikser & Karıştırıcı</li>
<li>Silikon</li>
<li>Tangitt</li>
<li>Lokma Takımı</li>
<li>Plastik Boru Makası</li>
<li>El Arabası</li>
<li>Vanalar</li>
<li>Boya</li>
<li>Kompresör</li>
</ul>
</div>
</div>');
echo $items[array_rand($items)];
And my expectaion is displaying randomly selected HTML code.
Meaning, i need to choose different links randomly from a list and i need to display them in html with codes as seen below.
<div class="custom-top-tags">
<div class="sm-top-tags-title">Öne Çıkanlar:</div>
<div class="sm-top-tags-keywords">
<ul class="sm-top-tags-keywords-list">
<li>Matkaplar</li>
<li>İş Eldivenleri</li>
<li>Mikser & Karıştırıcı</li>
<li>Silikon</li>
<li>Tangitt</li>
<li>Lokma Takımı</li>
<li>Plastik Boru Makası</li>
<li>El Arabası</li>
<li>Vanalar</li>
<li>Boya</li>
<li>Kompresör</li>
</ul>
</div>
</div>
Waiting for the suggestions and helps.
The line breaks in you code won't work...
I would suggest to split the code into a temp variable and then array allocation
And use the proper multi lkne syntax
1st of all for styling purposes, and also to prevent this error
You should use $tempvar and the code I wrote for each array insertion
$items = array();
$tempvar = <<<TEMP
<div class="custom-top-tags">
<div class="sm-top-tags-title">Öne Çıkanlar:</div>
<div class="sm-top-tags-keywords">
<ul class="sm-top-tags-keywords-list"> <li>Matkaplar</li> <li>İş Eldivenleri</li>
<li>Mikser & Karıştırıcı</li>
<li>Silikon</li>
<li>Tangitt</li> <li>Lokma Takımı</li>
<li>Plastik Boru Makası</li>
<li>El Arabası</li> <li>Vanalar</li>
<li>Boya</li> <li>Kompresör</li> </ul> </div> </div>
TEMP;
$items[] = $tempvar;
...
that is one possible solution
echo htmlspecialchars($items[array_rand($items)]);

How access nested elements with repeated class xpath

I need access a nested element with repeated class, like that:
<div class="container">
<div class="first"></div>
<div class="first"></div>
<div class="first">
<div class="second"></div>
<div class="second">
<p>I need that text</p>
</div>
</div>
</div>
So i try something like that:
$localizacao_x = $xpath_det_page->query('//div[#class="container"]/div[#class="first"][3]/div[#class="second"][2]/p');
$localizacao = $localizacao_x->item(0)->nodeValue;
echo "[Localizacao] : [".$localizacao."]"."<br/>";
But result in non object, any tip?
Your XPath seems to be correct. I tested
//div[#class="container"]/div[#class="first"][3]/div[#class="second"][2]/p
which result is
I need that text

Include php is throwing result in header

I am trying to use the php code to display a comment box on a page:
$object_id = 'article_12';
include('commentanything/php/loadComments.php');
For some reason the comment box is not appearing where i want it to appear,it keeps throwing the comment box to header, is there any way i can make it appear exactly where i want it to?, here is where i am trying to use it(not a complete code):
<!--<h1 align="center" style="font-size:1.2em"><strong>'.$row['title'].'</strong></h1>-->
<div class="song-art"><img src="'.$this->url.'/thumb.php?src='.$row['art'].'&t=m&w=112&h=112" id="song-art'.$row['id'].'" title="'.$row['title'].'" alt="'.$row['title'].'"/></div>
<div class="song-top">
<div class="song-timeago">
<a href="'.$this->url.'/mp3download/'.$row['id'].'/'.$this->genSlug($row['title']).'.html"><span id="time'.$row['id'].'">
<div class="timeago'.$b.'" title="'.$time.'">
'.$time.'
</div>
</span>
</a>
</div>
<div data-track-name="'.$row['name'].'" data-track-id="'.$row['id'].'" id="play'.$row['id'].'" class="track song-play-btn">
</div>
<div class="song-titles">
<div class="song-author"><a onmouseover="profileCard('.$row['idu'].', '.$row['id'].', 0, 0);" onmouseout="profileCard(0, 0, 0, 1);" onclick="profileCard(0, 0, 1, 1);" href="'.$this->url.'/index.php?a=profile&u='.$row['username'].'">'.realName($row['username'], $row['first_name'], $row['last_name']).'</a></div>
<div class="song-tag">
'.$tag.'
</div>
<div class="song-title">
<div class="track-link"><h2 style="font-size:1.0em">
<!--<div id="song-name'.$row['id'].'">'.$row['title'].'</div>-->
</h2></div>
</div>
</div>
</div>
<div class="player-controls">
<div id="song-controls'.$row['id'].'">
<div id="jp_container_123" class="jp-audio">
<div class="jp-type-single">
<div class="jp-gui jp-interface">
<div class="jp-progress">
<div class="jp-seek-bar">
<div class="jp-play-bar"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="track-actions-container">
</div>
'.$extra_links.'
'.$seo_links.'
'.$comment.'
'.$charts.'
</div>';
$object_id = 'article_12';
include('commentanything/php/loadComments.php');
$start = (isset($row['extra_id'])) ? $row['extra_id'] : $row['id'];
}
}
I am trying to display it right after '.$charts.' but it keeps showing up in header
it's looking like you haven't worked to match HTML in index and loadComments.php .
first copy your loadComments.php code in place of include('commentanything/php/loadComments.php');
and check for html/css errors. It's there.
And if it failed too, your php/html nesting is wrong which i cant tell because u don't have even used to know us how and where u are stuffing php. You simply added variables in above displayed code

Categories