Vary css element width dynamically - php

I have the following shortcodes in a page or post:
[custom_width width='500' color='red']
[custom_width width='600' color='blue']
The shortcode function is to display 2 buttons with their respective width. The problem is that the buttons are displayed the same width using the last width of 600.
The function:
<?php
function xyz ($attr) {
$xyzwidth = $attr['width'];
$xyzcolor = $attr['color'];
my_style($xyzcolor);
?>
<style type="text/css">
.mystyle {
width: <?php echo $xyzwidth; ?>px;
}
</style>
<div class="mystyle">
My Text
</div>
<?php } ?>
note: the rest of the css is in its own .css file
function my_style($xyzcolor) {
switch ($xyzcolor) {
case 'red': my_red();
break;
case 'blue': my_blue();
break;
}
<?php function my_red() { ?>
<style type="text/css">
.mystyle {
............
}
.mystyle: hover {
............
}
.mystyle a {
............
}
.mystyle a:hover {
............
}
</style>
<?php } ?>

If you're using the same class (mystyle) on both buttons, the last width defined on that class will apply to both elements.
For your purposes, you're probably better off with:
<?php
function xyz ($attr) {
$xyzwidth = $attr['width'];
?>
<div style="width: <?php echo $xyzwidth; ?>px">
My Text
</div>
<?php
}
?>

Define your blue and red CSS in general. Not in your loop every time. Something like this:
<style type="text/css">
// red
.mystyle.red {
............
}
.mystyle.red:hover {
............
}
.mystyle.red a {
............
}
.mystyle.red a:hover {
............
}
// blue
.mystyle.blue {
............
}
.mystyle.blue:hover {
............
}
.mystyle.blue a {
............
}
.mystyle.blue a:hover {
............
}
</style>
And in your xyz function, give your div the desired class, and set the width of div internal:
<?php
function xyz ($attr) {
$xyzwidth = $attr['width'];
$xyzcolor = $attr['color'];
my_style($xyzcolor);
?>
<div class="mystyle <?php echo $xyzcolor; ?>" style="width: <?php echo $xyzwidth; ?>px;">
My Text
</div>
<?php } ?>

Related

Wordpress unique background in posts that are in specific category

i have a code:
<body <?php if( in_category( 11446 ) ) { echo "style=\"background-image: url('my-background-url-of-image');background-repeat:repeat;\" onclick=\"window.open('http://www.domain.com');\""; } ?> >
This code works only until page loads fully than something happens and it doesn't work i assume from inspect element that onclick function changes and i'm failing to find what part tricks that.
What this code does is it sets unique body background that are in specific category and background is clickable.
But because of some javascript error it doesn't work when page loads full so maybe somebody could explain me how to remove attr on Javascript and than add my with domain i want. Or maybe give example how to do alternative code just with href.
Thank you.
I'm assuming you are building a Wordpress template and the background image to be used is based upon the category of a Wordpress post.
This uses no Javascript. Instead, what it does is create the CSS declaration block inside the head tag of the HTML5 document on the fly. It does no inline CSS for the body tag.
<?php
// ====================================================
// Solution #1
// Background Image Only
// ====================================================
function GetThisWordpressPostCategory() {
// Do post category to filename mapping here
return("cars");
}
function in_category() {
// Just a dummy to simulate Wordpress in_category call
return(true);
}
function BodyBackground($category) {
$bodyCss = "";
if (in_category($category)) {
$bodyCss =<<<CSS
<style type="text/css">
body {
background-image: url('{$category}.jpg');
}
</style>
CSS;
}
return($bodyCss);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Category background</title>
<?php
$category = GetThisWordpressPostCategory();
echo BodyBackground($category);
?>
</head>
<body>
</body>
</html>
<?php
// ====================================================
// Solution: 2
// Clickable div spanning viewport
// ====================================================
function GetThisWordpressPostCategory() {
// Do post category to filename mapping here
return("cars");
}
function in_category() {
// Just a dummy to simulate Wordpress in_category call
return(true);
}
function PageCss($category) {
$pageCss = "";
if (in_category($category)) {
$pageCss =<<<CSS
<style type="text/css">
html, body, #page {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
background-image: url('{$category}.jpg');
}
#page-anchor {
display: block;
width: 100%;
height: 100%;
}
</style>
CSS;
}
return($pageCss);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Category background</title>
<?php echo PageCss(GetThisWordpressPostCategory()); ?>
</head>
<body>
<div id="page">
<a id="page-anchor" href="http://www.google.com"></a>
</div>
</body>
</html>

How to pass dynamic variable through class function

I'm trying to pass every value through the function (it's a part of cms) so later on user can view content and then when he confirms it is saved to database and a separate file is made that looks exactly as preview but I don't know and have no idea how to pass a dynamic variable like $_POST['champno'.$i] right below
public function display($patch, $champ_number){
echo '<h1 style="float: left;">PATCH ';
echo $patch.'</h1>
<nav id="header-menu">
<ul>
<li>PATCHES</li>
<li>CHAMPIONS</li>
<li>ITEMS</li>
<li>CHANGES</li>
</ul>
</nav>
<div id="title">
<h2>CHAMPION BALANCE CHANGES</h2>
</div>';
for($i=1;$i<=$champ_number; $i++){
?>
<style type="text/css" scoped>
#media(min-width: 640px){
.<?php echo $_POST['champno'.$i]; ?>{
background-image: url('../../assets/champions/<?php echo $_POST['champno'.$i]; ?>_pc.jpg');
Extract the information from $_POST['champno'.$i] into a dedicated array and call the function with that:
$champions = array();
for ($i = 1; $i <= $champ_number; $i++)
$champions[] = $_POST['champno' . $i];
$obj->display($patch, $champions);
In the function:
function display($patch, $champions) {
...
foreach ($champions as $champion) {
$champion = htmlspecialchars($champion);
?>
<style type="text/css" scoped>
#media(min-width: 640px) {
.<?php echo $champion; ?> {
background-image: url('../../assets/champions/<?php echo $champion; ?>_pc.jpg');
...
<?php
}
}
Then you can use the function with the database and all other input sources.

The CSS & jQuery code getting printed as plaintext on 200th iteration [php]

I was working on a script where I had to show some query respective to domain type(Here mentioned as typeX & type Y).So I used jquery slidetoggle function & little bit of css.Things worked perfect until 200th iteration.
Till 199th iteration data is displayed properly with indentation like below:
Click To show typeX data
//Some data shown via toggle
Click To show typeY data
//Some data shown via toggle
But beyond that its prints all data & even jQuery,css as continuous line of text,even break tag is not working.Something like this:
$(document).ready(function(){ $("#typeX201").click(function(){ $("#typeX201").slideToggle("slow"); }); }); $(document).ready(function(){ $("#typeY201").click(function(){ $("#itypeY201").slideToggle("slow"); }); }); #typeX,#typeY { background-color:#cccccc; border:solid 1px #a9a9a9; } #TypeX201,#typeY201 { display:none; }
//followed by some data
Below is my php code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
<?php
class Delete_bm
{
function __construct($site)
{
//Creating DB Connection
}
function __destruct()
{
//Closing DB Connection
}
function display_data()
{
$sql="Some SQL query;
$res=mysql_query($sql,$this->db_cluster1);
$i=1;
while ($line = mysql_fetch_assoc($res))
{ ?>
<script>
$(document).ready(function(){
$("#typeX<?php echo $i?>").click(function(){
$("#typeXqry<?php echo $i?>").slideToggle("slow");
});
});
$(document).ready(function(){
$("#typeY<?php echo $i?>").click(function(){
$("#typeYqry<?php echo $i?>").slideToggle("slow");
});
});
</script>
<style>
#typeX?php echo $i?>,#typeY<?php echo $i?>
{
background-color:#cccccc;
border:solid 1px #a9a9a9;
}
#typeXqry<?php echo $i?>,#typeYqry<?php echo $i?>
{
display:none;
}
</style>
<div id="typeX<?php echo $i?>"><b>Click to show typeX Query</b></div>
<div id="typeXqry<?php echo $i?>">
<?php $str="Some Query";
//typeX text to be displayed in every iteration;?>
<div id="typeXqry<?php echo $i?>">
<?php $str="Some Query";
//typeX text to be displayed in every iteration;?>
$i++;
}
}
}
$list = new Delete_bm();
$data = $list->display_data();
How to solve this problem?
Leave this part out of the loop. There is no need to duplicate code like this.
<script>
$(document).ready(function() {
$(".typeX, .typeY").click(function() {
$(this).next().slideToggle("slow");
});
});
</script>
<style> .typeX, .typeY { background-color:#cccccc; border:solid 1px #a9a9a9; } .Xqry, .Yqry { display:none; } </style>
Your loop should output only this stuff.
<br><b>199:Domain name:abc.com<br>
<div id="typeX199" class="typeX"><b>Click to show typeX Query</b></div>
<div id="Xqry199" class="Xqry"> //MySQL Queries <br><br> </div>
<div id="typeY199" class="typeY"><b>Click to show typeY Query</b></div>
<div id="Yqry199" class="Yqry"> //MySQL Queries <br><br> </div>

echo Div visibility using CSS and PHP?

I am trying to echo a CSS style in php. basically I want to make a Div visible.
the Div's visibility is set to hidden in the css stylesheet and i need to make it visible in an else statement in php.
I am using this code:
<?php
} else {
echo '<style type="text/css">
#apDiv1 {
visibility:visible;
}
</style>';
}
?>
but that doesn't work! it doesn't make the Div visible. I tried it this way as a test and this way worked and it echo-ed hello world:
<?php
} else {
echo 'hello world';
}
?>
so is there any other way to do this? am I doing anything wrong?
You can also try like
<?php
} else {
?>
<style type="text/css">
#apDiv1 {
visibility:visible;
}
</style>
<?php
}
?>
Or directly give the div style like
<?php
} else {
?>
<div id="appDiv1" style="visibility:visible"></div>
<?php }
?>
Even you can put !important if any other styles are applied on that div.
try visibility: visible !important;, or display: block.
You can use javascript instead of css
<script>
document.getElementById('apDiv1').style.visibility = 'visible';
</script>
Do directly on the div:
<div id="apDiv1" <?php if(/* Your condition */){ /* Do something */ }else{ echo 'style="visibility:visible;"';} ?>></div>

