Help with foreach loop PHP - php

Currently producing a Wordpress plugin that allows for multiple image sliders. At the moment, to make sure that the code is valid I am having to load each sliders dynamic styling into the tags. This is fine, however it loads the styling for all the sliders, which can really start to add a lot of code to the pages source if the users uses many image sliders.
So I'm trying to get the styling to only be added to the tags if the slider is actually displayed on the page. Is this possible? This is how I am currently displaying the styling:
function test() {
global $wpdb;
$table_name = $wpdb->prefix . "premiumslider";
$number = $wpdb->get_results("SELECT * FROM $table_name");
foreach ($number as $slider) { ?>
<style type="text/css"><?php if($slider->paginationstyle == 'icons') { ?>
#lofslidecontent<?php echo $slider->id; ?> .lof-navigator li{<?php if($slider- >paginationstyle=='icons'){ ?>background: url(<?php if($slider->paginationoff=='') echo WP_PLUGIN_URL.'/premium-slider/images/pagination.png'; if($slider->paginationoff!='') echo $slider->paginationoff; ?>) 0 0 no-repeat;<?php } ?>}
#lofslidecontent<?php echo $slider->id; ?> .lof-navigator li.active{<?php if($slider->paginationstyle=='icons'){ ?>background: url(<?php if($slider->paginationon=='') echo WP_PLUGIN_URL.'/premium-slider/images/pagination_current.png'; if($slider->paginationon!='') echo $slider->paginationon; ?>) 0 0 no-repeat;<?php } ?>}
<?php } ?>
<?php if($slider->paginationstyle == 'images') { ?>
#lofslidecontent<?php echo $slider->id; ?> .lof-navigator { margin-top: 50px; }
#lofslidecontent<?php echo $slider->id; ?> .lof-navigator li img { border: <?php echo $slider->imgborder; ?>px solid #<?php echo $slider->imgcolour; ?>; }
#lofslidecontent<?php echo $slider->id; ?> .lof-navigator li.active img { border: <?php echo $slider->imgborder; ?>px solid #<?php echo $slider->imghover; ?>; }
#lofslidecontent<?php echo $slider->id; ?> .lof-navigator li.active img:hover { border: <?php echo $slider->imgborder; ?>px solid #<?php echo $slider->imghover; ?>; }
<?php } ?></style><?php
}
}
How can I achieve this?
To display the above code I use:
add_action('wp_head','test');
And to display the slider ($id being the id of that particular slider; 1, 2, 3, etc):
premium_slider($id);

This is sort of a complicated question. First of all, I disparage you for using a global variable, especially a DB resource. I'm also unclear as to why you need to echo the styles directly in this way.
Now to help you:
The only way to really solve this that I can see is if you have all of the styles stored somewhere, such as a DB, a config file, or any such thing per style. You must know the styles beforehand because you are hard coding them here. So just cycle through and if the ID of your slider matches its style, print the style. If you stored all of the styles in a big array in another php file hashed with the ID of the slider, this would be simple.

How do you determine which sliders appear on the page? Effectively you would need to alter your query to be the same as the one which actually loads the sliders, that way the CSS would only be loaded to display the same sliders.
$table_name = $wpdb->prefix . "premiumslider";
$number = $wpdb->get_results("SELECT * FROM $table_name WHERE category = 'cars'");
This would only load the elements into the foreach array that are displayed on that particular page.

Related

i want to change the color of the dynamic div(by using backend am getting 4 divs ) using php

