How to only include specific div element to a page in php? - php

Is this possible? I have not seen this kind of codes. I really wanted to implement this but i was not able to search for it or I do not know how this kind of execution is called.
On a php page I have this elements of divs.
elements.php
<div id="countries">
<p>Malaysia</p>
<p>India</p>
</div>
<div id="cities">
<h2>sihgn</h2>
<p>sighn city</p>
</div>
<div id="street">
<h2>nowhere</h2>
<p>somewhere</p>
</div>
and on the showcountryonly.php which displays the specific div only.
<?php include (elements.php)?> ???? echo div #countries
on the showcitiiesonly.php which displays the city only.
<?php include (elements.php)?> ???? echo div #cities
on the showstreetonly.php which displays the street div only.
<?php include (elements.php)?> ???? echo div #street
The reason I want to implement this is I want to only edit one page for all of my includes.
Thank you.

You can define a variable for each div in elements.php:
$countries = '<div id="countries">
<p>Malaysia</p>
<p>India</p>
</div>';
$cities = '<div id="cities">
<h2>sihgn</h2>
<p>sighn city</p>
</div>';
$streets = '<div id="street">
<h2>nowhere</h2>
<p>somewhere</p>
</div>';
After including the elements file, you can just echo the needed variable:
<?php
include (elements.php);
echo $countries; // $cities or $streets
?>
Hope this helps.

Related

Getting rows from db and adding reserved- tag

what would be the right way to mark item as reserved? my current way works but i doubt it's "right" way to do it.
i've a simple query to get rows from database and then outputting them inside html tags. Here is example of my php variables:
$name = $row['name'];
$price = $row['price'];
$reserved = $row['reserved'];
and html example:
<div class="container">
<div class="item">
<div class="name">
$name
</div>
<div class="price">
$price
</div>
<div class="reserved">
$reserved
</div>
</div>
</div>
if a item is reserved, then i just add text to reserved- field in database.
I hide all "reserved" divs in css. Then i'm using jquery to loop through all "reserved" items. I check if that div has text, if it has, then i show it and after that i clear it.
$(".reserved").each(function(index) {
if($(this).is(":empty")) {
$(this).hide();
}
$(this).empty();
});
After that all items which had text in "reserved" field in database will have different layout. As they're supposed to have. It works but i don't think this is the right way.. can someone lead me to the right way? any help would be appreciated.

Hide Div title if there's no content

I'm very newbie regarding PHP, I'm trying to configure my wordpress Loop.
I figured out how to display my customfield in my template, I manually added a title before my taxonomy and custom fields, but I'd like it doesn't show if the taxonomy is empty.
Here is the code:
<div class="customfields">
<h2 class="widgettitle">My Title</h2>
<?php echo do_shortcode('[tax id="agences" before="Agences : " separator=", " after=""]'); ?>
<?php echo do_shortcode('[custom_fields_block]'); ?>
</div>
I would very much appreciate your help!
Thanks a ton,
T.
So code should be
<?php $taxonomy = do_shortcode('[tax id="agences" before="Agences : " separator=", " after=""]'); ?>
<div class="customfields">
<?php if(!empty($taxonomy)) : ?>
<h2 class="widgettitle">My Title</h2>
<?php echo $taxonomy; ?>
<?php endif; ?>
<?php echo do_shortcode('[custom_fields_block]'); ?>
</div>
If you want to show content in HTML only if certain variables contain a value then you can create a simple if statement that uses PHP's isset function to determine whether or not the variables are set, and if true place some HTML on the page. You can read more about isset here.

jquery toggle one div in php while loop

I have this While loop in my php code where I echo rows inside one div that acts like a button and when I press that "button" I want to hide/show the connected div with the jQuery function "toggle". I only get it to work so that when I click any of the "buttons" it opens all divs and not just that one that's connected.
<script>
$(document).ready(function(){
$(".scorec").click(function(){
$(".scorematcho").slideToggle('slow');
});
});
</script>
That's the jQuery I'm using at the moment.
<?php $RM = mysql_query("SELECT * FROM score ORDER BY ScoreID DESC LIMIT 3");
while ($ScoreD = mysql_fetch_assoc($RM)) {
$a = $ScoreD["ScoreOne"]; $b = $ScoreD["ScoreTwo"];?>
<div class="scorec" >
<?php if($a>$b){;?><font color="green"><?php }else{?><font color="red"><?php }?>
<div class="scorel"><img src="flags/<?php echo $ScoreD["FlagOne"]; ?>.png" style="float:left;">
<?php echo $ScoreD["TeamOne"]; ?> | <?php echo $ScoreD["ScoreOne"]; ?></div>
<div class="scorem">-</div>
<div class="scorer"><?php echo $ScoreD["ScoreTwo"]; ?> | <?php echo $ScoreD["TeamTwo"]; ?>
<img src="flags/<?php echo $ScoreD["FlagTwo"]; ?>.png" style="float:right;"></div></font>
</div>
<div class="scorematcho" >
<div class="scorematch">
de_dust2
16-2
</div>
<div class="scorematch">
de_nuke
16-2
</div>
<div class="scorematch">
de_dust
16-2
</div>
</div>
<?PHP } session_destroy()?>
That's the HTML/PHP I'm using. The div "scorec" is the "button" and "scorematcho" is the div I wan't to toggle.The text inside "scorematcho" shall be PHP also, just haven't changed it yet. I have searched and tested some things but can't get anything to work properly, I have noticed that some put all their PHP code inside an "echo", why is that and could that be the problem? Hope that's enough information, please tell if not. Also, if you have some other improvements you think I should make to the code, please tell.
I have added this jfiddle Hope it helps!! http://jsfiddle.net/H77yf/
$(".scorec").click(function(){
$(this).next().slideToggle('slow');
});

Attribute assign image to each multi option

I have created a multi option attribute so that I show an image for each option but i can not get it to work.
I have used this code from another post on here to get a list of the options to show.
The code I used is:
<?php if($_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)): ?>
<h4>Suitable for:</h4>
<ul><li><?php
$_comma = ",";
$_list = "</li><li>";
echo str_replace($_comma,$_list,$_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)) ?>
</li></ul>
<?php endif; ?>
So this now shows a list of the options, one of on top of each other.
As I said I would like an image to be shown for each option.
I thought the best way would be to have divs and assign an image to each div.
I was hoping that I could get the output to be:
<div class="option1"></div>
<div class="option2"></div>
<div class="option3"></div>
<div class="option3"></div>
instead of the output that the code above has:
<ul>
<li>option1</li>
<li>option2</li>
<li>option3</li>
<li>option4</li>
</ul>
Change your code to
<?php if($_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)): ?>
<h4>Suitable for:</h4>
<div class="<?php
$_comma = ",";
$_list = "\"></div><div class=\"";
echo str_replace($_comma,$_list,$_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)) ?>
</div>
<?php endif; ?>

Is it possible to show the database values in jquery thickbox

In hiddenModalContent div is it possible to display the database value as once thickbox is opening it just showing nothing
<?php
foreach($this->list_details as $key=>$details)
{?>
<div class="sc_content_subheading"><? echo $this->escape($details['list_name'])?></div>
<div id="hiddenModalContent" style="display:none;">
<span class="content_subheading">List name </span><? echo $details['list_name'];?><br/>
</div>
<?php }?>
It seems all you are only outputting the list name. What else in the array would you like to show?

Categories