How to apply css to php 'echo $tube->error;'?

The CSS that I'm attempted to attach to 'echo $tube->error;' is not showing.. It just shows as default black text as if it had no css... How would I fix this?
Thanks (:
Below is what I need to fix..
The html/php part:
<div id="error">
<pre>
<?php } else {
echo $tube->error;
}
}
?>
</pre>
</div>
The html/php part in context:
<?php
if(isset($_POST['url']))
{
include('curl.php');
include('youtube.php');
$tube = new youtube();
$links = $tube->get($_POST['url']);
if($links) { ?>
<div id="result">
<b>Download Links ( Same IP Downloading only )</b> :
<?php
$format = '<p>Download video.%s - %s<br/>Right-click download link and choose etc...</p>';
while($link = array_shift($links))
{
echo vsprintf($format,$link);
}
?>
</div>
<div id="error">
<pre>
<?php } else {
echo $tube->error;
}
}
?>
</pre>
</div>
The CSS:
#error {
width:404px;
margin-left: auto;
margin-right: auto;
font-size:12;
font-family: 'Dosis', sans-serif;
color:white;
padding-top:20px;
}
The part of youtube.php(additional info.. not sure if you guys need this):
function get($url)
{
$this->conn = new Curl('youtube');
$html = $this->conn->get($url);
if(strstr($html,'verify-age-thumb'))
{
$this->error = "Adult Video Detected";
return false;
}
if(strstr($html,'das_captcha'))
{
$this->error = "Captcah Found please run on diffrent server";
return false;
}
if(!preg_match('/stream_map=(.[^&]*?)&/i',$html,$match))
{
$this->error = "Error Locating Downlod URL's";
return false;
}
Thanks again for any help!!
EDIT: I was wrong, it wasn't a CSS error, but a construct error, as others have noticed. I just rewrote the "The html/php part in context" to make it more clear and hopefully working:
<?php
if(isset($_POST['url']))
{
include('curl.php');
include('youtube.php');
$tube = new youtube();
$links = $tube->get($_POST['url']);
if($links)
{
?>
<div id="result">
<b>Download Links ( Same IP Downloading only )</b> :
<?php
$format = '<p>Download video.%s - %s<br/>Right-click download link and choose etc...</p>';
while($link = array_shift($links))
{
echo vsprintf($format,$link);
}
?>
</div>
<?php
}
else
{
?>
<div id="error">
<pre>
<?php echo $tube->error; ?>
</pre>
</div>
<?php
}
}
?>
Hope everything is ok now :)
Try this code:
<?php
if (isset($_POST['url'])) {
include('curl.php');
include('youtube.php');
$tube = new youtube();
$links = $tube->get($_POST['url']);
if ($links) {
?>
<div id="result">
<b>Download Links ( Same IP Downloading only )</b> :
<?php
$format = '<p>Download video.%s - %s<br/>Right-click download link and choose etc...</p>';
while($link = array_shift($links)) {
echo vsprintf($format,$link);
}
?>
</div>
<?php } else { ?>
<div id="error">
<pre>
<?php echo $tube->error; ?>
</pre>
</div>
<?php } } ?>
Don't forget to include the CSS... and please, please write more readable in the future.
Your <div id="error"> is never rendered if there is an error, because your else statement only includes the error text itself. Move the div and pre inside the else to get it working.
<?php } else { ?>
<div id="error">
<pre>
<?php echo $tube->error; ?>
</pre>
</div>
<?php } ?>
Try this css:
#error pre {
width:404px;
margin-left: auto;
margin-right: auto;
font-size:12;
font-family: 'Dosis', sans-serif;
color:white;
padding-top:20px;
}
Try to define your CSS also for #error pre and not only #error:
div#error {
width:404px;
margin-left: auto;
margin-right: auto;
padding-top:20px;
}
div#error pre
{
font-size:12;
font-family: 'Dosis', sans-serif;
color:white;
}
Use also an inspector (like Chrome's one, that you call with CTRL+I) to make sure:
- there are not overrides that undo there lines
- CSS is properly loaded (if in an external file)

Categories