I want this type of appearance enter image description here
But am getting below result
enter image description here
I want different color for each div...by using while loop am getting those four divs..Here am writing css properties for one div those are reflecting for four divs
Declare an array for the four different colos (CSS classes for the colors), e.g.
$colors = array('green', 'blue', 'red', 'yellow');
Then declare the four classes in CSS, e.g.,
.green { background-color: green; /* and other styles etc */ }
.red { background-color: red; /* and other styles etc */ }
.blue { background-color: blue; /* and other styles etc */ }
.yellow { background-color: yellow; /* and other styles etc */ }
Then in you PHP script, use some loop counter like $i to pick a color from the array:
$i=0;
foreach($testimonials as $testimonial)
{
echo '<div class="' . $colors[$i++] . '">other content</div>';
}
I hope it helps!
Set four classes And before while loop set variable $i=1;
Now
<?php echo '
<style>
.div-1{background:#186aa9;}/*blue*/
.div-2{background:#e1404f;}/*red*/
.div-3{background:#f8bc34;}/*yellow*/
.div-4{background:#9cc03c;}/*green*/
</style>';
$i=1;
while($row = mysqli_fetch_assoc($result)){echo '
<div class="col-md-6 div-'.$i.'">
<div class=" kwikmint-message">
<p class="kwikmint-message-p">"'.$row['Message'].'"</p>
</div>
<div class="mint-testi-h5">
<h5>'.$row['Name'].'<br><span class="specialization">'.$row['Specilisation'].'</span></h5>
</div>
</div>';
$i++;
}?>

how to use php function within style tag

I want to add background image dynamically in style tag and return to div using php code. So I use my general logic. but It doesn't work. I write code as following
CSS
<style>
.topadd{
background-image: url('../add/<?php echo $add?>');
width:100%;
height:auto;
}
</style>
</head>
PHP
<?php
$query="SELECT img FROM addbar where position='logo'";
$result=$con->query($query) or die($con->error);
while($row=$result->fetch_assoc()){
$add=$row['img'];
if($add!=""){
echo "<div class='topadd'></div>";
}
}
?>
I know there is no connection between style tag which is included in head and php code. I can't use php function within css that returns variable as html. Or any idea to fix this problem ?
You can modified this with
CSS
<style>
.topadd{
width:100%;
height:auto;
}
</style>
</head>
PHP
<?php
$query="SELECT img FROM addbar where position='logo'";
$result=$con->query($query) or die($con->error);
while($row=$result->fetch_assoc()){
$add=$row['img'];
if($add!=""){
echo "<div class='topadd' style='background-image: url(../add/'".$add.")'></div>";
}
}
?>
i hope it may help you

Make div overflow scroll on the x axis

I scan trough a folder for images by using php,
i put them in a div with the ID of images.
I want that div to scroll on the x-axis and hide everything that overflows on the y-axis.
So here's my code.
HTML and PHP:
<div id="images">
<?php
//Displaying images
$imgID = 0;
foreach($images as $image) {
echo "<img id='img".$imgID."' onClick='displayImg(".$imgID.");' src='".$image."' height='".$imgHeight."' width='".$imgWidth."' />";
$imgID++;
}
echo "<script>var maxImages = ".$imgID.";</script>";
?>
CSS:
#images {
overflow-y:hidden;
overflow-x:scroll;
height:<?php echo $imgHeight; ?>px;
border:solid 1px #c4c4c4;
align-content:flex-start;
}
#images img {
padding:5px;
cursor:pointer;
float:left;
}
I've experimented a bit, But nothing i've tried seems to work.
Your question is slightly out of context but how about something like:
http://jsfiddle.net/bassmanpaul/u9Xx6/
This goes on the assumption that you have the image widths available in php ($imgWidth) which can allow you to create a large inner container.
Failing that you may need to give us a JSFiddle to demonstrate your issue in greater detail...

How can i change the font color of a variable in PHP inside echo

echo $display;
so we know the default color is black. But i want to change it to white. Tried codes such as
"<div style=\"color: white;\">$display</div>";
but it doesn't work. Can you help me with this guys? thanks in advance.
<?php
$Color = "red";
$Text = "This text is red";
echo '<div style="Color:'.$Color.'">'.$Text.'</div>';
?>
In php, you can use . to add variables together. In this case, the final output of the echo line is:
<div style="Color:red">This text is red</div>
An example of this code in use can be seen here.
<span style="color:white;"><?php echo $display; ?></span>
Hope it might help.
<?php
$yourtext = "Some text";
?>
<div style="color:<?php echo ((#$_GET["color"]) && !empty(#$_GET["color"])) ? $_GET["color"] : "black"; ?>" ><?php echo $yourtext; ?></div>
In Url http://yoursite.you/yourpage.php?color=white
Green
Yellow
Blue
etc...
If applied color doesn't work even inline-style, so you can use !important word, cos maybe all div colors come from css file and suppress inline-styles.
"<div style=\"color: white !important;\">$display</div>";
If you have a div and you want to change its text and color of text, then simply copy and paste this code it will work for you.
echo "<script type='text/javascript'>";
echo"var ele= document.getElementById('login_status');
ele.innerHTML ='* Your <font color=#ff3322> Email </font> is incorrect ';";
echo "</script>";
I know this is an old question but I write the answer in case someone has the same question and does not know where to look. I think you wanted to print links so this is why the previous methods didn't work.Inside your php file write the following:
echo "<html>
<head>
<style>
/* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
}
/* mouse over link */
a:hover {
color: hotpink;
}
/* selected link */
a:active {
color: blue;
}
</style>
</head>
<body>
$variableYouWantToChangeColor
</body>
</html>";
You can change/remove parts of the css so it fits your needs.
its usable with bootstrap
<p class="text-primary"><?php echo $distance; ?></p>
i found that when i try echo result from a calculation
<p class="text-primary"><?php echo round ($distance,2); ?></p>

can this be made to work? (multiple ul in columns)

Can get something working with css3 for safari and firefox but ie won't have a piece of it. I'm outputting a list of shopping categorys and sub categorys with a seperate for each list.
This is what I've got (not working so far).. Any help would be most welcomed.
<div style="float:left; width: 232px;">
<?
$rowcount = 1;
$strSql = "SELECT * FROM Category ORDER BY CategoryName ASC";
$r = $db->select($strSql);
while ($row=$db->get_row($r, 'MYSQL_ASSOC')) {
$CategoryID = $row['CategoryID'];
$CategoryName = $row['CategoryName'];
?>
<div style="width: 232px; background-color:#f2f2f2; padding: 5px; margin-top: 5px; -webkit-border-radius: 4px; border-radius: 4px; overflow:hidden; padding-left: 5px;">
<h1><a href="/<?=$colour?>/<?=$CategoryURL?>/" ><?=$CategoryName?></a></h1>
<ul>
<?
$strSql2 = "SELECT
SubCategory.SubCategoryID,
SubCategory.SubCategoryName,
SubCategory.SubCategoryURL,
SubCategory.CategoryID
FROM
SubCategory
WHERE
CategoryID = $CategoryID
ORDER BY
SubCategoryName";
// echo "<pre>$strSql</pre>";
$r2 = $db->select($strSql2);
while ($row2=$db->get_row($r2, 'MYSQL_ASSOC')) {
$SubCategoryID = $row2['SubCategoryID'];
$SubCategoryName = $row2['SubCategoryName'];
$SubCategoryURL = $row2['SubCategoryURL'];
?>
<li><?=$SubCategoryName?> (<?=$rowcount?>)</li>
<?
$rowcount++;
if ($rowcount == 35) { echo"</ul></div><div style=\"float:left; width: 232px;\"><ul>"; $rowcount = 0;}
} // end get SubCategory
?>
<? if ($rowcount == 0) { echo""; } else {echo"</ul>";} ?>
</div>
<? } // end get category list
?>
</ul> <!-- END CATEGORY UL -->
</div>
</div>
An alternative would be to use a table-based layout, which in this case I don't think would be that bad. Tables are for tabular data and catalog categories would fit well. Edit: But you'd still have to figure out your PHP logic to get this working. Derp on my part.
Alternatively, there is the Columnizer jQuery plugin to do the trick automagically but I'm not fond of having to rely on a javascript-driven solution for layout elements. Although you could trigger it for only the browsers that don't support your CSS3 method via Modernizr. Still not a huge fan of this.
Can you post the CSS3 you used that is working in other browsers? Perhaps there is an available workaround that would do the trick. I'm still a bit fuzzy on the details for your end result. I'm focusing on the CSS aspect of your issue and less on the PHP. Finding a working CSS-driven solution is obviously far easier than trying to figure out the logic to divide your categories into columns accordingly.

Categories